chart2.vue 4.6 KB

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