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

Charts: Add legend component #40594

Draft
wants to merge 8 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Chart library: add legend component
1 change: 1 addition & 0 deletions projects/js-packages/charts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@react-spring/web": "9.7.3",
"@visx/axis": "^3.12.0",
"@visx/group": "^3.12.0",
"@visx/legend": "^3.12.0",
"@visx/scale": "^3.12.0",
"@visx/shape": "^3.12.0",
"@visx/text": "3.12.0",
Expand Down
68 changes: 68 additions & 0 deletions projects/js-packages/charts/src/components/legend/base-legend.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { LegendOrdinal } from '@visx/legend';
import { scaleOrdinal } from '@visx/scale';
import clsx from 'clsx';
import { FC } from 'react';
import styles from './legend.module.scss';
import type { LegendProps } from './types';

/**
* Base legend component that displays color-coded items with labels using visx
* @param {object} props - Component properties
* @param {Array} props.items - Array of legend items to display
* @param {string} props.className - Additional CSS class names
* @param {string} props.orientation - Layout orientation (horizontal/vertical)
* @return {JSX.Element} Rendered legend component
*/
const orientationToFlexDirection = {
horizontal: 'row' as const,
vertical: 'column' as const,
};

export const BaseLegend: FC< LegendProps > = ( {
items,
className,
orientation = 'horizontal',
} ) => {
const legendScale = scaleOrdinal( {
domain: items.map( item => item.label ),
range: items.map( item => item.color ),
} );

return (
<div className={ clsx( styles.legend, className ) } role="list">
<LegendOrdinal
scale={ legendScale }
direction={ orientationToFlexDirection[ orientation ] }
shape="rect"
shapeWidth={ 16 }
shapeHeight={ 16 }
className={ styles[ 'legend-items' ] }
>
{ labels => (
<div className={ styles[ `legend--${ orientation }` ] }>
{ labels.map( label => (
<div key={ label.text } className={ styles[ 'legend-item' ] }>
<svg width={ 16 } height={ 16 }>
<rect
width={ 16 }
height={ 16 }
fill={ label.value }
className={ styles[ 'legend-item-swatch' ] }
/>
</svg>
<span className={ styles[ 'legend-item-label' ] }>
{ label.text }
{ items.find( item => item.label === label.text )?.value && (
<span className={ styles[ 'legend-item-value' ] }>
{ items.find( item => item.label === label.text )?.value }
</span>
) }
</span>
</div>
) ) }
</div>
) }
</LegendOrdinal>
</div>
);
};
2 changes: 2 additions & 0 deletions projects/js-packages/charts/src/components/legend/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { BaseLegend } from './base-legend';
export type { LegendProps } from './types';
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.legend {
&--horizontal {
display: flex;
flex-direction: row;
flex-wrap: wrap;
gap: 16px;
}

&--vertical {
display: flex;
flex-direction: column;
gap: 8px;
}
}

.legend-item {
display: flex;
align-items: center;
gap: 8px;
font-size: 0.875rem;
}

.legend-item-swatch {
border-radius: 2px;
}

.legend-item-label {
color: var(--jp-gray-80);
display: flex;
align-items: center;
gap: 0.5rem;
}

.legend-item-value {
font-weight: 500;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Meta, StoryObj } from '@storybook/react';
import { BaseLegend } from '../base-legend';

const meta: Meta< typeof BaseLegend > = {
title: 'JS Packages/Charts/Legend',
component: BaseLegend,
parameters: {
layout: 'centered',
docs: {
description: {
component:
'A flexible legend component that can be customized with different styles and orientations.',
},
},
},
};

export default meta;
type Story = StoryObj< typeof BaseLegend >;

const mockData = [
{ label: 'Desktop', value: '86%', color: '#3858E9' },
{ label: 'Mobile', value: '52%', color: '#80C8FF' },
];

export const Horizontal: Story = {
args: {
items: mockData,
orientation: 'horizontal',
},
};

export const Vertical: Story = {
args: {
items: mockData,
orientation: 'vertical',
},
};

export const WithLongLabels: Story = {
args: {
items: [
{ label: 'Very Long Desktop Usage', value: '86%', color: '#3858E9' },
{ label: 'Extended Mobile Sessions', value: '52%', color: '#80C8FF' },
{ label: 'Tablet Device Access', value: '35%', color: '#44B556' },
],
orientation: 'horizontal',
},
};
14 changes: 14 additions & 0 deletions projects/js-packages/charts/src/components/legend/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { scaleOrdinal } from '@visx/scale';

export type LegendItem = {
label: string;
value: number | string;
color: string;
};

export type LegendProps = {
items: LegendItem[];
className?: string;
orientation?: 'horizontal' | 'vertical';
scale?: ReturnType< typeof scaleOrdinal >;
};
Loading