Repository navigation
Add search pie chart component #81000
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
Changes from all commits
606679c
6782c20
6f1cde4
275d567
a70b5c1
b9f06cd
9f54f5f
08865f6
38e55da
0339b9e
bc08314
bb73d26
43ba339
4949699
24293ce
1075ae1
72667cf
6757883
f23b2ef
6fb44eb
e4d4266
b34da1b
020334d
f105069
575bfd1
c8899cd
1b88e83
f0e5574
5441f87
05fbc6d
9004095
6386e27
940b8b9
b378b51
6703ddc
d5a2dfd
1a5f00b
cfeab63
ea23c22
14adf52
edade67
f0eb859
7b4d824
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import React, {useState} from 'react'; | ||
| import type {LayoutChangeEvent} from 'react-native'; | ||
| import {View} from 'react-native'; | ||
| import {Gesture, GestureDetector} from 'react-native-gesture-handler'; | ||
| import Animated, {useSharedValue} from 'react-native-reanimated'; | ||
| import {scheduleOnRN} from 'react-native-worklets'; | ||
| import {Pie, PolarChart} from 'victory-native'; | ||
| import ActivityIndicator from '@components/ActivityIndicator'; | ||
| import ChartHeader from '@components/Charts/components/ChartHeader'; | ||
| import ChartTooltip from '@components/Charts/components/ChartTooltip'; | ||
| import {PIE_CHART_START_ANGLE} from '@components/Charts/constants'; | ||
| import {TOOLTIP_BAR_GAP, useChartLabelFormats, useTooltipData} from '@components/Charts/hooks'; | ||
| import type {ChartDataPoint, ChartProps, PieSlice, UnitPosition} from '@components/Charts/types'; | ||
| import {findSliceAtPosition, processDataIntoSlices} from '@components/Charts/utils'; | ||
| import Text from '@components/Text'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
|
|
||
| type PieChartProps = ChartProps & { | ||
| /** Callback when a slice is pressed */ | ||
| onSlicePress?: (dataPoint: ChartDataPoint, index: number) => void; | ||
|
|
||
| /** Symbol/unit for value labels in tooltip (e.g., '$', '€'). */ | ||
| valueUnit?: string; | ||
|
|
||
| /** Position of the unit symbol relative to the value. Defaults to 'left'. */ | ||
| valueUnitPosition?: UnitPosition; | ||
| }; | ||
|
|
||
| function PieChartContent({data, title, titleIcon, isLoading, valueUnit, valueUnitPosition, onSlicePress}: PieChartProps) { | ||
| const styles = useThemeStyles(); | ||
| const [canvasWidth, setCanvasWidth] = useState(0); | ||
| const [canvasHeight, setCanvasHeight] = useState(0); | ||
| const [activeSliceIndex, setActiveSliceIndex] = useState(-1); | ||
|
|
||
| // Shared values for hover state | ||
| const isHovering = useSharedValue(false); | ||
| const cursorX = useSharedValue(0); | ||
| const cursorY = useSharedValue(0); | ||
| const tooltipPosition = useSharedValue({x: 0, y: 0}); | ||
|
|
||
| const handleLayout = (event: LayoutChangeEvent) => { | ||
| setCanvasWidth(event.nativeEvent.layout.width); | ||
| setCanvasHeight(event.nativeEvent.layout.height); | ||
| }; | ||
|
|
||
| // Slices are sorted by absolute value (largest first) for color assignment, | ||
| // so slice indices don't match the original data array. We map back via | ||
| // originalIndex so the tooltip can display the original (possibly negative) value. | ||
| const processedSlices = processDataIntoSlices(data, PIE_CHART_START_ANGLE); | ||
| const activeOriginalDataIndex = activeSliceIndex >= 0 ? (processedSlices.at(activeSliceIndex)?.originalIndex ?? -1) : -1; | ||
|
|
||
| const {formatValue} = useChartLabelFormats({data, unit: valueUnit, unitPosition: valueUnitPosition}); | ||
| const tooltipData = useTooltipData(activeOriginalDataIndex, data, formatValue); | ||
|
|
||
| // Calculate pie geometry | ||
| const pieGeometry = {radius: Math.min(canvasWidth, canvasHeight) / 2, centerX: canvasWidth / 2, centerY: canvasHeight / 2}; | ||
|
|
||
| // Handle hover state updates | ||
| const updateActiveSlice = (x: number, y: number) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)This function should be wrapped in const updateActiveSlice = useCallback((x: number, y: number) => {
const {radius, centerX, centerY} = pieGeometry;
const sliceIndex = findSliceAtPosition(x, y, centerX, centerY, radius, 0, processedSlices);
setActiveSliceIndex(sliceIndex);
}, [pieGeometry, processedSlices]);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah a lot of stuff can be used with |
||
| const {radius, centerX, centerY} = pieGeometry; | ||
| const sliceIndex = findSliceAtPosition(x, y, centerX, centerY, radius, 0, processedSlices); | ||
| setActiveSliceIndex(sliceIndex); | ||
| }; | ||
|
|
||
| // Handle slice press callback | ||
| const handleSlicePress = (sliceIndex: number) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)This function should be wrapped in const handleSlicePress = useCallback((sliceIndex: number) => {
if (sliceIndex < 0 || sliceIndex >= processedSlices.length) {
return;
}
const slice = processedSlices.at(sliceIndex);
if (!slice || slice.isOther) {
return;
}
const originalDataPoint = data.at(slice.originalIndex);
if (originalDataPoint && onSlicePress) {
onSlicePress(originalDataPoint, slice.originalIndex);
}
}, [processedSlices, data, onSlicePress]);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| if (sliceIndex < 0 || sliceIndex >= processedSlices.length) { | ||
| return; | ||
| } | ||
| const slice = processedSlices.at(sliceIndex); | ||
| if (!slice) { | ||
| return; | ||
| } | ||
| const originalDataPoint = data.at(slice.originalIndex); | ||
| if (originalDataPoint && onSlicePress) { | ||
| onSlicePress(originalDataPoint, slice.originalIndex); | ||
| } | ||
| }; | ||
|
|
||
| // Hover gesture | ||
| const hoverGesture = () => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)This gesture handler function should be wrapped in const hoverGesture = useCallback(
() =>
Gesture.Hover()
.onBegin((e) => {
'worklet';
isHovering.set(true);
cursorX.set(e.x);
cursorY.set(e.y);
scheduleOnRN(updateActiveSlice, e.x, e.y);
})
.onUpdate((e) => {
'worklet';
cursorX.set(e.x);
cursorY.set(e.y);
scheduleOnRN(updateActiveSlice, e.x, e.y);
})
.onEnd(() => {
'worklet';
isHovering.set(false);
scheduleOnRN(setActiveSliceIndex, -1);
}),
[updateActiveSlice, isHovering, cursorX, cursorY]
);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| Gesture.Hover() | ||
| .onBegin((e) => { | ||
| 'worklet'; | ||
|
|
||
| isHovering.set(true); | ||
| cursorX.set(e.x); | ||
| cursorY.set(e.y); | ||
| tooltipPosition.set({x: e.x, y: e.y - TOOLTIP_BAR_GAP}); | ||
| scheduleOnRN(updateActiveSlice, e.x, e.y); | ||
| }) | ||
| .onUpdate((e) => { | ||
| 'worklet'; | ||
|
|
||
| cursorX.set(e.x); | ||
| cursorY.set(e.y); | ||
| tooltipPosition.set({x: e.x, y: e.y - TOOLTIP_BAR_GAP}); | ||
| scheduleOnRN(updateActiveSlice, e.x, e.y); | ||
| }) | ||
| .onEnd(() => { | ||
| 'worklet'; | ||
|
|
||
| isHovering.set(false); | ||
| scheduleOnRN(setActiveSliceIndex, -1); | ||
| }); | ||
|
|
||
| // Tap gesture for click/tap navigation | ||
| const tapGesture = () => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)This gesture handler function should be wrapped in const tapGesture = useCallback(
() =>
Gesture.Tap().onEnd((e) => {
'worklet';
const {radius, centerX, centerY} = pieGeometry;
const sliceIndex = findSliceAtPosition(e.x, e.y, centerX, centerY, radius, 0, processedSlices);
if (sliceIndex >= 0) {
scheduleOnRN(handleSlicePress, sliceIndex);
}
}),
[pieGeometry, processedSlices, handleSlicePress]
);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| Gesture.Tap().onEnd((e) => { | ||
| 'worklet'; | ||
|
|
||
| const {radius, centerX, centerY} = pieGeometry; | ||
| const sliceIndex = findSliceAtPosition(e.x, e.y, centerX, centerY, radius, 0, processedSlices); | ||
|
|
||
| if (sliceIndex >= 0) { | ||
| scheduleOnRN(handleSlicePress, sliceIndex); | ||
| } | ||
| }); | ||
|
Comment on lines
+80
to
+118
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. already called out by GH review but we should use |
||
|
|
||
| // Combined gestures - Race allows both hover and tap to work independently | ||
| const combinedGesture = Gesture.Race(hoverGesture(), tapGesture()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❌ PERF-6 (docs)This combined gesture should be wrapped in const combinedGesture = useMemo(
() => Gesture.Race(hoverGesture(), tapGesture()),
[hoverGesture, tapGesture]
);Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
|
|
||
| const renderLegendItem = (slice: PieSlice) => { | ||
| return ( | ||
| <View | ||
| key={`legend-${slice.label}`} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we include a unique ID as well? Like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's still an index, but with a permutation applied, so I don't think we should add it to the key.
borys3kk marked this conversation as resolved.
Resolved
|
||
| style={[styles.flexRow, styles.alignItemsCenter, styles.mr4, styles.mb2]} | ||
| > | ||
| <View style={[styles.pieChartLegendDot, {backgroundColor: slice.color}]} /> | ||
| <Text style={[styles.textNormal, styles.ml2]}>{slice.label}</Text> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| if (isLoading) { | ||
| return ( | ||
| <View style={[styles.pieChartContainer, styles.highlightBG, styles.justifyContentCenter, styles.alignItemsCenter]}> | ||
| <ActivityIndicator size="large" /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| if (data.length === 0) { | ||
| return null; | ||
| } | ||
|
Comment on lines
+143
to
+145
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just checking if this is expected that we return null here? It seems like when we are loading data, we show a loading spinner. Is a placeholder going to render in this case elsewhere?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this is more of a defensive condition. In practice, Search will render the |
||
|
|
||
| return ( | ||
| <View style={[styles.pieChartContainer, styles.highlightBG]}> | ||
| <ChartHeader | ||
| title={title} | ||
| titleIcon={titleIcon} | ||
| /> | ||
|
|
||
| <GestureDetector gesture={combinedGesture}> | ||
| <Animated.View | ||
| style={styles.pieChartChartContainer} | ||
| onLayout={handleLayout} | ||
| > | ||
| {processedSlices.length > 0 && ( | ||
| <PolarChart | ||
| data={processedSlices} | ||
| labelKey="label" | ||
| valueKey="value" | ||
| colorKey="color" | ||
| > | ||
| <Pie.Chart startAngle={PIE_CHART_START_ANGLE} /> | ||
| </PolarChart> | ||
| )} | ||
|
|
||
| {/* Tooltip */} | ||
| {activeSliceIndex >= 0 && !!tooltipData && ( | ||
| <ChartTooltip | ||
| label={tooltipData.label} | ||
| amount={tooltipData.amount} | ||
| percentage={tooltipData.percentage} | ||
| chartWidth={canvasWidth} | ||
| initialTooltipPosition={tooltipPosition} | ||
| /> | ||
| )} | ||
| </Animated.View> | ||
| </GestureDetector> | ||
| <View style={styles.pieChartLegendContainer}>{processedSlices.map((slice) => renderLegendItem(slice))}</View> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| export default PieChartContent; | ||
| export type {PieChartProps}; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import React from 'react'; | ||
| import type {PieChartProps} from './PieChartContent'; | ||
| import PieChartContent from './PieChartContent'; | ||
|
|
||
| function PieChart(props: PieChartProps) { | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| return <PieChartContent {...props} />; | ||
| } | ||
|
|
||
| PieChart.displayName = 'PieChart'; | ||
|
|
||
| export default PieChart; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import {WithSkiaWeb} from '@shopify/react-native-skia/lib/module/web'; | ||
| import React from 'react'; | ||
| import {View} from 'react-native'; | ||
| import ActivityIndicator from '@components/ActivityIndicator'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import type {PieChartProps} from './PieChartContent'; | ||
|
|
||
| const getPieChartContent = () => import('./PieChartContent'); | ||
|
|
||
| function PieChart(props: PieChartProps) { | ||
| const styles = useThemeStyles(); | ||
|
|
||
| return ( | ||
| <WithSkiaWeb | ||
| opts={{locateFile: (file: string) => `/${file}`}} | ||
| getComponent={getPieChartContent} | ||
| componentProps={props} | ||
| fallback={ | ||
| <View style={[styles.flex1, styles.justifyContentCenter, styles.alignItemsCenter, styles.highlightBG, styles.br4, styles.p5]}> | ||
| <ActivityIndicator size="large" /> | ||
| </View> | ||
| } | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| PieChart.displayName = 'PieChart'; | ||
|
|
||
| export default PieChart; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,6 @@ | ||
| export {useChartInteractionState} from './useChartInteractionState'; | ||
| export {useChartLabelLayout} from './useChartLabelLayout'; | ||
| export {useChartInteractions} from './useChartInteractions'; | ||
| export {useChartInteractions, TOOLTIP_BAR_GAP} from './useChartInteractions'; | ||
| export type {HitTestArgs} from './useChartInteractions'; | ||
| export type {ChartInteractionState} from './useChartInteractionState'; | ||
| export {default as useChartLabelFormats} from './useChartLabelFormats'; | ||
| export {default as useDynamicYDomain} from './useDynamicYDomain'; | ||
| export {useTooltipData} from './useTooltipData'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
memoize this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mateuuszzzzz any idea on this?