feat: change PieChart to display Revenue by Product instead of quantity
This commit is contained in:
@@ -35,19 +35,24 @@ const Dashboard = () => {
|
||||
});
|
||||
}, [dateRange, ordersData]);
|
||||
|
||||
const { totalRevenue, totalOrders, averageOrderValue, salesByProduct } = useMemo(() => {
|
||||
const { totalRevenue, totalOrders, averageOrderValue, salesByProduct, revenueByProduct } = useMemo(() => {
|
||||
let revenue = 0;
|
||||
let totalItems = 0;
|
||||
const productSalesMap: Record<string, number> = {};
|
||||
const productRevenueMap: Record<string, number> = {};
|
||||
|
||||
filteredData.forEach(order => {
|
||||
revenue += (order.Quantidade * order.Valor_Unitario);
|
||||
const itemRevenue = order.Quantidade * order.Valor_Unitario;
|
||||
revenue += itemRevenue;
|
||||
totalItems += order.Quantidade;
|
||||
const productName = order.Descricao_Produto.split(' TAMANHO')[0];
|
||||
|
||||
if (productSalesMap[productName]) {
|
||||
productSalesMap[productName] += order.Quantidade;
|
||||
productRevenueMap[productName] += itemRevenue;
|
||||
} else {
|
||||
productSalesMap[productName] = order.Quantidade;
|
||||
productRevenueMap[productName] = itemRevenue;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -59,7 +64,15 @@ const Dashboard = () => {
|
||||
fill: COLORS[index % COLORS.length]
|
||||
}));
|
||||
|
||||
return { totalRevenue: revenue, totalOrders: totalItems, averageOrderValue: revenue / (filteredData.length || 1), salesByProduct: productsData };
|
||||
const revenueData = Object.keys(productRevenueMap).map(key => ({
|
||||
name: key,
|
||||
value: productRevenueMap[key]
|
||||
})).sort((a, b) => b.value - a.value).slice(0, 10).map((item, index) => ({
|
||||
...item,
|
||||
fill: COLORS[index % COLORS.length]
|
||||
}));
|
||||
|
||||
return { totalRevenue: revenue, totalOrders: totalItems, averageOrderValue: revenue / (filteredData.length || 1), salesByProduct: productsData, revenueByProduct: revenueData };
|
||||
}, [filteredData]);
|
||||
|
||||
const formatCurrency = (value: number) => {
|
||||
@@ -146,26 +159,21 @@ const Dashboard = () => {
|
||||
</div>
|
||||
|
||||
<div className="bg-dark-card p-6 rounded-2xl border border-dark-border shadow-sm flex flex-col">
|
||||
<h3 className="text-lg font-bold mb-6 text-dark-text">Distribuição de Produtos</h3>
|
||||
<h3 className="text-lg font-bold mb-6 text-dark-text">Receita por Produto</h3>
|
||||
<div className="h-80 w-full flex items-center justify-center">
|
||||
<ResponsiveContainer width="100%" height="100%">
|
||||
<PieChart>
|
||||
<Pie data={salesByProduct} cx="50%" cy="50%" innerRadius={80} outerRadius={110} paddingAngle={5} dataKey="value" stroke="none">
|
||||
{salesByProduct.map((_, index) => (
|
||||
<Pie data={revenueByProduct} cx="50%" cy="50%" innerRadius={80} outerRadius={110} paddingAngle={5} dataKey="value" stroke="none">
|
||||
{revenueByProduct.map((_, index) => (
|
||||
<Cell key={`cell-${index}`} fill={COLORS[index % COLORS.length]} />
|
||||
))}
|
||||
</Pie>
|
||||
<Tooltip
|
||||
contentStyle={{
|
||||
backgroundColor: '#141414', borderColor: 'transparent', borderRadius: '12px',
|
||||
boxShadow: '0 10px 15px -3px rgb(0 0 0 / 0.5)', border: 'none', color: '#ededed'
|
||||
}}
|
||||
/>
|
||||
<Tooltip content={<CustomTooltip isCurrency={true} />} cursor={{ fill: '#222222' }} />
|
||||
</PieChart>
|
||||
</ResponsiveContainer>
|
||||
</div>
|
||||
<div className="mt-4 grid grid-cols-2 gap-2">
|
||||
{salesByProduct.map((entry, index) => (
|
||||
{revenueByProduct.map((entry, index) => (
|
||||
<div key={entry.name} className="flex items-center text-[10px]">
|
||||
<span className="w-2.5 h-2.5 rounded-full mr-2 shrink-0" style={{ backgroundColor: COLORS[index % COLORS.length] }}></span>
|
||||
<span className="text-dark-muted truncate font-semibold">{entry.name}</span>
|
||||
|
||||
Reference in New Issue
Block a user