chart.vue 4.4 KB

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