echarts.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <div>
  3. <div v-if="flag" :ref="id" class="charts-div" />
  4. </div>
  5. </template>
  6. <script>
  7. import Vue from 'vue';
  8. import Component from 'vue-class-component';
  9. import * as echarts from 'echarts';
  10. @Component({
  11. props: {
  12. id: {
  13. type: String,
  14. default: 'myChart',
  15. },
  16. data: {
  17. type: Object,
  18. default: null,
  19. },
  20. s: {
  21. type: String,
  22. default: '',
  23. },
  24. },
  25. computed: {
  26. result() {
  27. const ca = JSON.parse(JSON.stringify(this.data));
  28. return ca;
  29. },
  30. },
  31. watch: {
  32. result() { // 监听data数据是否有变化
  33. this.drawLine();
  34. console.log('111111111', this.data);
  35. },
  36. },
  37. })
  38. export default class Echarts extends Vue {
  39. flag = true;
  40. myChart = null;
  41. created() {
  42. // const that = this;
  43. // window.onresize = () => (() => {
  44. // that.myChart.setOption(that.data, true);
  45. // console.log('1111111111');
  46. // })();
  47. this.$nextTick(() => {
  48. if (!this.myChart) {
  49. if (this.s) {
  50. this.myChart = echarts.init(this.$refs[this.id], this.s);
  51. } else {
  52. this.myChart = echarts.init(this.$refs[this.id]);
  53. }
  54. this.$emit('onload', this.myChart);
  55. }
  56. this.drawLine();
  57. });
  58. }
  59. drawLine() {
  60. if (this.data && this.myChart) {
  61. // 绘制图表
  62. this.myChart.setOption(this.data);
  63. }
  64. }
  65. }
  66. </script>
  67. <style scoped lang="scss">
  68. .charts-div {
  69. width: 100%;
  70. height: 100%;
  71. }
  72. </style>