|
a |
|
b/client/src/components/Graphs.jsx |
|
|
1 |
import { useContext, useEffect, useState } from "react"; |
|
|
2 |
import SelectedContext from "../Contexts"; |
|
|
3 |
import { getTestCase, getTestResult } from "../API"; |
|
|
4 |
import PieChart, { COLORS } from "./PieChart"; |
|
|
5 |
import LineChart from "./LineChart"; |
|
|
6 |
|
|
|
7 |
function Graphs() { |
|
|
8 |
const {selected} = useContext(SelectedContext); |
|
|
9 |
|
|
|
10 |
const [BVP, setBVP] = useState(null); |
|
|
11 |
const [EDA, setEDA] = useState(null); |
|
|
12 |
const [res, setRes] = useState(null); |
|
|
13 |
|
|
|
14 |
const labels = ["Amused", "Neutral", "Stressed"]; |
|
|
15 |
|
|
|
16 |
useEffect(() => { |
|
|
17 |
if (selected) { |
|
|
18 |
async function getTestData(num) { |
|
|
19 |
let temp = await getTestCase(num); |
|
|
20 |
setBVP(temp.BVP); |
|
|
21 |
setEDA(temp.EDA); |
|
|
22 |
temp = await getTestResult(num); |
|
|
23 |
temp = JSON.parse(temp); |
|
|
24 |
let x = [] |
|
|
25 |
for (let i = 0; i < 3; i++) { |
|
|
26 |
let t = { |
|
|
27 |
name: labels[i], |
|
|
28 |
prob: temp[i] |
|
|
29 |
}; |
|
|
30 |
x.push(t); |
|
|
31 |
} |
|
|
32 |
setRes(x); |
|
|
33 |
} |
|
|
34 |
getTestData(selected); |
|
|
35 |
} |
|
|
36 |
}, [selected]) |
|
|
37 |
|
|
|
38 |
|
|
|
39 |
return ( |
|
|
40 |
<div className="rounded-lg bg-black/20 w-[90%] h-[75vh] flex items-center mt-8 mx-auto"> |
|
|
41 |
{res && ( |
|
|
42 |
<> |
|
|
43 |
<div className="flex flex-col justify-center items-center mx-16 mt-4"> |
|
|
44 |
<PieChart data={res} width={150} height={150} outerRadius={70} /> |
|
|
45 |
<div className="flex flex-col justify-center gap-2 pl-4"> |
|
|
46 |
{res.map((entry, index) => ( |
|
|
47 |
<div className="flex gap-4 items-center" key={index}> |
|
|
48 |
<div className="w-4 h-3 border-[1px] border-white" style={{backgroundColor: COLORS[index % COLORS.length]}}></div> |
|
|
49 |
{entry.name} - {entry.prob}% |
|
|
50 |
</div> |
|
|
51 |
))} |
|
|
52 |
|
|
|
53 |
</div> |
|
|
54 |
</div> |
|
|
55 |
<div> |
|
|
56 |
<LineChart data={BVP} range={[-400, 600]} color="#db29ab" yname="Beats Per Second" name="Blood Volume Pulse" /> |
|
|
57 |
<LineChart data={EDA} range={['auto', 'auto']} color="#06b20b" width={2} yname="Microsiemens" name="Electrodermal Activity" /> |
|
|
58 |
</div> |
|
|
59 |
</> |
|
|
60 |
)} |
|
|
61 |
</div> |
|
|
62 |
); |
|
|
63 |
} |
|
|
64 |
|
|
|
65 |
export default Graphs; |