Skip to content

Commit 70e1248

Browse files
committed
Refactored chart.js from bar to line graph to display change in memory over time
1 parent 92592d9 commit 70e1248

1 file changed

Lines changed: 50 additions & 33 deletions

File tree

app/charts/memory-chart.jsx

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,76 @@
11
import React, { useContext } from 'react';
2-
import { Bar } from 'react-chartjs-2';
2+
import { Line } from 'react-chartjs-2';
33
import HealthContext from '../context/DetailsContext';
44

55
const MemoryChart = () => {
66
const healthData = useContext(HealthContext);
77
// const health = healthData.detailData;
88

99
const createChart = () => {
10-
const memoryObj = {
11-
free: 0,
12-
active: 0,
13-
used: 0,
14-
total: 0,
15-
};
10+
let free = [];
11+
let used= [];
12+
let active = [];
13+
let total = [];
1614

1715
for (let i = 0; i < healthData.length; i += 1) {
18-
console.log(healthData);
1916
// If Mongo
20-
if (healthData[i].freeMemory) {
21-
memoryObj.free += healthData[i].freeMemory;
22-
memoryObj.active += healthData[i].activeMemory;
23-
memoryObj.used += healthData[i].usedMemory;
24-
memoryObj.total += healthData[i].totalMemory;
25-
} else if (healthData[i].freememory) {
26-
memoryObj.free += healthData[i].freememory;
27-
memoryObj.active += healthData[i].activememory;
28-
memoryObj.used += healthData[i].usedmemory;
29-
memoryObj.total += healthData[i].totalmemory;
17+
if (healthData[i].freeMemory && healthData[i].activeMemory && healthData[i].usedMemory && healthData[i].totalMemory) {
18+
free.push(healthData[i].freeMemory);
19+
active.push(healthData[i].activeMemory);
20+
used.push(healthData[i].usedMemory);
21+
total.push(healthData[i].totalMemory);
22+
} else {
23+
free.push(healthData[i].freememory);
24+
active.push(healthData[i].activememory);
25+
used.push(healthData[i].usedmemory);
26+
total.push(healthData[i].totalmemory);
3027
}
3128
}
3229

33-
memoryObj.free /= 1000000000 * healthData.length;
34-
memoryObj.active /= 1000000000 * healthData.length;
35-
memoryObj.used /= 1000000000 * healthData.length;
36-
memoryObj.total /= 1000000000 * healthData.length;
30+
31+
32+
const memoryObj = {
33+
freeMem: free,
34+
usedMem: used,
35+
activeMem: active,
36+
totalMem: total
37+
}
3738

3839
const chartData = {
3940
datasets: [
4041
{
41-
label: 'Breakdown of Memory in Gigabytes',
42-
data: Object.values(memoryObj),
42+
label: "Free Memory",
43+
data: Object.values(memoryObj.freeMem),
44+
backgroundColor: [
45+
"rgb(2, 210, 249)"
46+
]
47+
},
48+
{
49+
label: "Used Memory",
50+
data: Object.values(memoryObj.usedMem),
51+
backgroundColor: [
52+
"rgb(198, 42, 177)",
53+
]
54+
},
55+
{
56+
label: "Active Memory",
57+
data: Object.values(memoryObj.activeMem),
58+
backgroundColor: [
59+
"rgb(252, 170, 52)"
60+
]
61+
},
62+
{
63+
label: "Total Memory",
64+
data: Object.values(memoryObj.activeMem),
4365
backgroundColor: [
44-
'rgb(2, 210, 249)',
45-
'rgb(198, 42, 177)',
46-
'rgb(252, 170, 52)',
47-
'rgb(239, 91, 145)',
48-
'rgb(182, 219, 26)',
49-
'rgb(254, 255, 0)',
50-
],
66+
"rgb(239, 91, 145)"
67+
]
5168
},
5269
],
53-
labels: ['Free Memory', 'Active Memory', 'Used Memory', 'Total Memory'],
70+
labels: ["Free Memory", "Active Memory", "Used Memory", "Total Memory"]
5471
};
5572

56-
return <Bar data={chartData} />;
73+
return <Line data={chartData} />;
5774
};
5875

5976
// Return div with helper function invoked

0 commit comments

Comments
 (0)