|
| 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 ColumnLayout from "@cloudscape-design/components/column-layout"; |
| 7 | +import SpaceBetween from "@cloudscape-design/components/space-between"; |
| 8 | + |
| 9 | +import { CoreChartProps } from "../../lib/components/core/interfaces"; |
| 10 | +import CoreChart from "../../lib/components/internal-do-not-use/core-chart"; |
| 11 | +import { PageSettingsForm, useChartSettings } from "../common/page-settings"; |
| 12 | +import { Page } from "../common/templates"; |
| 13 | +import pseudoRandom from "../utils/pseudo-random"; |
| 14 | + |
| 15 | +function randomInt(min: number, max: number) { |
| 16 | + return min + Math.floor(pseudoRandom() * (max - min)); |
| 17 | +} |
| 18 | + |
| 19 | +const lineChartData = [ |
| 20 | + { x: 1600984800000, y: 58020 }, |
| 21 | + { x: 1600985700000, y: 102402 }, |
| 22 | + { x: 1600986600000, y: 104920 }, |
| 23 | + { x: 1600987500000, y: 94031 }, |
| 24 | + { x: 1600988400000, y: 125021 }, |
| 25 | + { x: 1600989300000, y: 159219 }, |
| 26 | + { x: 1600990200000, y: 193082 }, |
| 27 | + { x: 1600991100000, y: 162592 }, |
| 28 | + { x: 1600992000000, y: 274021 }, |
| 29 | + { x: 1600992900000, y: 264286 }, |
| 30 | + { x: 1600993800000, y: 289210 }, |
| 31 | + { x: 1600994700000, y: 256362 }, |
| 32 | + { x: 1600995600000, y: 257306 }, |
| 33 | + { x: 1600996500000, y: 186776 }, |
| 34 | + { x: 1600997400000, y: 294020 }, |
| 35 | + { x: 1600998300000, y: 385975 }, |
| 36 | + { x: 1600999200000, y: 486039 }, |
| 37 | + { x: 1601000100000, y: 490447 }, |
| 38 | + { x: 1601001000000, y: 361845 }, |
| 39 | + { x: 1601001900000, y: 339058 }, |
| 40 | + { x: 1601002800000, y: 298028 }, |
| 41 | + { x: 1601003400000, y: 255555 }, |
| 42 | + { x: 1601003700000, y: 231902 }, |
| 43 | + { x: 1601004600000, y: 224558 }, |
| 44 | + { x: 1601005500000, y: 253901 }, |
| 45 | + { x: 1601006400000, y: 102839 }, |
| 46 | + { x: 1601007300000, y: 234943 }, |
| 47 | + { x: 1601008200000, y: 204405 }, |
| 48 | + { x: 1601009100000, y: 190391 }, |
| 49 | + { x: 1601010000000, y: 183570 }, |
| 50 | + { x: 1601010900000, y: 162592 }, |
| 51 | + { x: 1601011800000, y: 148910 }, |
| 52 | +]; |
| 53 | + |
| 54 | +export default function () { |
| 55 | + const { chartProps } = useChartSettings({ solidgauge: true }); |
| 56 | + |
| 57 | + const charts: ((horizontalAlignment: CoreChartProps.LegendOptionsHorizontalAlignment) => CoreChartProps)[] = [ |
| 58 | + (horizontalAlignment) => ({ |
| 59 | + ...omit(chartProps.cartesian, "ref"), |
| 60 | + legend: { |
| 61 | + horizontalAlignment, |
| 62 | + title: chartProps.cartesian.legend?.title, |
| 63 | + actions: chartProps.cartesian.legend?.actions, |
| 64 | + }, |
| 65 | + options: { |
| 66 | + chart: { |
| 67 | + type: "pie", |
| 68 | + }, |
| 69 | + title: { |
| 70 | + text: `${horizontalAlignment} aligned Pie`, |
| 71 | + }, |
| 72 | + yAxis: { |
| 73 | + min: 0, |
| 74 | + max: 100, |
| 75 | + title: { |
| 76 | + text: "Usage", |
| 77 | + }, |
| 78 | + }, |
| 79 | + series: [ |
| 80 | + { |
| 81 | + name: "Storage Distribution", |
| 82 | + type: "pie", |
| 83 | + data: [ |
| 84 | + { y: randomInt(30, 40), name: "Documents" }, |
| 85 | + { y: randomInt(20, 30), name: "Images" }, |
| 86 | + { y: randomInt(15, 25), name: "Videos" }, |
| 87 | + { y: randomInt(10, 15), name: "Other" }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + }), |
| 93 | + (horizontalAlignment) => ({ |
| 94 | + ...omit(chartProps.cartesian, "ref"), |
| 95 | + legend: { |
| 96 | + horizontalAlignment, |
| 97 | + title: chartProps.cartesian.legend?.title, |
| 98 | + actions: chartProps.cartesian.legend?.actions, |
| 99 | + }, |
| 100 | + options: { |
| 101 | + chart: { |
| 102 | + type: "solidgauge", |
| 103 | + }, |
| 104 | + title: { |
| 105 | + text: `${horizontalAlignment} aligned Gauge`, |
| 106 | + }, |
| 107 | + yAxis: { |
| 108 | + min: 0, |
| 109 | + max: 100, |
| 110 | + }, |
| 111 | + series: [ |
| 112 | + { |
| 113 | + type: "solidgauge", |
| 114 | + name: "Download speed", |
| 115 | + data: [randomInt(50, 100)], |
| 116 | + showInLegend: true, |
| 117 | + dataLabels: { |
| 118 | + format: "{y} MB/s", |
| 119 | + }, |
| 120 | + }, |
| 121 | + ], |
| 122 | + }, |
| 123 | + }), |
| 124 | + (horizontalAlignment) => ({ |
| 125 | + ...omit(chartProps.cartesian, "ref"), |
| 126 | + legend: { |
| 127 | + horizontalAlignment, |
| 128 | + title: chartProps.cartesian.legend?.title, |
| 129 | + actions: chartProps.cartesian.legend?.actions, |
| 130 | + }, |
| 131 | + options: { |
| 132 | + chart: { |
| 133 | + type: "line", |
| 134 | + }, |
| 135 | + title: { |
| 136 | + text: `${horizontalAlignment} aligned Line`, |
| 137 | + }, |
| 138 | + series: [ |
| 139 | + { |
| 140 | + type: "line", |
| 141 | + name: "Download speed", |
| 142 | + data: lineChartData, |
| 143 | + }, |
| 144 | + ], |
| 145 | + }, |
| 146 | + }), |
| 147 | + ]; |
| 148 | + |
| 149 | + return ( |
| 150 | + <Page |
| 151 | + title="Core Legend horizontal alignment demo" |
| 152 | + subtitle="The page demonstrates the horizontal alignments of the core legend." |
| 153 | + settings={<PageSettingsForm selectedSettings={["showLegendTitle", "showLegendActions"]} />} |
| 154 | + > |
| 155 | + <SpaceBetween direction="vertical" size="m"> |
| 156 | + {charts.map((chart, i) => ( |
| 157 | + <ColumnLayout key={i} columns={2}> |
| 158 | + <CoreChart {...chart("start")}></CoreChart> |
| 159 | + <CoreChart {...chart("center")}></CoreChart> |
| 160 | + </ColumnLayout> |
| 161 | + ))} |
| 162 | + </SpaceBetween> |
| 163 | + </Page> |
| 164 | + ); |
| 165 | +} |
0 commit comments