chart.vue 3.0 KB

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