|
| 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 CoreChart from "../../lib/components/internal-do-not-use/core-chart"; |
| 8 | +import { dateFormatter, numberFormatter } from "../common/formatters"; |
| 9 | +import { PageSettingsForm, useChartSettings } from "../common/page-settings"; |
| 10 | +import { Page } from "../common/templates"; |
| 11 | + |
| 12 | +const baseline = [ |
| 13 | + { x: 1600984800000, y1: 58020, y2: 45 }, |
| 14 | + { x: 1600985700000, y1: 102402, y2: 52 }, |
| 15 | + { x: 1600986600000, y1: 104920, y2: 48 }, |
| 16 | + { x: 1600987500000, y1: 94031, y2: 55 }, |
| 17 | + { x: 1600988400000, y1: 125021, y2: 62 }, |
| 18 | + { x: 1600989300000, y1: 159219, y2: 58 }, |
| 19 | + { x: 1600990200000, y1: 193082, y2: 71 }, |
| 20 | + { x: 1600991100000, y1: 162592, y2: 65 }, |
| 21 | + { x: 1600992000000, y1: 274021, y2: 78 }, |
| 22 | + { x: 1600992900000, y1: 264286, y2: 82 }, |
| 23 | + { x: 1600993800000, y1: 289210, y2: 75 }, |
| 24 | + { x: 1600994700000, y1: 256362, y2: 68 }, |
| 25 | + { x: 1600995600000, y1: 257306, y2: 72 }, |
| 26 | + { x: 1600996500000, y1: 186776, y2: 58 }, |
| 27 | + { x: 1600997400000, y1: 294020, y2: 85 }, |
| 28 | + { x: 1600998300000, y1: 385975, y2: 92 }, |
| 29 | + { x: 1600999200000, y1: 486039, y2: 88 }, |
| 30 | + { x: 1601000100000, y1: 490447, y2: 95 }, |
| 31 | + { x: 1601001000000, y1: 361845, y2: 78 }, |
| 32 | + { x: 1601001900000, y1: 339058, y2: 72 }, |
| 33 | + { x: 1601002800000, y1: 298028, y2: 65 }, |
| 34 | + { x: 1601003400000, y1: 255555, y2: 58 }, |
| 35 | + { x: 1601003700000, y1: 231902, y2: 52 }, |
| 36 | + { x: 1601004600000, y1: 224558, y2: 48 }, |
| 37 | + { x: 1601005500000, y1: 253901, y2: 55 }, |
| 38 | + { x: 1601006400000, y1: 102839, y2: 42 }, |
| 39 | + { x: 1601007300000, y1: 234943, y2: 62 }, |
| 40 | + { x: 1601008200000, y1: 204405, y2: 58 }, |
| 41 | + { x: 1601009100000, y1: 190391, y2: 52 }, |
| 42 | + { x: 1601010000000, y1: 183570, y2: 48 }, |
| 43 | + { x: 1601010900000, y1: 162592, y2: 45 }, |
| 44 | + { x: 1601011800000, y1: 148910, y2: 38 }, |
| 45 | + { x: 1601012700000, y1: null, y2: null }, |
| 46 | + { x: 1601013600000, y1: 293910, y2: 72 }, |
| 47 | +]; |
| 48 | + |
| 49 | +const series: Highcharts.SeriesOptionsType[] = [ |
| 50 | + { |
| 51 | + name: "Network Traffic", |
| 52 | + type: "line", |
| 53 | + data: baseline.map(({ x, y1 }) => ({ x, y: y1 })), |
| 54 | + yAxis: "events", |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Request Count", |
| 58 | + type: "line", |
| 59 | + data: baseline.map(({ x, y1 }) => ({ x, y: y1 === null ? null : y1 * 0.8 })), |
| 60 | + yAxis: "events", |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "CPU Usage", |
| 64 | + type: "line", |
| 65 | + data: baseline.map(({ x, y2 }) => ({ x, y: y2 })), |
| 66 | + yAxis: "percentage", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "Memory Usage", |
| 70 | + type: "line", |
| 71 | + data: baseline.map(({ x, y2 }) => ({ x, y: y2 === null ? null : y2 * 0.9 })), |
| 72 | + yAxis: "percentage", |
| 73 | + }, |
| 74 | +]; |
| 75 | + |
| 76 | +export default function () { |
| 77 | + const { chartProps } = useChartSettings(); |
| 78 | + return ( |
| 79 | + <Page |
| 80 | + title="Dual axis chart demo" |
| 81 | + subtitle="Chart with multiple legends and two y-axes." |
| 82 | + settings={ |
| 83 | + <PageSettingsForm |
| 84 | + selectedSettings={[ |
| 85 | + "showLegend", |
| 86 | + "legendPosition", |
| 87 | + "legendHorizontalAlign", |
| 88 | + "showLegendTitle", |
| 89 | + "showLegendActions", |
| 90 | + "useFallback", |
| 91 | + ]} |
| 92 | + /> |
| 93 | + } |
| 94 | + > |
| 95 | + <CoreChart |
| 96 | + {...omit(chartProps.cartesian, "ref")} |
| 97 | + highcharts={Highcharts} |
| 98 | + options={{ |
| 99 | + lang: { |
| 100 | + accessibility: { |
| 101 | + chartContainerLabel: "Dual axis line chart", |
| 102 | + }, |
| 103 | + }, |
| 104 | + series: series, |
| 105 | + xAxis: [ |
| 106 | + { |
| 107 | + type: "datetime", |
| 108 | + title: { text: "Time (UTC)" }, |
| 109 | + valueFormatter: dateFormatter, |
| 110 | + }, |
| 111 | + ], |
| 112 | + yAxis: [ |
| 113 | + { |
| 114 | + title: { text: "Events" }, |
| 115 | + id: "events", |
| 116 | + }, |
| 117 | + { |
| 118 | + title: { text: "Percentage (%)" }, |
| 119 | + opposite: true, |
| 120 | + id: "percentage", |
| 121 | + }, |
| 122 | + ], |
| 123 | + chart: { |
| 124 | + zooming: { |
| 125 | + type: "x", |
| 126 | + }, |
| 127 | + }, |
| 128 | + }} |
| 129 | + chartHeight={400} |
| 130 | + tooltip={{ placement: "outside" }} |
| 131 | + getTooltipContent={() => ({ |
| 132 | + point({ item }) { |
| 133 | + const value = item ? (item.point.y ?? null) : null; |
| 134 | + return { |
| 135 | + value: <div>{numberFormatter(value)}</div>, |
| 136 | + }; |
| 137 | + }, |
| 138 | + })} |
| 139 | + /> |
| 140 | + </Page> |
| 141 | + ); |
| 142 | +} |
0 commit comments