|
| 1 | +import React, { useEffect, useRef } from "react"; |
| 2 | +import { |
| 3 | + Chart as ChartJS, |
| 4 | + BarController, |
| 5 | + BarElement, |
| 6 | + CategoryScale, |
| 7 | + LinearScale, |
| 8 | + Tooltip, |
| 9 | + Legend, |
| 10 | +} from "chart.js"; |
| 11 | + |
| 12 | +ChartJS.register( |
| 13 | + BarController, |
| 14 | + BarElement, |
| 15 | + CategoryScale, |
| 16 | + LinearScale, |
| 17 | + Tooltip, |
| 18 | + Legend, |
| 19 | +); |
| 20 | + |
| 21 | +export default function AccuracyChart() { |
| 22 | + const canvasRef = useRef(null); |
| 23 | + const chartInstanceRef = useRef(null); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + const ctx = canvasRef.current.getContext("2d"); |
| 27 | + |
| 28 | + if (chartInstanceRef.current) { |
| 29 | + chartInstanceRef.current.destroy(); |
| 30 | + } |
| 31 | + |
| 32 | + const data = { |
| 33 | + labels: ["Microsoft", "Face++", "IBM", "Amazon", "Kairos"], |
| 34 | + datasets: [ |
| 35 | + { |
| 36 | + label: "Darker female", |
| 37 | + backgroundColor: "#e58a84", |
| 38 | + data: [79.2, 65.5, 65.3, 68.63, 77.5], |
| 39 | + }, |
| 40 | + { |
| 41 | + label: "Darker male", |
| 42 | + backgroundColor: "#b39ce4", |
| 43 | + data: [94, 99.3, 88, 98.74, 98.7], |
| 44 | + }, |
| 45 | + { |
| 46 | + label: "Lighter female", |
| 47 | + backgroundColor: "#8cb492", |
| 48 | + data: [98.3, 90.2, 92.9, 92.88, 93.6], |
| 49 | + }, |
| 50 | + { |
| 51 | + label: "Lighter male", |
| 52 | + backgroundColor: "#f7c795", |
| 53 | + data: [100, 99.2, 99.7, 100, 100], |
| 54 | + }, |
| 55 | + ], |
| 56 | + }; |
| 57 | + |
| 58 | + const chart = new ChartJS(ctx, { |
| 59 | + type: "bar", |
| 60 | + data, |
| 61 | + options: { |
| 62 | + responsive: true, |
| 63 | + plugins: { |
| 64 | + legend: { |
| 65 | + position: "bottom", |
| 66 | + }, |
| 67 | + tooltip: { |
| 68 | + callbacks: { |
| 69 | + label: (context) => |
| 70 | + `${context.dataset.label}: ${context.parsed.y}%`, |
| 71 | + }, |
| 72 | + }, |
| 73 | + title: { |
| 74 | + display: true, |
| 75 | + text: "Accuracy of Face Recognition Technologies", |
| 76 | + }, |
| 77 | + }, |
| 78 | + scales: { |
| 79 | + y: { |
| 80 | + beginAtZero: true, |
| 81 | + title: { |
| 82 | + display: true, |
| 83 | + text: "Accuracy (%)", |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + chartInstanceRef.current = chart; |
| 91 | + |
| 92 | + return () => { |
| 93 | + if (chartInstanceRef.current) { |
| 94 | + chartInstanceRef.current.destroy(); |
| 95 | + } |
| 96 | + }; |
| 97 | + }, []); |
| 98 | + |
| 99 | + return ( |
| 100 | + <div style={{ width: "100%", maxWidth: "800px", margin: "0 auto" }}> |
| 101 | + <canvas ref={canvasRef} /> |
| 102 | + </div> |
| 103 | + ); |
| 104 | +} |
0 commit comments