Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.3 kB
2
Indexable
Never
import { PieChart } from '@mui/x-charts/PieChart';
import { useEffect, useState } from 'react';

const Chart = () => {

    const [myDonation, setMyDonation] = useState(0);

    useEffect(() => {
        const storedItem = JSON.parse(localStorage.getItem('donate'));
        if (storedItem !== null) {
            setMyDonation(storedItem.length);
        }
    }, [])

    const pieChartData = [
        { label: 'My Donation', value: myDonation, color: '#00C49F' },
        { label: 'Total Donation', value: 12 - myDonation, color: 'red' },
    ];

    const sizing = {
        margin: { right: 10 },
        width: 500,
        height: 500
    };

    return (
        <PieChart
            series={[
                {
                    outerRadius: 160,
                    pieChartData,
                    arcLabel: (p) => `${((p.value / 12) * 100).toFixed(1)}%`,
                },
            ]}
            legend={{
                direction: "col",
                position: {
                  vertical: "middle",
                  horizontal: "middle"
                }
              }}
              sx={{
                "--ChartsLegend-rootOffsetX": "0px",
                "--ChartsLegend-rootOffsetY": "200px",
              }}
            {...sizing}
        />
    )
};

export default Chart;