Skip to content

Commit

Permalink
added logic to handle cases where not all of the data keys have been …
Browse files Browse the repository at this point in the history
…included in categories.

added categories demos
  • Loading branch information
nstolpe committed Oct 28, 2024
1 parent 5810e0c commit 7623959
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
12 changes: 12 additions & 0 deletions demo/ts/components/victory-pie-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ export default class VictoryPieDemo extends React.Component<
style={{ parent: parentStyle }}
labelPosition="startAngle"
/>
<VictoryPie
theme={VictoryTheme.clean}
style={{ parent: parentStyle }}
categories={{ x: ["E", "A", "D", "C", "B"] }}
/>

<VictoryPie
theme={VictoryTheme.clean}
style={{ parent: parentStyle }}
categories={{ x: ["E", "A", "C"] }}
/>

<VictoryPie
theme={VictoryTheme.clean}
style={{ parent: parentStyle }}
Expand Down
25 changes: 15 additions & 10 deletions packages/victory-pie/src/helper-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ const getSlices = (props, data) => {
return layoutFunction(data);
};

const sortDataByCategories = (props, data) =>
Array.isArray(props?.categories?.x)
? props.categories.x.reduce((newData, category) => {
const datum = data.find(({ x }) => x === category);
if (datum) {
newData.push(datum);
}
return newData;
}, [])
: data;
// sorts data by the categories prop. if all of the data keys aren't included in categories,
// any remaining data will be appended to the data array.
const sortDataByCategories = (props, data) => {
if (Array.isArray(props?.categories?.x)) {
const sorted: string[] = [];
props.categories.x.forEach((category) => {
const idx = data.findIndex(({ x }) => x === category);
const datum = data.splice(idx, 1)[0];
sorted.push(datum);
});
return [...sorted, ...data];
}

return data;
};

const getCalculatedValues = (props) => {
const { colorScale, theme } = props;
Expand Down

0 comments on commit 7623959

Please sign in to comment.