VciChart.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <div class="VciChart"></div>
  3. </template>
  4. <script>
  5. import Chart from "./Chart";
  6. import { isEmpty } from "@vci/helper/src/other";
  7. export default {
  8. name: "VciChart",
  9. props: {
  10. chart: {
  11. type: Function,
  12. default: null
  13. },
  14. chartOption: {
  15. type: Object,
  16. default: null
  17. },
  18. chartData: {
  19. type: [Array, Object, Number],
  20. default: null
  21. },
  22. renderOption: {
  23. type: Object,
  24. default: null
  25. },
  26. customRender: {
  27. type: Function,
  28. default: null
  29. },
  30. onAfterRender: {
  31. type: Function,
  32. default: null
  33. },
  34. enableWatchElement: {
  35. type: Boolean,
  36. default: false
  37. }
  38. },
  39. data() {
  40. return {};
  41. },
  42. watch: {
  43. chartData: {
  44. immediate: true,
  45. deep: true,
  46. handler: "render"
  47. }
  48. },
  49. mounted() {
  50. this.enableWatchElement && this.watchElement();
  51. },
  52. methods: {
  53. render() {
  54. this.$nextTick(() => {
  55. if(!this.ci) {
  56. if(!this.chart) {
  57. console.info("请传入图表类");
  58. return false;
  59. }
  60. this.ci = new this.chart({
  61. el: this.$el,
  62. ...(this.chartOption || {})
  63. });
  64. if(!(this.ci instanceof Chart)) {
  65. console.warn(`图表类不是继承于"@vci/vue/v2/components/chart"`);
  66. delete this.ci;
  67. return false;
  68. }
  69. this.$once("hook:beforeDestroy", () => this.ci && this.ci.destroy());
  70. }
  71. if(this.customRender) this.customRender && this.customRender(this.ci, this.chartData);
  72. else !isEmpty(this.chartData) && this.ci.render(this.chartData, this.renderOption);
  73. this.onAfterRender && this.onAfterRender(this.ci);
  74. });
  75. },
  76. watchElement() {
  77. const observer = new MutationObserver(() => this.ci && this.ci.resize());
  78. observer.observe(this.$el, { childList: true, subtree: true });
  79. this.$on("hook:beforeDestroy", () => observer.disconnect());
  80. }
  81. }
  82. };
  83. </script>
  84. <style lang="scss" scoped>
  85. .VciChart {
  86. width: 100%;
  87. height: 100%;
  88. }
  89. </style>