chart.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. export default {
  9. name: 'chart',
  10. props: {
  11. },
  12. data() {
  13. return {
  14. option: {
  15. tooltip: {
  16. formatter: "{a} <br/>{b} : {c}%"
  17. },
  18. toolbox: {
  19. feature: {
  20. restore: {},
  21. saveAsImage: {}
  22. }
  23. },
  24. series: [{
  25. name: '业务指标',
  26. type: 'gauge',
  27. detail: {
  28. formatter: '{value}%'
  29. },
  30. data: [{
  31. value: 50,
  32. name: '完成率'
  33. }],
  34. axisLine: {
  35. show: true,
  36. lineStyle: {
  37. color: [
  38. [1, new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
  39. offset: 0.1,
  40. color: "#FFC600"
  41. },
  42. {
  43. offset: 0.6,
  44. color: "#30D27C"
  45. },
  46. {
  47. offset: 1,
  48. color: "#0B95FF"
  49. }
  50. ])]
  51. ]
  52. }
  53. }
  54. }]
  55. }
  56. }
  57. },
  58. onLoad() {
  59. },
  60. methods: {
  61. changeOption() {
  62. const data = this.option.series[0].data
  63. // 随机更新示例数据
  64. data.forEach((item, index) => {
  65. data.splice(index, 1, Math.random() * 40)
  66. })
  67. },
  68. onViewClick(options) {
  69. console.log(options)
  70. }
  71. }
  72. }
  73. </script>
  74. <script module="echarts" lang="renderjs">
  75. let myChart
  76. export default {
  77. mounted() {
  78. if (typeof window.echarts === 'function') {
  79. this.initEcharts()
  80. } else {
  81. // 动态引入较大类库避免影响页面展示
  82. const script = document.createElement('script')
  83. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  84. script.src = '/static/echarts.min.js'
  85. script.onload = this.initEcharts.bind(this)
  86. document.head.appendChild(script)
  87. }
  88. },
  89. methods: {
  90. //串联引入,保证文件加载顺序,在onload 方法内引入下一个js ,在最后一个onload方法内进行图表初始化
  91. // init() {
  92. // // 动态引入较大类库避免影响页面展示
  93. // const script = document.createElement('script')
  94. // script.src = 'static/china.js'
  95. // script.onload = this.initEcharts.bind(this)
  96. // document.head.appendChild(script)
  97. // },
  98. initEcharts() {
  99. myChart = echarts.init(document.getElementById('echarts'))
  100. // 观测更新的数据在 view 层可以直接访问到
  101. myChart.setOption(this.option)
  102. },
  103. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  104. // 监听 service 层数据变更
  105. myChart.setOption(newValue)
  106. },
  107. onClick(event, ownerInstance) {
  108. // 调用 service 层的方法
  109. ownerInstance.callMethod('onViewClick', {
  110. test: 'test'
  111. })
  112. }
  113. }
  114. }
  115. </script>
  116. <style>
  117. .content {
  118. display: flex;
  119. flex-direction: column;
  120. align-items: center;
  121. justify-content: center;
  122. }
  123. .echarts {
  124. width: 100%;
  125. height: 400rpx;
  126. }
  127. </style>