Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dashed support for legends #1773

Merged
merged 4 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/polaris-viz-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export interface DataGroup {

export type Shape = 'Line' | 'Bar';

export type LineStyle = 'solid' | 'dotted';
export type LineStyle = 'solid' | 'dotted' | 'dashed';

export interface GradientStop {
offset: number;
Expand Down
4 changes: 4 additions & 0 deletions packages/polaris-viz/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [15.3.4] - 2024-12-03

### Added
mollerjorge marked this conversation as resolved.
Show resolved Hide resolved

- Added dashed `lineStyle` support for custom legends in `<LineChartRelational />`

### Fixed

- Fixed issue where `<DonutChart />` would run the `seriesNameFormatter` for multiple times on a `<Legend />`.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {LabelFormatter} from '@shopify/polaris-viz-core';
import type {LabelFormatter, LineStyle} from '@shopify/polaris-viz-core';
import {
getColorVisionEventAttrs,
getColorVisionStylesForActiveIndex,
Expand Down Expand Up @@ -40,6 +40,7 @@ export interface LegendItemProps extends LegendData {
truncate?: boolean;
showLegendValues?: boolean;
seriesNameFormatter?: LabelFormatter;
lineStyle?: LineStyle;
}

export function LegendItem({
Expand All @@ -58,6 +59,7 @@ export function LegendItem({
truncate = false,
showLegendValues = false,
seriesNameFormatter = (value) => `${value}`,
lineStyle,
}: LegendItemProps) {
const selectedTheme = useTheme(theme);
const ref = useRef<HTMLButtonElement | null>(null);
Expand Down Expand Up @@ -120,7 +122,11 @@ export function LegendItem({
style={{height: PREVIEW_ICON_SIZE, width: PREVIEW_ICON_SIZE}}
className={style.IconContainer}
>
<SeriesIcon shape={shape} color={color} isComparison={isComparison} />
<SeriesIcon
lineStyle={isComparison ? 'dotted' : lineStyle}
shape={shape}
color={isComparison ? selectedTheme.seriesColors.comparison : color}
/>
</span>
) : (
renderSeriesIcon()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {mount} from '@shopify/react-testing';

import type {LegendItemProps} from '../LegendItem';
import {LegendItem, MINIMUM_LEGEND_ITEM_WIDTH} from '../LegendItem';
import {SeriesIcon} from '../../../../shared/SeriesIcon';

const mockProps: LegendItemProps = {
activeIndex: 2,
Expand Down Expand Up @@ -144,4 +145,12 @@ describe('<LegendItem />', () => {
});
});
});

describe('lineStyle', () => {
it('renders a dotted line when isComparison prop is true', () => {
const item = mount(<LegendItem {...mockProps} isComparison />);

expect(item.find(SeriesIcon)).toHaveReactProps({lineStyle: 'dotted'});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export function CustomLegend({
}

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

return (
<li
key={index}
Expand All @@ -51,6 +50,7 @@ export function CustomLegend({
isComparison={isComparison}
name={seriesNameFormatter(name ?? '')}
shape="Line"
lineStyle={metadata?.lineStyle}
theme={theme}
/>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {DEFAULT_THEME_NAME} from '@shopify/polaris-viz-core';

import type {Props} from '../CustomLegend';
import {CustomLegend} from '../CustomLegend';
import {LegendItem} from '../../../../Legend';

describe('<CustomLegend />', () => {
describe('seriesNameFormatter', () => {
Expand All @@ -18,13 +19,26 @@ describe('<CustomLegend />', () => {
expect(component).toContainReactText('Name: 75th - 25th percentile');
});
});

describe('lineStyle', () => {
it('renders a LegendItem with the lineStyle from metadata.lineStyle', () => {
const component = mount(<CustomLegend {...MOCK_PROPS} />);

expect(component).toContainReactComponent(LegendItem, {
lineStyle: 'dashed',
});
});
});
});

const MOCK_PROPS: Props = {
data: [
{
name: 'Average',
data: [{value: 333, key: '2020-03-01T12:00:00'}],
metadata: {
lineStyle: 'dashed',
},
},
{
name: '75th Percentile',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ export const DEFAULT_DATA: DataSeries[] = [
{value: 21, key: '2020-03-14T12:00:00'},
],
color: LIGHT_THEME.seriesColors.limited[5],
metadata: {
Copy link
Collaborator Author

@mollerjorge mollerjorge Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using metadata for this looks like a good fit after checking others like:

metadata: {
   relatedIndex: 2,
   areaColor: 'rgba(218, 182, 242, 0.2)',
   legendLabel: '75th - 25th percentile',
},

lineStyle: 'dashed',
},

styleOverride: {
line: {
hasArea: false,
Expand Down
17 changes: 16 additions & 1 deletion packages/polaris-viz/src/components/LinePreview/LinePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
uniqueId,
} from '@shopify/polaris-viz-core';

import {PREVIEW_ICON_SIZE, XMLNS} from '../../constants';
import {
DASHED_LINE_PREVIEW_STROKE_DASHARRAY,
PREVIEW_ICON_SIZE,
XMLNS,
} from '../../constants';

import {
DOTTED_LINE_PREVIEW_CY,
Expand Down Expand Up @@ -102,6 +106,17 @@ function getLinePreview({
})}
</g>
);
case 'dashed':
return (
<path
d={`M1 1 H${PREVIEW_ICON_SIZE}`}
stroke={color}
strokeLinejoin="round"
strokeLinecap="round"
strokeWidth={width}
strokeDasharray={DASHED_LINE_PREVIEW_STROKE_DASHARRAY}
/>
);
default:
return (
<path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ describe('<LinePreview />', () => {

expect(linePreview).toContainReactComponentTimes('circle', 3);
});

it('renders a dashed path if lineStyle is dashed', () => {
const linePreview = mount(<LinePreview color="red" lineStyle="dashed" />);

expect(linePreview).toContainReactComponent('path', {
strokeDasharray: '1.5 3',
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ export function TooltipRow({
<div style={{width: PREVIEW_ICON_SIZE}}>
{renderSeriesIcon?.() ?? (
<SeriesIcon
color={color!}
isComparison={isComparison}
color={
isComparison ? selectedTheme.seriesColors.comparison : color!
}
shape={shape}
lineStyle={isComparison ? 'dotted' : 'solid'}
/>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ describe('color', () => {
});
});

describe('isComparison', () => {
it('renders a comparison line when true', () => {
describe('lineStyle', () => {
it('renders a dotted line when lineStyle prop is set to dotted', () => {
const component = mount(
<SeriesIcon shape="Line" color="red" isComparison="true" />,
<SeriesIcon shape="Line" color="red" lineStyle="dotted" />,
);

expect(component.find(LinePreview)).toHaveReactProps({lineStyle: 'dotted'});
});

it('does not render a comparison line when isComparison prop is not passed', () => {
const component = mount(<SeriesIcon shape="Line" color="red" />);
it('renders a dashed line when lineStyle prop is set to dashed', () => {
const component = mount(
<SeriesIcon shape="Line" color="red" lineStyle="dashed" />,
);

expect(component.find(LinePreview)).not.toHaveReactProps({
lineStyle: 'dotted',
});
expect(component.find(LinePreview)).toHaveReactProps({lineStyle: 'dashed'});
});
});
Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
import type {Shape, Color} from '@shopify/polaris-viz-core';
import {useTheme} from '@shopify/polaris-viz-core';
import type {Shape, Color, LineStyle} from '@shopify/polaris-viz-core';

import {LinePreview} from '../../LinePreview';
import {SquareColorPreview} from '../../SquareColorPreview';

interface Props {
color: Color;
isComparison?: boolean;
shape?: Shape;
lineStyle?: LineStyle;
}

export function SeriesIcon({
color,
isComparison = false,
shape = 'Bar',
}: Props) {
const selectedTheme = useTheme();

export function SeriesIcon({color, lineStyle, shape = 'Bar'}: Props) {
switch (shape) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought of refactoring this a bit, it didn't feel like SeriesIcon should concern about wether if its a comparison or not, I think we should pass in the color and lineStyle instead and calculate those from the parents

case 'Line': {
const style = isComparison ? 'dotted' : 'solid';
const lineColor = isComparison
? selectedTheme.seriesColors.comparison
: color;
const style = lineStyle ?? 'solid';

return <LinePreview color={lineColor} lineStyle={style} />;
return <LinePreview color={color} lineStyle={style} />;
}
case 'Bar':
default:
Expand Down
1 change: 1 addition & 0 deletions packages/polaris-viz/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const Y_AXIS_LABEL_OFFSET = 2;
export const TOOLTIP_BG_OPACITY = 0.8;
export const COLLAPSED_ANNOTATIONS_COUNT = 3;
export const PREVIEW_ICON_SIZE = 12;
export const DASHED_LINE_PREVIEW_STROKE_DASHARRAY = '1.5 3';
export const ARC_PAD_ANGLE = 0.02;
export const ZERO_VALUE_LINE_HEIGHT = 6;
export const NEGATIVE_ZERO_LINE_OFFSET = 10;
Expand Down
Loading