chart2.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <view class="content">
  3. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts2"
  4. class="echarts"></view>
  5. </view>
  6. </template>
  7. <script>
  8. let title = '总数';
  9. export default {
  10. props: {
  11. bindData: {
  12. type: Object,
  13. default: ''
  14. }
  15. },
  16. data() {
  17. console.log('bindData')
  18. console.log(this.bindData)
  19. return {
  20. option: {
  21. color: ['#F67F32', '#3BD2BD', '#1DABFE'],
  22. title: {
  23. left: 'center'
  24. },
  25. tooltip: {
  26. trigger: 'item',
  27. // formatter: "{a} <br/>{b}: {c} ({d}%)"
  28. formatter: "{b}: {c} "
  29. },
  30. title: [{ //aa标题
  31. text: '{val|' + (parseInt(this.bindData.alarm_count) + parseInt(this.bindData
  32. .event_count) + parseInt(this.bindData.hidden_danger_count)) + '}\n{name|' +
  33. title + '}',
  34. top: '30%',
  35. left: 'center',
  36. textStyle: {
  37. rich: {
  38. name: {
  39. fontSize: 14,
  40. fontWeight: 'normal',
  41. color: '#666666',
  42. padding: [5, 0]
  43. },
  44. val: {
  45. fontSize: 24,
  46. color: '#333333',
  47. }
  48. }
  49. }
  50. }],
  51. legend: { //aa图例
  52. bottom: '30',
  53. // right: 50,
  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%'],
  76. center: ['50%', '35%'],
  77. itemStyle: {
  78. normal: {
  79. shadowBlur: 20,
  80. shadowColor: '#F9F5F7',
  81. shadowOffsetX: 0,
  82. shadowOffsetY: 0,
  83. },
  84. },
  85. data: [{
  86. name: "报警",
  87. value: this.bindData.alarm_count
  88. },
  89. {
  90. name: "事件",
  91. value: this.bindData.event_count
  92. },
  93. {
  94. name: "隐患",
  95. value: this.bindData.hidden_danger_count
  96. },
  97. ],
  98. emphasis: {
  99. itemStyle: {
  100. shadowBlur: 10,
  101. shadowOffsetX: 0,
  102. shadowColor: 'rgba(0, 0, 0, 0.5)'
  103. }
  104. },
  105. labelLine: {
  106. normal: {
  107. length: 10, //aa折线长度
  108. length2: 20, //aa折线长度
  109. }
  110. },
  111. label: {
  112. normal: {
  113. formatter: '{b}:{d}%',
  114. // formatter: params => {
  115. // var percent = 0;
  116. // var total = 0;
  117. // for (var i = 0; i < echartData.length; i++) {
  118. // total += echartData[i].value;
  119. // }
  120. // console.log(111);
  121. // console.log(total);
  122. // percent = ((params.value / total) * 100).toFixed(0);
  123. // return params.name + ': ' + percent + '%';
  124. // },
  125. }
  126. },
  127. }]
  128. }
  129. }
  130. },
  131. onLoad() {},
  132. methods: {
  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.min.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('echarts2'))
  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>