chart4.vue 3.9 KB

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