Skip to content

Commit ba82454

Browse files
committed
feat: support tooltip on solidgauge
1 parent baeb4e7 commit ba82454

File tree

2 files changed

+97
-1
lines changed

2 files changed

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

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,22 @@ 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+
let hasPieSeries = false;
102+
let hasSolidGaugeSeries = false;
103+
for (const s of this.context.chart().series) {
104+
if (s.type === "pie") {
105+
hasPieSeries = true;
106+
break;
107+
}
108+
if (s.type === "solidgauge") {
109+
hasSolidGaugeSeries = true;
110+
break;
111+
}
112+
}
113+
if (hasPieSeries) {
102114
return this.onRenderTooltipPie(props.group[0]);
115+
} else if (hasSolidGaugeSeries) {
116+
return this.onRenderTooltipSolidGauge(props.group[0]);
103117
} else {
104118
return this.onRenderTooltipCartesian(props);
105119
}
@@ -128,6 +142,13 @@ export class ChartExtraTooltip extends AsyncStore<ReactiveTooltipState> {
128142
this.targetTrack.rect(this.context.chart().renderer, { ...pointRect, ...this.commonTrackAttrs });
129143
};
130144

145+
private onRenderTooltipSolidGauge = (point: Highcharts.Point) => {
146+
// Solid gauge charts are similar to pie charts in that they have a circular shape
147+
// and don't support grouping. We use a similar approach to pie charts.
148+
const pointRect = getSolidGaugeTargetPlacement(point);
149+
this.targetTrack.rect(this.context.chart().renderer, { ...pointRect, ...this.commonTrackAttrs });
150+
};
151+
131152
private get commonTrackAttrs() {
132153
return { fill: "transparent", zIndex: -1, style: "pointer-events:none", direction: this.direction };
133154
}
@@ -217,3 +238,13 @@ function getPieMiddlePlacement(point: Highcharts.Point): Rect {
217238
(typeof relativeDiameter === "number" ? relativeDiameter : (relativeDiameter / 100) * chart.plotWidth) / 2;
218239
return { x: centerX, y: centerY - radius, width: 1, height: 2 * radius };
219240
}
241+
242+
function getSolidGaugeTargetPlacement(point: Highcharts.Point): Rect {
243+
const chart = point.series.chart;
244+
return {
245+
x: chart.plotLeft + chart.plotWidth / 2,
246+
y: chart.plotTop + chart.plotHeight / 2,
247+
width: 0.1,
248+
height: 0.1,
249+
};
250+
}

0 commit comments

Comments
 (0)