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

New component - PieChart #3470

Merged
merged 27 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
020ee68
Added new component - PieChart
nitzanyiz Dec 25, 2024
7e67b6d
Added PieChart component with more props
nitzanyiz Dec 25, 2024
946865b
Added custom hook and richer api
nitzanyiz Dec 26, 2024
f255a3c
Removed default padding
nitzanyiz Dec 26, 2024
4654d42
Rename to constants
nitzanyiz Dec 26, 2024
cfc1a85
Moved more logic into the custom hook
nitzanyiz Dec 26, 2024
b7c9e80
Fixed divider width
nitzanyiz Dec 26, 2024
c908a35
Fixed props deconstruct
nitzanyiz Dec 26, 2024
6a2d2da
Improved PieChart api and added PieChart screen
nitzanyiz Jan 1, 2025
d17152b
Changed example to fit gidelines
nitzanyiz Jan 1, 2025
622300a
Removed padding prop from PieChart
nitzanyiz Jan 2, 2025
ede9897
Removed stylesheet from piechart screen
nitzanyiz Jan 2, 2025
07e9217
Fix import path for PieChart component
nitzanyiz Jan 8, 2025
6cf616b
Rename PartialCircle component to PieSegment and update props type
nitzanyiz Jan 8, 2025
1817bf5
Rename PartialCircle component to PieSegment and update export statement
nitzanyiz Jan 8, 2025
3aeeafd
Add error handling for missing "@react-native-svg" dependency in PieS…
nitzanyiz Jan 8, 2025
b6d84cb
Moved PieChart into Charts Category
nitzanyiz Jan 8, 2025
645e547
Add support for partial pie charts and update PieChartScreen
nitzanyiz Jan 8, 2025
ec6f9a1
Enhance PieSegment and PieChart components with dividerWidth and divi…
nitzanyiz Jan 8, 2025
0775c74
rename size to diameter and added documentation
nitzanyiz Jan 8, 2025
56c30c5
completed rename size -> diameter
nitzanyiz Jan 8, 2025
5849aa3
Merge remote-tracking branch 'origin' into feat/new-component-PieChart
nitzanyiz Jan 8, 2025
94af279
Replace PartialCircle with PieSegment in PieChart component
nitzanyiz Jan 12, 2025
4bf82d2
Moved default values to props deconstruct in PieSegment.
nitzanyiz Jan 15, 2025
62d23fa
Add dividerWidth and dividerColor props to PieChart API json
nitzanyiz Jan 15, 2025
9724813
Moved react-native-svg error handling to the PieChart component
nitzanyiz Jan 15, 2025
d04464e
Add null return for PieChart component when Svg or Path is not available
nitzanyiz Jan 22, 2025
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
3 changes: 3 additions & 0 deletions demo/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ module.exports = {
get Pinterest() {
return require('./screens/realExamples/Pinterest').default;
},
get PieChartScreen() {
return require('./screens/componentScreens/PieChartScreen.tsx').default;
},
get ListActionsScreen() {
return require('./screens/realExamples/ListActions/ListActionsScreen').default;
},
Expand Down
6 changes: 6 additions & 0 deletions demo/src/screens/MenuStructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ export const navigationData = {
{title: 'SortableGridList', tags: 'sort grid list drag', screen: 'unicorn.components.SortableGridListScreen'}
]
},
Charts: {
title: 'Charts',
screens: [
{title: 'PieChart', tags: 'pie chart data', screen: 'unicorn.components.PieChartScreen'}
]
},
LayoutsAndTemplates: {
title: 'Layouts & Templates',
screens: [
Expand Down
101 changes: 101 additions & 0 deletions demo/src/screens/componentScreens/PieChartScreen.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import React from 'react';
import {ScrollView} from 'react-native';
import {View, PieChart, Card, Text, Badge, PieChartSegmentProps, Colors} from 'react-native-ui-lib';

const SEGMENTS: PieChartSegmentProps[] = [
{
percentage: 40,
color: Colors.blue30
},
{
percentage: 30,
color: Colors.red30
},
{
percentage: 20,
color: Colors.green30
},
{
percentage: 10,
color: Colors.purple30
}
];

const MONOCHROME_SEGMENTS: PieChartSegmentProps[] = [
{
percentage: 40,
color: Colors.blue70
},
{
percentage: 30,
color: Colors.blue50
},
{
percentage: 20,
color: Colors.blue30
},
{
percentage: 10,
color: Colors.blue10
}
];

const NOT_FULL_PIECHART: PieChartSegmentProps[] = [
{
percentage: 30,
color: Colors.blue30
},
{
percentage: 40,
color: Colors.red30
}
];

const PieChartScreen = () => {
const renderSegmentLabel = (segment: PieChartSegmentProps, text: string) => {
const {percentage, color} = segment;
return (
<View row gap-s1 marginB-s1 key={text}>
<Badge size={10} containerStyle={{justifyContent: 'center'}} backgroundColor={color}/>
<View>
<Text>{text}</Text>
<Text marginL-s1>{percentage}%</Text>
</View>
</View>
);
};

const renderPieChartCard = (segments: PieChartSegmentProps[]) => {
return (
<Card row spread paddingL-s2 paddingR-s10 paddingV-s2>
<View centerV>
<PieChart segments={segments} diameter={150}/>
</View>
<View height={'100%'} gap-s1>
{segments.map((segment, index) => renderSegmentLabel(segment, `Value ${index + 1}`))}
</View>
</Card>
);
};

return (
<ScrollView>
<View padding-page gap-s2>
<Text text50L marginB-s2>
PieChart
</Text>
{renderPieChartCard(SEGMENTS)}
<Text text50L marginV-s2>
Monochrome colors
</Text>
{renderPieChartCard(MONOCHROME_SEGMENTS)}
<Text text50L marginV-s2>
Not Full PieChart
</Text>
{renderPieChartCard(NOT_FULL_PIECHART)}
</View>
</ScrollView>
);
};

export default PieChartScreen;
1 change: 1 addition & 0 deletions demo/src/screens/componentScreens/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export function registerScreens(registrar) {
registrar('unicorn.components.ActionSheetScreen', () => require('./ActionSheetScreen').default);
registrar('unicorn.components.PieChartScreen', () => require('./PieChartScreen').default);
registrar('unicorn.components.ActionBarScreen', () => require('./ActionBarScreen').default);
registrar('unicorn.components.AvatarsScreen', () => require('./AvatarsScreen').default);
registrar('unicorn.components.AnimatedImageScreen', () => require('./AnimatedImageScreen').default);
Expand Down
15 changes: 15 additions & 0 deletions src/components/pieChart/PieChart.api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "PieChart",
"category": "charts",
"description": "Pie Chart",
"example": "https://github.com/wix/react-native-ui-lib/blob/master/demo/src/screens/componentScreens/PieChartScreen.tsx",
"props": [
{"name": "segments", "type": "PieChartSegmentProps[]", "description": "Pie chart segments array"},
{"name": "diameter", "type": "number", "description": "Pie chart diameter"},
{"name": "dividerWidth", "type": "number", "description": "The width of the divider between the segments"},
{"name": "dividerColor", "type": "ColorValue", "description": "The color of the divider between the segments"}
],
"snippet": [
"<PieChart segments={[{percentage: 50, color: Colors.blue30}, {percentage: 30, color: Colors.red30}, {percentage: 20, color: Colors.green30}]} diameter={144}/>"
]
}
106 changes: 106 additions & 0 deletions src/components/pieChart/PieSegment.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import React from 'react';
import {ColorValue, StyleSheet} from 'react-native';
import View from '../view';
import {SvgPackage} from '../../optionalDependencies';
import {Colors} from '../../style';
const {Svg, Path} = SvgPackage;

export type PieSegmentProps = {
/**
* The percentage of pie the segment should cover
*/
percentage: number;
/**
* The radius of the containing pie
*/
radius: number;
/**
* The color of the segment
*/
color: string;
/**
* The start angle of the segment
*/
startAngle?: number;
/**
* The padding between the segments and the container of the pie.
*/
padding?: number;
/**
* The width of the divider between the segments
*/
dividerWidth?: number;
/**
* The color of the divider between the segments
*/
dividerColor?: ColorValue;
};

const PieSegment = (props: PieSegmentProps) => {
const {
percentage,
radius,
color,
startAngle = 0,
padding = 0,
dividerWidth = 4,
dividerColor = Colors.$backgroundDefault
} = props;

const actualRadius = radius - padding;
const centerXAndY = radius;
const amountToCover = (percentage / 100) * 360;
const angleFromTop = startAngle - 90;

const startRad = (angleFromTop * Math.PI) / 180;
const endRad = startRad + (amountToCover * Math.PI) / 180;

const startX = centerXAndY + Math.cos(startRad) * actualRadius;
const startY = centerXAndY + Math.sin(startRad) * actualRadius;
const endX = centerXAndY + Math.cos(endRad) * actualRadius;
const endY = centerXAndY + Math.sin(endRad) * actualRadius;

const largeArcFlag = amountToCover > 180 ? 1 : 0;
const sweepFlag = 1;

const arcPath = `
M ${centerXAndY} ${centerXAndY}
L ${startX} ${startY}
A ${actualRadius} ${actualRadius} 0 ${largeArcFlag} ${sweepFlag} ${endX} ${endY}
Z
`;
const startBorderLine = `M ${centerXAndY} ${centerXAndY} L ${startX} ${startY}`;
const endBorderLine = `M ${centerXAndY} ${centerXAndY} L ${endX} ${endY}`;

const arc = <Path d={arcPath} fill={color}/>;
const borders = (
<Path
d={`${startBorderLine} ${endBorderLine}`}
fill="none"
stroke={dividerColor}
strokeWidth={dividerWidth / 2}
strokeLinejoin="round"
/>
);
const totalSize = radius * 2 + padding;

return (
<View style={styles.container}>
<Svg width={totalSize} height={totalSize} viewBox={`0 0 ${totalSize} ${totalSize}`} style={styles.svg}>
{arc}
{borders}
</Svg>
</View>
);
};

export default PieSegment;

const styles = StyleSheet.create({
container: {
position: 'absolute'
},
svg: {
position: 'absolute'
}
});
48 changes: 48 additions & 0 deletions src/components/pieChart/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React from 'react';
import View from '../view';
import PieSegment, {PieSegmentProps} from './PieSegment';
import {SvgPackage} from '../../optionalDependencies';
const {Svg, Path} = SvgPackage;

export type PieChartSegmentProps = Pick<PieSegmentProps, 'percentage' | 'color'>;

export type PieChartProps = {
/**
* Pie chart segments array
*/
segments: PieChartSegmentProps[];
/**
* Pie chart diameter
*/
diameter?: number;
} & Pick<PieSegmentProps, 'dividerWidth' | 'dividerColor'>;

const DEFAULT_DIAMETER = 144;

const PieChart = (props: PieChartProps) => {
const {segments, diameter = DEFAULT_DIAMETER, ...others} = props;

if (!Svg || !Path) {
console.error(`RNUILib PieChart requires installing "@react-native-svg" dependency`);
return null;
}

const renderPieSegments = () => {
let currentStartAngle = 0;

return segments.map((segment, index) => {
const startAngle = currentStartAngle;
currentStartAngle += (segment.percentage / 100) * 360;
return (
<PieSegment key={index} {...segment} {...others} startAngle={startAngle} radius={diameter / 2}/>
);
});
};
return (
<View width={diameter} height={diameter}>
{renderPieSegments()}
</View>
);
};

export default PieChart;
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export {
PickerItemsListProps,
PickerMethods
} from './components/picker';
export {default as PieChart, PieChartSegmentProps} from './components/pieChart';
export {default as ProgressBar, ProgressBarProps} from './components/progressBar';
export {default as ProgressiveImage, ProgressiveImageProps} from './components/progressiveImage';
export {default as RadioButton, RadioButtonProps} from './components/radioButton';
Expand Down