import React, { useEffect, useState } from 'react';
import { __ } from '@wordpress/i18n';
import { Tooltip, ResponsiveContainer, Cell, Pie, PieChart, Legend } from 'recharts';
import { isEmpty } from 'ramda';
// tools
import getBandoLabel from '../../helpers/getBandoLabel';
const ChartDomandePerStato = ({ title, data = [] }) => {
const COLORS = ['#0088FE', '#00C49F', '#FFBB28', '#FF8042', '#8884d8', '#82ca9d'];
const [chartData, setChartData] = useState({});
const CustomTooltip = ({ active, payload }) => {
if (active && payload && payload.length) {
return (
{getBandoLabel(payload[0].name)}
{payload[0].name}: {payload[0].value}
);
}
return null;
};
useEffect(() => {
const grouped = data.reduce((acc, cur) => {
if (cur.status === 'APPROVED') {
acc.approved.value = cur.numberOfApplication;
} else if (cur.status === 'REJECTED') {
acc.rejected.value = cur.numberOfApplication;
} else {
acc.inProgress.value += cur.numberOfApplication;
}
return acc;
}, {
inProgress: {value: 0, label: __('In corso', 'gepafin')},
approved: {value: 0, label: __('Approvato', 'gepafin')},
rejected: {value: 0, label: __('Respinto', 'gepafin')}
});
setChartData(grouped)
}, [data]);
return (
{title ?
{title} : null}
{chartData && !isEmpty(chartData)
?
`${(percent * 100).toFixed(0)}%`}
outerRadius={120}
fill="#8884d8"
dataKey="value"
nameKey="label"
>
{Object.values(chartData).map((entry, index) => (
|
))}
} />
: null}
)
}
export default ChartDomandePerStato;