|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { omit } from "lodash"; |
| 5 | + |
| 6 | +import CoreChart from "../../lib/components/internal-do-not-use/core-chart"; |
| 7 | +import { dateFormatter } from "../common/formatters"; |
| 8 | +import { PageSettingsForm, useChartSettings } from "../common/page-settings"; |
| 9 | +import { Page } from "../common/templates"; |
| 10 | +import pseudoRandom from "../utils/pseudo-random"; |
| 11 | + |
| 12 | +function randomInt(min: number, max: number) { |
| 13 | + return min + Math.floor(pseudoRandom() * (max - min)); |
| 14 | +} |
| 15 | + |
| 16 | +function shuffle<T>(array: T[]): void { |
| 17 | + let currentIndex = array.length; |
| 18 | + while (currentIndex !== 0) { |
| 19 | + const randomIndex = Math.floor(Math.random() * currentIndex); |
| 20 | + currentIndex--; |
| 21 | + [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +const colors = [ |
| 26 | + "#F15C80", |
| 27 | + "#2B908F", |
| 28 | + "#F45B5B", |
| 29 | + "#91E8E1", |
| 30 | + "#8085E9", |
| 31 | + "#E4D354", |
| 32 | + "#8D4654", |
| 33 | + "#7798BF", |
| 34 | + "#AAEEEE", |
| 35 | + "#FF9655", |
| 36 | +]; |
| 37 | + |
| 38 | +const dashStyles: Highcharts.DashStyleValue[] = [ |
| 39 | + "Dash", |
| 40 | + "DashDot", |
| 41 | + "Dot", |
| 42 | + "LongDash", |
| 43 | + "LongDashDot", |
| 44 | + "LongDashDotDot", |
| 45 | + "ShortDash", |
| 46 | + "ShortDashDot", |
| 47 | + "ShortDashDotDot", |
| 48 | + "ShortDot", |
| 49 | + "Solid", |
| 50 | +]; |
| 51 | + |
| 52 | +const baseline = [ |
| 53 | + { x: 1600984800000, y: 58020 }, |
| 54 | + { x: 1600985700000, y: 102402 }, |
| 55 | + { x: 1600986600000, y: 104920 }, |
| 56 | + { x: 1600987500000, y: 94031 }, |
| 57 | + { x: 1600988400000, y: 125021 }, |
| 58 | + { x: 1600989300000, y: 159219 }, |
| 59 | + { x: 1600990200000, y: 193082 }, |
| 60 | + { x: 1600991100000, y: 162592 }, |
| 61 | + { x: 1600992000000, y: 274021 }, |
| 62 | + { x: 1600992900000, y: 264286 }, |
| 63 | + { x: 1600993800000, y: 289210 }, |
| 64 | + { x: 1600994700000, y: 256362 }, |
| 65 | + { x: 1600995600000, y: 257306 }, |
| 66 | + { x: 1600996500000, y: 186776 }, |
| 67 | + { x: 1600997400000, y: 294020 }, |
| 68 | + { x: 1600998300000, y: 385975 }, |
| 69 | + { x: 1600999200000, y: 486039 }, |
| 70 | + { x: 1601000100000, y: 490447 }, |
| 71 | + { x: 1601001000000, y: 361845 }, |
| 72 | + { x: 1601001900000, y: 339058 }, |
| 73 | + { x: 1601002800000, y: 298028 }, |
| 74 | + { x: 1601003400000, y: 255555 }, |
| 75 | + { x: 1601003700000, y: 231902 }, |
| 76 | + { x: 1601004600000, y: 224558 }, |
| 77 | + { x: 1601005500000, y: 253901 }, |
| 78 | + { x: 1601006400000, y: 102839 }, |
| 79 | + { x: 1601007300000, y: 234943 }, |
| 80 | + { x: 1601008200000, y: 204405 }, |
| 81 | + { x: 1601009100000, y: 190391 }, |
| 82 | + { x: 1601010000000, y: 183570 }, |
| 83 | + { x: 1601010900000, y: 162592 }, |
| 84 | + { x: 1601011800000, y: 148910 }, |
| 85 | +]; |
| 86 | + |
| 87 | +const generatePrimaryAxisData = (letter: string, index: number) => { |
| 88 | + return baseline.map(({ x, y }) => ({ |
| 89 | + name: `Events ${letter}`, |
| 90 | + x, |
| 91 | + y: y === null ? null : y + randomInt(-100000 * ((index % 3) + 1), 100000 * ((index % 3) + 1)), |
| 92 | + })); |
| 93 | +}; |
| 94 | + |
| 95 | +const generateSecondaryAxisData = (letter: string, index: number) => { |
| 96 | + return baseline.map(({ x, y }) => ({ |
| 97 | + name: `Percentage ${letter}`, |
| 98 | + x, |
| 99 | + y: y === null ? null : (y / 10000) * randomInt(3 + (index % 5), 10 + (index % 10)), |
| 100 | + })); |
| 101 | +}; |
| 102 | + |
| 103 | +const primarySeriesData: Record<string, any[]> = {}; |
| 104 | +for (let i = 0; i < 10; i++) { |
| 105 | + const letter = String.fromCharCode(65 + i); |
| 106 | + primarySeriesData[`data${letter}`] = generatePrimaryAxisData(letter, i); |
| 107 | +} |
| 108 | + |
| 109 | +const secondarySeriesData: Record<string, any[]> = {}; |
| 110 | +for (let i = 0; i < 10; i++) { |
| 111 | + const letter = String.fromCharCode(65 + i); |
| 112 | + secondarySeriesData[`data${letter}`] = generateSecondaryAxisData(letter, i); |
| 113 | +} |
| 114 | + |
| 115 | +const series: Highcharts.SeriesOptionsType[] = []; |
| 116 | + |
| 117 | +Object.entries(primarySeriesData).forEach(([, data], index) => { |
| 118 | + series.push({ |
| 119 | + name: data[0].name, |
| 120 | + type: "line", |
| 121 | + data: data, |
| 122 | + yAxis: 0, |
| 123 | + color: colors[index], |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +Object.entries(secondarySeriesData).forEach(([, data], index) => { |
| 128 | + series.push({ |
| 129 | + name: data[0].name, |
| 130 | + type: "line", |
| 131 | + data: data, |
| 132 | + yAxis: 1, |
| 133 | + color: colors[index], |
| 134 | + dashStyle: dashStyles[index % dashStyles.length], |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +shuffle(series); |
| 139 | + |
| 140 | +export default function () { |
| 141 | + const { chartProps } = useChartSettings(); |
| 142 | + return ( |
| 143 | + <Page |
| 144 | + title="Core dual-axis chart demo" |
| 145 | + subtitle="This page demonstrates the use of the core chart with two Y axes for displaying data with different scales." |
| 146 | + settings={ |
| 147 | + <PageSettingsForm |
| 148 | + selectedSettings={[ |
| 149 | + "showLegend", |
| 150 | + "legendType", |
| 151 | + "legendPosition", |
| 152 | + "legendBottomMaxHeight", |
| 153 | + "showLegendTitle", |
| 154 | + "showOppositeLegendTitle", |
| 155 | + "showLegendActions", |
| 156 | + ]} |
| 157 | + /> |
| 158 | + } |
| 159 | + > |
| 160 | + <CoreChart |
| 161 | + {...omit(chartProps.cartesian, "ref")} |
| 162 | + chartHeight={400} |
| 163 | + ariaLabel="Dual axis line chart" |
| 164 | + tooltip={{ placement: "outside" }} |
| 165 | + options={{ |
| 166 | + series: series, |
| 167 | + xAxis: [ |
| 168 | + { |
| 169 | + type: "datetime", |
| 170 | + title: { text: "Time (UTC)" }, |
| 171 | + valueFormatter: dateFormatter, |
| 172 | + }, |
| 173 | + ], |
| 174 | + yAxis: [ |
| 175 | + { |
| 176 | + title: { text: "Events" }, |
| 177 | + }, |
| 178 | + { |
| 179 | + opposite: true, |
| 180 | + title: { text: "Percentage (%)" }, |
| 181 | + }, |
| 182 | + ], |
| 183 | + }} |
| 184 | + /> |
| 185 | + </Page> |
| 186 | + ); |
| 187 | +} |
0 commit comments