-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineGraph.js
120 lines (100 loc) · 2.64 KB
/
LineGraph.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import React, { useEffect, useState } from "react";
import { Line } from "react-chartjs-2";
import numeral from "numeral";
const options={
legend:{
display:false,
},
elements:{
point:{
radius:0,
},
},
maintainAspectRatio: false,
tooltips :{
mode : "index",
intersect:false,
callbacks:{
label: function(tooltipItem,data){
return numeral(tooltipItem.value).format("+0,0");
},
},
},
scales:{
xAxes: [
{
type:"time",
time:{
format:"MM/DD/YY",
tootipFormat:"ll",
},
},
],
yAxes : [
{
gridLines:{
display:false,
},
ticks: {
//include a dollar sign in the ticks
callback: function (value,index,values){
return numeral (value).format("0a");
},
},
},
],
},
};
const buildChartData = (data, casesType) => {
let chartData = [];
let lastDataPoint;
for(let date in data.cases) {
if (lastDataPoint) {
let newDataPoint ={
x: date,
y:data[casesType][date]-lastDataPoint,
};
chartData.push(newDataPoint);
}
lastDataPoint= data[casesType][date];
}
return chartData;
};
function LineGraph({casesType="cases", ...props}) {
const [data, setData] = useState({});
useEffect(() => {
const fetchData = async () =>{
await fetch('https://disease.sh/v3/covid-19/historical/all?lastdays=30')
.then((response) => {
return response.json();
})
.then((data) => {
//cleaver stuff go here..
let chartData =buildChartData(data,casesType);
setData(chartData);
//chart data/graph
});
};
fetchData();
}, [casesType]);
return (
<div className={props.className}>
{/*-------------checked clear from up to here-----------------------------------------*/}
{data?.length > 0 && (
<Line
options={options}
data={{
datasets:[
{
backgroundColor:"rgba(204,16,52,0.5)" ,
borderColor:"#CC1034",
data:data,
},
],
}}
/>
)}
</div>
)
}
export default LineGraph