|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { useRef, useState } from "react"; |
| 5 | +import Highcharts from "highcharts"; |
| 6 | +import { omit } from "lodash"; |
| 7 | + |
| 8 | +import ColumnLayout from "@cloudscape-design/components/column-layout"; |
| 9 | +import { |
| 10 | + colorChartsPaletteCategorical1, |
| 11 | + colorChartsPaletteCategorical2, |
| 12 | + colorChartsPaletteCategorical3, |
| 13 | +} from "@cloudscape-design/design-tokens"; |
| 14 | + |
| 15 | +import { ChartSeriesMarker } from "../../lib/components/internal/components/series-marker"; |
| 16 | +import CoreChart from "../../lib/components/internal-do-not-use/core-chart"; |
| 17 | +import { CoreLegend } from "../../lib/components/internal-do-not-use/core-legend"; |
| 18 | +import { CoreChartProps } from "../../src/core/interfaces"; |
| 19 | +import { LegendItem } from "../../src/internal/components/interfaces"; |
| 20 | +import { dateFormatter } from "../common/formatters"; |
| 21 | +import { useChartSettings } from "../common/page-settings"; |
| 22 | +import { Page } from "../common/templates"; |
| 23 | +import pseudoRandom from "../utils/pseudo-random"; |
| 24 | + |
| 25 | +function randomInt(min: number, max: number) { |
| 26 | + return min + Math.floor(pseudoRandom() * (max - min)); |
| 27 | +} |
| 28 | + |
| 29 | +const baseline = [ |
| 30 | + { x: 1600984800000, y: 58020 }, |
| 31 | + { x: 1600985700000, y: 102402 }, |
| 32 | + { x: 1600986600000, y: 104920 }, |
| 33 | + { x: 1600987500000, y: 94031 }, |
| 34 | + { x: 1600988400000, y: 125021 }, |
| 35 | + { x: 1600989300000, y: 159219 }, |
| 36 | + { x: 1600990200000, y: 193082 }, |
| 37 | + { x: 1600991100000, y: 162592 }, |
| 38 | + { x: 1600992000000, y: 274021 }, |
| 39 | + { x: 1600992900000, y: 264286 }, |
| 40 | +]; |
| 41 | + |
| 42 | +const dataA = baseline.map(({ x, y }) => ({ x, y })); |
| 43 | +const dataB = baseline.map(({ x, y }) => ({ x, y: y === null ? null : y + randomInt(-100000, 100000) })); |
| 44 | +const dataC = baseline.map(({ x, y }) => ({ x, y: y === null ? null : y + randomInt(-150000, 50000) })); |
| 45 | + |
| 46 | +const series = [ |
| 47 | + { |
| 48 | + name: "Series A", |
| 49 | + type: "line" as const, |
| 50 | + data: dataA, |
| 51 | + color: colorChartsPaletteCategorical1, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "Series B", |
| 55 | + type: "line" as const, |
| 56 | + data: dataB, |
| 57 | + color: colorChartsPaletteCategorical2, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "Series C", |
| 61 | + type: "line" as const, |
| 62 | + data: dataC, |
| 63 | + color: colorChartsPaletteCategorical3, |
| 64 | + }, |
| 65 | +]; |
| 66 | + |
| 67 | +export default function LegendEventsDemo() { |
| 68 | + const [visibleItems, setVisibleItems] = useState(new Set<string>(["Series A", "Series B", "Series C"])); |
| 69 | + const [highlightedItem, setHighlightedItem] = useState<null | string>(null); |
| 70 | + |
| 71 | + const { chartProps } = useChartSettings(); |
| 72 | + const chart1API = useRef<CoreChartProps.ChartAPI>(null) as React.MutableRefObject<CoreChartProps.ChartAPI>; |
| 73 | + const chart2API = useRef<CoreChartProps.ChartAPI>(null) as React.MutableRefObject<CoreChartProps.ChartAPI>; |
| 74 | + |
| 75 | + const createOnItemHighlight = |
| 76 | + (referrer: "legend" | "chart1" | "chart2") => |
| 77 | + ({ detail }: { detail: { item: LegendItem } }) => { |
| 78 | + setHighlightedItem(detail.item.name); |
| 79 | + if (referrer !== "chart1") { |
| 80 | + chart1API.current.highlightItems([detail.item.name]); |
| 81 | + } |
| 82 | + if (referrer !== "chart2") { |
| 83 | + chart2API.current.highlightItems([detail.item.name]); |
| 84 | + } |
| 85 | + }; |
| 86 | + const createOnClearHighlight = |
| 87 | + (referrer: "legend" | "chart1" | "chart2") => |
| 88 | + ({ detail }: { detail: { isApiCall: boolean } }) => { |
| 89 | + if (!detail.isApiCall) { |
| 90 | + setHighlightedItem(null); |
| 91 | + if (referrer !== "chart1") { |
| 92 | + chart1API.current.clearChartHighlight(); |
| 93 | + } |
| 94 | + if (referrer !== "chart2") { |
| 95 | + chart2API.current.clearChartHighlight(); |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + const createOnHighlight = |
| 100 | + (referrer: "chart1" | "chart2") => |
| 101 | + ({ detail }: { detail: { point: null | Highcharts.Point; isApiCall: boolean } }) => { |
| 102 | + if (detail.point && !detail.isApiCall) { |
| 103 | + setHighlightedItem(detail.point.name); |
| 104 | + if (referrer !== "chart1") { |
| 105 | + chart1API.current.highlightChartPoint(detail.point); |
| 106 | + } |
| 107 | + if (referrer !== "chart2") { |
| 108 | + chart2API.current.highlightChartPoint(detail.point); |
| 109 | + } |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + const legendItems: LegendItem[] = series.map((s) => ({ |
| 114 | + id: s.name, |
| 115 | + name: s.name, |
| 116 | + marker: <ChartSeriesMarker color={s.color} type="line" />, |
| 117 | + visible: visibleItems.has(s.name), |
| 118 | + highlighted: highlightedItem === s.name, |
| 119 | + })); |
| 120 | + |
| 121 | + return ( |
| 122 | + <Page |
| 123 | + title="Events sync" |
| 124 | + subtitle="Demonstrates the highlight synchronization between standalone legends and charts" |
| 125 | + > |
| 126 | + <ColumnLayout columns={2}> |
| 127 | + <CoreLegend |
| 128 | + items={legendItems} |
| 129 | + title="Standalone legend 1" |
| 130 | + onItemHighlight={createOnItemHighlight("legend")} |
| 131 | + onClearHighlight={createOnClearHighlight("legend")?.bind(null, { detail: { isApiCall: false } })} |
| 132 | + onVisibleItemsChange={({ detail }) => setVisibleItems(new Set(detail.items))} |
| 133 | + /> |
| 134 | + |
| 135 | + <CoreLegend |
| 136 | + items={legendItems} |
| 137 | + title="Standalone legend 2" |
| 138 | + onItemHighlight={createOnItemHighlight("legend")} |
| 139 | + onClearHighlight={createOnClearHighlight("legend")?.bind(null, { detail: { isApiCall: false } })} |
| 140 | + onVisibleItemsChange={({ detail }) => setVisibleItems(new Set(detail.items))} |
| 141 | + /> |
| 142 | + |
| 143 | + <CoreChart |
| 144 | + {...omit(chartProps.cartesian, "ref")} |
| 145 | + highcharts={Highcharts} |
| 146 | + callback={(chartApi) => (chart1API.current = chartApi)} |
| 147 | + ariaLabel="Chart 1" |
| 148 | + options={{ |
| 149 | + series: series, |
| 150 | + xAxis: [{ type: "datetime", title: { text: "Time (UTC)" }, valueFormatter: dateFormatter }], |
| 151 | + yAxis: [{ title: { text: "Events" } }], |
| 152 | + }} |
| 153 | + legend={{ title: "Chart legend 1" }} |
| 154 | + onLegendItemHighlight={createOnItemHighlight("chart1")} |
| 155 | + onClearHighlight={createOnClearHighlight("chart1")} |
| 156 | + onHighlight={createOnHighlight("chart1")} |
| 157 | + visibleItems={[...visibleItems]} |
| 158 | + onVisibleItemsChange={({ detail }) => |
| 159 | + setVisibleItems(new Set(detail.items.filter((i) => i.visible).map((i) => i.name))) |
| 160 | + } |
| 161 | + /> |
| 162 | + |
| 163 | + <CoreChart |
| 164 | + {...omit(chartProps.cartesian, "ref")} |
| 165 | + highcharts={Highcharts} |
| 166 | + callback={(chartApi) => (chart2API.current = chartApi)} |
| 167 | + ariaLabel="Chart 2" |
| 168 | + options={{ |
| 169 | + series: series, |
| 170 | + xAxis: [{ type: "datetime", title: { text: "Time (UTC)" }, valueFormatter: dateFormatter }], |
| 171 | + yAxis: [{ title: { text: "Events" } }], |
| 172 | + }} |
| 173 | + legend={{ title: "Chart legend 2" }} |
| 174 | + onLegendItemHighlight={createOnItemHighlight("chart2")} |
| 175 | + onClearHighlight={createOnClearHighlight("chart2")} |
| 176 | + onHighlight={createOnHighlight("chart2")} |
| 177 | + visibleItems={[...visibleItems]} |
| 178 | + onVisibleItemsChange={({ detail }) => |
| 179 | + setVisibleItems(new Set(detail.items.filter((i) => i.visible).map((i) => i.name))) |
| 180 | + } |
| 181 | + /> |
| 182 | + </ColumnLayout> |
| 183 | + </Page> |
| 184 | + ); |
| 185 | +} |
0 commit comments