Skip to content

Commit 794af2d

Browse files
jsilllpan-kot
andauthored
feat: support tooltip on solidgauge (#74)
* feat: support tooltip on solidgauge * fix: improve chart type logic * fix: add solidgauge in page settings --------- Co-authored-by: Andrei Zhaleznichenka <[email protected]>
1 parent e2fe5c2 commit 794af2d

File tree

4 files changed

+94
-4
lines changed

4 files changed

+94
-4
lines changed

pages/03-core/solidgauge.page.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { omit } from "lodash";
5+
6+
import CoreChart from "../../lib/components/internal-do-not-use/core-chart";
7+
import { PageSettingsForm, useChartSettings } from "../common/page-settings";
8+
import { Page } from "../common/templates";
9+
import pseudoRandom from "../utils/pseudo-random";
10+
11+
function randomInt(min: number, max: number) {
12+
return min + Math.floor(pseudoRandom() * (max - min));
13+
}
14+
15+
const series: Highcharts.SeriesOptionsType[] = [
16+
{
17+
name: "CPU Usage",
18+
type: "solidgauge",
19+
data: [randomInt(60, 90)],
20+
},
21+
];
22+
23+
export default function () {
24+
const { chartProps } = useChartSettings({ solidgauge: true });
25+
return (
26+
<Page
27+
title="Solid Gauge Chart Demo"
28+
subtitle="This page demonstrates the use of solid gauge charts for displaying single-value metrics."
29+
settings={
30+
<PageSettingsForm
31+
selectedSettings={["showLegend", "legendPosition", "showLegendTitle", "showLegendActions", "useFallback"]}
32+
/>
33+
}
34+
>
35+
<CoreChart
36+
{...omit(chartProps.cartesian, "ref")}
37+
options={{
38+
series,
39+
chart: {
40+
type: "solidgauge",
41+
},
42+
title: {
43+
text: "Resource Usage",
44+
},
45+
yAxis: {
46+
min: 0,
47+
max: 100,
48+
title: {
49+
text: "Usage",
50+
},
51+
stops: [
52+
[0.1, "#55BF3B"],
53+
[0.5, "#DDDF0D"],
54+
[0.8, "#DF5353"],
55+
],
56+
},
57+
}}
58+
/>
59+
</Page>
60+
);
61+
}

pages/common/page-settings.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export function useChartSettings<SettingsType extends PageSettings = PageSetting
7373
options: {
7474
more?: boolean;
7575
xrange?: boolean;
76+
solidgauge?: boolean;
7677
} = {},
7778
): {
7879
settings: SettingsType;

pages/common/use-highcharts.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import type Highcharts from "highcharts";
66

77
import { isDevelopment } from "@cloudscape-design/component-toolkit/internal";
88

9-
export function useHighcharts({ more = false, xrange = false }: { more?: boolean; xrange?: boolean } = {}) {
9+
export function useHighcharts({
10+
more = false,
11+
xrange = false,
12+
solidgauge = false,
13+
}: { more?: boolean; xrange?: boolean; solidgauge?: boolean } = {}) {
1014
const [highcharts, setHighcharts] = useState<null | typeof Highcharts>(null);
1115

1216
useEffect(() => {
@@ -16,12 +20,15 @@ export function useHighcharts({ more = false, xrange = false }: { more?: boolean
1620
await import("highcharts/modules/accessibility");
1721

1822
// Required for arearange, areasplinerange, columnrange, gauge, boxplot, errorbar, waterfall, polygon, bubble
19-
if (more) {
23+
if (more || solidgauge) {
2024
await import("highcharts/highcharts-more");
2125
}
2226
if (xrange) {
2327
await import("highcharts/modules/xrange");
2428
}
29+
if (solidgauge) {
30+
await import("highcharts/modules/solid-gauge");
31+
}
2532

2633
if (isDevelopment) {
2734
await import("highcharts/modules/debugger");
@@ -31,7 +38,7 @@ export function useHighcharts({ more = false, xrange = false }: { more?: boolean
3138
};
3239

3340
load();
34-
}, [more, xrange]);
41+
}, [more, xrange, solidgauge]);
3542

3643
return highcharts;
3744
}

src/core/chart-api/chart-extra-tooltip.tsx

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,12 @@ export class ChartExtraTooltip extends AsyncStore<ReactiveTooltipState> {
9898
}
9999

100100
private onRenderTooltip = (props: { point: null | Highcharts.Point; group: readonly Highcharts.Point[] }) => {
101-
if (this.context.chart().series.some((s) => s.type === "pie")) {
101+
const chartType =
102+
this.context.chart().series.find((s) => s.type === "pie" || s.type === "solidgauge")?.type ?? "cartesian";
103+
if (chartType === "pie") {
102104
return this.onRenderTooltipPie(props.group[0]);
105+
} else if (chartType === "solidgauge") {
106+
return this.onRenderTooltipSolidGauge(props.group[0]);
103107
} else {
104108
return this.onRenderTooltipCartesian(props);
105109
}
@@ -128,6 +132,13 @@ export class ChartExtraTooltip extends AsyncStore<ReactiveTooltipState> {
128132
this.targetTrack.rect(this.context.chart().renderer, { ...pointRect, ...this.commonTrackAttrs });
129133
};
130134

135+
private onRenderTooltipSolidGauge = (point: Highcharts.Point) => {
136+
// Solid gauge charts are similar to pie charts in that they have a circular shape
137+
// and don't support grouping. We use a similar approach to pie charts.
138+
const pointRect = getSolidGaugeTargetPlacement(point);
139+
this.targetTrack.rect(this.context.chart().renderer, { ...pointRect, ...this.commonTrackAttrs });
140+
};
141+
131142
private get commonTrackAttrs() {
132143
return { fill: "transparent", zIndex: -1, style: "pointer-events:none", direction: this.direction };
133144
}
@@ -217,3 +228,13 @@ function getPieMiddlePlacement(point: Highcharts.Point): Rect {
217228
(typeof relativeDiameter === "number" ? relativeDiameter : (relativeDiameter / 100) * chart.plotWidth) / 2;
218229
return { x: centerX, y: centerY - radius, width: 1, height: 2 * radius };
219230
}
231+
232+
function getSolidGaugeTargetPlacement(point: Highcharts.Point): Rect {
233+
const chart = point.series.chart;
234+
return {
235+
x: chart.plotLeft + chart.plotWidth / 2,
236+
y: chart.plotTop + chart.plotHeight / 2,
237+
width: 0.1,
238+
height: 0.1,
239+
};
240+
}

0 commit comments

Comments
 (0)