chart.vue 4.3 KB

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