Chart.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { mergeDeep } from "@vci/helper/src/object";
  2. import * as echarts from "echarts";
  3. import { clearScheduledTasks } from "@vci/helper/src/browser";
  4. import { viewport } from "../../../../other/adapt";
  5. import EventDispatcher from "@vci/helper/src/EventDispatcher";
  6. /**
  7. * 图表组件
  8. */
  9. export default class Chart extends EventDispatcher {
  10. #proportion;
  11. constructor(option) {
  12. super();
  13. this.option = mergeDeep({
  14. el: null,
  15. enableClearBeforeRender: false
  16. }, option);
  17. this.init();
  18. }
  19. init() {
  20. this.isChart = true;
  21. this.el = this.option.el;
  22. this.inter = {};
  23. this.initEc();
  24. this.initEcOption();
  25. }
  26. initEcOption(ecOption) {
  27. if(!ecOption) throw new Error("请设置初始化图表[echarts]配置");
  28. this.ecOption = ecOption;
  29. }
  30. initEc() {
  31. this.ec = echarts.init(this.el);
  32. this.#updateProportion();
  33. this.#addAdapt();
  34. }
  35. #setData(data) {
  36. this._data = data;
  37. }
  38. getData() {
  39. return this._data;
  40. }
  41. #setRenderOption(renderOption) {
  42. this._renderOption = renderOption;
  43. }
  44. getRenderOption() {
  45. return this._renderOption;
  46. }
  47. render(data, renderOption) {
  48. this.#setData(data);
  49. this.#setRenderOption(renderOption);
  50. this.option.enableClearBeforeRender && this.ec.clear();
  51. this.updateEcOption(this.ecOption, data, renderOption);
  52. this.ec.setOption(this.ecOption);
  53. }
  54. // eslint-disable-next-line no-unused-vars
  55. updateEcOption(ecOption, data, renderOption) {
  56. throw new Error("请实现渲染前数据配置函数 updateEcOption)");
  57. }
  58. #updateProportion() {
  59. this.#proportion = {
  60. width: window.innerWidth / viewport.width,
  61. height: window.innerHeight / viewport.height
  62. };
  63. }
  64. w(px) {
  65. return px * this.#proportion.width;
  66. }
  67. h(hpx) {
  68. return hpx * this.#proportion.height;
  69. }
  70. #addAdapt() {
  71. this.#removeAdapt();
  72. this._resize = this.resize.bind(this);
  73. window.addEventListener("resize", this._resize);
  74. }
  75. #removeAdapt() {
  76. if(this._resize) {
  77. window.removeEventListener("resize", this._resize);
  78. delete this._resize;
  79. }
  80. }
  81. resize() {
  82. clearTimeout(this.inter.resize);
  83. this.inter.resize = setTimeout(() => {
  84. this.#updateProportion();
  85. this.initEcOption();
  86. this.dispatchEvent("beforeResize");
  87. this.ec && this.ec.resize();
  88. this.render(this._data, this._renderOption);
  89. this.dispatchEvent("afterResize");
  90. }, 100);
  91. }
  92. destroy() {
  93. this.#removeAdapt();
  94. clearScheduledTasks(this.inter);
  95. this.ec && this.ec.dispose();
  96. }
  97. }