Skip to content

Commit

Permalink
feat: Introduce fitHeight property for chart components
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot authored Jul 21, 2023
1 parent 385429b commit afe73ee
Show file tree
Hide file tree
Showing 28 changed files with 785 additions and 120 deletions.
104 changes: 104 additions & 0 deletions pages/area-chart/fit-height.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useContext } from 'react';

import { createLinearTimeLatencyProps } from './series';
import { AreaChart, Box, Button, Checkbox, SpaceBetween } from '~components';
import AppContext, { AppContextType } from '../app/app-context';
import ScreenshotArea from '../utils/screenshot-area';

type DemoContext = React.Context<
AppContextType<{ fitHeight: boolean; hideFilter: boolean; hideLegend: boolean; minHeight: number }>
>;

const chartData = createLinearTimeLatencyProps();

export default function () {
const { urlParams, setUrlParams } = useContext(AppContext as DemoContext);
const minHeight = parseInt(urlParams.minHeight?.toString() || '0');
const heights = [800, 600, 400, 300, 200, 100];
const fitHeight = urlParams.fitHeight ?? true;
return (
<Box padding="m">
<h1>Area chart fit height</h1>

<Box>
<Checkbox checked={fitHeight} onChange={e => setUrlParams({ fitHeight: e.detail.checked })}>
fit height
</Checkbox>
<Checkbox checked={urlParams.hideFilter} onChange={e => setUrlParams({ hideFilter: e.detail.checked })}>
hide filter
</Checkbox>
<Checkbox checked={urlParams.hideLegend} onChange={e => setUrlParams({ hideLegend: e.detail.checked })}>
hide legend
</Checkbox>
<SpaceBetween size="xs" direction="horizontal" alignItems="center">
<input
id="min-height-input"
type="number"
value={minHeight}
onChange={e => setUrlParams({ minHeight: parseInt(e.target.value) })}
/>
<label htmlFor="min-height-input">min height</label>
</SpaceBetween>
</Box>

<ScreenshotArea>
<SpaceBetween size="l">
{heights.map(height => (
<Box key={height}>
<Box>{height}px</Box>
<div
style={{ boxSizing: 'border-box', width: '100%', padding: '8px', border: '2px solid black', height }}
>
<AreaChart
fitHeight={fitHeight}
height={minHeight}
hideFilter={urlParams.hideFilter}
hideLegend={urlParams.hideLegend}
ariaLabel="Linear latency chart"
ariaDescription="Use up/down arrow keys to navigate between series, and left/right arrow keys to navigate within a series."
loadingText="Loading chart data..."
errorText="Error loading chart data."
recoveryText="Retry"
onRecoveryClick={() => {}}
empty={
<Box textAlign="center" color="inherit">
<b>No data</b>
<Box variant="p" color="inherit">
There is no data to display
</Box>
</Box>
}
noMatch={
<Box textAlign="center" color="inherit">
<b>No matching data</b>
<Box padding={{ bottom: 's' }} variant="p" color="inherit">
There is no data to display
</Box>
<Button onClick={() => alert('Not implemented in the example')}>Clear filter</Button>
</Box>
}
i18nStrings={{
filterLabel: 'Filter displayed data',
filterPlaceholder: 'Filter data',
filterSelectedAriaLabel: '(selected)',
detailTotalLabel: 'Total',
detailPopoverDismissAriaLabel: 'Dismiss',
legendAriaLabel: 'Legend',
chartAriaRoleDescription: 'area chart',
xAxisAriaRoleDescription: 'x axis',
yAxisAriaRoleDescription: 'y axis',
xTickFormatter: value => `${value}\nxxx`,
}}
xDomain={[0, 119]}
{...chartData}
/>
</div>
</Box>
))}
</SpaceBetween>
</ScreenshotArea>
</Box>
);
}
80 changes: 80 additions & 0 deletions pages/mixed-line-bar-chart/fit-height.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useContext } from 'react';

import { Box, Button, Checkbox, MixedLineBarChart, SpaceBetween } from '~components';
import AppContext, { AppContextType } from '../app/app-context';
import ScreenshotArea from '../utils/screenshot-area';
import { barChartInstructions, commonProps, data3, data4 } from './common';
import { colorChartsThresholdInfo } from '~design-tokens';

