|
1 | 1 | import React, { useContext } from 'react'; |
2 | | -import { Bar } from 'react-chartjs-2'; |
| 2 | +import { Line } from 'react-chartjs-2'; |
3 | 3 | import HealthContext from '../context/DetailsContext'; |
4 | 4 |
|
5 | 5 | const MemoryChart = () => { |
6 | 6 | const healthData = useContext(HealthContext); |
7 | 7 | // const health = healthData.detailData; |
8 | 8 |
|
9 | 9 | 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 = []; |
16 | 14 |
|
17 | 15 | for (let i = 0; i < healthData.length; i += 1) { |
18 | | - console.log(healthData); |
19 | 16 | // 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); |
30 | 27 | } |
31 | 28 | } |
32 | 29 |
|
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 | + } |
37 | 38 |
|
38 | 39 | const chartData = { |
39 | 40 | datasets: [ |
40 | 41 | { |
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), |
43 | 65 | 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 | + ] |
51 | 68 | }, |
52 | 69 | ], |
53 | | - labels: ['Free Memory', 'Active Memory', 'Used Memory', 'Total Memory'], |
| 70 | + labels: ["Free Memory", "Active Memory", "Used Memory", "Total Memory"] |
54 | 71 | }; |
55 | 72 |
|
56 | | - return <Bar data={chartData} />; |
| 73 | + return <Line data={chartData} />; |
57 | 74 | }; |
58 | 75 |
|
59 | 76 | // Return div with helper function invoked |
|
0 commit comments