echarts.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <view>
  3. <view @click="echarts.onClick" :prop="option" :change:prop="echarts.updateEcharts" id="echarts" class="echarts"
  4. :style="'height:'+(height?height:'640rpx')">
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. export default {
  10. data() {
  11. return {}
  12. },
  13. props: ['option', 'height'],
  14. onLoad() {},
  15. methods: {
  16. onViewClick(options) {
  17. console.log(options)
  18. }
  19. }
  20. }
  21. </script>
  22. <script module="echarts" lang="renderjs">
  23. let myChart
  24. export default {
  25. mounted() {
  26. if (typeof window.echarts === 'function') {
  27. this.initEcharts()
  28. } else {
  29. // 动态引入较大类库避免影响页面展示
  30. const script = document.createElement('script')
  31. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  32. script.src = 'static/js/echarts.min.js'
  33. script.onload = this.initEcharts.bind(this)
  34. document.head.appendChild(script)
  35. }
  36. },
  37. methods: {
  38. initEcharts() {
  39. myChart = echarts.init(document.getElementById('echarts'))
  40. // 观测更新的数据在 view 层可以直接访问到
  41. myChart.setOption(this.option)
  42. },
  43. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  44. // 监听 service 层数据变更
  45. myChart.setOption(newValue)
  46. },
  47. onClick(event, ownerInstance) {
  48. // 调用 service 层的方法
  49. ownerInstance.callMethod('onViewClick', {
  50. test: 'test'
  51. })
  52. }
  53. }
  54. }
  55. </script>
  56. <style>
  57. .content {
  58. display: flex;
  59. flex-direction: column;
  60. align-items: center;
  61. justify-content: center;
  62. }
  63. .echarts {
  64. /* margin-top: 100px; */
  65. width: 100%;
  66. height: 640rpx;
  67. /* height: 300px; */
  68. }
  69. </style>