Skip to content

Commit 85bfe06

Browse files
authored
feat(legend): expose extra raw values (#2308)
This commit exposes both the formatted and the raw value in when used in the custom legend. BREAKING CHANGE: The `CustomLegend.item` now exposes both the `raw` and the `formatted` version of the extra value.
1 parent 48cade4 commit 85bfe06

File tree

10 files changed

+44
-43
lines changed

10 files changed

+44
-43
lines changed

packages/charts/api/charts.api.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,10 @@ export interface CustomLegendProps {
819819
label: CategoryLabel;
820820
seriesType?: SeriesType;
821821
pointStyle?: PointStyle;
822-
extraValue?: PrimitiveValue;
822+
extraValue?: {
823+
raw: PrimitiveValue;
824+
formatted: string;
825+
};
823826
isSeriesHidden?: boolean;
824827
onItemOverActon: () => void;
825828
onItemOutAction: () => void;

packages/charts/src/chart_types/partition_chart/layout/viewmodel/hierarchy_of_arrays.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ export function getExtraValueMap(
117117
const [key, arrayNode] = branch;
118118
const { value, path, [CHILDREN_KEY]: children } = arrayNode;
119119
const values: LegendItemExtraValues = new Map();
120-
const formattedValue = valueFormatter ? valueFormatter(value) : value;
121-
values.set(key, formattedValue);
120+
const formattedValue = valueFormatter ? valueFormatter(value) : `${value}`;
121+
values.set(key, { formatted: formattedValue, raw: value });
122122
keys.set(path.map(({ index }) => index).join('__'), values);
123123
if (depth < maxDepth) getExtraValueMap(layers, valueFormatter, children, maxDepth, depth + 1, keys);
124124
}

packages/charts/src/chart_types/xy_chart/legend/legend.test.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@ import { computeSeriesDomainsSelector } from '../state/selectors/compute_series_
2222
import { getSeriesName } from '../utils/series';
2323
import { AxisSpec, BasicSeriesSpec, SeriesType } from '../utils/specs';
2424

25-
const nullDisplayValue = {
26-
raw: null,
27-
formatted: '',
28-
legendSizingLabel: '',
29-
};
25+
const nullDisplayValue = undefined;
3026

3127
const spec1: BasicSeriesSpec = {
3228
chartType: ChartType.XYAxis,

packages/charts/src/chart_types/xy_chart/legend/legend.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,14 @@ export function computeLegend(
135135
isSeriesHidden,
136136
isItemHidden: hideInLegend,
137137
isToggleable: true,
138-
defaultExtra: {
139-
raw: itemValue,
140-
formatted: formattedItemValue,
141-
legendSizingLabel: formattedItemValue,
142-
},
138+
defaultExtra:
139+
itemValue !== null
140+
? {
141+
raw: itemValue,
142+
formatted: formattedItemValue,
143+
legendSizingLabel: formattedItemValue,
144+
}
145+
: undefined,
143146
path: [{ index: 0, value: seriesIdentifier.key }],
144147
keys: [specId, spec.groupId, yAccessor, ...series.splitAccessors.values()],
145148
pointStyle,
@@ -159,11 +162,14 @@ export function computeLegend(
159162
isSeriesHidden,
160163
isItemHidden: hideInLegend,
161164
isToggleable: true,
162-
defaultExtra: {
163-
raw: bandedItemValue,
164-
formatted: bandedFormattedItemValue,
165-
legendSizingLabel: bandedFormattedItemValue,
166-
},
165+
defaultExtra:
166+
bandedItemValue !== null
167+
? {
168+
raw: bandedItemValue,
169+
formatted: bandedFormattedItemValue,
170+
legendSizingLabel: bandedFormattedItemValue,
171+
}
172+
: undefined,
167173
path: [{ index: 0, value: seriesIdentifier.key }],
168174
keys: [specId, spec.groupId, yAccessor, ...series.splitAccessors.values()],
169175
pointStyle,

packages/charts/src/chart_types/xy_chart/tooltip/tooltip.ts

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,13 @@ export const Y0_ACCESSOR_POSTFIX = ' - lower';
2323
export const Y1_ACCESSOR_POSTFIX = ' - upper';
2424

2525
/** @internal */
26-
export function getLegendItemExtraValues(
27-
tooltipValues: TooltipValue[],
28-
defaultValue?: string,
29-
): Map<SeriesKey, LegendItemExtraValues> {
26+
export function getLegendItemExtraValues(tooltipValues: TooltipValue[]): Map<SeriesKey, LegendItemExtraValues> {
3027
const seriesTooltipValues = new Map<SeriesKey, LegendItemExtraValues>();
3128

32-
tooltipValues.forEach(({ formattedValue, seriesIdentifier, valueAccessor }) => {
33-
const seriesValue = defaultValue || formattedValue;
29+
tooltipValues.forEach(({ formattedValue, value, seriesIdentifier, valueAccessor }) => {
3430
const current: LegendItemExtraValues = seriesTooltipValues.get(seriesIdentifier.key) ?? new Map();
35-
if (defaultValue) {
36-
if (!current.has(BandedAccessorType.Y0)) {
37-
current.set(BandedAccessorType.Y0, defaultValue);
38-
}
39-
if (!current.has(BandedAccessorType.Y1)) {
40-
current.set(BandedAccessorType.Y1, defaultValue);
41-
}
42-
}
43-
4431
if (valueAccessor === BandedAccessorType.Y0 || valueAccessor === BandedAccessorType.Y1) {
45-
current.set(valueAccessor, seriesValue);
32+
current.set(valueAccessor, { formatted: formattedValue, raw: value });
4633
}
4734
seriesTooltipValues.set(seriesIdentifier.key, current);
4835
});

packages/charts/src/common/legend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ export type LegendItem = {
4343
};
4444

4545
/** @internal */
46-
export type LegendItemExtraValues = Map<LegendItemChildId, PrimitiveValue>;
46+
export type LegendItemExtraValues = Map<LegendItemChildId, { raw: PrimitiveValue; formatted: string }>;

packages/charts/src/components/legend/extra.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import React from 'react';
1313
* @param extra
1414
* @param isSeriesHidden
1515
*/
16-
export function renderExtra(extra: string | number) {
16+
export function renderExtra(extra: string) {
1717
return (
1818
<div className="echLegendItem__extra" title={`${extra}`}>
1919
{extra}

packages/charts/src/components/legend/legend_item.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class LegendListItem extends Component<LegendItemProps, LegendItemState>
190190
'echLegendItem--vertical': positionConfig.direction === LayoutDirection.Vertical,
191191
});
192192
const hasColorPicker = Boolean(colorPicker);
193-
const extra = showExtra && getExtra(extraValues, item, totalItems);
193+
const extra = showExtra ? getExtra(extraValues, item, totalItems) : null;
194194
const style: CSSProperties = flatLegend
195195
? {}
196196
: {
@@ -225,7 +225,7 @@ export class LegendListItem extends Component<LegendItemProps, LegendItemState>
225225
onToggle={this.onLabelToggle(seriesIdentifiers)}
226226
isSeriesHidden={isSeriesHidden}
227227
/>
228-
{extra && !isSeriesHidden && renderExtra(extra)}
228+
{extra && !isSeriesHidden && renderExtra(extra.formatted)}
229229
{Action && (
230230
<div className="echLegendItem__action">
231231
<Action series={seriesIdentifiers} color={color} label={label} />

packages/charts/src/components/legend/utils.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@
66
* Side Public License, v 1.
77
*/
88

9+
import { PrimitiveValue } from '../../chart_types/partition_chart/layout/utils/group_by_rollup';
910
import { LegendItemExtraValues, LegendItem } from '../../common/legend';
1011

1112
/** @internal */
12-
export function getExtra(extraValues: Map<string, LegendItemExtraValues>, item: LegendItem, totalItems: number) {
13+
export function getExtra(
14+
extraValues: Map<string, LegendItemExtraValues>,
15+
item: LegendItem,
16+
totalItems: number,
17+
): { raw: PrimitiveValue; formatted: string } | null {
1318
const { seriesIdentifiers, defaultExtra, childId, path } = item;
1419
// don't show extra if the legend item is associated with multiple series
1520
if (extraValues.size === 0 || seriesIdentifiers.length > 1 || !seriesIdentifiers[0]) {
16-
return defaultExtra?.formatted ?? '';
21+
return defaultExtra ? { formatted: `${defaultExtra.formatted ?? ''}`, raw: defaultExtra.raw } : null;
1722
}
1823
const [{ key }] = seriesIdentifiers;
1924
const extraValueKey = path.map(({ index }) => index).join('__');
2025
const itemExtraValues = extraValues.has(extraValueKey) ? extraValues.get(extraValueKey) : extraValues.get(key);
21-
const actionExtra = childId !== undefined && itemExtraValues?.get(childId);
22-
return actionExtra ?? (extraValues.size === totalItems ? defaultExtra?.formatted : null) ?? '';
26+
const actionExtra = childId !== undefined ? itemExtraValues?.get(childId) : undefined;
27+
return actionExtra
28+
? actionExtra
29+
: extraValues.size === totalItems && defaultExtra
30+
? { formatted: `${defaultExtra.formatted ?? ''}`, raw: defaultExtra.raw }
31+
: null;
2332
}

packages/charts/src/specs/settings.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ export interface CustomLegendProps {
390390
label: CategoryLabel;
391391
seriesType?: SeriesType;
392392
pointStyle?: PointStyle;
393-
extraValue?: PrimitiveValue;
393+
extraValue?: { raw: PrimitiveValue; formatted: string };
394394
isSeriesHidden?: boolean;
395395
onItemOverActon: () => void;
396396
onItemOutAction: () => void;

0 commit comments

Comments
 (0)