chart4.vue 4.3 KB

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