Skip to content

Commit a4f7d96

Browse files
authored
docs(charts): add example for normalized stacked chart (#6762)
1 parent 4d7c8e1 commit a4f7d96

File tree

15 files changed

+82
-19
lines changed

15 files changed

+82
-19
lines changed

packages/charts/src/components/BarChart/BarChart.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
22
import { Canvas, Meta } from '@storybook/addon-docs';
33
import TooltipStory from '../../resources/TooltipConfig.mdx';
44
import LegendStory from '../../resources/LegendConfig.mdx';
5+
import NormalizedStackedChartStory from '../../resources/NormalizedStackedChart.mdx';
56
import * as ComponentStories from './BarChart.stories';
67

78
<Meta of={ComponentStories} />
@@ -37,6 +38,8 @@ import * as ComponentStories from './BarChart.stories';
3738

3839
<Canvas of={ComponentStories.WithFormatter} />
3940

41+
<NormalizedStackedChartStory of={ComponentStories.WithNormalizedStacks} />
42+
4043
### Loading Placeholder
4144

4245
<Canvas of={ComponentStories.LoadingPlaceholder} />

packages/charts/src/components/BarChart/BarChart.stories.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
legendConfig,
55
secondaryDimensionDataSet,
66
simpleDataSet,
7+
stackedNormalizedConfig,
78
tooltipConfig
89
} from '../../resources/DemoProps.js';
910
import { BarChart } from './BarChart.js';
@@ -159,6 +160,10 @@ export const WithHighlightedMeasure: Story = {
159160
}
160161
};
161162

163+
export const WithNormalizedStacks: Story = {
164+
args: stackedNormalizedConfig
165+
};
166+
162167
export const WithCustomTooltipConfig: Story = {
163168
args: tooltipConfig
164169
};

packages/charts/src/components/BarChart/BarChart.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,6 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>((props, ref) => {
236236
resizeDebounce={chartConfig.resizeDebounce}
237237
{...propsWithoutOmitted}
238238
>
239-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
240-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
241239
<BarChartLib
242240
syncId={syncId}
243241
onClick={onClickInternal}

packages/charts/src/components/BulletChart/BulletChart.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ const BulletChart = forwardRef<HTMLDivElement, BulletChartProps>((props, ref) =>
281281
resizeDebounce={chartConfig.resizeDebounce}
282282
{...propsWithoutOmitted}
283283
>
284-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
285-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
286284
<ComposedChartLib
287285
syncId={syncId}
288286
onClick={onClickInternal}

packages/charts/src/components/ColumnChart/ColumnChart.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ControlsWithNote, DocsHeader, Footer } from '@sb/components';
22
import { Canvas, Meta } from '@storybook/addon-docs';
33
import TooltipStory from '../../resources/TooltipConfig.mdx';
44
import LegendStory from '../../resources/LegendConfig.mdx';
5+
import NormalizedStackedChartStory from '../../resources/NormalizedStackedChart.mdx';
56
import * as ComponentStories from './ColumnChart.stories';
67

78
<Meta of={ComponentStories} />
@@ -32,6 +33,8 @@ import * as ComponentStories from './ColumnChart.stories';
3233

3334
<Canvas of={ComponentStories.WithStacks} />
3435

36+
<NormalizedStackedChartStory of={ComponentStories.WithNormalizedStacks} />
37+
3538
### With Formatter
3639

3740
<Canvas of={ComponentStories.WithFormatter} />

packages/charts/src/components/ColumnChart/ColumnChart.stories.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
legendConfig,
55
secondaryDimensionDataSet,
66
simpleDataSet,
7+
stackedNormalizedConfig,
78
tooltipConfig
89
} from '../../resources/DemoProps.js';
910
import { ColumnChart } from './ColumnChart.js';
@@ -86,6 +87,10 @@ export const WithStacks: Story = {
8687
}
8788
};
8889

