-
Notifications
You must be signed in to change notification settings - Fork 0
/
lovro.html
288 lines (242 loc) · 8.09 KB
/
lovro.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 14px sans-serif;
align:center;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
stroke-width: 1.5px;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 3.5px;
}
.dot {
stroke: #000;
}
div.tooltip {
position: absolute;
text-align: center;
width: 80px;
height: 55px;
padding: 4px;
font: 16px sans-serif;
color: white;
font-weight: 500;
background: black;
border: 0px;
border-radius: 4px;
pointer-events: none;
}
/* http://www.d3noob.org/2013/01/adding-grid-lines-to-d3js-graph.html */
.grid .tick {
stroke: lightgrey;
opacity: 0.7;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.js"></script>
<script>
//I increased the bottom margin a little bit because the x label is tied to it; so I could lower the x label a little bit
var margin = {top: 50, right: 20, bottom: 60, left: 90},
width = 900 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(d3.date,360)
//makes the xAxis ticks a little longer than the xMinorAxis ticks
.tickSize(15)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.date); })
.y(function(d) { return y(d.Podravska); });
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var svg = d3.select("body").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 + ")");
//The format in the CSV, which d3 will read
var parseDate = d3.time.format("%Y");
//format for tooltip
//https://github.com/mbostock/d3/wiki/Time-Formatting
//var formatTime = d3.time.format("%e %b");
var formatTime = d3.time.format("%e %b %-I:%M %p");
var formatCount = d3.format(",");
// function for the y grid lines
function make_y_axis() {
return d3.svg.axis()
.scale(y)
.orient("left")
//.ticks(5)
}
//reading in CSV which contains data
d3.csv("vse.csv", function(error, data) {
data.forEach(function(d) {
//console.log(d.time)
d.date = parseDate.parse(d.time);
console.log(d.date);
d.Podravska = +d.Podravska;
console.log(d.Podravska);
});
//using imported data to define extent of x and y domains
x.domain(d3.extent(data, function(d) { return d.date; }));
y.domain(d3.extent(data, function(d) { return d.Podravska; }));
// Draw the y Grid lines
svg.append("g")
.attr("class", "grid")
.call(make_y_axis()
.tickSize(-width, 0, 0)
.tickFormat("")
)
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
//taken from http://bl.ocks.org/mbostock/3887118
//and http://www.d3noob.org/2013/01/change-line-chart-into-scatter-plot.html
//creating a group(g) and will append a circle and 2 lines inside each group
var g = svg.selectAll()
.data(data).enter().append("g");
//The markers on the line
g.append("circle")
//circle radius is increased
.attr("r", 4.5)
.attr("cx", function(d) { return x(d.date); })
.attr("cy", function(d) { return y(d.Podravska); });
//The horizontal dashed line that appears when a circle marker is moused over
g.append("line")
.attr("class", "x")
.attr("id", "dashedLine")
.style("stroke", "steelblue")
.style("stroke-dasharray", "3,3")
.style("opacity", 0)
.attr("x1", function(d) { return x(d.date); })
.attr("y1", function(d) { return y(d.Podravska); })
//d3.min gets the min date from the date x-axis scale
.attr("x2", function(d) { return x(d3.min(x)); })
.attr("y2", function(d) { return y(d.Podravska); });
//The vertical dashed line that appears when a circle marker is moused over
g.append("line")
.attr("class", "y")
.attr("id", "dashedLine")
.style("stroke", "steelblue")
.style("stroke-dasharray", "3,3")
.style("opacity", 0)
.attr("x1", function(d) { return x(d.date); })
.attr("y1", function(d) { return y(d.Podravska); })
.attr("x2", function(d) { return x(d.date); })
.attr("y2", height);
//circles are selected again to add the mouseover functions
g.selectAll("circle")
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div.html(formatCount(d.Podravska) + " €" + "<br/>" + "Leto:" + "<br/>"+d.time)
.style("left", (d3.event.pageX - 20) + "px")
.style("top", (d3.event.pageY + 6) + "px");
//selects the horizontal dashed line in the group
d3.select(this.nextElementSibling).transition()
.duration(200)
.style("opacity", .9);
//selects the vertical dashed line in the group
d3.select(this.nextElementSibling.nextElementSibling).transition()
.duration(200)
.style("opacity", .9);
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
d3.select(this.nextElementSibling).transition()
.duration(500)
.style("opacity", 0);
d3.select(this.nextElementSibling.nextElementSibling).transition()
.duration(500)
.style("opacity", 0);
});
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll(".tick text")
.call(wrap, 35);
//http://www.d3noob.org/2012/12/adding-axis-labels-to-d3js-graph.html
svg.append("text") // text label for the x-axis
.attr("x", width / 2 )
.attr("y", height + margin.bottom)
.style("text-anchor", "middle")
.text("Leto");
svg.append("text") // text label for the y-axis
.attr("y",30 - margin.left)
.attr("x",50 - (height / 2))
.attr("transform", "rotate(-90)")
.style("text-anchor", "end")
.style("font-size", "16px")
.text("Povprecna mesecna neto placa (eur)");
//http://www.d3noob.org/2013/01/adding-title-to-your-d3js-graph.html
svg.append("text") // text label for chart Title
.attr("x", width / 2 )
.attr("y", 0 - (margin.top/2))
.style("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Povprecna mesecna neto placa v Sloveniji");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
//text label for the y-axis inside chart
/*
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.style("font-size", "16px")
.style("background-color","red")
.text("road length (km)");
*/
//http://bl.ocks.org/mbostock/7555321
//This code wraps label text if it has too much text
function wrap(text, width) {
text.each(function() {
var text = d3.select(this),
words = text.text().split(/\s+/).reverse(),
word,
line = [],
lineNumber = 0,
lineHeight = 1.1, // ems
y = text.attr("y"),
dy = parseFloat(text.attr("dy")),
tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(" "));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(" "));
line = [word];
tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
}
}
});
}
});
</script>
</body>