-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindicator-tracker.html
137 lines (129 loc) · 3.92 KB
/
indicator-tracker.html
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Indicator tracker</title>
<style>
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
.chartCard {
width: 100vw;
height: calc(100vh - 40px);
background: rgba(75, 192, 192, 0.2);
display: flex;
align-items: center;
justify-content: center;
}
.chartBox {
width: 700px;
padding: 20px;
border-radius: 20px;
border: solid 3px rgba(75, 192, 192, 1);
background: white;
}
</style>
</head>
<body>
<div class="chartCard">
<div class="chartBox">
<canvas id="myChart"></canvas>
<button onclick="addValue(this)" value="20">Danger Zone</button>
<button onclick="addValue(this)" value="7">Success Zone</button>
<button onclick="addValue(this)" value="1">Reset Zone</button>
</div>
<div class="chartBox">
<p>Status: <span id="status">...</span></p>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
const data = {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [
{
label: "# of Votes",
data: [12, 19, 3, 5, 2, 2],
backgroundColor: [
"rgba(255, 99, 132, 0.2)",
"rgba(54, 162, 235, 0.2)",
"rgba(255, 206, 86, 0.2)",
"rgba(75, 192, 192, 0.2)",
"rgba(153, 102, 255, 0.2)",
"rgba(255, 159, 64, 0.2)",
],
borderColor: [
"rgba(255, 99, 132, 1)",
"rgba(54, 162, 235, 1)",
"rgba(255, 206, 86, 1)",
"rgba(75, 192, 192, 1)",
"rgba(153, 102, 255, 1)",
"rgba(255, 159, 64, 1)",
],
},
],
};
const statusChecker = {
id: "statusChecker",
beforeDatasetsDraw(chart, args, pluginsOptions) {
const {
ctx,
chartArea: { top, bottom, left, right, width, height },
scales: { x, y },
} = chart;
ctx.save();
drawLines(12, "rgba(255, 99, 132, 1)");
drawLines(4, "rgba(255, 206, 86, 1)");
function drawLines(yValue, color) {
ctx.beginPath();
ctx.lineWidth = 3;
ctx.strokeStyle = color;
ctx.moveTo(left, y.getPixelForValue(yValue));
ctx.lineTo(right, y.getPixelForValue(yValue));
ctx.stroke();
ctx.closePath();
ctx.restore();
}
tracker();
},
};
const config = {
type: "line",
data: data,
options: {
scales: {
y: {
beginAtZero: true,
},
},
},
plugins: [statusChecker],
};
const myChart = new Chart(document.getElementById("myChart"), config);
function tracker() {
const datapoints = myChart.data.datasets[0].data;
const datapointsLength = myChart.data.datasets[0].data.length - 1;
const status = document.getElementById("status");
if (datapoints[datapointsLength] > 12) {
status.innerText = "Danger";
status.style.color = "rgba(255, 99, 132, 1)";
} else if (datapoints[datapointsLength] < 4) {
status.innerText = "Please Reset System";
status.style.color = "rgba(255, 206, 86, 1)";
} else {
status.innerText = "Success";
status.style.color = "black";
}
}
function addValue(element) {
console.log(element.value);
myChart.data.datasets[0].data.push(element.value);
myChart.data.labels.push("New Value");
myChart.update();
}
</script>
</body>
</html>