chart2.vue 4.2 KB

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