chart.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. var fontColor = "#fff";
  8. let noramlSize = 16;
  9. var datas = {
  10. value: 90,
  11. company: "%",
  12. ringColor: [{
  13. offset: 0,
  14. color: '#0BA0D7' // 0% 处的颜色
  15. }, {
  16. offset: 1,
  17. color: '#24F0E4' // 100% 处的颜色
  18. }]
  19. }
  20. export default {
  21. name: 'chart',
  22. props: {
  23. },
  24. data() {
  25. return {
  26. option: {
  27. title: {
  28. text: '{name|' + datas.value + datas.company + '}\n{val|任务完成率}',
  29. x: 'center',
  30. y: 'center',
  31. textStyle: {
  32. rich: {
  33. name: {
  34. fontSize: 32,
  35. fontWeight: 'normal',
  36. color: '#24F0E4',
  37. fontFamily: 'IMPACT',
  38. padding: [8, 0]
  39. },
  40. val: {
  41. fontSize: 16,
  42. color: '#333333',
  43. }
  44. }
  45. }
  46. },
  47. color: ['#F4F5F7'],
  48. legend: {
  49. show: false,
  50. data: []
  51. },
  52. series: [{
  53. name: 'Line 1',
  54. type: 'pie',
  55. clockWise: true,
  56. radius: ['64%', '80%'],
  57. itemStyle: {
  58. normal: {
  59. label: {
  60. show: false
  61. },
  62. labelLine: {
  63. show: false
  64. }
  65. }
  66. },
  67. hoverAnimation: false,
  68. data: [{
  69. value: datas.value,
  70. name: '',
  71. itemStyle: {
  72. normal: {
  73. color: { // 完成的圆环的颜色
  74. colorStops: datas.ringColor
  75. },
  76. label: {
  77. show: false
  78. },
  79. labelLine: {
  80. show: false
  81. }
  82. }
  83. }
  84. }, {
  85. name: '',
  86. value: 100 - datas.value
  87. }]
  88. }]
  89. }
  90. }
  91. },
  92. onLoad() {
  93. },
  94. methods: {
  95. changeOption() {
  96. const data = this.option.series[0].data
  97. // 随机更新示例数据
  98. data.forEach((item, index) => {
  99. data.splice(index, 1, Math.random() * 40)
  100. })
  101. },
  102. onViewClick(options) {
  103. console.log(options)
  104. }
  105. }
  106. }
  107. </script>
  108. <script module="echarts" lang="renderjs">
  109. let myChart
  110. export default {
  111. mounted() {
  112. if (typeof window.echarts === 'function') {
  113. this.initEcharts()
  114. } else {
  115. // 动态引入较大类库避免影响页面展示
  116. const script = document.createElement('script')
  117. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  118. script.src = '/static/echarts.min.js'
  119. script.onload = this.initEcharts.bind(this)
  120. document.head.appendChild(script)
  121. }
  122. },
  123. methods: {
  124. initEcharts() {
  125. myChart = echarts.init(document.getElementById('echarts'))
  126. // 观测更新的数据在 view 层可以直接访问到
  127. myChart.setOption(this.option)
  128. },
  129. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  130. // 监听 service 层数据变更
  131. myChart.setOption(newValue)
  132. },
  133. onClick(event, ownerInstance) {
  134. // 调用 service 层的方法
  135. ownerInstance.callMethod('onViewClick', {
  136. test: 'test'
  137. })
  138. }
  139. }
  140. }
  141. </script>
  142. <style>
  143. .content {
  144. display: flex;
  145. flex-direction: column;
  146. align-items: center;
  147. justify-content: center;
  148. }
  149. .echarts {
  150. width: 100%;
  151. height: 400rpx;
  152. }
  153. </style>