ChartHighLight.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import Chart from "./Chart";
  2. import { isEmpty } from "@vci/helper/src/other";
  3. export default class ChartHighLight extends Chart {
  4. #ic;
  5. init() {
  6. super.init();
  7. this.isChartHighLight = true;
  8. this.amtHighLight = { indexCurrent: 0 };
  9. }
  10. initEc() {
  11. super.initEc();
  12. this._pause = this.pause.bind(this);
  13. this._play = this.play.bind(this);
  14. this.el.addEventListener("pointermove", this._pause);
  15. this.el.addEventListener("pointerleave", this._play);
  16. this.ec.on("highlight", e => {
  17. if(!isEmpty(e.dataIndex)) this.amtHighLight.indexCurrent = e.dataIndex;
  18. if(e.batch && e.batch.length > 0) this.amtHighLight.indexCurrent = e.batch[0].dataIndex;
  19. this.dispatchEvent("highlight", { indexCurrent: this.amtHighLight.indexCurrent });
  20. });
  21. }
  22. getLength() {
  23. throw new Error("请实现获取数据长度方法 getLength");
  24. }
  25. play() {
  26. const { indexCurrent } = this.amtHighLight;
  27. const length = this.getLength();
  28. this.pause();
  29. !isEmpty(this.#ic) && this.ec.dispatchAction({
  30. type: "downplay",
  31. dataIndex: this.#ic
  32. });
  33. this.#ic = Math.min(indexCurrent, length - 1);
  34. this.ec.dispatchAction({
  35. type: "highlight",
  36. dataIndex: this.#ic
  37. });
  38. this.dispatchEvent("play", { indexCurrent: this.#ic });
  39. if(indexCurrent + 1 >= length) {
  40. this.amtHighLight.indexCurrent = 0;
  41. this.dispatchEvent("playEnd");
  42. } else this.amtHighLight.indexCurrent++;
  43. this.inter.highLight = setTimeout(this.play.bind(this), 2e3);
  44. }
  45. pause() {
  46. clearTimeout(this.inter.highLight);
  47. }
  48. stop() {
  49. this.pause();
  50. this.ec.dispatchAction({ type: "downplay" });
  51. }
  52. destroy() {
  53. super.destroy();
  54. this.el.removeEventListener("pointermove", this._pause);
  55. this.el.removeEventListener("pointerleave", this._play);
  56. this.stop();
  57. }
  58. }