chart.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. },
  76. },
  77. data: [{
  78. name: "TTU",
  79. value: '11'
  80. },
  81. {
  82. name: "电气火灾",
  83. value: 22
  84. },
  85. {
  86. name: "视频监测",
  87. value: 8
  88. },
  89. {
  90. name: "水系统",
  91. value:3
  92. },
  93. {
  94. name: "烟感",
  95. value: 11
  96. },
  97. {
  98. name: "消防栓",
  99. value: 23
  100. },
  101. {
  102. name: "液位",
  103. value: 21
  104. },
  105. ],
  106. labelLine: {
  107. normal: {
  108. length: 10, //aa折线长度
  109. length2: 20, //aa折线长度
  110. }
  111. },
  112. label: {
  113. normal: {
  114. formatter: '{b}:\n{d}%',
  115. }
  116. },
  117. }]
  118. }
  119. }
  120. },
  121. onLoad() {
  122. },
  123. methods: {
  124. changeOption() {
  125. const data = this.option.series[0].data
  126. // 随机更新示例数据
  127. data.forEach((item, index) => {
  128. data.splice(index, 1, Math.random() * 40)
  129. })
  130. },
  131. onViewClick(options) {
  132. console.log(options)
  133. }
  134. }
  135. }
  136. </script>
  137. <script module="echarts" lang="renderjs">
  138. let myChart
  139. export default {
  140. mounted() {
  141. if (typeof window.echarts === 'function') {
  142. this.initEcharts()
  143. } else {
  144. // 动态引入较大类库避免影响页面展示
  145. const script = document.createElement('script')
  146. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  147. script.src = 'static/echarts.min.js'
  148. script.onload = this.initEcharts.bind(this)
  149. document.head.appendChild(script)
  150. }
  151. },
  152. methods: {
  153. initEcharts() {
  154. myChart = echarts.init(document.getElementById('echarts2'))
  155. // 观测更新的数据在 view 层可以直接访问到
  156. myChart.setOption(this.option)
  157. },
  158. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  159. // 监听 service 层数据变更
  160. myChart.setOption(newValue)
  161. },
  162. onClick(event, ownerInstance) {
  163. // 调用 service 层的方法
  164. ownerInstance.callMethod('onViewClick', {
  165. test: 'test'
  166. })
  167. }
  168. }
  169. }
  170. </script>
  171. <style>
  172. .content {
  173. display: flex;
  174. flex-direction: column;
  175. align-items: center;
  176. justify-content: center;
  177. }
  178. .echarts {
  179. width: 100%;
  180. height: 500rpx;
  181. }
  182. </style>