-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchart.jsx
More file actions
102 lines (94 loc) · 3.4 KB
/
chart.jsx
File metadata and controls
102 lines (94 loc) · 3.4 KB
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
import React, { Component } from "react";
import { VictoryChart, VictoryArea } from "victory";
const APIBASEURL = "http://localhost:8000/heartbeat/api/";
const APIKEY = "0987612345ZYXW";
var json_data = false;
//var graph_data = false;
class Chart extends Component {
state = {
data: []
};
constructor(request) {
super(); //using arrow fucntions for bind
}
//basic request handler
handleRequest = url => {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status) {
json_data = xhttp.responseText;
}
};
xhttp.open("GET", url, true);
xhttp.send();
return json_data;
};
getGraphData = graph => {
//rebind for setInterval scope
var self = this;
var url = APIBASEURL + type + "/" + graph.id + "/" + APIKEY + "/";
var type = false;
setInterval(function(e) {
var graph_data = self.handleRequest(url);
var vic_format = [];
JSON.parse(graph_data, function(key, value) {
if (key === "system_info") {
var json_str = JSON.stringify(value)
.replace(/"/g, "")
.replace(/'/g, '"');
var obj = JSON.parse(json_str);
var created_at = new Date(obj.created_at);
var seconds = created_at.getSeconds();
type = graph.type + "_usage";
vic_format.push(
{
x: seconds, //time in s, m, h
y: 0, //ground level
y0: obj[type] //percent use
}
// test data
// { x: 20, y: 0, y0: 60 },
// { x: 45, y: 0, y0: 46 },
// { x: 50, y: 0, y0: 25 },
// { x: 55, y: 0, y0: 18 },
// { x: 60, y: 0, y0: 19 }
);
}
});
//do something with the data each time we get it, like update state
self.setState({
data: vic_format
});
}, 1000);
};
render() {
var type = this.props.type;
var id = this.props.id;
return (
<div>
<div id="Charts" className="container mx-auto">
<h5>{type} usage</h5>
<button onClick={e => this.getGraphData({ type, id })}>
Set New State Manually
</button>
<div className="row">
<div className="col">
<div id="usage" className="" />
<VictoryChart>
<VictoryArea
id={type}
standalone={"false"}
width={100}
height={100}
padding={0}
data={this.state.data}
/>
</VictoryChart>
</div>
</div>
</div>
</div>
);
}
}
export default Chart;