File size: 2,089 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
51
52
53
54
55
56
57
58
59
import * as Plot from "@observablehq/plot";
import { Card } from "react-bootstrap";
import React, {useRef, useEffect} from "react";

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

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

            const data = tasks.reduce((acc, task) => {
                // Tìm object có cùng assignee và status trong accumulator
                const existing = acc.find(
                    item => item.assignee === task.assignee && item.status === task.status
                );
            
                if (existing) {
                    // Nếu đã tồn tại, cộng thêm vào count
                    existing.count += 1;
                } else {
                    // Nếu chưa tồn tại, thêm một object mới vào accumulator
                    acc.push({ assignee: task.assignee, status: task.status, count: 1 });
                }
            
                return acc;
            }, []).sort((a, b) => a.status.length - b.status.length);

            const chart = Plot.plot({
                marks: [
                    Plot.barY(data, {
                        x: "assignee",
                        y: "count",
                        fill: "status",
                        sort: { x: "y", reverse: true },
                        stack: true,
                    })
                ],
                height: 400,
                width: 600,
                color: { legend: true },
            });
            chartRef.current.append(chart);
        }
    }, [tasks]);

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

export default StackedBarChartComponent;