chart.vue 4.2 KB

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