Skip to content

Commit

Permalink
Implement design changes to LineChartRelational
Browse files Browse the repository at this point in the history
  • Loading branch information
envex committed Mar 20, 2024
1 parent 7ba2aff commit 2712166
Show file tree
Hide file tree
Showing 18 changed files with 179 additions and 87 deletions.
3 changes: 3 additions & 0 deletions packages/polaris-viz-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ interface StyleOverride {
width?: number;
strokeDasharray?: string;
};
tooltip?: {
shape?: Shape;
};
}

export interface DataGroup {
Expand Down
13 changes: 12 additions & 1 deletion packages/polaris-viz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

<!-- ## Unreleased -->
## Unreleased

### Added

- Added `tooltip.shape` override value to `DataSeries.styleOverride`.
- **Breaking change** Added custom legend to `<LineChartRelational />`.


### Changed

- **Breaking change** `<LineChartRelational />` no longer renders all lines in the `DataSeries[]`. Any `DataSeries` with `metadata.relatedIndex` will only render the area in the chart.
- **Breaking change** `metadata.relatedIndex` should now refer to the single "median" index and not the next index to draw the area to.

## [11.1.0] - 2024-03-19

Expand Down
4 changes: 4 additions & 0 deletions packages/polaris-viz/src/components/LineChart/Chart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ export function Chart({
})}

