chart.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. return {
  17. option: {
  18. color: ['#00B7FC', '#935AF9', '#FF5354', '#FF8758', '#FFCF34', '#00E9CD', '#00B7FC'],
  19. title: {
  20. left: 'center'
  21. },
  22. tooltip: {
  23. trigger: 'item',
  24. // formatter: "{a} <br/>{b}: {c} ({d}%)"
  25. formatter: "{b}: {c} "
  26. },
  27. title: [{ //aa标题
  28. // text: '{val|121}',
  29. text: '{val|121}\n{name|' + title + '}',
  30. top: '45%',
  31. left: 'center',
  32. textStyle: {
  33. rich: {
  34. name: {
  35. fontSize: 14,
  36. fontWeight: 'normal',
  37. color: '#666666',
  38. padding: [5, 0]
  39. },
  40. val: {
  41. fontSize: 24,
  42. color: '#333333',
  43. }
  44. }
  45. }
  46. }],
  47. legend: { //aa图例
  48. show:false,
  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: "TTU",
  83. value: '11'
  84. },
  85. {
  86. name: "电气火灾",
  87. value: 22
  88. },
  89. {
  90. name: "视频监测",
  91. value: 8
  92. },
  93. {
  94. name: "水系统",
  95. value:3
  96. },
  97. {
  98. name: "烟感",
  99. value: 11
  100. },
  101. {
  102. name: "消防栓",
  103. value: 23
  104. },
  105. {
  106. name: "液位",
  107. value: 21
  108. },
  109. ],
  110. labelLine: {
  111. normal: {
  112. length: 10, //aa折线长度
  113. length2: 20, //aa折线长度
  114. }
  115. },
  116. label: {
  117. normal: {
  118. formatter: '{b}:{d}%',
  119. // formatter: params => {
  120. // var percent = 0;
  121. // var total = 0;
  122. // for (var i = 0; i < echartData.length; i++) {
  123. // total += echartData[i].value;
  124. // }
  125. // console.log(111);
  126. // console.log(total);
  127. // percent = ((params.value / total) * 100).toFixed(0);
  128. // return params.name + ': ' + percent + '%';
  129. // },
  130. }
  131. },
  132. }]
  133. }
  134. }
  135. },
  136. onLoad() {
  137. },
  138. methods: {
  139. changeOption() {
  140. const data = this.option.series[0].data
  141. // 随机更新示例数据
  142. data.forEach((item, index) => {
  143. data.splice(index, 1, Math.random() * 40)
  144. })
  145. },
  146. onViewClick(options) {
  147. console.log(options)
  148. }
  149. }
  150. }
  151. </script>
  152. <script module="echarts" lang="renderjs">
  153. let myChart
  154. export default {
  155. mounted() {
  156. if (typeof window.echarts === 'function') {
  157. this.initEcharts()
  158. } else {
  159. // 动态引入较大类库避免影响页面展示
  160. const script = document.createElement('script')
  161. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  162. script.src = '/static/echarts.min.js'
  163. script.onload = this.initEcharts.bind(this)
  164. document.head.appendChild(script)
  165. }
  166. },
  167. methods: {
  168. initEcharts() {
  169. myChart = echarts.init(document.getElementById('echarts2'))
  170. // 观测更新的数据在 view 层可以直接访问到
  171. myChart.setOption(this.option)
  172. },
  173. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  174. // 监听 service 层数据变更
  175. myChart.setOption(newValue)
  176. },
  177. onClick(event, ownerInstance) {
  178. // 调用 service 层的方法
  179. ownerInstance.callMethod('onViewClick', {
  180. test: 'test'
  181. })
  182. }
  183. }
  184. }
  185. </script>
  186. <style>
  187. .content {
  188. display: flex;
  189. flex-direction: column;
  190. align-items: center;
  191. justify-content: center;
  192. }
  193. .echarts {
  194. width: 100%;
  195. height: 500rpx;
  196. }
  197. </style>