Skip to content

Commit

Permalink
Add dashed support for legends
Browse files Browse the repository at this point in the history
  • Loading branch information
mollerjorge committed Dec 4, 2024
1 parent b38b128 commit 293ef57
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
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
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,12 @@ export function LegendItem({
style={{height: PREVIEW_ICON_SIZE, width: PREVIEW_ICON_SIZE}}
className={style.IconContainer}
>
<SeriesIcon shape={shape} color={color} isComparison={isComparison} />
<SeriesIcon
lineStyle={lineStyle}
shape={shape}
color={color}
isComparison={isComparison}
/>
</span>
) : (
renderSeriesIcon()
Expand Down
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 @@ -105,6 +105,10 @@ export const DEFAULT_DATA: DataSeries[] = [
{value: 21, key: '2020-03-14T12:00:00'},
],
color: LIGHT_THEME.seriesColors.limited[5],
metadata: {
lineStyle: 'dashed',
},

styleOverride: {
line: {
hasArea: false,
Expand Down
11 changes: 11 additions & 0 deletions packages/polaris-viz/src/components/LinePreview/LinePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ function getLinePreview({
})}
</g>
);
case 'dashed':
return (
<path
d={`M1 1 H${PREVIEW_ICON_SIZE}`}
stroke={color}
strokeLinejoin="round"
strokeLinecap="round"
strokeWidth={width}
strokeDasharray="1.5 3"
/>
);
default:
return (
<path
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Shape, Color} from '@shopify/polaris-viz-core';
import type {Shape, Color, LineStyle} from '@shopify/polaris-viz-core';
import {useTheme} from '@shopify/polaris-viz-core';

import {LinePreview} from '../../LinePreview';
Expand All @@ -8,18 +8,20 @@ interface Props {
color: Color;
isComparison?: boolean;
shape?: Shape;
lineStyle?: LineStyle;
}

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

switch (shape) {
case 'Line': {
const style = isComparison ? 'dotted' : 'solid';
const style = isComparison ? 'dotted' : lineStyle ?? 'solid';
const lineColor = isComparison
? selectedTheme.seriesColors.comparison
: color;
Expand Down

0 comments on commit 293ef57

Please sign in to comment.