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

feat(charts): allow configuring the internal Legend component #6493

Merged
merged 1 commit into from
Oct 17, 2024
Merged
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
16 changes: 16 additions & 0 deletions cypress/support/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,19 @@ export function testChartZoomingTool(Component, props) {
cy.get('.recharts-brush [stroke="red"]').should('be.visible');
});
}

export function testChartLegendConfig(Component, props) {
it('legendConfig', () => {
cy.mount(
<Component
{...props}
chartConfig={{
legendConfig: {
formatter: (value) => <span data-testid="catval">{value}🐱</span>
}
}}
/>
);
cy.findAllByTestId('catval').should('be.visible');
});
}
4 changes: 3 additions & 1 deletion packages/charts/src/components/BarChart/BarChart.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { complexDataSet } from '../../resources/DemoProps.js';
import { BarChart } from './BarChart.js';
import { cypressPassThroughTestsFactory, testChartZoomingTool } from '@/cypress/support/utils';
import { cypressPassThroughTestsFactory, testChartLegendConfig, testChartZoomingTool } from '@/cypress/support/utils';

const dimensions = [
{
Expand Down Expand Up @@ -88,6 +88,8 @@ describe('BarChart', () => {
cy.contains('Loading...').should('exist');
});

testChartLegendConfig(BarChart, { dataset: complexDataSet, dimensions, measures });

testChartZoomingTool(BarChart, { dataset: complexDataSet, dimensions, measures });

cypressPassThroughTestsFactory(BarChart, { dimensions: [], measures: [] });
Expand Down
6 changes: 6 additions & 0 deletions packages/charts/src/components/BarChart/BarChart.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
import { Canvas, Meta } from '@storybook/addon-docs';
import TooltipStory from '../../resources/TooltipConfig.mdx';
import LegendStory from '../../resources/LegendConfig.mdx';
import * as ComponentStories from './BarChart.stories';

<Meta of={ComponentStories} />
Expand Down Expand Up @@ -65,4 +66,9 @@ You can set a reference line to any value by using the `referenceLine` `chartCon

<TooltipStory of={ComponentStories.WithCustomTooltipConfig} />

<LegendStory of={ComponentStories.WithCustomLegendConfig} />

<br />
<br />

<Footer />
12 changes: 11 additions & 1 deletion packages/charts/src/components/BarChart/BarChart.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { Meta, StoryObj } from '@storybook/react';
import { complexDataSet, secondaryDimensionDataSet, simpleDataSet, tooltipConfig } from '../../resources/DemoProps.js';
import {
complexDataSet,
legendConfig,
secondaryDimensionDataSet,
simpleDataSet,
tooltipConfig
} from '../../resources/DemoProps.js';
import { BarChart } from './BarChart.js';

const meta = {
Expand Down Expand Up @@ -156,3 +162,7 @@ export const WithHighlightedMeasure: Story = {
export const WithCustomTooltipConfig: Story = {
args: tooltipConfig
};

export const WithCustomLegendConfig: Story = {
args: legendConfig
};
1 change: 1 addition & 0 deletions packages/charts/src/components/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
* @param measure {IChartMeasure} Current measure object
* @param dataElement {object} Current data element
*/
highlightColor?: (value: number, measure: MeasureConfig, dataElement: Record<string, any>) => CSSProperties['color'];

Check warning on line 79 in packages/charts/src/components/BarChart/BarChart.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}

interface DimensionConfig extends IChartDimension {
Expand Down Expand Up @@ -186,7 +186,7 @@
? dataKeys.findIndex((key) => key === chartConfig.secondYAxis?.dataKey)
: 0;

const [componentRef, chartRef] = useSyncRef<any>(ref);

Check warning on line 189 in packages/charts/src/components/BarChart/BarChart.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

const onItemLegendClick = useLegendItemClick(onLegendClick);
const labelFormatter = useLabelFormatter(primaryDimension);
Expand Down Expand Up @@ -362,6 +362,7 @@
align={chartConfig.legendHorizontalAlign}
onClick={onItemLegendClick}
wrapperStyle={legendPosition}
{...chartConfig.legendConfig}
/>
)}
{referenceLine && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { complexDataSet } from '../../resources/DemoProps.js';
import { BulletChart } from './BulletChart.js';
import { cypressPassThroughTestsFactory, testChartZoomingTool } from '@/cypress/support/utils';
import { cypressPassThroughTestsFactory, testChartLegendConfig, testChartZoomingTool } from '@/cypress/support/utils';

const dimensions = [
{
Expand Down Expand Up @@ -83,5 +83,7 @@ describe('BulletChart', () => {

testChartZoomingTool(BulletChart, { dataset: complexDataSet, dimensions, measures });

testChartLegendConfig(BulletChart, { dataset: complexDataSet, dimensions, measures });

cypressPassThroughTestsFactory(BulletChart, { dimensions: [], measures: [] });
});
6 changes: 6 additions & 0 deletions packages/charts/src/components/BulletChart/BulletChart.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
import { Canvas, Meta } from '@storybook/addon-docs';
import TooltipStory from '../../resources/TooltipConfig.mdx';
import LegendStory from '../../resources/LegendConfig.mdx';
import * as ComponentStories from './BulletChart.stories';

<Meta of={ComponentStories} />
Expand Down Expand Up @@ -67,4 +68,9 @@ You can do this via the `measures` array:

<TooltipStory of={ComponentStories.WithCustomTooltipConfig} />

<LegendStory of={ComponentStories.WithCustomLegendConfig} />

<br />
<br />

<Footer />
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react';
import { complexBulletDataset, tooltipConfig } from '../../resources/DemoProps.js';
import { complexBulletDataset, legendConfig, tooltipConfig } from '../../resources/DemoProps.js';
import { BulletChart } from './BulletChart.js';

const meta = {
Expand Down Expand Up @@ -120,3 +120,7 @@ export const LoadingPlaceholder: Story = {
export const WithCustomTooltipConfig: Story = {
args: tooltipConfig
};

export const WithCustomLegendConfig: Story = {
args: legendConfig
};
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
* @param measure {IChartMeasure} Current measure object
* @param dataElement {object} Current data element
*/
highlightColor?: (value: number, measure: MeasureConfig, dataElement: Record<string, any>) => CSSProperties['color'];

Check warning on line 69 in packages/charts/src/components/BulletChart/BulletChart.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
}

interface DimensionConfig extends IChartDimension {
Expand Down Expand Up @@ -146,7 +146,7 @@
...rest
} = props;

const [componentRef, chartRef] = useSyncRef<any>(ref);

Check warning on line 149 in packages/charts/src/components/BulletChart/BulletChart.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

const chartConfig: BulletChartProps['chartConfig'] = {
yAxisVisible: false,
Expand Down Expand Up @@ -225,7 +225,7 @@
);
} else {
onDataPointClick(
enrichEventWithDetails({} as any, {

Check warning on line 228 in packages/charts/src/components/BulletChart/BulletChart.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
value: eventOrIndex.value,
dataKey: eventOrIndex.dataKey,
dataIndex: eventOrIndex.index,
Expand Down Expand Up @@ -303,7 +303,7 @@
{chartConfig.xAxisVisible &&
dimensions.map((dimension, index) => {
let AxisComponent;
const axisProps: any = {

Check warning on line 306 in packages/charts/src/components/BulletChart/BulletChart.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
dataKey: dimension.accessor,
interval: dimension?.interval ?? (isBigDataSet ? 'preserveStartEnd' : 0),
tickLine: index < 1,
Expand Down Expand Up @@ -440,10 +440,11 @@
align={chartConfig.legendHorizontalAlign}
onClick={onItemLegendClick}
wrapperStyle={legendPosition}
{...chartConfig.legendConfig}
/>
)}
{sortedMeasures?.map((element, index) => {
const chartElementProps: any = {

Check warning on line 447 in packages/charts/src/components/BulletChart/BulletChart.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
isAnimationActive: !noAnimation
};
let labelPosition = 'top';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { complexDataSet } from '../../resources/DemoProps.js';
import { ColumnChart } from './ColumnChart.js';
import { cypressPassThroughTestsFactory, testChartZoomingTool } from '@/cypress/support/utils';
import { cypressPassThroughTestsFactory, testChartLegendConfig, testChartZoomingTool } from '@/cypress/support/utils';

const dimensions = [
{
Expand Down Expand Up @@ -80,5 +80,7 @@ describe('ColumnChart', () => {

testChartZoomingTool(ColumnChart, { dataset: complexDataSet, dimensions, measures });

testChartLegendConfig(ColumnChart, { dataset: complexDataSet, dimensions, measures });

cypressPassThroughTestsFactory(ColumnChart, { dimensions: [], measures: [] });
});
6 changes: 6 additions & 0 deletions packages/charts/src/components/ColumnChart/ColumnChart.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
import { Canvas, Meta } from '@storybook/addon-docs';
import TooltipStory from '../../resources/TooltipConfig.mdx';
import LegendStory from '../../resources/LegendConfig.mdx';
import * as ComponentStories from './ColumnChart.stories';

<Meta of={ComponentStories} />
Expand Down Expand Up @@ -64,4 +65,9 @@ You can set a reference line to any value by using the `referenceLine` `chartCon

<TooltipStory of={ComponentStories.WithCustomTooltipConfig} />;

<LegendStory of={ComponentStories.WithCustomLegendConfig} />

<br />
<br />

<Footer />
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { Meta, StoryObj } from '@storybook/react';
import { complexDataSet, secondaryDimensionDataSet, simpleDataSet, tooltipConfig } from '../../resources/DemoProps.js';
import {
complexDataSet,
legendConfig,
secondaryDimensionDataSet,
simpleDataSet,
tooltipConfig
} from '../../resources/DemoProps.js';
import { ColumnChart } from './ColumnChart.js';

const meta = {
Expand Down Expand Up @@ -147,3 +153,7 @@ export const WithHighlightedMeasure: Story = {
export const WithCustomTooltipConfig: Story = {
args: tooltipConfig
};

export const WithCustomLegendConfig: Story = {
args: legendConfig
};
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ const ColumnChart = forwardRef<HTMLDivElement, ColumnChartProps>((props, ref) =>
align={chartConfig.legendHorizontalAlign}
onClick={onItemLegendClick}
wrapperStyle={legendPosition}
{...chartConfig.legendConfig}
/>
)}
{referenceLine && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { complexDataSet } from '../../resources/DemoProps.js';
import { ColumnChartWithTrend } from './ColumnChartWithTrend.js';
import { cypressPassThroughTestsFactory, testChartZoomingTool } from '@/cypress/support/utils';
import { cypressPassThroughTestsFactory, testChartLegendConfig, testChartZoomingTool } from '@/cypress/support/utils';

const dimensions = [
{
Expand Down Expand Up @@ -87,5 +87,7 @@ describe('ColumnChartWithTrend', () => {

testChartZoomingTool(ColumnChartWithTrend, { dataset: complexDataSet, dimensions, measures });

testChartLegendConfig(ColumnChartWithTrend, { dataset: complexDataSet, dimensions, measures });

cypressPassThroughTestsFactory(ColumnChartWithTrend, { dimensions: [], measures: [] });
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
import { Canvas, Meta } from '@storybook/addon-docs';
import * as ComponentStories from './ColumnChartWithTrend.stories';
import LegendStory from '../../resources/LegendConfig.mdx';

<Meta of={ComponentStories} />

Expand All @@ -16,4 +17,9 @@ import * as ComponentStories from './ColumnChartWithTrend.stories';

<Canvas of={ComponentStories.LoadingPlaceholder} />

<LegendStory of={ComponentStories.WithCustomLegendConfig} />

<br />
<br />

<Footer />
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react';
import { complexDataSet } from '../../resources/DemoProps.js';
import { complexDataSet, legendConfig } from '../../resources/DemoProps.js';
import { ColumnChartWithTrend } from './ColumnChartWithTrend.js';

const meta = {
Expand Down Expand Up @@ -40,3 +40,7 @@ export const LoadingPlaceholder: Story = {
dataset: []
}
};

export const WithCustomLegendConfig: Story = {
args: legendConfig
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { complexDataSet } from '../../resources/DemoProps.js';
import { ComposedChart } from './index.js';
import { cypressPassThroughTestsFactory, testChartZoomingTool } from '@/cypress/support/utils';
import { cypressPassThroughTestsFactory, testChartLegendConfig, testChartZoomingTool } from '@/cypress/support/utils';

const dimensions = [
{
Expand Down Expand Up @@ -87,5 +87,7 @@ describe('ComposedChart', () => {

testChartZoomingTool(ComposedChart, { dataset: complexDataSet, dimensions, measures });

testChartLegendConfig(ComposedChart, { dataset: complexDataSet, dimensions, measures });

cypressPassThroughTestsFactory(ComposedChart, { dimensions: [], measures: [] });
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
import { Canvas, Meta } from '@storybook/addon-docs';
import TooltipStory from '../../resources/TooltipConfig.mdx';
import LegendStory from '../../resources/LegendConfig.mdx';
import * as ComponentStories from './ComposedChart.stories';

<Meta of={ComponentStories} />
Expand Down Expand Up @@ -65,4 +66,9 @@ You can set a reference line to any value by using the `referenceLine` `chartCon

<TooltipStory of={ComponentStories.WithCustomTooltipConfig} />

<LegendStory of={ComponentStories.WithCustomLegendConfig} />

<br />
<br />

<Footer />
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react';
import { bigDataSet, complexDataSet, simpleDataSet, tooltipConfig } from '../../resources/DemoProps.js';
import { bigDataSet, complexDataSet, legendConfig, simpleDataSet, tooltipConfig } from '../../resources/DemoProps.js';
import { ComposedChart } from './index.js';

const meta = {
Expand Down Expand Up @@ -207,3 +207,7 @@ export const LoadingPlaceholder: Story = {
export const WithCustomTooltipConfig: Story = {
args: tooltipConfig
};

export const WithCustomLegendConfig: Story = {
args: legendConfig
};
1 change: 1 addition & 0 deletions packages/charts/src/components/ComposedChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ const ComposedChart = forwardRef<HTMLDivElement, ComposedChartProps>((props, ref
align={chartConfig.legendHorizontalAlign}
onClick={onItemLegendClick}
wrapperStyle={legendPosition}
{...chartConfig.legendConfig}
/>
)}
{measures?.map((element, index) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/charts/src/components/DonutChart/DonutChart.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { simpleDataSet } from '../../resources/DemoProps.js';
import { complexDataSet, simpleDataSet } from '../../resources/DemoProps.js';
import { DonutChart } from './DonutChart.js';
import { cypressPassThroughTestsFactory } from '@/cypress/support/utils';
import { cypressPassThroughTestsFactory, testChartLegendConfig } from '@/cypress/support/utils';

const dimension = {
accessor: 'name'
Expand Down Expand Up @@ -61,4 +61,6 @@ describe('DonutChart', () => {
});

cypressPassThroughTestsFactory(DonutChart, { dimension: {}, measure: {} });

testChartLegendConfig(DonutChart, { dataset: complexDataSet, dimension, measure });
});
6 changes: 6 additions & 0 deletions packages/charts/src/components/DonutChart/DonutChart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Canvas, Meta } from '@storybook/addon-docs';
import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
import TooltipStory from '../../resources/TooltipConfig.mdx';
import * as ComponentStories from './DonutChart.stories';
import LegendStory from '../../resources/LegendConfig.mdx';

<Meta of={ComponentStories} />

Expand Down Expand Up @@ -50,4 +51,9 @@ import * as ComponentStories from './DonutChart.stories';

<TooltipStory of={ComponentStories.WithCustomTooltipConfig} />

<LegendStory of={ComponentStories.WithCustomLegendConfig} />

<br />
<br />

<Footer />
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Meta, StoryObj } from '@storybook/react';
import { simpleDataSet, simpleDataSetWithSmallValues, tooltipConfig } from '../../resources/DemoProps.js';
import { legendConfig, simpleDataSet, simpleDataSetWithSmallValues, tooltipConfig } from '../../resources/DemoProps.js';
import { DonutChart } from './DonutChart.js';

const meta = {
Expand Down Expand Up @@ -99,3 +99,7 @@ export const HideLabels: Story = {
export const WithCustomTooltipConfig: Story = {
args: tooltipConfig
};

export const WithCustomLegendConfig: Story = {
args: legendConfig
};
4 changes: 3 additions & 1 deletion packages/charts/src/components/LineChart/LineChart.cy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { complexDataSet } from '../../resources/DemoProps.js';
import { LineChart } from './LineChart.js';
import { cypressPassThroughTestsFactory, testChartZoomingTool } from '@/cypress/support/utils';
import { cypressPassThroughTestsFactory, testChartLegendConfig, testChartZoomingTool } from '@/cypress/support/utils';

const dimensions = [
{
Expand Down Expand Up @@ -80,5 +80,7 @@ describe('LineChart', () => {

testChartZoomingTool(LineChart, { dataset: complexDataSet, dimensions, measures });

testChartLegendConfig(LineChart, { dataset: complexDataSet, dimensions, measures });

cypressPassThroughTestsFactory(LineChart, { dimensions: [], measures: [] });
});
6 changes: 6 additions & 0 deletions packages/charts/src/components/LineChart/LineChart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Canvas, Meta } from '@storybook/addon-docs';
import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
import TooltipStory from '../../resources/TooltipConfig.mdx';
import * as ComponentStories from './LineChart.stories';
import LegendStory from '../../resources/LegendConfig.mdx';

<Meta of={ComponentStories} />

Expand Down Expand Up @@ -92,4 +93,9 @@ export const LineChartWithLinearGradient = () => {

<TooltipStory of={ComponentStories.WithCustomTooltipConfig} />

<LegendStory of={ComponentStories.WithCustomLegendConfig} />

<br />
<br />

<Footer />
Loading
Loading