Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 3x 3x 2x 1x 3x | import type { ReactNode } from "react";
import { useMemo } from "react";
import Svg, { Circle, G } from "react-native-svg";
import { Dimensions, StyleProp, Text, TextStyle, View, ViewStyle } from "react-native";
import { makeStyles } from "@repo/ui/themes/makeStyles";
import { useTheme } from "@repo/ui/themes/ThemeContext";
import { textStyles } from "@repo/ui/themes/typography";
import { createSegments, createTailOverlays, formatDisplayTotal } from "./utils";
export interface DonutChartItem {
category: string;
count: number;
color: string;
}
export type DonutChartSize = "sm" | "md" | "lg";
const MD_SIZE_BY_SCREEN_WIDTH = 0.55;
const DONUT_CHART_PRESETS: Record<
DonutChartSize,
{ chartSize: number; strokeWidth: number; segmentGap: number }
> = {
sm: {
chartSize: 188,
strokeWidth: 22,
segmentGap: 0,
},
md: {
chartSize: 236,
strokeWidth: 26,
segmentGap: 0,
},
lg: {
chartSize: 280,
strokeWidth: 30,
segmentGap: 0,
},
};
export interface DonutChartProps {
items: DonutChartItem[];
size?: DonutChartSize;
chartSize?: number;
strokeWidth?: number;
segmentGap?: number;
trackColor?: string;
centerLabel?: string;
centerLabelStyle?: TextStyle;
centerValueStyle?: TextStyle;
formatCenterValue?: (total: number) => string;
centerContent?: ReactNode;
style?: StyleProp<ViewStyle>;
testID?: string;
accessibilityLabel?: string;
}
export const DonutChart = ({
items,
size = "md",
chartSize: chartSizeOverride,
strokeWidth,
segmentGap,
trackColor,
centerLabel,
centerLabelStyle,
centerValueStyle,
formatCenterValue,
centerContent,
style,
testID,
accessibilityLabel,
}: DonutChartProps) => {
const { theme } = useTheme();
const styles = useStyles();
const preset = DONUT_CHART_PRESETS[size];
const mdChartSize = Math.floor(Dimensions.get("window").width * MD_SIZE_BY_SCREEN_WIDTH);
const defaultChartSize = size === "md" ? mdChartSize : preset.chartSize;
const chartSize = chartSizeOverride ?? defaultChartSize;
const resolvedStrokeWidth = strokeWidth ?? preset.strokeWidth;
const resolvedSegmentGap = segmentGap ?? preset.segmentGap;
const radius = (chartSize - resolvedStrokeWidth) / 2;
const circumference = 2 * Math.PI * radius;
const resolvedTrackColor =
(trackColor ?? theme.isNewTheme)
? theme.colors.background.surface
: theme.colors.background.primary;
const centerTextMaxWidth = Math.max(chartSize - resolvedStrokeWidth * 2, 0);
const activeItems = useMemo(
() => items.filter((item) => Number.isFinite(item.count) && item.count > 0),
[items],
);
const totalCount = useMemo(
() => activeItems.reduce((sum, item) => sum + item.count, 0),
[activeItems],
);
const displayTotal = useMemo(
() => formatDisplayTotal(totalCount, formatCenterValue),
[formatCenterValue, totalCount],
);
const segments = useMemo(
() =>
createSegments({
activeItems,
totalCount,
circumference,
segmentGap: resolvedSegmentGap,
}),
[activeItems, circumference, resolvedSegmentGap, totalCount],
);
const tailOverlays = useMemo(
() => createTailOverlays({ segments, strokeWidth: resolvedStrokeWidth }),
[resolvedStrokeWidth, segments],
);
let centerNode: ReactNode = null;
Iif (centerContent) {
centerNode = <View style={styles.centerContent}>{centerContent}</View>;
} else if (centerLabel) {
centerNode = (
<View style={[styles.chartCenter, { width: centerTextMaxWidth }]}>
<Text style={[styles.centerLabel, centerLabelStyle]} numberOfLines={1} ellipsizeMode="tail">
{centerLabel}
</Text>
<Text style={[styles.centerValue, centerValueStyle]}>{displayTotal}</Text>
</View>
);
}
const resolvedAccessibilityLabel =
accessibilityLabel ??
(centerLabel ? `${centerLabel}: ${displayTotal}` : `Chart with ${totalCount} total`);
return (
<View
style={[styles.chartWrap, { width: chartSize, height: chartSize }, style]}
accessible
accessibilityRole="image"
accessibilityLabel={resolvedAccessibilityLabel}
>
<Svg width={chartSize} height={chartSize} testID={testID}>
<Circle
cx={chartSize / 2}
cy={chartSize / 2}
r={radius}
fill="transparent"
stroke={resolvedTrackColor}
strokeWidth={resolvedStrokeWidth}
/>
<G rotation={0} originX={chartSize / 2} originY={chartSize / 2}>
{segments.map((segment) => (
<Circle
key={segment.key}
cx={chartSize / 2}
cy={chartSize / 2}
r={radius}
fill="transparent"
stroke={segment.color}
strokeWidth={resolvedStrokeWidth}
strokeLinecap="round"
strokeDasharray={[segment.length, circumference]}
strokeDashoffset={segment.offset}
/>
))}
{tailOverlays.map((overlay) => (
<Circle
key={overlay.key}
cx={chartSize / 2}
cy={chartSize / 2}
r={radius}
fill="transparent"
stroke={overlay.color}
strokeWidth={resolvedStrokeWidth}
strokeLinecap="round"
strokeDasharray={[overlay.length, circumference]}
strokeDashoffset={overlay.offset}
/>
))}
</G>
</Svg>
{centerNode}
</View>
);
};
const useStyles = makeStyles((theme) => ({
chartWrap: {
alignItems: "center",
justifyContent: "center",
},
centerContent: {
position: "absolute",
alignItems: "center",
justifyContent: "center",
},
chartCenter: {
position: "absolute",
alignItems: "center",
justifyContent: "center",
},
centerLabel: {
...textStyles.content.regular,
color: theme.isNewTheme ? theme.colors.text.normal : theme.colors.text.muted,
fontSize: theme.metrics.textSize[20],
textAlign: "center",
width: "100%",
},
centerValue: {
...textStyles.content.regular,
color: theme.isNewTheme ? theme.colors.text.normal : theme.colors.text.muted,
fontSize: theme.metrics.textSize[48],
lineHeight: theme.metrics.spacing[15],
},
}));
|