forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPieChart.tsx
More file actions
138 lines (129 loc) · 3.93 KB
/
Copy pathPieChart.tsx
File metadata and controls
138 lines (129 loc) · 3.93 KB
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
import React from "react";
import { StyleSheet, Text, View } from "react-native";
const CHART_COLORS = [
"#BEC2FF",
"#85ECCE",
"#FFAC4D",
"#FFF388",
"#189370",
"#EEE6FE",
"#FA5F67",
];
interface PieChartData {
label: string;
value: number;
}
interface PieChartProps {
title: string;
description: string;
data: PieChartData[];
theme: { card: string; text: string; muted: string };
}
export function PieChart({ title, description, data, theme }: PieChartProps) {
if (!data || data.length === 0) {
return (
<View style={[chartStyles.card, { backgroundColor: theme.card }]}>
<Text style={[chartStyles.title, { color: theme.text }]}>{title}</Text>
<Text style={[chartStyles.description, { color: theme.muted }]}>
{description}
</Text>
<Text style={[chartStyles.noData, { color: theme.muted }]}>
No data available
</Text>
</View>
);
}
const total = data.reduce((sum, d) => sum + (Number(d.value) || 0), 0);
return (
<View style={[chartStyles.card, { backgroundColor: theme.card }]}>
<Text style={[chartStyles.title, { color: theme.text }]}>{title}</Text>
<Text style={[chartStyles.description, { color: theme.muted }]}>
{description}
</Text>
<View style={chartStyles.barContainer}>
{data.map((item, i) => {
const pct = total > 0 ? (item.value / total) * 100 : 0;
if (pct <= 0) return null;
return (
<View
key={i}
style={[
chartStyles.barSegment,
{
flex: pct,
backgroundColor: CHART_COLORS[i % CHART_COLORS.length],
},
i === 0 && chartStyles.barFirst,
i === data.length - 1 && chartStyles.barLast,
]}
/>
);
})}
</View>
<View style={chartStyles.legend}>
{data.map((item, i) => {
const val = Number(item.value) || 0;
const pct = total > 0 ? ((val / total) * 100).toFixed(0) : "0";
return (
<View key={i} style={chartStyles.legendRow}>
<View
style={[
chartStyles.legendDot,
{ backgroundColor: CHART_COLORS[i % CHART_COLORS.length] },
]}
/>
<Text
style={[chartStyles.legendLabel, { color: theme.text }]}
numberOfLines={1}
>
{item.label}
</Text>
<Text style={[chartStyles.legendValue, { color: theme.muted }]}>
{val.toLocaleString()}
</Text>
<Text style={[chartStyles.legendPct, { color: theme.muted }]}>
{pct}%
</Text>
</View>
);
})}
</View>
</View>
);
}
const chartStyles = StyleSheet.create({
card: {
borderRadius: 16,
padding: 16,
marginBottom: 8,
shadowColor: "#000",
shadowOffset: { width: 0, height: 1 },
shadowOpacity: 0.1,
shadowRadius: 2,
elevation: 1,
},
title: { fontSize: 17, fontWeight: "700" },
description: { fontSize: 13, marginTop: 2, marginBottom: 16 },
noData: { fontSize: 14, textAlign: "center", paddingVertical: 24 },
barContainer: {
flexDirection: "row",
height: 28,
borderRadius: 14,
overflow: "hidden",
marginBottom: 16,
},
barSegment: { height: "100%" },
barFirst: { borderTopLeftRadius: 14, borderBottomLeftRadius: 14 },
barLast: { borderTopRightRadius: 14, borderBottomRightRadius: 14 },
legend: { gap: 10 },
legendRow: { flexDirection: "row", alignItems: "center", gap: 8 },
legendDot: { width: 12, height: 12, borderRadius: 6 },
legendLabel: { flex: 1, fontSize: 14 },
legendValue: { fontSize: 14, fontVariant: ["tabular-nums"] },
legendPct: {
width: 40,
fontSize: 13,
textAlign: "right",
fontVariant: ["tabular-nums"],
},
});