chart.vue 3.2 KB

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