-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempgraph.js
161 lines (138 loc) · 4.74 KB
/
tempgraph.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
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
/* Creates line graph visualization for webpage */
function draw_line_Temperature(areaID, repoNameWOwner) {
// load data file, process data, and draw visualization
Promise.all([d3.json('./data.json')]).then(result => {
let data = reformatData(result[0]);
drawGraph(data, areaID);
})
//var parseTime = d3.timeParse('%Y-%m-%d');
var formatTime = date => d3.isoParse(date);
// Draw graph from data
function drawGraph(data, areaID) {
const sensors = Object.keys(data);
const margin = { top: stdMargin.top, right: stdMargin.right, bottom: stdMargin.bottom, left: stdMargin.left * 1.15 },
width = stdTotalWidth * 2 - margin.left - margin.right,
height = stdHeight;
const dotRadius = stdDotRadius / 3;
const colors = d3.scaleOrdinal()
.domain(sensors)
.range(["#377eb8","#e41a1c","#4daf4a","#984ea3","#ff7f00","#ffff33","#a65628","#f781bf","#999999"]);
const firstDate = d3.min(sensors, sensor => {
const date = d3.min(data[sensor], d => d.date);
return(date);
});
const lastDate = d3.max(sensors, sensor => {
const date = d3.max(data[sensor], d => d.date);
return(date);
});
const lowestTemp = d3.min(sensors, sensor => {
const temp = d3.min(data[sensor], d => d.temp);
return(temp);
});
const highestTemp = d3.max(sensors, sensor => {
const temp = d3.max(data[sensor], d => d.temp);
return(temp);
});
const x = d3
.scaleTime()
.domain([
firstDate,
lastDate
])
.range([0,width]);
const y = d3
.scaleLinear()
.domain([Math.floor(lowestTemp), Math.ceil(highestTemp)])
.range([height, 0]);
const xAxis = d3.axisBottom().scale(x);
const yAxis = d3.axisLeft().scale(y);
var valueline = d3
.line()
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.temp);
});
var chart = d3
.select('.' + areaID)
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
// Add the x axis
chart
.append('g')
.attr('class', 'x axis')
.attr('transform', 'translate(0,' + height + ')')
.call(xAxis);
// Add the y axis
chart
.append('g')
.attr('class', 'y axis')
.call(yAxis);
// Add y axis label
chart
.append('text')
.attr('transform', 'rotate(-90)')
.attr('y', 0 - margin.left + margin.left / 4)
.attr('x', 0 - height / 2)
.attr('text-anchor', 'middle')
.text('Temperature');
sensors.forEach(sensor => {
// Draw line
chart
.append('g')
.append('path')
.datum(data[sensor])
.attr("fill", "none")
.attr("stroke", colors(sensor))
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.x(function(d) { return x(d.date) })
.y(function(d) { return y(d.temp) }));
// Draw dots
chart
.append('g')
.selectAll('circle')
.data(data[sensor])
.join('circle')
.attr('cx', function(d) {
return x(d.date);
})
.attr('cy', function(d) {
return y(d.temp);
})
.attr('r', dotRadius);
})
}
// Turn json obj into desired working data
// {
// sensor1:
// [
// {date: ..., temp: ...}
// ...
// {date: ..., temp: ...}
// ]
// ...
// sensorn:
// [
// {date: ..., temp: ...}
// ...
// {date: ..., temp: ...}
// ]
// }
function reformatData(obj) {
const data = {};
const sensors = Object.keys(obj);
sensors.forEach(sensor => {
data[sensor] = [];
const arrays = obj[sensor];
arrays.forEach(array => {
data[sensor].push({ date: formatTime(array[0]), temp: array[1] });
})
});
console.debug(data);
return data;
}
}