-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
198 lines (187 loc) · 5.88 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style>
.line {
fill: none;
stroke: steelblue;
stroke-linejoin: round;
stroke-linecap: round;
stroke-width: 1.5px;
}
</style>
<script src="//d3js.org/d3.v4.min.js"></script>
<script>
/**
* Pad a string to specified length with specified character.
*/
function padString(str, padChar, length) {
str = str.toString();
while(str.length < length) {
str = padChar + str;
}
return str;
}
/**
* Draw the graph, including axes and captions.
*/
function drawPowerGraph(measure) {
// And create the new graph.
var svg = d3.select(".power"),
margin = {top: 20, right: 80, bottom: 30, left: 50},
width = svg.attr("width") - margin.left - margin.right,
height = svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Scale the horizontal and vertical axes, to make everything fit precisely.
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var maxY = d3.max(measure, function(row) { return row.p1_current_power_in; });
x.domain(d3.extent(measure, function(row) { return new Date(row.p1_timestamp); }));
y.domain([0, maxY]);
// The functions that will draw the line.
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) {
var time = new Date(d.p1_timestamp);
return x(time);
})
.y(function(d) {
return y(d.p1_current_power_in);
});
// Draw the horizontal axis.
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Draw the vertical axis, with caption.
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Power IN, kW");
// Big moment, drawing the line itself !!!
g.append("path")
.datum(measure)
.attr("class", "line")
.attr("d", line);
}
/**
* Draw the graph, including axes and captions.
*/
function drawGasGraph(measure) {
// And create the new graph.
var svg = d3.select(".gas"),
margin = {top: 20, right: 80, bottom: 30, left: 50},
width = svg.attr("width") - margin.left - margin.right,
height = svg.attr("height") - margin.top - margin.bottom,
g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Scale the horizontal and vertical axes, to make everything fit precisely.
var x = d3.scaleTime().range([0, width]);
var y = d3.scaleLinear().range([height, 0]);
var minY = d3.min(measure, function(row) { return row.p1_channel_1_meterreading; });
var maxY = d3.max(measure, function(row) { return row.p1_channel_1_meterreading; });
x.domain(d3.extent(measure, function(row) { return new Date(row.p1_channel_1_timestamp); }));
y.domain([0, maxY - minY]);
// The functions that will draw the line.
var line = d3.line()
.curve(d3.curveBasis)
.x(function(d) {
var time = new Date(d.p1_channel_1_timestamp);
return x(time);
})
.y(function(d) {
return y(d.p1_channel_1_meterreading - minY);
});
// Draw the horizontal axis.
g.append("g")
.attr("class", "axis axis--x")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// Draw the vertical axis, with caption.
g.append("g")
.attr("class", "axis axis--y")
.call(d3.axisLeft(y))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", "0.71em")
.attr("fill", "#000")
.text("Gas IN, m3");
// Big moment, drawing the line itself !!!
g.append("path")
.datum(measure)
.attr("class", "line")
.attr("d", line);
}
/**
* Draw the Power and Gas graph, including axes and captions.
*/
function drawGraphs(timestamp) {
// Remove the old graph.
d3.select(".power").selectAll("*").remove();
d3.select(".gas").selectAll("*").remove();
var date = new Date(timestamp);
// Update the title of the page.
document.title = "Meterstanden " + date.toDateString();
document.getElementById("header").innerHTML = document.title;
var csvFile =
"P1_" +
(1900 + date.getYear()) + "-" +
padString(date.getMonth() + 1, "0", 2) + "-" +
padString(date.getDate(), "0", 2) +
"_60s.csv";
// Load the CSV file into the page.
d3.text(csvFile, function(error, text) {
if (!text) return;
// Interpret the CVS file.
var parser = d3.dsvFormat(';');
var measure = parser.parse(text);
drawPowerGraph(measure);
drawGasGraph(measure);
});
}
/**
* To change the day.
*/
function onPickerChanged() {
var selElem = document.getElementById("picker");
console.log(selElem.options[selElem.selectedIndex].value);
// Draw the graph of that day and clear the previous.
drawGraphs(selElem.options[selElem.selectedIndex].value);
}
/**
* Add a day to the dropdown box.
*/
function addOption(elem, text, delta) {
var day = new Date();
day.setDate(day.getDate() - delta);
elem.options[elem.options.length] = new Option(text, day.toString());
}
/**
Page is loaded, time for action.
*/
function onBodyLoad() {
// Find the dropdown box.
var pickerElem = document.getElementById("picker");
// Add days for the complete week, I think this is enough.
addOption(pickerElem, "Vandaag", 0);
addOption(pickerElem, "Gisteren", 1);
addOption(pickerElem, "Eergisteren", 2);
for(var i = 3; i < 7; i++) {
addOption(pickerElem, "" + i + " dagen geleden", i);
}
// By default, show today.
drawGraphs((new Date()).toString());
}
</script>
</head>
<body onload="onBodyLoad();">
<select id="picker" onchange="onPickerChanged();"></select>
<h1 id="header"></h1><br/>
<svg class="power" width="960" height="500"></svg>
<svg class="gas" width="960" height="500"></svg>
</body>