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 82d1eac
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 25 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
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 @@ -35,7 +39,19 @@ export function LineChartRelational(props: LineChartProps) {
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
@@ -1,6 +1,7 @@
import type {Story} from '@storybook/react';
import type {RenderTooltipContentData} from 'types';
import type {DataSeries} from '@shopify/polaris-viz-core';
import {UPLIFT_THEME} from '@shopify/polaris-viz-core';
import type {LineChartProps} from 'components/LineChart/LineChart';

import {LineChartRelational} from '../LineChartRelational';
Expand Down Expand Up @@ -80,10 +81,7 @@ export const DEFAULT_DATA: DataSeries[] = [
{value: 849, key: '2020-03-13T12:00:00'},
{value: 129, key: '2020-03-14T12:00:00'},
],
color: [
{offset: 0, color: 'rgba(149, 101, 255, 1)'},
{offset: 100, color: 'rgba(75, 146, 229, 1)'},
],
color: UPLIFT_THEME.seriesColors.upToEight[0],
},
{
name: '75th Percentile',
Expand All @@ -103,14 +101,16 @@ export const DEFAULT_DATA: DataSeries[] = [
{value: 773, key: '2020-03-13T12:00:00'},
{value: 171, key: '2020-03-14T12:00:00'},
],
color: 'rgba(103, 197, 228, 1)',
color: 'rgba(218, 182, 242, 1)',
metadata: {
relatedIndex: 2,
areaColor: 'rgba(103, 197, 228, 0.1)',
areaColor: 'rgba(218, 182, 242, 0.2)',
isVisuallyHidden: true,
legendLabel: '75th - 25th percentile',
},
styleOverride: {
line: {
hasArea: false,
tooltip: {
shape: 'Bar',
},
},
},
Expand All @@ -132,11 +132,7 @@ export const DEFAULT_DATA: DataSeries[] = [
{value: 623, key: '2020-03-13T12:00:00'},
{value: 21, key: '2020-03-14T12:00:00'},
],
color: 'rgba(40, 106, 123, 1)',
metadata: {
relatedIndex: 3,
areaColor: 'rgba(47, 175, 218, 0.2)',
},
color: UPLIFT_THEME.seriesColors.upToEight[5],
styleOverride: {
line: {
hasArea: false,
Expand All @@ -162,10 +158,16 @@ export const DEFAULT_DATA: DataSeries[] = [
{value: 473, key: '2020-03-13T12:00:00'},
{value: 0, key: '2020-03-14T12:00:00'},
],
color: 'rgba(103, 197, 228, 1)',
color: 'rgba(218, 182, 242, 1)',
metadata: {
relatedIndex: 2,
areaColor: 'rgba(218, 182, 242, 0.2)',
isVisuallyHidden: true,
legendLabel: '75th - 25th percentile',
},
styleOverride: {
line: {
hasArea: false,
tooltip: {
shape: 'Bar',
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function renderLinearTooltipContent(
};

function renderSeriesIcon(color, styleOverride): ReactNode {
if (styleOverride == null) {
if (styleOverride?.line == null) {
return null;
}

Expand Down Expand Up @@ -110,7 +110,7 @@ export function renderLinearTooltipContent(
renderSeriesIcon={() =>
renderSeriesIcon(color, styleOverride)
}
shape="Line"
shape={styleOverride?.tooltip?.shape ?? 'Line'}
value={formatters.valueFormatter(item.value ?? 0)}
/>
);
Expand Down

0 comments on commit 82d1eac

Please sign in to comment.