Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@visactor/vchart",
"comment": "fix: preserve crosshair visibility for explicitly bound multiple band axes (Issue #4127)",
"type": "patch"
}
],
"packageName": "@visactor/vchart",
"email": "biukam.w@gmail.com"
}
65 changes: 65 additions & 0 deletions packages/vchart/__tests__/unit/core/vchart-event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type StageEventListener = {
};

type CrosshairStateForTest = {
enable?: boolean;
_layoutCrosshair?: (x: number, y: number) => void;
_stateByField?: {
xField?: {
currentValue?: Map<unknown, unknown>;
Expand Down Expand Up @@ -522,6 +524,69 @@ describe('vchart event test', () => {
removeDom(lineContainer);
});

it('should keep crosshair enabled when bound to top and bottom band axes', () => {
const crosshairContainer = createDiv();
const crosshairDom = createDiv(crosshairContainer);
crosshairDom.id = 'multiple-crosshair-axis-container';
crosshairContainer.style.position = 'fixed';
crosshairContainer.style.width = '500px';
crosshairContainer.style.height = '500px';
crosshairContainer.style.top = '0px';
crosshairContainer.style.left = '0px';

const chart = new VChart(
{
type: 'common',
data: {
id: 'data',
values: [
{ x: 'Mon', type: 'Breakfast', y: 15 },
{ x: 'Mon', type: 'Lunch', y: 25 },
{ x: 'Tue', type: 'Breakfast', y: 12 },
{ x: 'Tue', type: 'Lunch', y: 30 }
]
},
series: [
{
type: 'bar',
dataId: 'data',
xField: ['x', 'type'],
yField: 'y',
seriesField: 'type'
}
],
axes: [
{ orient: 'left' },
{ orient: 'bottom', type: 'band' },
{ orient: 'top', type: 'band' },
{ orient: 'top', type: 'band' }
],
crosshair: {
xField: {
visible: true,
bindingAxesIndex: [1, 2]
}
}
} as ICommonChartSpec,
{ dom: crosshairDom, animation: false }
);

try {
chart.renderSync();
const crosshair = chart.getComponents().find(component => component.type === 'cartesianCrosshair') as
| CrosshairStateForTest
| undefined;

crosshair?._layoutCrosshair?.(250, 250);

expect(crosshair?.enable).toBe(true);
expect(Array.from(crosshair?._stateByField?.xField?.currentValue?.keys() ?? [])).toEqual([1, 2]);
} finally {
chart.release();
removeDom(crosshairContainer);
}
});

it('should fire tooltipRelease before release chart', () => {
const handleTooltipRelease = jest.fn();
vchart.on('tooltipRelease', handleTooltipRelease);
Expand Down
10 changes: 8 additions & 2 deletions packages/vchart/src/component/crosshair/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,23 @@ export abstract class BaseCrossHair<T extends ICartesianCrosshairSpec | IPolarCr
* @returns
*/
protected _setAllAxisValues(axisMap: IAxisInfo<IAxis>, point: IPoint, field: string): boolean {
// 首先不能存在两个离散轴
// 未显式绑定轴时,同方向的多个离散轴无法确定唯一的维度位置;显式绑定表示调用方已选择这些轴。
let discrete = false;
let multipleDiscreteAxes = false;
const bindingAxesIndex = get(this._spec, `${field}.bindingAxesIndex`);
const hasExplicitBinding = isArray(bindingAxesIndex) && bindingAxesIndex.length > 0;
axisMap.forEach(item => {
if (isDiscrete(item.axis.getScale().type)) {
if (!discrete) {
discrete = true;
} else {
this.enable = false;
multipleDiscreteAxes = true;
}
}
});
if (multipleDiscreteAxes && !hasExplicitBinding) {
this.enable = false;
}
if (!this.enable) {
return false;
}
Expand Down
Loading