chart2.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <view class="content">
  3. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts2" class="echarts"></view>
  4. </view>
  5. </template>
  6. <script>
  7. // let echartData = [{
  8. // name: "报警",
  9. // value: 1000
  10. // },
  11. // {
  12. // name: "事件",
  13. // value: 2920
  14. // },
  15. // {
  16. // name: "隐患",
  17. // value: 2200
  18. // },
  19. // ];
  20. let title = '总数';
  21. // let total = echartData.reduce((a, b) => {
  22. // return a + b.value * 1
  23. // }, 0);
  24. export default {
  25. props:{
  26. bindData:{
  27. type:Object,
  28. default: ''
  29. }
  30. },
  31. data() {
  32. return {
  33. option: {
  34. color: ['#F67F32', '#3BD2BD', '#1DABFE'],
  35. title: {
  36. left: 'center'
  37. },
  38. tooltip: {
  39. trigger: 'item',
  40. formatter: "{a} <br/>{b}: {c} ({d}%)"
  41. },
  42. title: [{ //aa标题
  43. text: '{val|' +(parseInt(this.bindData.alarmCount)+parseInt(this.bindData.eventCount)+ parseInt(this.bindData.hiddenDangerCount))+ '}\n{name|' + title + '}',
  44. top: '45%',
  45. left: 'center',
  46. textStyle: {
  47. rich: {
  48. name: {
  49. fontSize: 14,
  50. fontWeight: 'normal',
  51. color: '#666666',
  52. padding: [5, 0]
  53. },
  54. val: {
  55. fontSize: 24,
  56. color: '#333333',
  57. }
  58. }
  59. }
  60. }],
  61. legend: { //aa图例
  62. orient: 'horizontal',
  63. icon: 'circle',
  64. itemWidth: 12,
  65. itemHeight: 12,
  66. itemGap: 60,
  67. // top: -10,
  68. textStyle: {
  69. fontSize: 14,
  70. rich: {
  71. name: {
  72. fontSize: 18
  73. },
  74. value: {
  75. fontSize: 14,
  76. padding: [0, 20, 0, 5]
  77. },
  78. }
  79. },
  80. },
  81. series: [{
  82. name: '访问来源',
  83. type: 'pie',
  84. radius: ['40%', '60%'],
  85. center: ['50%', '55%'],
  86. itemStyle: {
  87. normal: {
  88. shadowBlur: 20,
  89. shadowColor: '#F9F5F7',
  90. shadowOffsetX: 0,
  91. shadowOffsetY: 0,
  92. },
  93. },
  94. data: [{
  95. name: "报警",
  96. value: this.bindData.alarmCount
  97. },
  98. {
  99. name: "事件",
  100. value: this.bindData.eventCount
  101. },
  102. {
  103. name: "隐患",
  104. value: this.bindData.hiddenDangerCount
  105. },
  106. ],
  107. emphasis: {
  108. itemStyle: {
  109. shadowBlur: 10,
  110. shadowOffsetX: 0,
  111. shadowColor: 'rgba(0, 0, 0, 0.5)'
  112. }
  113. },
  114. labelLine: {
  115. normal: {
  116. length: 10, //aa折线长度
  117. length2: 20, //aa折线长度
  118. }
  119. },
  120. label: {
  121. normal: {
  122. formatter: '{b}:{d}%',
  123. // formatter: params => {
  124. // var percent = 0;
  125. // var total = 0;
  126. // for (var i = 0; i < echartData.length; i++) {
  127. // total += echartData[i].value;
  128. // }
  129. // console.log(111);
  130. // console.log(total);
  131. // percent = ((params.value / total) * 100).toFixed(0);
  132. // return params.name + ': ' + percent + '%';
  133. // },
  134. }
  135. },
  136. }]
  137. }
  138. }
  139. },
  140. onLoad() {
  141. },
  142. methods: {
  143. changeOption() {
  144. const data = this.option.series[0].data
  145. // 随机更新示例数据
  146. data.forEach((item, index) => {
  147. data.splice(index, 1, Math.random() * 40)
  148. })
  149. },
  150. onViewClick(options) {
  151. console.log(options)
  152. }
  153. }
  154. }
  155. </script>
  156. <script module="echarts" lang="renderjs">
  157. let myChart
  158. export default {
  159. mounted() {
  160. if (typeof window.echarts === 'function') {
  161. this.initEcharts()
  162. } else {
  163. // 动态引入较大类库避免影响页面展示
  164. const script = document.createElement('script')
  165. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  166. script.src = '/static/echarts.js'
  167. script.onload = this.initEcharts.bind(this)
  168. document.head.appendChild(script)
  169. }
  170. },
  171. methods: {
  172. initEcharts() {
  173. myChart = echarts.init(document.getElementById('echarts2'))
  174. // 观测更新的数据在 view 层可以直接访问到
  175. myChart.setOption(this.option)
  176. },
  177. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  178. // 监听 service 层数据变更
  179. myChart.setOption(newValue)
  180. },
  181. onClick(event, ownerInstance) {
  182. // 调用 service 层的方法
  183. ownerInstance.callMethod('onViewClick', {
  184. test: 'test'
  185. })
  186. }
  187. }
  188. }
  189. </script>
  190. <style>
  191. .content {
  192. display: flex;
  193. flex-direction: column;
  194. align-items: center;
  195. justify-content: center;
  196. }
  197. .echarts {
  198. width: 100%;
  199. height: 500rpx;
  200. margin-top: 70rpx;
  201. }
  202. </style>