chart2.vue 4.5 KB

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