diff --git a/packages/vchart/src/chart/base/base-chart.ts b/packages/vchart/src/chart/base/base-chart.ts index 3c8f443749..1f3149189e 100644 --- a/packages/vchart/src/chart/base/base-chart.ts +++ b/packages/vchart/src/chart/base/base-chart.ts @@ -1396,12 +1396,18 @@ export class BaseChart extends CompilableBase implements I }); const isUnableValue = isNil(value) || !dimensionInfo || dimensionInfo.every(d => isDiscrete(d.axis.getScale().type) && isNil(d.index)); + + const isUnableTooltip = + // 如果数据本身不可用 + isUnableValue || + // 或者数据为空 + dimensionInfo.every(d => isDiscrete(d.axis.getScale().type) && d.data.every(_data => _data.datum.length === 0)); // tooltip if (opt.tooltip !== false) { const tooltip = this.getComponentsByType(ComponentTypeEnum.tooltip)[0] as unknown as ITooltip; if (tooltip?.getVisible()) { - if (isUnableValue) { + if (isUnableValue || isUnableTooltip) { (tooltip).hideTooltip?.(); } else { const dataFilter: { [key: string]: any } = {}; @@ -1431,6 +1437,27 @@ export class BaseChart extends CompilableBase implements I } } + showCrosshair(cb: (axis: IAxis) => false | string | number) { + const crosshair = + this.getComponentsByType(ComponentTypeEnum.cartesianCrosshair)[0] ?? + this.getComponentsByType(ComponentTypeEnum.polarCrosshair)[0]; + if (!crosshair) { + return; + } + const axes = this.getComponentsByKey('axes') as IAxis[]; + const dimInfo: { axis: IAxis; value: string | number }[] = []; + axes.forEach(axis => { + const value = cb(axis); + if (value !== false) { + dimInfo.push({ + axis, + value + }); + } + }); + (crosshair as ICrossHair).showCrosshair(dimInfo); + } + getColorScheme() { return this._option.getTheme?.('colorScheme'); } diff --git a/packages/vchart/src/chart/interface/chart.ts b/packages/vchart/src/chart/interface/chart.ts index 01d96d08a1..a25a398d03 100644 --- a/packages/vchart/src/chart/interface/chart.ts +++ b/packages/vchart/src/chart/interface/chart.ts @@ -32,6 +32,7 @@ import type { DataView } from '@visactor/vdataset'; import type { IGlobalScale } from '../../scale/interface'; import type { IMorphConfig } from '../../animation/spec'; import type { IMarkGraphic } from '../../mark/interface/common'; +import type { IAxis } from '../../component/axis/interface'; export type DimensionIndexOption = { filter?: (cmp: IComponent) => boolean; @@ -217,6 +218,12 @@ export interface IChart extends ICompilable { getSeriesData: (id: StringOrNumber | undefined, index: number | undefined) => DataView | undefined; // setDimensionIndex setDimensionIndex: (value: StringOrNumber, opt: DimensionIndexOption) => void; + /** + * 显示交叉线 + * @since 2.0.9 + */ + showCrosshair: (cb: (axis: IAxis) => false | string | number) => void; + /** * 根据数据筛选图元 * @since 1.13.9 diff --git a/packages/vchart/src/core/vchart.ts b/packages/vchart/src/core/vchart.ts index bcd8e012e9..ebcc222ce1 100644 --- a/packages/vchart/src/core/vchart.ts +++ b/packages/vchart/src/core/vchart.ts @@ -1908,6 +1908,10 @@ export class VChart implements IVChart { return this._chart?.setDimensionIndex(value, opt); } + showCrosshair(cb: (axis: IAxis) => false | string | number) { + this._chart?.showCrosshair(cb); + } + /** 停止正在进行的所有动画 */ stopAnimation() { (this.getStage() as any)?.stopAnimation(true);