|
| 1 | +import React, {useState} from 'react'; |
| 2 | +import type {LayoutChangeEvent} from 'react-native'; |
| 3 | +import {View} from 'react-native'; |
| 4 | +import {Gesture, GestureDetector} from 'react-native-gesture-handler'; |
| 5 | +import Animated, {useSharedValue} from 'react-native-reanimated'; |
| 6 | +import {scheduleOnRN} from 'react-native-worklets'; |
| 7 | +import {Pie, PolarChart} from 'victory-native'; |
| 8 | +import ActivityIndicator from '@components/ActivityIndicator'; |
| 9 | +import ChartHeader from '@components/Charts/components/ChartHeader'; |
| 10 | +import ChartTooltip from '@components/Charts/components/ChartTooltip'; |
| 11 | +import {PIE_CHART_START_ANGLE} from '@components/Charts/constants'; |
| 12 | +import {TOOLTIP_BAR_GAP, useChartLabelFormats, useTooltipData} from '@components/Charts/hooks'; |
| 13 | +import type {ChartDataPoint, ChartProps, PieSlice, UnitPosition} from '@components/Charts/types'; |
| 14 | +import {findSliceAtPosition, processDataIntoSlices} from '@components/Charts/utils'; |
| 15 | +import Text from '@components/Text'; |
| 16 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 17 | + |
| 18 | +type PieChartProps = ChartProps & { |
| 19 | + /** Callback when a slice is pressed */ |
| 20 | + onSlicePress?: (dataPoint: ChartDataPoint, index: number) => void; |
| 21 | + |
| 22 | + /** Symbol/unit for value labels in tooltip (e.g., '$', '€'). */ |
| 23 | + valueUnit?: string; |
| 24 | + |
| 25 | + /** Position of the unit symbol relative to the value. Defaults to 'left'. */ |
| 26 | + valueUnitPosition?: UnitPosition; |
| 27 | +}; |
| 28 | + |
| 29 | +function PieChartContent({data, title, titleIcon, isLoading, valueUnit, valueUnitPosition, onSlicePress}: PieChartProps) { |
| 30 | + const styles = useThemeStyles(); |
| 31 | + const [canvasWidth, setCanvasWidth] = useState(0); |
| 32 | + const [canvasHeight, setCanvasHeight] = useState(0); |
| 33 | + const [activeSliceIndex, setActiveSliceIndex] = useState(-1); |
| 34 | + |
| 35 | + // Shared values for hover state |
| 36 | + const isHovering = useSharedValue(false); |
| 37 | + const cursorX = useSharedValue(0); |
| 38 | + const cursorY = useSharedValue(0); |
| 39 | + const tooltipPosition = useSharedValue({x: 0, y: 0}); |
| 40 | + |
| 41 | + const handleLayout = (event: LayoutChangeEvent) => { |
| 42 | + setCanvasWidth(event.nativeEvent.layout.width); |
| 43 | + setCanvasHeight(event.nativeEvent.layout.height); |
| 44 | + }; |
| 45 | + |
| 46 | + // Slices are sorted by absolute value (largest first) for color assignment, |
| 47 | + // so slice indices don't match the original data array. We map back via |
| 48 | + // originalIndex so the tooltip can display the original (possibly negative) value. |
| 49 | + const processedSlices = processDataIntoSlices(data, PIE_CHART_START_ANGLE); |
| 50 | + const activeOriginalDataIndex = activeSliceIndex >= 0 ? (processedSlices.at(activeSliceIndex)?.originalIndex ?? -1) : -1; |
| 51 | + |
| 52 | + const {formatValue} = useChartLabelFormats({data, unit: valueUnit, unitPosition: valueUnitPosition}); |
| 53 | + const tooltipData = useTooltipData(activeOriginalDataIndex, data, formatValue); |
| 54 | + |
| 55 | + // Calculate pie geometry |
| 56 | + const pieGeometry = {radius: Math.min(canvasWidth, canvasHeight) / 2, centerX: canvasWidth / 2, centerY: canvasHeight / 2}; |
| 57 | + |
| 58 | + // Handle hover state updates |
| 59 | + const updateActiveSlice = (x: number, y: number) => { |
| 60 | + const {radius, centerX, centerY} = pieGeometry; |
| 61 | + const sliceIndex = findSliceAtPosition(x, y, centerX, centerY, radius, 0, processedSlices); |
| 62 | + setActiveSliceIndex(sliceIndex); |
| 63 | + }; |
| 64 | + |
| 65 | + // Handle slice press callback |
| 66 | + const handleSlicePress = (sliceIndex: number) => { |
| 67 | + if (sliceIndex < 0 || sliceIndex >= processedSlices.length) { |
| 68 | + return; |
| 69 | + } |
| 70 | + const slice = processedSlices.at(sliceIndex); |
| 71 | + if (!slice) { |
| 72 | + return; |
| 73 | + } |
| 74 | + const originalDataPoint = data.at(slice.originalIndex); |
| 75 | + if (originalDataPoint && onSlicePress) { |
| 76 | + onSlicePress(originalDataPoint, slice.originalIndex); |
| 77 | + } |
| 78 | + }; |
| 79 | + |
| 80 | + // Hover gesture |
| 81 | + const hoverGesture = () => |
| 82 | + Gesture.Hover() |
| 83 | + .onBegin((e) => { |
| 84 | + 'worklet'; |
| 85 | + |
| 86 | + isHovering.set(true); |
| 87 | + cursorX.set(e.x); |
| 88 | + cursorY.set(e.y); |
| 89 | + tooltipPosition.set({x: e.x, y: e.y - TOOLTIP_BAR_GAP}); |
| 90 | + scheduleOnRN(updateActiveSlice, e.x, e.y); |
| 91 | + }) |
| 92 | + .onUpdate((e) => { |
| 93 | + 'worklet'; |
| 94 | + |
| 95 | + cursorX.set(e.x); |
| 96 | + cursorY.set(e.y); |
| 97 | + tooltipPosition.set({x: e.x, y: e.y - TOOLTIP_BAR_GAP}); |
| 98 | + scheduleOnRN(updateActiveSlice, e.x, e.y); |
| 99 | + }) |
| 100 | + .onEnd(() => { |
| 101 | + 'worklet'; |
| 102 | + |
| 103 | + isHovering.set(false); |
| 104 | + scheduleOnRN(setActiveSliceIndex, -1); |
| 105 | + }); |
| 106 | + |
| 107 | + // Tap gesture for click/tap navigation |
| 108 | + const tapGesture = () => |
| 109 | + Gesture.Tap().onEnd((e) => { |
| 110 | + 'worklet'; |
| 111 | + |
| 112 | + const {radius, centerX, centerY} = pieGeometry; |
| 113 | + const sliceIndex = findSliceAtPosition(e.x, e.y, centerX, centerY, radius, 0, processedSlices); |
| 114 | + |
| 115 | + if (sliceIndex >= 0) { |
| 116 | + scheduleOnRN(handleSlicePress, sliceIndex); |
| 117 | + } |
| 118 | + }); |
| 119 | + |
| 120 | + // Combined gestures - Race allows both hover and tap to work independently |
| 121 | + const combinedGesture = Gesture.Race(hoverGesture(), tapGesture()); |
| 122 | + |
| 123 | + const renderLegendItem = (slice: PieSlice) => { |
| 124 | + return ( |
| 125 | + <View |
| 126 | + key={`legend-${slice.label}`} |
| 127 | + style={[styles.flexRow, styles.alignItemsCenter, styles.mr4, styles.mb2]} |
| 128 | + > |
| 129 | + <View style={[styles.pieChartLegendDot, {backgroundColor: slice.color}]} /> |
| 130 | + <Text style={[styles.textNormal, styles.ml2]}>{slice.label}</Text> |
| 131 | + </View> |
| 132 | + ); |
| 133 | + }; |
| 134 | + |
| 135 | + if (isLoading) { |
| 136 | + return ( |
| 137 | + <View style={[styles.pieChartContainer, styles.highlightBG, styles.justifyContentCenter, styles.alignItemsCenter]}> |
| 138 | + <ActivityIndicator size="large" /> |
| 139 | + </View> |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + if (data.length === 0) { |
| 144 | + return null; |
| 145 | + } |
| 146 | + |
| 147 | + return ( |
| 148 | + <View style={[styles.pieChartContainer, styles.highlightBG]}> |
| 149 | + <ChartHeader |
| 150 | + title={title} |
| 151 | + titleIcon={titleIcon} |
| 152 | + /> |
| 153 | + |
| 154 | + <GestureDetector gesture={combinedGesture}> |
| 155 | + <Animated.View |
| 156 | + style={styles.pieChartChartContainer} |
| 157 | + onLayout={handleLayout} |
| 158 | + > |
| 159 | + {processedSlices.length > 0 && ( |
| 160 | + <PolarChart |
| 161 | + data={processedSlices} |
| 162 | + labelKey="label" |
| 163 | + valueKey="value" |
| 164 | + colorKey="color" |
| 165 | + > |
| 166 | + <Pie.Chart startAngle={PIE_CHART_START_ANGLE} /> |
| 167 | + </PolarChart> |
| 168 | + )} |
| 169 | + |
| 170 | + {/* Tooltip */} |
| 171 | + {activeSliceIndex >= 0 && !!tooltipData && ( |
| 172 | + <ChartTooltip |
| 173 | + label={tooltipData.label} |
| 174 | + amount={tooltipData.amount} |
| 175 | + percentage={tooltipData.percentage} |
| 176 | + chartWidth={canvasWidth} |
| 177 | + initialTooltipPosition={tooltipPosition} |
| 178 | + /> |
| 179 | + )} |
| 180 | + </Animated.View> |
| 181 | + </GestureDetector> |
| 182 | + <View style={styles.pieChartLegendContainer}>{processedSlices.map((slice) => renderLegendItem(slice))}</View> |
| 183 | + </View> |
| 184 | + ); |
| 185 | +} |
| 186 | + |
| 187 | +export default PieChartContent; |
| 188 | +export type {PieChartProps}; |
0 commit comments