-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
packages/polaris-viz/src/components/BarChart/stories/playground/MisMatchedData.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type {Story} from '@storybook/react'; | ||
|
||
import {BarChart, BarChartProps} from '../../../../components'; | ||
import {META} from '../meta'; | ||
|
||
export default { | ||
...META, | ||
title: `${META.title}/Playground`, | ||
}; | ||
|
||
const DATA = [ | ||
{ | ||
name: 'Canada', | ||
data: [ | ||
{key: 'Mice', value: 13.28}, | ||
{key: 'Dogs', value: 23.43}, | ||
{key: 'Cats', value: 6.64}, | ||
{key: 'Birds', value: 54.47}, | ||
], | ||
}, | ||
{ | ||
name: 'United States', | ||
data: [ | ||
{key: 'Lizards', value: 350.13}, | ||
{key: 'Turtles', value: 223.43}, | ||
{key: 'Mice', value: 15.38}, | ||
{key: 'Snakes', value: 122.68}, | ||
{key: 'Dogs', value: 31.54}, | ||
{key: 'Birds', value: 94.84}, | ||
], | ||
}, | ||
{ | ||
name: 'China', | ||
data: [ | ||
{key: 'Snakes', value: 0}, | ||
{key: 'Dogs', value: 0}, | ||
], | ||
}, | ||
]; | ||
|
||
const Template: Story<BarChartProps> = () => { | ||
return <BarChart data={DATA} />; | ||
}; | ||
|
||
export const MisMatchedData = Template.bind({}); |
49 changes: 49 additions & 0 deletions
49
...s/polaris-viz/src/components/SimpleBarChart/stories/playground/MisMatchedData.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type {Story} from '@storybook/react'; | ||
|
||
import {SimpleBarChart, SimpleBarChartProps} from '../../../../components'; | ||
import {META} from '../meta'; | ||
|
||
export default { | ||
...META, | ||
title: `${META.title}/Playground`, | ||
}; | ||
|
||
const DATA = [ | ||
{ | ||
name: 'Canada', | ||
data: [ | ||
{key: 'Mice', value: 13.28}, | ||
{key: 'Dogs', value: 23.43}, | ||
{key: 'Cats', value: 6.64}, | ||
{key: 'Birds', value: 54.47}, | ||
], | ||
}, | ||
{ | ||
name: 'United States', | ||
data: [ | ||
{key: 'Lizards', value: 350.13}, | ||
{key: 'Turtles', value: 223.43}, | ||
{key: 'Mice', value: 15.38}, | ||
{key: 'Snakes', value: 122.68}, | ||
{key: 'Dogs', value: 31.54}, | ||
{key: 'Birds', value: 94.84}, | ||
], | ||
}, | ||
{ | ||
name: 'China', | ||
data: [ | ||
{key: 'Snakes', value: 0}, | ||
{key: 'Dogs', value: 0}, | ||
], | ||
}, | ||
]; | ||
|
||
const Template: Story<SimpleBarChartProps> = () => { | ||
return ( | ||
<div style={{height: 600, width: 800}}> | ||
<SimpleBarChart data={DATA} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const MisMatchedData = Template.bind({}); |
27 changes: 27 additions & 0 deletions
27
packages/polaris-viz/src/utilities/fillMissingDataPoints.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type {DataSeries} from '@shopify/polaris-viz-core'; | ||
|
||
export function fillMissingDataPoints(dataSeries: DataSeries[]) { | ||
const allKeys = new Set<string>(); | ||
|
||
const areAnyDataLengthsDifferent = dataSeries.some( | ||
({data}) => data.length !== dataSeries[0].data.length, | ||
); | ||
|
||
if (!areAnyDataLengthsDifferent) { | ||
return dataSeries; | ||
} | ||
|
||
for (const {data} of dataSeries) { | ||
for (const {key} of data) { | ||
allKeys.add(`${key}`); | ||
} | ||
} | ||
|
||
return dataSeries.map(({name, data}) => ({ | ||
name, | ||
data: [...allKeys].map((key) => { | ||
const matchedValue = data.find((item) => item.key === key); | ||
return matchedValue ? matchedValue : {key, value: null}; | ||
}), | ||
})); | ||
} |