chart.vue 3.9 KB

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