Skip to content

Commit 651edd1

Browse files
authored
fix(tooltip): fix tooltip formatting for rotated charts (#285)
The X and Y axis spec, used to format the tooltip, needs to be inverted when rendering a rotated chart. fix #273
1 parent c8b206c commit 651edd1

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/chart_types/xy_chart/store/chart_state.interactions.test.ts

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { BarGeometry } from '../rendering/rendering';
22
import { computeXScale, computeYScales } from '../utils/scales';
33
import { DataSeriesColorsValues } from '../utils/series';
4-
import { BarSeriesSpec, BasicSeriesSpec, RectAnnotationSpec } from '../utils/specs';
5-
import { getAnnotationId, getGroupId, getSpecId } from '../../../utils/ids';
4+
import { BarSeriesSpec, BasicSeriesSpec, RectAnnotationSpec, Position } from '../utils/specs';
5+
import { getAnnotationId, getGroupId, getSpecId, getAxisId } from '../../../utils/ids';
66
import { TooltipType } from '../utils/interactions';
77
import { ScaleContinuous } from '../../../utils/scales/scale_continuous';
88
import { ScaleType } from '../../../utils/scales/scales';
@@ -392,4 +392,42 @@ function mouseOverTestSuite(scaleType: ScaleType) {
392392
expect(store.tooltipPosition.transform).toBe(expectedTransform);
393393
});
394394
});
395+
describe('can format tooltip values on rotated chart', () => {
396+
beforeEach(() => {
397+
store.addAxisSpec({
398+
hide: true,
399+
id: getAxisId('yaxis'),
400+
groupId: GROUP_ID,
401+
position: Position.Left,
402+
tickFormat: (value) => `left ${Number(value)}`,
403+
showOverlappingLabels: false,
404+
showOverlappingTicks: false,
405+
tickPadding: 0,
406+
tickSize: 0,
407+
});
408+
store.addAxisSpec({
409+
hide: true,
410+
id: getAxisId('xaxis'),
411+
groupId: GROUP_ID,
412+
position: Position.Bottom,
413+
tickFormat: (value) => `bottom ${Number(value)}`,
414+
showOverlappingLabels: false,
415+
showOverlappingTicks: false,
416+
tickPadding: 0,
417+
tickSize: 0,
418+
});
419+
});
420+
test('chart 0 rotation', () => {
421+
store.setCursorPosition(chartLeft + 0, chartTop + 99);
422+
expect(store.tooltipData[0].value).toBe('bottom 0');
423+
expect(store.tooltipData[1].value).toBe('left 10');
424+
});
425+
426+
test('chart 90 deg rotated', () => {
427+
store.chartRotation = 90;
428+
store.setCursorPosition(chartLeft + 0, chartTop + 99);
429+
expect(store.tooltipData[0].value).toBe('left 1');
430+
expect(store.tooltipData[1].value).toBe('bottom 5');
431+
});
432+
});
395433
}

src/chart_types/xy_chart/store/chart_state.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,13 @@ export class ChartStore {
367367
}
368368

369369
// format the tooltip values
370-
const formattedTooltip = formatTooltip(indexedGeometry, spec, false, isHighlighted, yAxis);
371-
370+
const yAxisFormatSpec = [0, 180].includes(this.chartRotation) ? yAxis : xAxis;
371+
const formattedTooltip = formatTooltip(indexedGeometry, spec, false, isHighlighted, yAxisFormatSpec);
372372
// format only one time the x value
373373
if (!xValueInfo) {
374374
// if we have a tooltipHeaderFormatter, then don't pass in the xAxis as the user will define a formatter
375-
const formatterAxis = this.tooltipHeaderFormatter ? undefined : xAxis;
375+
const xAxisFormatSpec = [0, 180].includes(this.chartRotation) ? xAxis : yAxis;
376+
const formatterAxis = this.tooltipHeaderFormatter ? undefined : xAxisFormatSpec;
376377
xValueInfo = formatTooltip(indexedGeometry, spec, true, false, formatterAxis);
377378
return [xValueInfo, ...acc, formattedTooltip];
378379
}

0 commit comments

Comments
 (0)