chart2.vue 3.5 KB

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