echart.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <template>
  2. <div :id="'echarts-' + _uid"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts';
  6. export default {
  7. name: 'zz-echart',
  8. props: ['option'],
  9. data() {
  10. return {
  11. echart: ''
  12. };
  13. },
  14. mounted() {
  15. this.initEchart();
  16. },
  17. computed: {
  18. resize() {
  19. // 通过scale值来判断窗口是变化
  20. return this.$store.getters['getScale'];
  21. },
  22. collapse() {
  23. return this.$store.getters['getCollapse'];
  24. }
  25. },
  26. methods: {
  27. initEchart() {
  28. this.echart = echarts.init(document.getElementById('echarts-' + this._uid));
  29. if (this.option) {
  30. this.echart.setOption(this.option);
  31. this.echart.on('rendered', () => {
  32. this.$emit('finish', this.echart);
  33. });
  34. this.echart.on('mouseover', (p) => {
  35. this.$emit('mouseover', this.echart, p);
  36. });
  37. }
  38. }
  39. },
  40. watch: {
  41. resize() {
  42. this.echart.resize();
  43. },
  44. collapse() {
  45. this.echart.resize();
  46. },
  47. option: {
  48. deep: true,
  49. handler(n) {
  50. this.echart.dispose();
  51. // this.echart.setOption(n);
  52. this.initEchart();
  53. }
  54. }
  55. },
  56. destroyed() {
  57. this.echart.dispose();
  58. }
  59. };
  60. </script>
  61. <style lang="scss" scoped>
  62. [id^='echarts-'] {
  63. width: 100%;
  64. height: calc(100% - 2px);
  65. }
  66. </style>