type DemoContext = React.Context<
AppContextType<{ fitHeight: boolean; hideFilter: boolean; hideLegend: boolean; minHeight: number }>
>;

export default function () {
const { urlParams, setUrlParams } = useContext(AppContext as DemoContext);
const minHeight = parseInt(urlParams.minHeight?.toString() || '0');
const heights = [800, 600, 400, 300, 200, 100];
const fitHeight = urlParams.fitHeight ?? true;
return (
<Box padding="m">
<h1>Mixed chart fit height</h1>

<Box>
<Checkbox checked={fitHeight} onChange={e => setUrlParams({ fitHeight: e.detail.checked })}>
fit height
</Checkbox>
<Checkbox checked={urlParams.hideFilter} onChange={e => setUrlParams({ hideFilter: e.detail.checked })}>
hide filter
</Checkbox>
<Checkbox checked={urlParams.hideLegend} onChange={e => setUrlParams({ hideLegend: e.detail.checked })}>
hide legend
</Checkbox>
<SpaceBetween size="xs" direction="horizontal" alignItems="center">
<input
id="min-height-input"
type="number"
value={minHeight}
onChange={e => setUrlParams({ minHeight: parseInt(e.target.value) })}
/>
<label htmlFor="min-height-input">min height</label>
</SpaceBetween>
</Box>

<ScreenshotArea>
<SpaceBetween size="l">
{heights.map(height => (
<Box key={height}>
<Box>{height}px</Box>
<div
style={{ boxSizing: 'border-box', width: '100%', padding: '8px', border: '2px solid black', height }}
>
<MixedLineBarChart
{...commonProps}
fitHeight={fitHeight}
height={minHeight}
hideFilter={urlParams.hideFilter}
hideLegend={urlParams.hideLegend}
series={[
{ title: 'Happiness', type: 'bar', data: data4.filter(({ x }) => x !== 'Chocolate') },
{ title: 'Calories', type: 'line', data: data3 },
{ title: 'Threshold', type: 'threshold', y: 420, color: colorChartsThresholdInfo },
]}
xDomain={data3.map(d => d.x)}
yDomain={[0, 650]}
xTitle="Food"
yTitle="Calories (kcal)"
xScaleType="categorical"
ariaLabel="Mixed chart 1"
ariaDescription={barChartInstructions}
detailPopoverFooter={xValue => <Button>Filter by {xValue}</Button>}
/>
</div>
</Box>
))}
</SpaceBetween>
</ScreenshotArea>
</Box>
);
}
81 changes: 81 additions & 0 deletions pages/pie-chart/fit-height.page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import React, { useContext } from 'react';

import { Box, Button, Checkbox, PieChart, SegmentedControl, SpaceBetween } from '~components';
import AppContext, { AppContextType } from '../app/app-context';
import ScreenshotArea from '../utils/screenshot-area';
import { FoodData, commonProps, data1 } from './common';

type DemoContext = React.Context<
AppContextType<{
fitHeight: boolean;
hideFilter: boolean;
hideLegend: boolean;
minSize: 'large' | 'medium' | 'small';
}>
>;

