Skip to content

Commit bea13c2

Browse files
committed
feat: Adds support for line dash styles in cartesian charts
1 parent 80a31a1 commit bea13c2

File tree

5 files changed

+117
-14
lines changed

5 files changed

+117
-14
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { CartesianChart, CartesianChartProps } from "../../lib/components";
5+
import { dateFormatter } from "../common/formatters";
6+
import { useChartSettings } from "../common/page-settings";
7+
import { Page } from "../common/templates";
8+
9+
const domain = [
10+
new Date(1600984800000),
11+
new Date(1600985700000),
12+
new Date(1600986600000),
13+
new Date(1600987500000),
14+
new Date(1600988400000),
15+
new Date(1600989300000),
16+
new Date(1600990200000),
17+
new Date(1600991100000),
18+
new Date(1600992000000),
19+
new Date(1600992900000),
20+
new Date(1600993800000),
21+
new Date(1600994700000),
22+
new Date(1600995600000),
23+
new Date(1600996500000),
24+
new Date(1600997400000),
25+
new Date(1600998300000),
26+
new Date(1600999200000),
27+
new Date(1601000100000),
28+
new Date(1601001000000),
29+
new Date(1601001900000),
30+
new Date(1601002800000),
31+
new Date(1601003700000),
32+
new Date(1601004600000),
33+
new Date(1601005500000),
34+
new Date(1601006400000),
35+
new Date(1601007300000),
36+
new Date(1601008200000),
37+
new Date(1601009100000),
38+
new Date(1601010000000),
39+
new Date(1601010900000),
40+
new Date(1601011800000),
41+
new Date(1601012700000),
42+
new Date(1601013600000),
43+
];
44+
const rawData = [
45+
58020, 102402, 104920, 94031, 125021, 159219, 193082, 162592, 274021, 264286, 289210, 256362, 257306, 186776, 294020,
46+
385975, 486039, 490447, 361845, 339058, 298028, 231902, 224558, 253901, 102839, 234943, 204405, 190391, 183570,
47+
162592, 148910, 229492, 293910,
48+
].map((v) => v * 0.9);
49+
50+
const series: CartesianChartProps.SeriesOptions[] = [];
51+
function createSeries(offset: number, dashStyle?: Highcharts.DashStyleValue): CartesianChartProps.SeriesOptions {
52+
return {
53+
name: `spline-${dashStyle ?? "default"}`,
54+
type: "spline",
55+
dashStyle,
56+
data: rawData.map((y, index) => ({
57+
x: domain[index].getTime(),
58+
y: offset + y + Math.round((index * 0.5 - 20) * (index + 5) * 5000),
59+
})),
60+
};
61+
}
62+
series.push(createSeries(0));
63+
series.push(createSeries(50_000, "Dash"));
64+
series.push(createSeries(250_000, "DashDot"));
65+
series.push(createSeries(440_000, "Dot"));
66+
series.push(createSeries(789_000, "LongDash"));
67+
series.push(createSeries(999_000, "LongDashDot"));
68+
series.push(createSeries(1_132_000, "LongDashDotDot"));
69+
series.push(createSeries(1_350_000, "ShortDash"));
70+
series.push(createSeries(1_400_000, "ShortDashDot"));
71+
series.push(createSeries(1_560_000, "ShortDashDotDot"));
72+
series.push(createSeries(1_880_000, "ShortDot"));
73+
series.push(createSeries(2_000_000, "Solid"));
74+
series.push({ type: "x-threshold", name: "threshold-default", value: 1601000100000 });
75+
series.push({ type: "y-threshold", name: "threshold-DashDot", value: 1_500_000, dashStyle: "DashDot" });
76+
77+
export default function () {
78+
const { chartProps } = useChartSettings();
79+
return (
80+
<Page title="Dash style demo" subtitle="This pages shows all supported line dash styles for line-like series.">
81+
<CartesianChart
82+
{...chartProps.cartesian}
83+
chartHeight={700}
84+
ariaLabel="Line chart"
85+
series={series}
86+
xAxis={{
87+
type: "datetime",
88+
title: "Time (UTC)",
89+
min: domain[0].getTime(),
90+
max: domain[domain.length - 1].getTime(),
91+
valueFormatter: dateFormatter,
92+
}}
93+
yAxis={{ title: "Bytes transferred" }}
94+
/>
95+
</Page>
96+
);
97+
}

