chart-model.vue 1.7 KB

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