-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGauge.js
More file actions
101 lines (92 loc) · 2.93 KB
/
Copy pathGauge.js
File metadata and controls
101 lines (92 loc) · 2.93 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
import React, { useEffect, useState } from "react";
import useWindowDimensions from "./WindowDimensions"
import "./Gauge.css";
export default function Gauge(props) {
const { height, width } = useWindowDimensions();
const [size, setSize] = useState(150);
const [progress, setProgress] = useState(395);
const [data, setData] = useState({value: 0, unit: "N/A"});
useEffect(() => {
setSize(Math.min((width - 16*17) / 8 + 16, (height - 18*10) / 4.5 + 35));
}, [height, width]);
const position = {
top: "30px",
left: (Math.min((width - 16*17) / 8 + 16 - size) / 2) + "px"
}
useEffect(() => {
if (props.telemetryData === null) return;
var name = props.gaugeId;
try {
var parsedData = props.telemetryData.crucial_data.find(o => o.name === props.gaugeId);
}
catch(err) {
if (name == "acceleration") {
var parsedData = {
"name": "acceleration",
"min": -50.0,
"max": 50.0,
"unit": "m/s^2",
"value": 0
};
} else if (name == "velocity") {
var parsedData = {
"name": "velocity",
"min": 0.0,
"max": 250.0,
"unit": "m/s",
"value": 1
};
} else if (name == "distance") {
var parsedData = {
"name": "distance",
"min": 0.0,
"max": 1250.0,
"unit": "m",
"value": 0
};
}
}
setData(parsedData);
setProgress(395 - 197 * ((parsedData.value - parsedData.min) / (parsedData.max - parsedData.min)));
}, [props.telemetryData, props.gaugeId]);
return(
<div id={"gauge-" + props.gaugeId} className="gauge container">
<div className="gauge-title">{props.title}</div>
<svg
className="progress-ring" width="150px" height="150px" transform={"scale(" + (size / 150) + ")"} style={position}>
<text className="progress-value" x="50%" y="50%" textAnchor="middle" fill="white" fontSize="37px">{data.value.toFixed(4 - Math.round(data.value).toString().length)}</text>
<text className="progress-unit" x="50%" y="65%" textAnchor="middle" fill="grey" fontSize="16px">{data.unit}</text>
<circle
className="progress-ring-progress"
stroke="grey"
strokeWidth="6"
strokeDasharray="395 395"
strokeDashoffset="197"
fill="transparent"
r="63"
cx="75"
cy="75"/>
<circle
className="progress-ring-progress"
stroke="white"
strokeWidth="6"
strokeDasharray="395 395"
strokeDashoffset={progress}
fill="transparent"
r="63"
cx="75"
cy="75"/>
<circle
className="progress-ring-stopper"
stroke="red"
strokeWidth="7"
strokeDasharray="395 395"
strokeDashoffset="351"
fill="transparent"
r="63"
cx="75"
cy="75"/>
</svg>
</div>
);
}