chart2.vue 3.4 KB

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