-
Notifications
You must be signed in to change notification settings - Fork 0
/
chart_d3.html
336 lines (283 loc) · 8.24 KB
/
chart_d3.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<style type="text/css">
body {
font-family: "Open Sans", sans-serif;
font-size: 12px;
}
.g-hed {
text-align: center;
font-weight: bold;
font-size: 22px;
margin: 3px 0;
}
.Todayslevels {
color: #00577B;
}
.year-label {
font-size: 9px;
stroke-width: 0px;
fill: grey;
font-weight: lighter;
}
#yearlabel-2017{
font-size: 12px;
font-weight: bold;
fill: #00577B;
}
.year {
fill: none;
stroke: red;
stroke-width: 2px;
stroke-opacity: .7;
shape-rendering: geometricPrecision;
opacity: .4;
}
#year-2017 {
stroke-opacity: 1;
stroke: #00577B;
opacity: 1;
stroke-width: 2px;
}
#year-2011 {
stroke: #a65628;
}
#year-2009 {
stroke: #984ea3;
}
#year-1997 {
stroke: #4daf4a;
}
#year-1950 {
stroke: #e41a1c;
}
.g-source {
padding: .5em 1em;
text-align: left;
background: #eee;
font-size: .8em;
}
.g-source-bold {
text-align: left;
font-size: 10px;
}
.g-intro {
font-weight: 400;
font-size: .9em;
color: #777;
margin-top: 2em;
text-align: center;
fill: #777;
}
.g-labels {
fill: white;
font-weight: bold;
font-size: 14px;
}
.axis line {
fill: red;
stroke: grey;
shape-rendering: crispEdges;
stroke-width: 1px;
}
.axis text {
font-size: .9em;
pointer-events: none;
fill: #555;
}
.y.axis text {
text-anchor: end !important;
font-size: .9em;
fill: #555;
}
.domain {
display: none;
}
.line {
stroke-width: 1.5px;
fill: none;
}
.overlay {
fill: none;
pointer-events: all;
}
#graph-container{
width:100;
}
</style>
<body>
<div id="graph-container">
<h4>The Red River is <span class="Todayslevels"> </span> feet above James Ave. datum</h4>
<div class="g-chart"></div>
<div class="g-source">
<span class="g-source-bold"></span><span class="g-source-reg"></span>
</div>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
<script>
//Margin conventions
var margin = {top: 20, right: 70, bottom: 40, left: 50};
var widther = document.getElementById("graph-container").clientWidth;
var width = widther - margin.left - margin.right,
height = 300 - margin.top - margin.bottom;
//Parses date for correct time format
var parseDate = d3.time.format("%d/%m").parse;
//Divides date for tooltip placement
var bisectDate = d3.bisector(function(d) {
return d.day;
}).left;
//Appends the svg to the chart-container div
var svg = d3.select(".g-chart")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
//Creates the xScale
var xScale = d3.time.scale().range([0, width]);
//Creates the yScale
var yScale = d3.scale.linear().range([height, 0]);
//Defines the y axis styles
var yAxis = d3.svg.axis().scale(yScale).tickSize(5).tickPadding(5).orient("left");
//Defines the y axis styles
var xAxis = d3.svg.axis().scale(xScale).tickPadding(8).orient("bottom").tickSize(5).ticks(numTicks(width)).tickFormat(d3.time.format("%b %d"));
//line function convention (feeds an array)
var line = d3.svg.line().x(function(d) {
return xScale(d.day);
}).y(function(d) {
return yScale(d.level);
});
var area = d3.svg.area().x(function(d) {
return x(d.day);
}).y0(height).y1(function(d) {
return y(d.close);
});
//Loads the data
d3.csv("https://s3.ca-central-1.amazonaws.com/data-test123456/water_levels+(2).csv", ready);
function ready(err, data) {
//Format data
data.forEach(function(d) {
d.level = +d.level;
d.day = parseDate(d.day);
});
//Appends latest value to headline
d3.select(".Todayslevels").text(data[data.length - 1].level);
//Defines the xScale max
xScale.domain(d3.extent(data, function(d) {
return d.day;
}));
//Defines the yScale max
yScale.domain(d3.extent(data, function(d) {
return d.level;
}));
// Nest the entries by year
var dataNest = d3.nest().key(function(d) {
return d.year;
}).entries(data);
// Loop through each year / key
dataNest.forEach(function(d) {
svg.append("path").attr("class", function() {
var thisClassList = "year";
return thisClassList;
}).attr("id", function() {
var thisID = "year-" + d.key;
return thisID;
}).attr("d", line(d.values));
// Add a label to each line for the relevant year
// Add the label at the Max point
// Get the last data values for each year
// var numberOfDataPoints = d.values.length;
// var labelPositionSourceValue = d.values[numberOfDataPoints - 1];
// tmp vars to track the max value and index
var valuesArray = d.values;
var currentYear = d.key;
var currentMaxDay = '';
var currentMaxValue = 0;
var currentMaxCounter = 0;
for (var i in valuesArray) {
if (valuesArray[i].level > currentMaxValue) {
currentMaxDay = valuesArray[i].day;
currentMaxValue = valuesArray[i].level;
currentMaxCounter = i;
}
}
// Calculate the X/Y position + the text label based on the above value
var labelXPos = xScale(currentMaxDay);
var labelYPos = yScale(currentMaxValue);
var labelText = currentYear;
// Draw the label
svg.append("text").attr("transform", "translate(" + labelXPos + "," + labelYPos + ")").attr("class", function() {
var thisClassList = "year-label";
return thisClassList;
}).attr("id", function() {
var thisID = "yearlabel-" + d.key;
return thisID;
}).style("opacity", 1).attr("dy", "-.7em").attr("dx", "0em").attr("text-anchor", "middle").text(labelText);
});
//Appends the y axis
var yAxisGroup = svg.append("g").attr("class", "y axis").call(yAxis);
//Appends the x axis
var xAxisGroup = svg.append("g").attr("class", "x axis").attr("transform", "translate(0," + height + ")").call(xAxis);
//Binds the data to the line
var drawline = svg.append("path").datum(data).attr("class", "line").attr("d", line);
svg.append("text").attr("text-anchor", "middle") //
.attr("class", "g-intro").attr("transform", "translate(" + (-margin.left / 2 - 5) + "," + (height / 2) + ")rotate(-90)") // text is drawn off the screen top left, move down and out and rotate
.text("feet");
//Appends chart source
d3.select(".g-source-reg").text("Live data source: Water Survey of Canada/Environment Canada via the City of Winnipeg").attr("class", "g-source-reg");
//RESPONSIVENESS
d3.select("div#chartId")
.append("div")
.classed("svg-container", true) //container class to make it responsive
.append("svg")
//responsive SVG needs these 2 attributes and no width and height attr
.attr("preserveAspectRatio", "xMinYMin meet")
.attr("viewBox", "0 0 600 400")
//class to make it responsive
.classed("svg-content-responsive", true);
d3.select(window).on("resize", resized);
function resized() {
//new margin
var newMargin = {top: 10, right: 80, bottom: 20, left: 50};
console.log(newMargin);
//Get the width of the window
var w = d3.select(".g-chart").node().clientWidth;
console.log("resized", w);
//Change the width of the svg
d3.select("svg")
.attr("width", w);
//Change the xScale
xScale
.range([0, w - newMargin.right]);
//Update the line
line = d3.svg.line()
.x(function(d) { return xScale(d.day); })
.y(function(d) { return yScale(d.level); });
d3.selectAll('.line')
.attr("d", line);
//Updates xAxis
xAxisGroup
.call(xAxis);
//Updates ticks
xAxis
.scale(xScale)
.ticks(numTicks(w));
//Updates yAxis
yAxis
.tickSize(-w - newMargin.right)
};
}
//Determines number of ticks base on width
function numTicks(widther) {
if (widther <= 900) {
return 4;
console.log("return 4");
}
else {
return 12;
console.log("return 5");
}
}
</script>