|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { useState } from "react"; |
| 5 | +import Highcharts from "highcharts"; |
| 6 | +import { omit } from "lodash"; |
| 7 | + |
| 8 | +import SpaceBetween from "@cloudscape-design/components/space-between"; |
| 9 | +import Toggle from "@cloudscape-design/components/toggle"; |
| 10 | + |
| 11 | +import CoreChart, { CoreChartProps } from "../../lib/components/internal-do-not-use/core-chart"; |
| 12 | +import { dateFormatter } from "../common/formatters"; |
| 13 | +import { useChartSettings } from "../common/page-settings"; |
| 14 | +import { Page } from "../common/templates"; |
| 15 | +import pseudoRandom from "../utils/pseudo-random"; |
| 16 | + |
| 17 | +function randomInt(min: number, max: number) { |
| 18 | + return min + Math.floor(pseudoRandom() * (max - min)); |
| 19 | +} |
| 20 | + |
| 21 | +const baseline = [ |
| 22 | + { x: 1600984800000, y: 58020 }, |
| 23 | + { x: 1600985700000, y: 102402 }, |
| 24 | + { x: 1600986600000, y: 104920 }, |
| 25 | + { x: 1600987500000, y: 94031 }, |
| 26 | + { x: 1600988400000, y: 125021 }, |
| 27 | + { x: 1600989300000, y: 159219 }, |
| 28 | + { x: 1600990200000, y: 193082 }, |
| 29 | + { x: 1600991100000, y: 162592 }, |
| 30 | + { x: 1600992000000, y: 274021 }, |
| 31 | + { x: 1600992900000, y: 264286 }, |
| 32 | + { x: 1600993800000, y: 289210 }, |
| 33 | + { x: 1600994700000, y: 256362 }, |
| 34 | + { x: 1600995600000, y: 257306 }, |
| 35 | + { x: 1600996500000, y: 186776 }, |
| 36 | + { x: 1600997400000, y: 294020 }, |
| 37 | +]; |
| 38 | + |
| 39 | +const dataA = baseline.map(({ x, y }) => ({ x, y })); |
| 40 | +const dataB = baseline.map(({ x, y }) => ({ x, y: y + randomInt(-100000, 100000) })); |
| 41 | +const dataC = baseline.map(({ x, y }) => ({ x, y: y + randomInt(-150000, 50000) })); |
| 42 | + |
| 43 | +const lineSeries: Highcharts.SeriesOptionsType[] = [ |
| 44 | + { name: "A", type: "line", data: dataA }, |
| 45 | + { name: "B", type: "line", data: dataB }, |
| 46 | + { name: "C", type: "line", data: dataC }, |
| 47 | +]; |
| 48 | + |
| 49 | +const pieSeries: Highcharts.SeriesOptionsType[] = [ |
| 50 | + { |
| 51 | + name: "Average", |
| 52 | + type: "pie", |
| 53 | + data: [ |
| 54 | + { name: "A", y: Math.round(dataA.reduce((acc, { y }) => acc + y, 0) / dataA.length) }, |
| 55 | + { name: "B", y: Math.round(dataB.reduce((acc, { y }) => acc + y, 0) / dataA.length) }, |
| 56 | + { name: "C", y: Math.round(dataC.reduce((acc, { y }) => acc + y, 0) / dataA.length) }, |
| 57 | + ], |
| 58 | + }, |
| 59 | +]; |
| 60 | + |
| 61 | +export default function () { |
| 62 | + const { chartProps } = useChartSettings(); |
| 63 | + const [chartType, setChartType] = useState<"line" | "pie">("line"); |
| 64 | + const options: CoreChartProps.ChartOptions = |
| 65 | + chartType === "line" |
| 66 | + ? { |
| 67 | + series: lineSeries, |
| 68 | + xAxis: [ |
| 69 | + { |
| 70 | + type: "datetime", |
| 71 | + title: { text: "Time (UTC)" }, |
| 72 | + valueFormatter: dateFormatter, |
| 73 | + }, |
| 74 | + ], |
| 75 | + yAxis: [{ title: { text: "Events" } }], |
| 76 | + } |
| 77 | + : { series: pieSeries, xAxis: [], yAxis: [] }; |
| 78 | + return ( |
| 79 | + <Page title="Dynamic chart type"> |
| 80 | + <SpaceBetween size="m"> |
| 81 | + <Toggle checked={chartType === "pie"} onChange={({ detail }) => setChartType(detail.checked ? "pie" : "line")}> |
| 82 | + Show average |
| 83 | + </Toggle> |
| 84 | + |
| 85 | + <CoreChart |
| 86 | + {...omit(chartProps.cartesian, "ref")} |
| 87 | + highcharts={Highcharts} |
| 88 | + ariaLabel="Events chart" |
| 89 | + options={options} |
| 90 | + /> |
| 91 | + </SpaceBetween> |
| 92 | + </Page> |
| 93 | + ); |
| 94 | +} |
0 commit comments