chart.vue 3.9 KB

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