File size: 1,593 Bytes
0d37b12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as Plot from "@observablehq/plot";
import { Card } from "react-bootstrap";
import React, {useRef, useEffect} from "react";


const PieChartComponent = ({ tasks }) => {
    const chartRef = useRef();

    useEffect(() => {
        if (chartRef.current) {
            while (chartRef.current.firstChild) {
                chartRef.current.removeChild(chartRef.current.firstChild);
            }

            const priorityCounts = tasks.reduce((acc, task) => {
                acc[task.priority] = (acc[task.priority] || 0) + 1;
                return acc;
            }, {});

            const data = Object.entries(priorityCounts).map(([priority, count]) => ({ priority, count }));
            
            const chart = Plot.plot({
                marks: [
                    Plot.barY(data, {
                        x: "priority",
                        y: "count",
                        fill: "priority",
                        stroke: "white",
                        title: d => `${d.priority}: ${d.count}`,
                        r: "count"
                    })
                ],
                height: 500,
                width: 600,
            });
            chartRef.current.append(chart);
        }
    }, [tasks]);

    return (
        <Card className="card-report card-nospan">
            <Card.Body >
                <Card.Title>Task Priority Distribution</Card.Title>
                <div ref={chartRef} className="d-flex justify-content-center align-items-center" />
            </Card.Body>
        </Card>
    );
};

export default PieChartComponent;