chart2.vue 4.1 KB

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