pages/01-cartesian-chart/many-series.page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const loremIpsum =
1313
export default function () {
1414
return (
1515
<Page
16-
title="Legend demo"
16+
title="Chart with many series"
1717
subtitle="This pages shows how legend works in a chart with many series."
1818
settings={
1919
<PageSettingsForm

src/cartesian-chart/chart-series-cartesian.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export const transformCartesianSeries = (
2727
const seriesId = getOptionsId(s);
2828
const style = { ...Styles.thresholdPlotLine, color: s.color ?? Styles.thresholdPlotLine.color };
2929
if (s.type === "x-threshold" && visibleSeries.includes(seriesId)) {
30-
xPlotLines.push({ id: seriesId, value: s.value, ...style });
30+
xPlotLines.push({ id: seriesId, value: s.value, ...style, dashStyle: s.dashStyle ?? style.dashStyle });
3131
}
3232
if (s.type === "y-threshold" && visibleSeries.includes(seriesId)) {
33-
yPlotLines.push({ id: seriesId, value: s.value, ...style });
33+
yPlotLines.push({ id: seriesId, value: s.value, ...style, dashStyle: s.dashStyle ?? style.dashStyle });
3434
}
3535
}
3636
// The threshold series require data points to enable keyboard navigation and hover effects.

src/cartesian-chart/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function transformLineLikeSeries<
108108
| CartesianChartProps.SplineSeriesOptions,
109109
>(s: S): null | S {
110110
const data = transformPointData(s.data);
111-
return { type: s.type, id: s.id, name: s.name, color: s.color, data } as S;
111+
return { type: s.type, id: s.id, name: s.name, color: s.color, dashStyle: s.dashStyle, data } as S;
112112
}
113113

114114
function transformColumnSeries<S extends CartesianChartProps.ColumnSeriesOptions>(s: S): null | S {
@@ -125,7 +125,7 @@ function transformScatterSeries<S extends CartesianChartProps.ScatterSeriesOptio
125125
function transformThresholdSeries<
126126
S extends CartesianChartProps.XThresholdSeriesOptions | CartesianChartProps.YThresholdSeriesOptions,
127127
>(s: S): null | S {
128-
return { type: s.type, id: s.id, name: s.name, color: s.color, value: s.value } as S;
128+
return { type: s.type, id: s.id, name: s.name, color: s.color, value: s.value, dashStyle: s.dashStyle } as S;
129129
}
130130

131131
function transformErrorBarSeries(

src/core/interfaces.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ export interface BaseTooltipPointFormatted {
188188
subItems?: ReadonlyArray<{ key: React.ReactNode; value: React.ReactNode }>;
189189
}
190190

191-
export interface AreaSeriesOptions extends BaseCartesianSeriesOptions {
191+
export interface AreaSeriesOptions extends BaseCartesianLineLikeOptions {
192192
type: "area";
193193
data: readonly PointDataItemType[];
194194
}
195195

196-
export interface AreaSplineSeriesOptions extends BaseCartesianSeriesOptions {
196+
export interface AreaSplineSeriesOptions extends BaseCartesianLineLikeOptions {
197197
type: "areaspline";
198198
data: readonly PointDataItemType[];
199199
}
@@ -203,12 +203,12 @@ export interface ColumnSeriesOptions extends BaseCartesianSeriesOptions {
203203
data: readonly PointDataItemType[];
204204
}
205205

206-
export interface LineSeriesOptions extends BaseCartesianSeriesOptions {
206+
export interface LineSeriesOptions extends BaseCartesianLineLikeOptions {
207207
type: "line";
208208
data: readonly PointDataItemType[];
209209
}
210210

211-
export interface SplineSeriesOptions extends BaseCartesianSeriesOptions {
211+
export interface SplineSeriesOptions extends BaseCartesianLineLikeOptions {
212212
type: "spline";
213213
data: readonly PointDataItemType[];
214214
}
@@ -226,22 +226,22 @@ export interface ErrorBarSeriesOptions extends Omit<BaseCartesianSeriesOptions,
226226
linkedTo: string;
227227
}
228228

229-
export interface XThresholdSeriesOptions extends BaseCartesianSeriesOptions {
229+
export interface XThresholdSeriesOptions extends BaseCartesianLineLikeOptions {
230230
type: "x-threshold";
231231
value: number;
232232
}
233233

234-
export interface YThresholdSeriesOptions extends BaseCartesianSeriesOptions {
234+
export interface YThresholdSeriesOptions extends BaseCartesianLineLikeOptions {
235235
type: "y-threshold";
236236
value: number;
237237
}
238238

239-
export interface PieSeriesOptions extends BaseCartesianSeriesOptions {
239+
export interface PieSeriesOptions extends BaseSeriesOptions {
240240
type: "pie";
241241
data: readonly PieSegmentOptions[];
242242
}
243243

244-
export interface DonutSeriesOptions extends BaseCartesianSeriesOptions {
244+
export interface DonutSeriesOptions extends BaseSeriesOptions {
245245
type: "donut";
246246
data: readonly PieSegmentOptions[];
247247
}
@@ -266,12 +266,18 @@ export interface RangeDataItemOptions {
266266
high: number;
267267
}
268268

269-
interface BaseCartesianSeriesOptions {
269+
interface BaseSeriesOptions {
270270
id?: string;
271271
name: string;
272272
color?: string;
273273
}
274274

275+
type BaseCartesianSeriesOptions = BaseSeriesOptions;
276+
277+
interface BaseCartesianLineLikeOptions extends BaseCartesianSeriesOptions {
278+
dashStyle?: Highcharts.DashStyleValue;
279+
}
280+
275281
// The symbols union matches that of Highcharts.PointMarkerOptionsObject
276282
interface PointMarkerOptions {
277283
symbol?: "circle" | "diamond" | "square" | "triangle" | "triangle-down";

0 commit comments

Comments
 (0)