Improve shipping pie chart

This commit is contained in:
2024-12-22 15:11:59 -05:00
parent 03d56c5f51
commit 8a33e494b2

View File

@@ -504,6 +504,66 @@ const ShippingDetails = ({ data }) => {
const locations = data[0]?.shipping?.locations || {}; const locations = data[0]?.shipping?.locations || {};
const methodStats = data[0]?.shipping?.methodStats || []; const methodStats = data[0]?.shipping?.methodStats || [];
// Custom colors for the pie chart
const COLORS = [
'hsl(171.2 77.2% 53.3%)',
'hsl(200 95% 50%)',
'hsl(280 95% 60%)',
'hsl(340 95% 60%)',
'hsl(40 95% 50%)',
'hsl(120 95% 45%)',
'hsl(240 95% 60%)',
'hsl(30 95% 50%)',
];
// Custom label renderer for pie chart
const renderCustomizedLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, percent, index, name }) => {
const RADIAN = Math.PI / 180;
// Extend the radius for label placement
const radius = outerRadius * 1.2;
// Calculate position
const x = cx + radius * Math.cos(-midAngle * RADIAN);
const y = cy + radius * Math.sin(-midAngle * RADIAN);
// Only show label if percentage is greater than 3%
if (percent < 0.03) return null;
return (
<text
x={x}
y={y}
fill="currentColor"
textAnchor={x > cx ? 'start' : 'end'}
dominantBaseline="central"
className="text-xs font-medium"
>
{name} ({(percent * 100).toFixed(1)}%)
</text>
);
};
// Custom legend renderer
const renderLegend = (props) => {
const { payload } = props;
return (
<div className="flex flex-wrap gap-4 justify-center mt-4">
{payload.map((entry, index) => (
<div key={`legend-${index}`} className="flex items-center gap-2">
<div
className="w-3 h-3 rounded-full"
style={{ backgroundColor: entry.color }}
/>
<span className="text-sm">
{entry.value}: {((entry.payload.value / shippedCount) * 100).toFixed(1)}%
</span>
</div>
))}
</div>
);
};
return ( return (
<div className="space-y-8"> <div className="space-y-8">
<div className="grid grid-cols-2 gap-4"> <div className="grid grid-cols-2 gap-4">
@@ -533,15 +593,15 @@ const ShippingDetails = ({ data }) => {
dataKey="value" dataKey="value"
nameKey="name" nameKey="name"
cx="50%" cx="50%"
cy="50%" cy="45%"
outerRadius={150} outerRadius={120}
fill="hsl(171.2 77.2% 53.3%)" labelLine={false}
label={({ name, percent }) => `${name} (${(percent * 100).toFixed(1)}%)`} label={renderCustomizedLabel}
> >
{methodStats.map((entry, index) => ( {methodStats.map((entry, index) => (
<Cell <Cell
key={`cell-${index}`} key={`cell-${index}`}
fill={`hsl(171.2 77.2% ${53.3 - (index * 10)}%)`} fill={COLORS[index % COLORS.length]}
/> />
))} ))}
</Pie> </Pie>
@@ -550,6 +610,17 @@ const ShippingDetails = ({ data }) => {
`${value.toLocaleString()} orders (${((value / shippedCount) * 100).toFixed(1)}%)`, `${value.toLocaleString()} orders (${((value / shippedCount) * 100).toFixed(1)}%)`,
name name
]} ]}
contentStyle={{
backgroundColor: 'hsl(var(--background))',
border: '1px solid hsl(var(--border))',
borderRadius: '0.5rem',
padding: '0.5rem'
}}
/>
<Legend
content={renderLegend}
verticalAlign="bottom"
align="center"
/> />
</PieChart> </PieChart>
</ResponsiveContainer> </ResponsiveContainer>