Skip to content

Commit c356b2f

Browse files
authored
feat: Adds support for line dash styles in cartesian charts (#114)
1 parent 77bc148 commit c356b2f

File tree

5 files changed

+88
-14
lines changed

5 files changed

+88
-14
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { range } from "lodash";
5+
6+
import { CartesianChart, CartesianChartProps } from "../../lib/components";
7+
import { dateFormatter } from "../common/formatters";
8+
import { useChartSettings } from "../common/page-settings";
9+
import { Page } from "../common/templates";
10+
11+
const startDate = new Date(1600984800000); // 2020-09-25T06:00:00.000Z
12+
const endDate = new Date(1601014500000); // 2020-09-25T14:00:00.000Z
13+
const domain = range(startDate.getTime(), endDate.getTime(), 15 * 60 * 1000).map((time) => new Date(time));
14+
const rawData = [
15+
58020, 102402, 104920, 94031, 125021, 159219, 193082, 162592, 274021, 264286, 289210, 256362, 257306, 186776, 294020,
16+
385975, 486039, 490447, 361845, 339058, 298028, 231902, 224558, 253901, 102839, 234943, 204405, 190391, 183570,
17+
162592, 148910, 229492, 293910,
18+
];
19+
20+
function createSeries(offset: number, dashStyle?: Highcharts.DashStyleValue): CartesianChartProps.SeriesOptions {
21+
return {
22+
name: `spline-${dashStyle ?? "default"}`,
23+
type: "spline",
24+
dashStyle,
25+
data: rawData.map((y, index) => ({
26+
x: domain[index].getTime(),
27+
y: offset + y + Math.round((index * 0.5 - 20) * (index + 5) * 5000),
28+
})),
29+
};
30+
}
31+
const series: CartesianChartProps.SeriesOptions[] = [
32+
createSeries(0),
33+
createSeries(50_000, "Dash"),
34+
createSeries(250_000, "DashDot"),
35+
createSeries(440_000, "Dot"),
36+
createSeries(789_000, "LongDash"),
37+
createSeries(999_000, "LongDashDot"),
38+
createSeries(1_132_000, "LongDashDotDot"),
39+
createSeries(1_350_000, "ShortDash"),
40+
createSeries(1_400_000, "ShortDashDot"),
41+
createSeries(1_560_000, "ShortDashDotDot"),
42+
createSeries(1_880_000, "ShortDot"),
43+
createSeries(2_000_000, "Solid"),
44+
{ type: "x-threshold", name: "threshold-default", value: 1601000100000 },
45+
{ type: "y-threshold", name: "threshold-DashDot", value: 1_500_000, dashStyle: "DashDot" },
46+
];
47+
48+
export default function () {
49+
const { chartProps } = useChartSettings();
50+
return (
51+
<Page title="Dash style demo" subtitle="This pages shows all supported line dash styles for line-like series.">
52+
<CartesianChart
53+
{...chartProps.cartesian}
54+
chartHeight={700}
55+
ariaLabel="Line chart"
56+
series={series}
57+
xAxis={{
58+
type: "datetime",
59+
title: "Time (UTC)",
60+
min: domain[0].getTime(),
61+
max: domain[domain.length - 1].getTime(),
62+
valueFormatter: dateFormatter,
63+
}}
64+
yAxis={{ title: "Bytes transferred" }}
65+
/>
66+
</Page>
67+
);
68+
}

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)