export default function () {
const { urlParams, setUrlParams } = useContext(AppContext as DemoContext);
const minSize = urlParams.minSize ?? 'small';
const heights = [800, 600, 400, 300, 200, 100];
const fitHeight = urlParams.fitHeight ?? true;
return (
<Box padding="m">
<h1>Pie chart fit height</h1>

<Box>
<Checkbox checked={fitHeight} onChange={e => setUrlParams({ fitHeight: e.detail.checked })}>
fit height
</Checkbox>
<Checkbox checked={urlParams.hideFilter} onChange={e => setUrlParams({ hideFilter: e.detail.checked })}>
hide filter
</Checkbox>
<Checkbox checked={urlParams.hideLegend} onChange={e => setUrlParams({ hideLegend: e.detail.checked })}>
hide legend
</Checkbox>
<SpaceBetween size="xs" direction="horizontal" alignItems="center">
<SegmentedControl
id="min-size-input"
label="Position"
options={[
{ id: 'large', text: 'Large' },
{ id: 'medium', text: 'Medium' },
{ id: 'small', text: 'Small' },
]}
selectedId={minSize}
onChange={e => setUrlParams({ minSize: e.detail.selectedId as any })}
/>
<label htmlFor="min-size-input">min size</label>
</SpaceBetween>
</Box>

<ScreenshotArea>
<SpaceBetween size="l">
{heights.map(height => (
<Box key={height}>
<Box>{height}px</Box>
<div
style={{ boxSizing: 'border-box', width: '100%', padding: '8px', border: '2px solid black', height }}
>
<PieChart<FoodData>
{...commonProps}
fitHeight={fitHeight}
hideFilter={urlParams.hideFilter}
hideLegend={urlParams.hideLegend}
data={data1}
ariaLabel="Food facts"
size={minSize}
detailPopoverFooter={segment => <Button>Filter by {segment.title}</Button>}
variant="donut"
innerMetricValue="180"
/>
</div>
</Box>
))}
</SpaceBetween>
</ScreenshotArea>
</Box>
);
}
45 changes: 40 additions & 5 deletions src/__tests__/__snapshots__/documenter.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,17 @@ Do not use \`ariaLabel\` and \`ariaLabelledby\` at the same time.",
"optional": true,
"type": "string",
},
Object {
"description": "Enable this property to make the chart fit into the available height of the parent container.",
"name": "fitHeight",
"optional": true,
"type": "boolean",
},
Object {
"defaultValue": "500",
"description": "An optional pixel value number that fixes the height of the chart area.
If not set explicitly, the component will use a default height that is defined internally.",
If not set explicitly, the component will use a default height that is defined internally.
When used with \`fitHeight\`, this property defines the minimum height of the chart area.",
"name": "height",
"optional": true,
"type": "number",
Expand Down Expand Up @@ -1975,10 +1982,17 @@ See the usage guidelines for more details.",
"optional": true,
"type": "string",
},
Object {
"description": "Enable this property to make the chart fit into the available height of the parent container.",
"name": "fitHeight",
"optional": true,
"type": "boolean",
},
Object {
"defaultValue": "500",
"description": "An optional pixel value number that fixes the height of the chart area.
If not set explicitly, the component will use a default height that is defined internally.",
If not set explicitly, the component will use a default height that is defined internally.
When used with \`fitHeight\`, this property defines the minimum height of the chart area.",
"name": "height",
"optional": true,
"type": "number",
Expand Down Expand Up @@ -7721,10 +7735,17 @@ See the usage guidelines for more details.",
"optional": true,
"type": "string",
},
Object {
"description": "Enable this property to make the chart fit into the available height of the parent container.",
"name": "fitHeight",
"optional": true,
"type": "boolean",
},
Object {
"defaultValue": "500",
"description": "An optional pixel value number that fixes the height of the chart area.
If not set explicitly, the component will use a default height that is defined internally.",
If not set explicitly, the component will use a default height that is defined internally.
When used with \`fitHeight\`, this property defines the minimum height of the chart area.",
"name": "height",
"optional": true,
"type": "number",
Expand Down Expand Up @@ -8317,10 +8338,17 @@ See the usage guidelines for more details.",
"optional": true,
"type": "string",
},
Object {
"description": "Enable this property to make the chart fit into the available height of the parent container.",
"name": "fitHeight",
"optional": true,
"type": "boolean",
},
Object {
"defaultValue": "500",
"description": "An optional pixel value number that fixes the height of the chart area.
If not set explicitly, the component will use a default height that is defined internally.",
If not set explicitly, the component will use a default height that is defined internally.
When used with \`fitHeight\`, this property defines the minimum height of the chart area.",
"name": "height",
"optional": true,
"type": "number",
Expand Down Expand Up @@ -9518,6 +9546,12 @@ Each pair has the following properties:
"optional": true,
"type": "string",
},
Object {
"description": "Enable this property to make the chart fit into the available height of the parent container.",
"name": "fitHeight",
"optional": true,
"type": "boolean",
},
Object {
"defaultValue": "false",
"description": "Hides the label descriptions next to the chart segments when set to \`true\`.",
Expand Down Expand Up @@ -9683,7 +9717,8 @@ The function is called with the data object of each segment and is expected to r
},
Object {
"defaultValue": "\\"medium\\"",
"description": "Specifies the size of the pie or donut chart.",
"description": "Specifies the size of the pie or donut chart.
When used with \`fitHeight\`, this property defines the minimum size of the chart area.",
"inlineType": Object {
"name": "",
"type": "union",
Expand Down
Loading

0 comments on commit afe73ee

Please sign in to comment.