chart2.vue 4.3 KB

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