chart2.vue 3.3 KB

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