Skip to content

Commit

Permalink
fix(tooltip): fix duplicate key warning for band area charts (#327)
Browse files Browse the repository at this point in the history
A band area chart has the same seriesKey for the Y0 and Y1 values. This cause the tooltip to emit a warning because of two react components with the same key. This commit fix that adding the accessor value as part of the react key

fix #326
  • Loading branch information
markov00 authored Aug 20, 2019
1 parent 577a034 commit 0ca1884
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/chart_types/xy_chart/store/chart_state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ describe('Chart Store', () => {
isHighlighted: false,
isXValue: false,
seriesKey: 'a',
yAccessor: 'y',
};
store.cursorPosition.x = -1;
store.cursorPosition.y = 1;
Expand Down Expand Up @@ -699,6 +700,7 @@ describe('Chart Store', () => {
isHighlighted: false,
isXValue: false,
seriesKey: 'a',
yAccessor: 'y',
};
store.xScale = new ScaleContinuous(ScaleType.Linear, [0, 100], [0, 100]);
store.cursorPosition.x = 1;
Expand Down Expand Up @@ -835,6 +837,7 @@ describe('Chart Store', () => {
isHighlighted: true,
isXValue: false,
seriesKey: 'foo',
yAccessor: 'y',
};
const unhighlightedTooltipValue = {
name: 'foo',
Expand All @@ -843,6 +846,7 @@ describe('Chart Store', () => {
isHighlighted: false,
isXValue: false,
seriesKey: 'foo',
yAccessor: 'y',
};

const expectedRectTooltipState = {
Expand Down Expand Up @@ -870,6 +874,7 @@ describe('Chart Store', () => {
isHighlighted: false,
isXValue: true,
seriesKey: 'headerSeries',
yAccessor: 'y',
};

store.tooltipData.replace([headerValue]);
Expand All @@ -882,6 +887,7 @@ describe('Chart Store', () => {
isHighlighted: false,
isXValue: false,
seriesKey: 'seriesKey',
yAccessor: 'y',
};
store.tooltipData.replace([headerValue, tooltipValue]);

Expand Down
4 changes: 4 additions & 0 deletions src/chart_types/xy_chart/tooltip/tooltip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ describe('Tooltip formatting', () => {
test('format simple tooltip', () => {
const tooltipValue = formatTooltip(indexedGeometry, SPEC_1, false, false, YAXIS_SPEC);
expect(tooltipValue).toBeDefined();
expect(tooltipValue.yAccessor).toBe('y1');
expect(tooltipValue.name).toBe('bar_1');
expect(tooltipValue.isXValue).toBe(false);
expect(tooltipValue.isHighlighted).toBe(false);
Expand All @@ -83,6 +84,7 @@ describe('Tooltip formatting', () => {
};
const tooltipValue = formatTooltip(geometry, SPEC_1, false, false, YAXIS_SPEC);
expect(tooltipValue).toBeDefined();
expect(tooltipValue.yAccessor).toBe('y1');
expect(tooltipValue.name).toBe('y1');
expect(tooltipValue.isXValue).toBe(false);
expect(tooltipValue.isHighlighted).toBe(false);
Expand All @@ -99,6 +101,7 @@ describe('Tooltip formatting', () => {
};
const tooltipValue = formatTooltip(geometry, SPEC_1, false, false, YAXIS_SPEC);
expect(tooltipValue).toBeDefined();
expect(tooltipValue.yAccessor).toBe('y0');
expect(tooltipValue.name).toBe('bar_1');
expect(tooltipValue.isXValue).toBe(false);
expect(tooltipValue.isHighlighted).toBe(false);
Expand All @@ -115,6 +118,7 @@ describe('Tooltip formatting', () => {
};
let tooltipValue = formatTooltip(geometry, SPEC_1, true, false, YAXIS_SPEC);
expect(tooltipValue).toBeDefined();
expect(tooltipValue.yAccessor).toBe('y0');
expect(tooltipValue.name).toBe('bar_1');
expect(tooltipValue.isXValue).toBe(true);
expect(tooltipValue.isHighlighted).toBe(false);
Expand Down
3 changes: 2 additions & 1 deletion src/chart_types/xy_chart/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function formatTooltip(
const { id } = spec;
const {
color,
value: { x, y },
value: { x, y, accessor },
geometryId: { seriesKey },
} = searchIndexValue;
const seriesKeyAsString = getColorValuesAsString(seriesKey, id);
Expand All @@ -49,6 +49,7 @@ export function formatTooltip(
color,
isHighlighted: isXValue ? false : isHighlighted,
isXValue,
yAccessor: accessor,
};
}

Expand Down
2 changes: 2 additions & 0 deletions src/chart_types/xy_chart/utils/interactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BarGeometry, IndexedGeometry, isBarGeometry, isPointGeometry, PointGeometry } from '../rendering/rendering';
import { Datum, Rotation } from './specs';
import { Dimensions } from '../../../utils/dimensions';
import { Accessor } from '../../../utils/accessor';

/** The type of tooltip to use */
export const TooltipType = Object.freeze({
Expand All @@ -27,6 +28,7 @@ export interface TooltipValue {
isHighlighted: boolean;
isXValue: boolean;
seriesKey: string;
yAccessor: Accessor;
}

export type TooltipValueFormatter = (data: TooltipValue) => JSX.Element | string;
Expand Down
4 changes: 2 additions & 2 deletions src/chart_types/xy_chart/utils/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function splitSeries(
const cleanedDatum = cleanDatum(datum, xAccessor, accessor, y0Accessors && y0Accessors[index]);
xValues.add(cleanedDatum.x);
updateSeriesMap(series, [...seriesKey, accessor], cleanedDatum, specId, colorValuesKey);
}, {});
});
} else {
const colorValues = getColorValues(datum, splitSeriesAccessors);
const colorValuesKey = getColorValuesAsString(colorValues, specId);
Expand All @@ -116,7 +116,7 @@ export function splitSeries(
xValues.add(cleanedDatum.x);
updateSeriesMap(series, [...seriesKey], cleanedDatum, specId, colorValuesKey);
}
}, {});
});
return {
rawDataSeries: [...series.values()],
colorsValues,
Expand Down
4 changes: 2 additions & 2 deletions src/components/tooltips.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ class TooltipsComponent extends React.Component<TooltipProps> {
<div className="echTooltip" style={{ transform: tooltipPosition.transform }}>
<div className="echTooltip__header">{this.renderHeader(tooltipData[0], tooltipHeaderFormatter)}</div>
<div className="echTooltip__list">
{tooltipData.slice(1).map(({ name, value, color, isHighlighted, seriesKey }) => {
{tooltipData.slice(1).map(({ name, value, color, isHighlighted, seriesKey, yAccessor }) => {
const classes = classNames('echTooltip__item', {
/* eslint @typescript-eslint/camelcase:0 */
echTooltip__rowHighlighted: isHighlighted,
});
return (
<div
key={seriesKey}
key={`${seriesKey}--${yAccessor}`}
className={classes}
style={{
borderLeftColor: color,
Expand Down
1 change: 1 addition & 0 deletions stories/area_chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ storiesOf('Area Chart', module)
const scaleToDataExtent = boolean('scale to extent', true);
return (
<Chart className={'story-chart'}>
<Settings showLegend legendPosition={Position.Right} />
<Axis
id={getAxisId('bottom')}
title={'timestamp per 1 minute'}
Expand Down

0 comments on commit 0ca1884

Please sign in to comment.