rootEchart.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <!--
  2. echart组件
  3. @author 刘梦祥
  4. @date 2023年2月9日
  5. -->
  6. <template>
  7. <div name="mechart" ref="mechart" class="mechart"></div>
  8. </template>
  9. <script>
  10. export default {
  11. props: {
  12. option: Object
  13. },
  14. data() {
  15. return {
  16. myChart: ""
  17. };
  18. },
  19. computed: {
  20. watchOptionStr() {
  21. let a = this.option;
  22. return JSON.stringify(a);
  23. }
  24. },
  25. watch: {
  26. watchOptionStr: {
  27. handler: function(val, oldVal) {
  28. if (val !== oldVal && this.myChart) {
  29. this.myChart.setOption(this.option, true);
  30. }
  31. }
  32. }
  33. },
  34. mounted() {
  35. window.addEventListener("resize", this.resizeChart);
  36. this.$nextTick(() => {
  37. this.initChart();
  38. });
  39. },
  40. beforeDestroy() {
  41. window.removeEventListener("resize", this.resizeChart);
  42. if (this.myChart) {
  43. this.myChart.dispose();
  44. }
  45. },
  46. methods: {
  47. resizeChart() {
  48. if (this.myChart !== "") {
  49. this.myChart.resize();
  50. }
  51. },
  52. initChart() {
  53. this.myChart = this.$echarts.init(this.$refs.mechart);
  54. if (this.option) {
  55. this.myChart.setOption(this.option);
  56. }
  57. }
  58. }
  59. };
  60. </script>
  61. <style lang="less" scoped>
  62. .mechart {
  63. position: relative;
  64. width: 100%;
  65. height: 100%;
  66. }
  67. </style>