chart.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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: ['40%', '60%'], //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. labelLine: {
  101. normal: {
  102. length: 10, //aa折线长度
  103. length2: 20, //aa折线长度
  104. }
  105. },
  106. label: {
  107. normal: {
  108. formatter: '{b}:{d}%',
  109. // formatter: params => {
  110. // var percent = 0;
  111. // var total = 0;
  112. // for (var i = 0; i < echartData.length; i++) {
  113. // total += echartData[i].value;
  114. // }
  115. // console.log(111);
  116. // console.log(total);
  117. // percent = ((params.value / total) * 100).toFixed(0);
  118. // return params.name + ': ' + percent + '%';
  119. // },
  120. }
  121. },
  122. }]
  123. }
  124. }
  125. },
  126. onLoad() {
  127. },
  128. methods: {
  129. changeOption() {
  130. const data = this.option.series[0].data
  131. // 随机更新示例数据
  132. data.forEach((item, index) => {
  133. data.splice(index, 1, Math.random() * 40)
  134. })
  135. },
  136. onViewClick(options) {
  137. console.log(options)
  138. }
  139. }
  140. }
  141. </script>
  142. <script module="echarts" lang="renderjs">
  143. let myChart
  144. export default {
  145. mounted() {
  146. if (typeof window.echarts === 'function') {
  147. this.initEcharts()
  148. } else {
  149. // 动态引入较大类库避免影响页面展示
  150. const script = document.createElement('script')
  151. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  152. script.src = '/static/echarts.min.js'
  153. script.onload = this.initEcharts.bind(this)
  154. document.head.appendChild(script)
  155. }
  156. },
  157. methods: {
  158. initEcharts() {
  159. myChart = echarts.init(document.getElementById('echarts'))
  160. // 观测更新的数据在 view 层可以直接访问到
  161. myChart.setOption(this.option)
  162. },
  163. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  164. // 监听 service 层数据变更
  165. myChart.setOption(newValue)
  166. },
  167. onClick(event, ownerInstance) {
  168. // 调用 service 层的方法
  169. ownerInstance.callMethod('onViewClick', {
  170. test: 'test'
  171. })
  172. }
  173. }
  174. }
  175. </script>
  176. <style>
  177. .content {
  178. display: flex;
  179. flex-direction: column;
  180. align-items: center;
  181. justify-content: center;
  182. width:100%;
  183. height:100%
  184. }
  185. .echarts {
  186. width: 100%;
  187. height: 520rpx;
  188. margin-top: 70rpx;
  189. }
  190. </style>