chart2.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view class="content">
  3. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts">
  4. </view>
  5. </view>
  6. </template>
  7. <!-- <script src="/static/echarts.min.js"></script> -->
  8. <script>
  9. // var abcolor = new echarts.graphic.LinearGradient(
  10. // 0, 0, 0, 1,
  11. // [{
  12. // offset: 0,
  13. // color: '#2378f7'
  14. // },
  15. // {
  16. // offset: 0.7,
  17. // color: '#2378f7'
  18. // },
  19. // {
  20. // offset: 1,
  21. // color: '#83bff6'
  22. // }
  23. // ]
  24. // )
  25. </script>
  26. <script>
  27. var abcolor = new echarts.graphic.LinearGradient(
  28. 0, 0, 0, 1,
  29. [{
  30. offset: 0,
  31. color: '#2378f7'
  32. },
  33. {
  34. offset: 0.7,
  35. color: '#2378f7'
  36. },
  37. {
  38. offset: 1,
  39. color: '#83bff6'
  40. }
  41. ]
  42. )
  43. export default {
  44. name: 'chart',
  45. props: {
  46. },
  47. data() {
  48. return {
  49. option: {
  50. series: [{
  51. type: 'gauge',
  52. //半径
  53. radius: '80%',
  54. detail: {
  55. color: '#FFC600',
  56. fontSize: 30,
  57. fontWeight: 'normal',
  58. offsetCenter: [0, '70%']
  59. },
  60. data: [{
  61. value: 30,
  62. name: '任务完成率'
  63. }],
  64. startAngle: 210,
  65. //结束角度。
  66. endAngle: -30,
  67. center: ['50%', '50%'], //中心位置
  68. pointer: {
  69. show: true,
  70. length: '70%',
  71. },
  72. title: {
  73. offsetCenter: [0, '30%'],
  74. fontSize: 15,
  75. color: '#444'
  76. },
  77. min: 0,
  78. max: 100,
  79. splitNumber: 10,
  80. //分隔线样式。
  81. splitLine: {
  82. show: false,
  83. },
  84. axisLine: {
  85. show: true,
  86. lineStyle: {
  87. width: 20,
  88. color: [
  89. [1, abcolor]
  90. ]
  91. }
  92. },
  93. }]
  94. }
  95. }
  96. },
  97. onLoad() {
  98. },
  99. methods: {
  100. changeOption() {
  101. const data = this.option.series[0].data
  102. // 随机更新示例数据
  103. data.forEach((item, index) => {
  104. data.splice(index, 1, Math.random() * 40)
  105. })
  106. },
  107. onViewClick(options) {
  108. console.log(options)
  109. }
  110. }
  111. }
  112. </script>
  113. <script module="echarts" lang="renderjs">
  114. let myChart
  115. export default {
  116. mounted() {
  117. if (typeof window.echarts === 'function') {
  118. this.initEcharts()
  119. } else {
  120. // 动态引入较大类库避免影响页面展示
  121. const script = document.createElement('script')
  122. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  123. script.src = '/static/echarts.min.js'
  124. script.onload = this.initEcharts.bind(this)
  125. document.head.appendChild(script)
  126. }
  127. },
  128. methods: {
  129. initEcharts() {
  130. myChart = echarts.init(document.getElementById('echarts'))
  131. // 观测更新的数据在 view 层可以直接访问到
  132. myChart.setOption(this.option)
  133. },
  134. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  135. // 监听 service 层数据变更
  136. myChart.setOption(newValue)
  137. },
  138. onClick(event, ownerInstance) {
  139. // 调用 service 层的方法
  140. ownerInstance.callMethod('onViewClick', {
  141. test: 'test'
  142. })
  143. }
  144. }
  145. }
  146. </script>
  147. <style>
  148. .content {
  149. display: flex;
  150. flex-direction: column;
  151. align-items: center;
  152. justify-content: center;
  153. }
  154. .echarts {
  155. width: 100%;
  156. height: 400rpx;
  157. }
  158. </style>