forked from cloudscape-design/chart-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore-tooltip.tsx
More file actions
325 lines (308 loc) · 11.4 KB
/
core-tooltip.tsx
File metadata and controls
325 lines (308 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { useState } from "react";
import { warnOnce } from "@cloudscape-design/component-toolkit/internal";
import Box from "@cloudscape-design/components/box";
import { InternalChartTooltip } from "@cloudscape-design/components/internal/do-not-use/chart-tooltip";
import LiveRegion from "@cloudscape-design/components/live-region";
import ChartSeriesDetails, { ChartSeriesDetailItem } from "../../internal/components/series-details";
import { useSelector } from "../../internal/utils/async-store";
import { getChartSeries } from "../../internal/utils/chart-series";
import { useDebouncedComponent } from "../../internal/utils/use-debounced-component";
import { ChartAPI } from "../chart-api";
import { getFormatter } from "../formatters";
import { BaseI18nStrings, CoreChartProps } from "../interfaces";
import { getPointColor, getSeriesColor, getSeriesId, getSeriesMarkerType, isXThreshold } from "../utils";
import styles from "../styles.css.js";
const MIN_VISIBLE_BLOCK_SIZE = 200;
const DEFAULT_DEBOUNCE = 200;
type ExpandedSeriesState = Record<string, Set<string>>;
interface ExpandedSeriesStateProps {
expandedSeries: ExpandedSeriesState;
setExpandedSeries: (cb: (state: ExpandedSeriesState) => ExpandedSeriesState) => void;
}
interface RenderedTooltipContent {
header: React.ReactNode;
body: React.ReactNode;
footer: React.ReactNode;
}
interface MatchedItem {
point: Highcharts.Point;
errorRanges: Highcharts.Point[];
}
export function ChartTooltip({
placement = "target",
size,
getTooltipContent: getTooltipContentOverrides,
api,
i18nStrings,
debounce = false,
}: CoreChartProps.TooltipOptions & {
i18nStrings?: BaseI18nStrings;
getTooltipContent?: CoreChartProps.GetTooltipContent;
api: ChartAPI;
}) {
const [expandedSeries, setExpandedSeries] = useState<ExpandedSeriesState>({});
const tooltip = useSelector(api.tooltipStore, (s) => s);
const debouncedTooltip = useDebouncedComponent(tooltip, debounce === true ? DEFAULT_DEBOUNCE : debounce || 0);
if (!debouncedTooltip || !debouncedTooltip.visible || debouncedTooltip.group.length === 0) {
return null;
}
const chart = debouncedTooltip.group[0]?.series.chart;
const renderers = getTooltipContentOverrides?.({ point: debouncedTooltip.point, group: debouncedTooltip.group });
const getTrack = placement === "target" ? api.getTargetTrack : api.getGroupTrack;
const position = (() => {
if (placement === "target" || placement === "middle") {
return !chart.inverted ? "right" : "bottom";
} else {
return !chart.inverted ? "bottom" : "right";
}
})();
const content = getTooltipContent(api, {
renderers,
point: debouncedTooltip.point,
group: debouncedTooltip.group,
expandedSeries,
setExpandedSeries,
hideTooltip: () => {
api.hideTooltip();
},
});
if (!content) {
return null;
}
return (
<InternalChartTooltip
getTrack={getTrack}
trackKey={getTrackKey(debouncedTooltip.point, debouncedTooltip.group)}
container={null}
dismissButton={debouncedTooltip.pinned}
dismissAriaLabel={i18nStrings?.detailPopoverDismissAriaLabel}
onDismiss={api.onDismissTooltip}
onMouseEnter={api.onMouseEnterTooltip}
onMouseLeave={api.onMouseLeaveTooltip}
title={content.header}
footer={
content.footer ? (
<>
<hr aria-hidden={true} />
{content.footer}
</>
) : null
}
size={size}
position={position}
minVisibleBlockSize={MIN_VISIBLE_BLOCK_SIZE}
>
<LiveRegion>{content.body}</LiveRegion>
</InternalChartTooltip>
);
}
function getTrackKey(point: null | Highcharts.Point, group: readonly Highcharts.Point[]) {
const pointId = point && (point.options.id || point.options.name);
if (point && pointId) {
return `p-${pointId}-${point.x}-${point.y}`;
}
if (point) {
return `p-${point.x}-${point.y}`;
}
return `g_${group.length}_${group[0].x}`;
}
function getTooltipContent(
api: ChartAPI,
props: CoreChartProps.GetTooltipContentProps & {
renderers?: CoreChartProps.TooltipContentRenderer;
hideTooltip: () => void;
} & ExpandedSeriesStateProps,
): null | RenderedTooltipContent {
if (props.point && props.point.series.type === "pie") {
return getTooltipContentPie(api, { ...props, point: props.point });
} else if (props.group.length > 0 && props.group[0].series.type !== "pie") {
return getTooltipContentCartesian(api, props);
} else {
return null;
}
}
function getTooltipContentCartesian(
api: ChartAPI,
{
point,
group,
expandedSeries,
renderers = {},
setExpandedSeries,
hideTooltip,
}: CoreChartProps.GetTooltipContentProps & {
renderers?: CoreChartProps.TooltipContentRenderer;
hideTooltip: () => void;
} & ExpandedSeriesStateProps,
): RenderedTooltipContent {
// The cartesian tooltip might or might not have a selected point, but it always has a non-empty group.
// By design, every point of the group has the same x value.
const x = group[0].x;
const chart = group[0].series.chart;
const getSeriesMarker = (series: Highcharts.Series) =>
api.renderMarker(getSeriesMarkerType(series), getSeriesColor(series), true);
const matchedItems = findTooltipSeriesItems(getChartSeries(chart.series), group);
const detailItems: ChartSeriesDetailItem[] = matchedItems.map((item) => {
const valueFormatter = getFormatter(item.point.series.yAxis);
const itemY = isXThreshold(item.point.series) ? null : (item.point.y ?? null);
const customContent = renderers.point
? renderers.point({
item,
hideTooltip,
})
: undefined;
return {
key: customContent?.key ?? item.point.series.name,
value: customContent?.value ?? valueFormatter(itemY),
marker: getSeriesMarker(item.point.series),
subItems: customContent?.subItems,
expandableId: customContent?.expandable ? item.point.series.name : undefined,
highlighted: item.point.x === point?.x && item.point.y === point?.y,
description:
customContent?.description === undefined && item.errorRanges.length ? (
<>
{item.errorRanges.map((errorBarPoint, index) => (
<div key={index} className={styles["error-range"]}>
<span>{errorBarPoint.series.userOptions.name ? errorBarPoint.series.userOptions.name : ""}</span>
<span>
{valueFormatter(errorBarPoint.options.low ?? 0)} - {valueFormatter(errorBarPoint.options.high ?? 0)}
</span>
</div>
))}
</>
) : (
customContent?.description
),
};
});
// We only support cartesian charts with a single x axis.
const titleFormatter = getFormatter(chart.xAxis[0]);
const slotRenderProps: CoreChartProps.TooltipSlotProps = {
x,
items: matchedItems,
hideTooltip: hideTooltip,
};
return {
header: renderers.header?.(slotRenderProps) ?? titleFormatter(x),
body: renderers.body?.(slotRenderProps) ?? (
<ChartSeriesDetails
details={detailItems}
expandedSeries={expandedSeries[x]}
setExpandedState={(id, isExpanded) => {
setExpandedSeries((oldState) => {
const expandedSeriesInCurrentCoordinate = new Set(oldState[x]);
if (isExpanded) {
expandedSeriesInCurrentCoordinate.add(id);
} else {
expandedSeriesInCurrentCoordinate.delete(id);
}
return { ...oldState, [x]: expandedSeriesInCurrentCoordinate };
});
}}
/>
),
footer: renderers.footer?.(slotRenderProps),
};
}
function getTooltipContentPie(
api: ChartAPI,
{
point,
renderers = {},
hideTooltip,
}: { point: Highcharts.Point } & { renderers?: CoreChartProps.TooltipContentRenderer; hideTooltip: () => void },
): RenderedTooltipContent {
const tooltipDetails: CoreChartProps.TooltipSlotProps = {
x: point.x,
items: [{ point, errorRanges: [] }],
hideTooltip,
};
return {
header: renderers.header?.(tooltipDetails) ?? (
<div className={styles["tooltip-default-header"]}>
{api.renderMarker(getSeriesMarkerType(point.series), getPointColor(point))}
<Box variant="span" fontWeight="bold">
{point.name}
</Box>
</div>
),
body:
renderers.body?.(tooltipDetails) ??
(renderers.details ? (
<ChartSeriesDetails
details={renderers.details({
point,
hideTooltip,
})}
compactList={true}
/>
) : (
// We expect all pie chart segments to have defined y values. We use y=0 as fallback
// because the property is optional in Highcharts types.
<ChartSeriesDetails details={[{ key: point.series.name, value: point.y ?? 0 }]} />
)),
footer: renderers.footer?.(tooltipDetails),
};
}
function findTooltipSeriesItems(
series: readonly Highcharts.Series[],
group: readonly Highcharts.Point[],
): MatchedItem[] {
const seriesOrder = series.reduce((d, s, i) => d.set(s, i), new Map<Highcharts.Series, number>());
const getSeriesIndex = (s: Highcharts.Series) => seriesOrder.get(s) ?? -1;
const seriesErrors = new Map<string, Highcharts.Point[]>();
const matchedSeries = new Set<Highcharts.Series>();
const matchedItems: MatchedItem[] = [];
for (const point of group) {
// We only support errorbar series that are linked to other series. In tooltip content we add
// linked error bars to series detail slot.
if (point.series.type === "errorbar") {
if (point.series.linkedParent) {
addError(getSeriesId(point.series.linkedParent), point);
} else {
warnOnce(
"chart-components",
'The `linkedTo` property of "errorbar" series is missing, or points to a series that does not exist.',
);
}
} else {
addMatchedPoints(point);
}
}
function addError(seriesId: string, errorPoint: Highcharts.Point) {
if (errorPoint.options.low !== undefined && errorPoint.options.high !== undefined) {
const errorRanges = seriesErrors.get(seriesId) ?? [];
errorRanges.push(errorPoint);
seriesErrors.set(seriesId, errorRanges);
}
}
function addMatchedPoints(point: Highcharts.Point) {
if (!matchedSeries.has(point.series)) {
matchedSeries.add(point.series);
if (isXThreshold(point.series)) {
matchedItems.push({ point, errorRanges: [] });
} else {
point.series.data
.filter((d) => d.x === point.x)
.sort((a, b) => (a.y ?? 0) - (b.y ?? 0))
.forEach((point) => matchedItems.push({ point, errorRanges: [] }));
}
}
}
return (
matchedItems
// We sort matched items by series order. If there are multiple items that belong to the same series, we sort them by value.
.sort((i1, i2) => {
const s1 = getSeriesIndex(i1.point.series) - getSeriesIndex(i2.point.series);
return s1 || (i1.point.y ?? 0) - (i2.point.y ?? 0);
})
// For each series item we add linked error ranges, sorted by their respective series indices.
.map((item) => {
const errorRanges = seriesErrors.get(getSeriesId(item.point.series)) ?? [];
const sortedErrorRanges = errorRanges.sort((i1, i2) => getSeriesIndex(i1.series) - getSeriesIndex(i2.series));
return { ...item, errorRanges: sortedErrorRanges };
})
);
}