ChartTooltip.js 1.8 KB

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