import Chart from "./Chart"; import { isEmpty } from "@vci/helper/src/other"; /** * @class ChartTooltip * @extends Chart */ export default class ChartTooltip extends Chart { #ic; init() { super.init(); this.isChartTooltip = true; this.amtTooltip = { indexCurrent: 0 }; } initEc() { super.initEc(); this._pause = this.pause.bind(this); this._play = this.play.bind(this); this.el.addEventListener("pointermove", this._pause); this.el.addEventListener("pointerleave", this._play); this.ec.on("showTip", e => { if(!isEmpty(e.dataIndex)) this.amtTooltip.indexCurrent = e.dataIndex; if(e.batch && e.batch.length > 0) this.amtTooltip.indexCurrent = e.batch[0].dataIndex; this.dispatchEvent("showTip", { indexCurrent: this.amtTooltip.indexCurrent }); }); setTimeout(this.play.bind(this)); } getLength() { throw new Error("请实现获取数据长度方法 getLength"); } play() { const { indexCurrent } = this.amtTooltip; const length = this.getLength(); this.pause(); this.#ic = Math.min(indexCurrent, length - 1); this.ec.dispatchAction({ type: "showTip", seriesIndex: 0, dataIndex: this.#ic }); this.dispatchEvent("play", { indexCurrent: this.#ic }); if(indexCurrent + 1 >= length) { this.amtTooltip.indexCurrent = 0; this.dispatchEvent("playEnd"); } else this.amtTooltip.indexCurrent++; this.inter.tooltip = setTimeout(this.play.bind(this), 2e3); } pause() { clearTimeout(this.inter.tooltip); } stop() { this.pause(); this.ec.dispatchAction({ type: "downplay" }); } destroy() { super.destroy(); this.el.removeEventListener("pointermove", this._pause); this.el.removeEventListener("pointerleave", this._play); this.stop(); } }