chart-model.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <view>
  3. <view
  4. @click="echarts.onClick"
  5. :prop="option"
  6. :change:prop="echarts.updateEcharts"
  7. id="echarts"
  8. class="echarts">
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'chartModel',
  15. props: {
  16. fatherData: {
  17. type: Object,
  18. default: function() {
  19. return {};
  20. }
  21. },
  22. },
  23. data() {
  24. return {
  25. option: {
  26. title: {
  27. text: 'ECharts 入门示例'
  28. },
  29. tooltip: {},
  30. legend: {
  31. data: ['销量']
  32. },
  33. xAxis: {
  34. data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
  35. },
  36. yAxis: {},
  37. series: [{
  38. name: '销量',
  39. type: 'bar',
  40. data: [5, 20, 36, 10, 10, 20]
  41. }]
  42. }
  43. }
  44. },
  45. methods: {
  46. }
  47. }
  48. </script>
  49. <script module="echarts" lang="renderjs">
  50. let myChart
  51. export default {
  52. mounted() {
  53. if (typeof window.echarts === 'function') {
  54. this.initEcharts()
  55. } else {
  56. // 动态引入较大类库避免影响页面展示
  57. const script = document.createElement('script')
  58. // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
  59. script.src = '/static/js/echarts.js'
  60. script.onload = this.initEcharts.bind(this)
  61. document.head.appendChild(script)
  62. }
  63. },
  64. methods: {
  65. initEcharts() {
  66. myChart = echarts.init(document.getElementById('echarts'))
  67. // 观测更新的数据在 view 层可以直接访问到
  68. myChart.setOption(this.option)
  69. },
  70. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  71. // 监听 service 层数据变更
  72. myChart.setOption(newValue)
  73. },
  74. onClick(event, ownerInstance) {
  75. // 调用 service 层的方法
  76. ownerInstance.callMethod('onViewClick', {
  77. test: 'test'
  78. })
  79. }
  80. }
  81. }
  82. </script>
  83. <style>
  84. </style>