chart.vue 4.3 KB

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