ChartsItem.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <div :id="id" :class="className" :style="{ height: height, width: width }" />
  3. </template>
  4. <script>
  5. import * as echarts from "echarts";
  6. import resize from "./mixins";
  7. export default {
  8. name: "ChartItem",
  9. // 应对须要定制化调用echartApi的场景
  10. mixins: [resize],
  11. props: {
  12. className: {
  13. type: String,
  14. default: "chart",
  15. },
  16. id: {
  17. type: String,
  18. default: "chart",
  19. },
  20. width: {
  21. type: String,
  22. default: "100%",
  23. },
  24. height: {
  25. type: String,
  26. default: "100%",
  27. },
  28. options: {
  29. type: Object,
  30. default: () => {
  31. return {};
  32. },
  33. },
  34. echartApi: {
  35. type: Function,
  36. default: () => {},
  37. },
  38. },
  39. watch: {
  40. options(newV) {
  41. console.log(newV);
  42. this.chart.setOption(newV, true);
  43. this.useApi();
  44. deep: true;
  45. },
  46. watch: {
  47. $route(to, from) {
  48. this.chart.resize();
  49. },
  50. },
  51. },
  52. data() {
  53. return {
  54. chart: null,
  55. };
  56. },
  57. created() {},
  58. mounted() {
  59. // window.addEventListener("resize", () => {
  60. // this.chart && this.chart.resize();
  61. // });
  62. this.initChart();
  63. this.useApi();
  64. },
  65. beforeDestroy() {
  66. if (!this.chart) {
  67. return;
  68. }
  69. this.chart.dispose();
  70. this.chart = null;
  71. },
  72. methods: {
  73. initChart() {
  74. this.chart = echarts.init(document.getElementById(this.id));
  75. const myChart = this.chart;
  76. myChart.clear();
  77. myChart.setOption(this.options);
  78. },
  79. useApi() {
  80. // 如果有需要默认加载的echartsAPI 在此处执行
  81. this.echartApi && this.echartApi(this.chart);
  82. },
  83. },
  84. };
  85. </script>