90+
export const WithNormalizedStacks: Story = {
91+
args: stackedNormalizedConfig
92+
};
93+
8994
export const WithFormatter: Story = {
9095
args: {
9196
dimensions: [{ accessor: 'name', formatter: (element) => element.slice(0, 3) }],

packages/charts/src/components/ColumnChart/ColumnChart.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,6 @@ const ColumnChart = forwardRef<HTMLDivElement, ColumnChartProps>((props, ref) =>
235235
resizeDebounce={chartConfig.resizeDebounce}
236236
{...propsWithoutOmitted}
237237
>
238-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
239-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
240238
<ColumnChartLib
241239
syncId={syncId}
242240
onClick={onClickInternal}

packages/charts/src/components/ComposedChart/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,6 @@ const ComposedChart = forwardRef<HTMLDivElement, ComposedChartProps>((props, ref
286286
resizeDebounce={chartConfig.resizeDebounce}
287287
{...propsWithoutOmitted}
288288
>
289-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
290-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
291289
<ComposedChartLib
292290
syncId={syncId}
293291
onClick={onClickInternal}

packages/charts/src/components/LineChart/LineChart.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
230230
resizeDebounce={chartConfig.resizeDebounce}
231231
{...propsWithoutOmitted}
232232
>
233-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
234-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
235233
<LineChartLib
236234
syncId={syncId}
237235
margin={marginChart}

packages/charts/src/components/PieChart/PieChart.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,6 @@ const PieChart = forwardRef<HTMLDivElement, PieChartProps>((props, ref) => {
289289
resizeDebounce={chartConfig.resizeDebounce}
290290
{...propsWithoutOmitted}
291291
>
292-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
293-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
294292
<PieChartLib
295293
onClick={onClickInternal}
296294
margin={chartConfig.margin}

packages/charts/src/components/RadarChart/RadarChart.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ const RadarChart = forwardRef<HTMLDivElement, RadarChartProps>((props, ref) => {
175175
resizeDebounce={chartConfig.resizeDebounce}
176176
{...propsWithoutOmitted}
177177
>
178-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
179-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
180178
<RadarChartLib
181179
onClick={onClickInternal}
182180
data={dataset}

packages/charts/src/components/RadialChart/RadialChart.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ const RadialChart = forwardRef<HTMLDivElement, RadialChartProps>((props, ref) =>
141141
style={style}
142142
{...rest}
143143
>
144-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
145-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
146144
<RadialBarChart
147145
onClick={onClickInternal}
148146
innerRadius="90%"

packages/charts/src/components/ScatterChart/ScatterChart.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ interface IScatterChartConfig extends ICartesianChartConfig {
6262
referenceLineX?: {
6363
value?: number;
6464
color?: string;
65-
//todo: remove "Omit" once ref type has been fixed
6665
} & Omit<ReferenceLineProps, 'ref'>;
6766
}
6867

@@ -227,8 +226,6 @@ const ScatterChart = forwardRef<HTMLDivElement, ScatterChartProps>((props, ref)
227226
resizeDebounce={chartConfig.resizeDebounce}
228227
{...propsWithoutOmitted}
229228
>
230-
{/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */}
231-
{/*// @ts-ignore:: todo not yet compatible with React19*/}
232229
<ScatterChartLib
233230
onClick={onClickInternal}
234231
margin={marginChart}

packages/charts/src/resources/DemoProps.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,3 +602,41 @@ export const complexBulletDataset = [
602602
volume: 320
603603
}
604604
];
605+
606+
const percentFormatter = (val) => `${val.toFixed(1)}%`;
607+
function normalizeData(data: Record<string, number | string>[]) {
608+
return data.map((item) => {
609+
const total = Object.values(item).reduce((acc: number, cur) => {
610+
return typeof cur === 'number' ? acc + cur : acc;
611+
}, 0) as number;
612+
613+
const normalizedItem = Object.entries(item).map(([key, val]) => {
614+
if (typeof val === 'number') {
615+
return [key, total ? (val / total) * 100 : 0];
616+
}
617+
return [key, val];
618+
});
619+
620+
return Object.fromEntries(normalizedItem);
621+
});
622+
}
623+
export const stackedNormalizedConfig = {
624+
measures: [
625+
{
626+
accessor: 'users',
627+
stackId: 'A',
628+
formatter: percentFormatter
629+
},
630+
{
631+
accessor: 'sessions',
632+
stackId: 'A',
633+
formatter: percentFormatter
634+
},
635+
{
636+
accessor: 'volume',
637+
stackId: 'A',
638+
formatter: percentFormatter
639+
}
640+
],
641+
dataset: normalizeData(complexDataSet)
642+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Canvas } from '@storybook/addon-docs';
2+
3+
### Normalized stacked chart
4+
5+
By transforming the `dataset`, a stacked chart can be normalized.
6+
7+
<Canvas of={props.of} />
8+
9+
#### Example normalize function
10+
11+
```ts
12+
function normalizeData(data: Record<string, number | string>[]) {
13+
return data.map((item) => {
14+
const total = Object.values(item).reduce((acc: number, cur) => {
15+
return typeof cur === 'number' ? acc + cur : acc;
16+
}, 0);
17+
18+
const normalizedItem = Object.entries(item).map(([key, val]) => {
19+
if (typeof val === 'number') {
20+
return [key, total ? (val / total) * 100 : 0];
21+
}
22+
return [key, val];
23+
});
24+
25+
return Object.fromEntries(normalizedItem);
26+
});
27+
}
28+
```

0 commit comments

Comments
 (0)