chart2.vue 4.7 KB

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