{data.map((singleSeries, index) => {
if (singleSeries.metadata?.isVisuallyHidden === true) {
return null;
}

return (
<LineSeries
activeLineIndex={activeLineIndex}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ export function Points({
return (
<Fragment>
{data.map((singleSeries, seriesIndex) => {
if (singleSeries?.metadata?.isVisuallyHidden === true) {
return null;
}

const index = singleSeries.metadata?.relatedIndex ?? seriesIndex;

if (hiddenIndexes.includes(index)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import {DEFAULT_CHART_PROPS} from '@shopify/polaris-viz-core';
import {
DEFAULT_CHART_PROPS,
DEFAULT_THEME_NAME,
} from '@shopify/polaris-viz-core';
import {Fragment} from 'react';

import type {LineChartProps} from '../LineChart';
import {LineChart} from '../LineChart';

import {RelatedAreas, MissingDataArea} from './components';
import {RelatedAreas, MissingDataArea, CustomLegend} from './components';

export function LineChartRelational(props: LineChartProps) {
export function LineChartRelational(
props: Omit<LineChartProps, 'renderLegendContent'>,
) {
const {
annotations = [],
data,
errorText,
emptyStateText,
id,
isAnimated,
renderLegendContent,
showLegend = true,
skipLinkText,
state,
Expand All @@ -27,15 +31,37 @@ export function LineChartRelational(props: LineChartProps) {
...props,
};

const dataWithHiddenRelational = data.map((series) => {
return {
...series,
metadata: {
...series.metadata,
isVisuallyHidden: series.metadata?.relatedIndex != null,
},
};
});

return (
<LineChart
annotations={annotations}
data={data}
data={dataWithHiddenRelational}
emptyStateText={emptyStateText}
errorText={errorText}
id={id}
isAnimated={isAnimated}
renderLegendContent={renderLegendContent}
renderLegendContent={({
getColorVisionStyles,
getColorVisionEventAttrs,
}) => {
return (
<CustomLegend
getColorVisionStyles={getColorVisionStyles}
getColorVisionEventAttrs={getColorVisionEventAttrs}
data={data}
theme={theme ?? DEFAULT_THEME_NAME}
/>
);
}}
showLegend={showLegend}
skipLinkText={skipLinkText}
slots={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function Area({
style={{
...getColorVisionStylesForActiveIndex({
activeIndex,
index: -1,
index,
fadedOpacity: 0.2,
}),
}}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.Container {
display: flex;
gap: 10px;
flex-wrap: wrap;
list-style: none;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type {DataSeries} from '@shopify/polaris-viz-core';

import type {ColorVisionInteractionMethods} from '../../../../types';
import {LegendItem} from '../../../../components/Legend';

import styles from './CustomLegend.scss';

interface Props extends ColorVisionInteractionMethods {
data: DataSeries[];
theme: string;
}

export function CustomLegend({
data,
getColorVisionEventAttrs,
getColorVisionStyles,
theme,
}: Props) {
const lineSeries = data.filter(
(series) => series?.metadata?.relatedIndex == null,
);

const percentileItems = data.filter(
(series) => series?.metadata?.relatedIndex != null,
);

const percentileIndex = lineSeries.length + 1;

return (
<ul className={styles.Container}>
{lineSeries.map(({color, name, isComparison, metadata}) => {
if (metadata?.isPredictive) {
return null;
}

const index = data.findIndex((series) => series.name === name);

return (
<li
key={index}
style={{
...getColorVisionStyles(index),
}}
{...getColorVisionEventAttrs(index)}
>
<LegendItem
color={color!}
index={index}
isComparison={isComparison}
name={name!}
shape="Line"
theme={theme}
/>
</li>
);
})}
<li
key={percentileIndex}
style={{
...getColorVisionStyles(percentileIndex),
}}
{...getColorVisionEventAttrs(percentileIndex)}
>
<LegendItem
color={
percentileItems[0].color ?? percentileItems[0]?.metadata?.areaColor
}
index={percentileIndex}
name={percentileItems[0]?.metadata?.legendLabel}
shape="Bar"
theme={theme}
/>
</li>
</ul>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {CustomLegend} from './CustomLegend';
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export interface RelatedAreaProps extends LineChartSlotProps {
export function RelatedAreas({yScale, xScale, data}: RelatedAreaProps) {
const [activeIndex, setActiveIndex] = useState(-1);

const lineSeries = data.filter(
(series) => series?.metadata?.relatedIndex == null,
);

const percentileIndex = lineSeries.length + 1;

const {hiddenIndexes} = useExternalHideEvents();
const {shouldAnimate, id} = useChartContext();

Expand Down Expand Up @@ -84,7 +90,7 @@ export function RelatedAreas({yScale, xScale, data}: RelatedAreaProps) {
fill={series.metadata?.areaColor}
getAreaGenerator={getAreaGenerator}
hiddenIndexes={hiddenIndexes}
index={index}
index={percentileIndex}
key={index}
series={series}
shouldAnimate={shouldAnimate}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export {Area} from './Area';
export {MissingDataArea} from './MissingDataArea';
export {RelatedAreas} from './RelatedAreas';
export {CustomLegend} from './CustomLegend';
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ Default.args = {
...DEFAULT_PROPS,
data: DEFAULT_DATA,
isAnimated: false,
showLegend: true,
theme: 'Uplift',
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,10 @@ export const MISSING_END_DATA = [
{value: null, key: '2020-03-13T12:00:00'},
{value: null, key: '2020-03-14T12:00:00'},
],
color: 'rgba(103, 197, 228, 1)',
metadata: {
relatedIndex: 2,
areaColor: 'rgba(103, 197, 228, 0.1)',
},
styleOverride: {
line: {
hasArea: false,
},
},
},
{
name: 'Median',
Expand All @@ -70,10 +64,6 @@ export const MISSING_END_DATA = [
{value: null, key: '2020-03-14T12:00:00'},
],
color: 'rgba(40, 106, 123, 1)',
metadata: {
relatedIndex: 3,
areaColor: 'rgba(47, 175, 218, 0.2)',
},
styleOverride: {
line: {
hasArea: false,
Expand All @@ -99,11 +89,9 @@ export const MISSING_END_DATA = [
{value: null, key: '2020-03-13T12:00:00'},
{value: null, key: '2020-03-14T12:00:00'},
],
color: 'rgba(103, 197, 228, 1)',
styleOverride: {
line: {
hasArea: false,
},
metadata: {
relatedIndex: 2,
areaColor: 'rgba(103, 197, 228, 0.1)',
},
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,10 @@ export const MISSING_MIDDLE_DATA = [
{value: 773, key: '2020-03-13T12:00:00'},
{value: 171, key: '2020-03-14T12:00:00'},
],
color: 'rgba(103, 197, 228, 1)',
metadata: {
relatedIndex: 2,
areaColor: 'rgba(103, 197, 228, 0.1)',
},
styleOverride: {
line: {
hasArea: false,
},
},
},
{
name: 'Median',
Expand All @@ -70,10 +64,6 @@ export const MISSING_MIDDLE_DATA = [
{value: 21, key: '2020-03-14T12:00:00'},
],
color: 'rgba(40, 106, 123, 1)',
metadata: {
relatedIndex: 3,
areaColor: 'rgba(47, 175, 218, 0.2)',
},
styleOverride: {
line: {
hasArea: false,
Expand All @@ -99,11 +89,9 @@ export const MISSING_MIDDLE_DATA = [
{value: 473, key: '2020-03-13T12:00:00'},
{value: 0, key: '2020-03-14T12:00:00'},
],
color: 'rgba(103, 197, 228, 1)',
styleOverride: {
line: {
hasArea: false,
},
metadata: {
relatedIndex: 2,
areaColor: 'rgba(103, 197, 228, 0.1)',
},
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,10 @@ export const MISSING_RANDOM_DATA = [
{value: 773, key: '2020-03-13T12:00:00'},
{value: 171, key: '2020-03-14T12:00:00'},
],
color: 'rgba(103, 197, 228, 1)',
metadata: {
relatedIndex: 2,
areaColor: 'rgba(103, 197, 228, 0.1)',
},
styleOverride: {
line: {
hasArea: false,
},
},
},
{
name: 'Median',
Expand All @@ -70,10 +64,6 @@ export const MISSING_RANDOM_DATA = [
{value: 21, key: '2020-03-14T12:00:00'},
],
color: 'rgba(40, 106, 123, 1)',
metadata: {
relatedIndex: 3,
areaColor: 'rgba(47, 175, 218, 0.2)',
},
styleOverride: {
line: {
hasArea: false,
Expand All @@ -99,11 +89,9 @@ export const MISSING_RANDOM_DATA = [
{value: 473, key: '2020-03-13T12:00:00'},
{value: 0, key: '2020-03-14T12:00:00'},
],
color: 'rgba(103, 197, 228, 1)',
styleOverride: {
line: {
hasArea: false,
},
metadata: {
relatedIndex: 2,
areaColor: 'rgba(103, 197, 228, 0.1)',
},
},
];
Loading

0 comments on commit 2712166

Please sign in to comment.