-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
268 lines (226 loc) · 6.44 KB
/
Copy pathindex.html
File metadata and controls
268 lines (226 loc) · 6.44 KB
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
<!DOCTYPE html>
<head profile="http://gmpg.org/xfn/11">
<title>Probando git</title>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
h1 {
text-align: center;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis .tick line {
stroke: #eee;
}
.domain {
display: none;
}
.dot {
stroke: white;
}
.selected-city {
stroke: black;
stroke-width: 2;
}
.text-label {
font-size: 14px;
font-weight: 700;
}
.label {
font-size: 20px;
fill: black;
}
.tooltip {
position: absolute;
background: white;
font-size: 14px;
padding: 10px;
border: 1px solid #ccc;
left;-400px;
}
.dot:hover {
stroke: black;
stroke-width: 2px;
}
</style>
</head>
<body>
<h1>Nivel de git vs sesiones de Bilbao Data Lab</h1>
<div id="vis"></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
function drawGraphic(container_width) {
if (container_width == undefined || isNaN(container_width)) {
container_width = 600;
}
}
var city = "Bilbao";
// Margin convention: https://bl.ocks.org/mbostock/3019563
var margin = {top: 20, right: 20, bottom: 30, left: 50},
ratio = 0.5, // Height ratio
width = d3.select("#vis").node().clientWidth - margin.left - margin.right, // Width depending on the div
height = width * ratio - margin.top - margin.bottom; // Height depending on the ratio
// X scale, linear, depending on the width
// var x = d3.scaleLinear()
// .range([0, width]);
var x = d3.scaleLinear()
.range([0, width]);
// Y scale, linear, depending on the height
var y = d3.scaleLinear()
.range([height, 0]);
var xAxis = d3.axisBottom()
//.tickSize(-height)
//.tickPadding(10)
//.ticks(10)
.scale(x);
var yAxis = d3.axisLeft()
//.tickSize(-width)
//.tickPadding(10)
.scale(y);
// Create SVG and container
var svg = d3.select("#vis").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 + ")");
var tooltip = d3.select("#vis")
.append("div")
.attr("class", "tooltip")
// Read the data
d3.csv("data.csv", function(error, data) {
if (error) throw error;
// Process the data, coerce them into numbers
data.forEach(function(d) {
d.renta_2013 = +d.nivel_git;
d.pop_2013 = +d.sesiones;
});
// Finding out the domain
//x.domain(d3.extent(data, function(d) { return d.pop_2013; })).nice();
x.domain([0,10]);
//y.domain(d3.extent(data, function(d) { return d.renta_2013; })).nice();
y.domain([0,10]);
// Append x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
// Append y axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
// Append the dots
svg.selectAll(".dot")
.data(data) // Reference to our data
.enter()
.append("circle") // Data join, now it's looping
.attr('class', function(d) { return d.name === city ? 'dot selected-city' : 'dot'; })
.attr("r", 8)
.attr("cx", function(d) { return x(d.pop_2013); }) // X value
.attr("cy", function(d) { return y(d.renta_2013); }) // Y value
.style("fill", function(d) { return d.color; }) // Fill according to the color assigned in column "color"
.attr("stroke", "white")
.on("mousemove", showTooltip) // AÑADIR EVENTO SHOW TOOLTIP
.on("mouseout", hideTooltip) // OCULTAR TOOLTIP
// Legend
svg.append('text')
.attr("class", "label pop")
.attr("x", width)
.attr("y", height)
.attr("dy", -5)
.attr("text-anchor", "end")
.text("Nº Sesiones →");
svg.append('text')
.attr("class", "label rent")
.attr("x", 0)
.attr("y", 0)
.attr("dy", 25)
.attr("dx", 5)
.attr("text-anchor", "start")
.text("Nivel git ↑");
// Add name of the current city
svg.selectAll('.text-label')
.data(data)
.enter()
.filter(function(d) { return d.name === city; })
.append('text')
.attr('class', 'text-label')
.attr('x', function(d) { return x(d.pop_2013) })
.attr('y', function(d) { return y(d.renta_2013) })
.attr('dy', -15)
.attr('dx', 0)
.attr('text-anchor', 'middle')
.text(function(d) { return d.name });
// Assign a listener to the resize event
window.onresize = resize;
function resize() {
// Update width & height
width = d3.select("#vis").node().clientWidth - margin.left - margin.right,
height = width * ratio - margin.top - margin.bottom;
// Update scales
x.range([0, width]);
y.range([height, 0]);
// Update axis
//xAxis.tickSize(-height);
//yAxis.tickPadding(10);
// Update SVG
d3.selectAll("svg")
.attr("height", height + margin.top + margin.bottom)
.attr("width", width + margin.left + margin.right);
// Update axis
d3.select(".y.axis").call(yAxis);
d3.select(".x.axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Update dots
svg.selectAll(".dot")
.attr("cx", function(d) { return x(d.pop_2013); })
.attr("cy", function(d) { return y(d.renta_2013); });
// Update label
svg.selectAll('.text-label')
.attr('x', function(d) { return x(d.pop_2013) })
.attr('y', function(d) { return y(d.renta_2013) });
svg.select('.label.pop')
.attr('x', width)
.attr('y', height);
svg.select('.label.rent')
.attr('x', 0)
.attr('y', 0);
}
function showTooltip(d) {
// Fill the tooltip
tooltip.html("<div class='tooltip-city'><strong>" + d.name + "</strong></div>" +
"<table class='tooltip-table'>" +
"<tr class='first-row'>" +
"<td><span class='table-n'>"+ d.pop_2013 +"</span> sesiones</td>" +
"</tr>" +
"<tr class='second-row'>" +
"<td>" + d.renta_2013 + " nivel git</td>" +
"</tr>" +
"</table>")
.style("opacity", 1)
tooltip.style("left", (d3.event.pageX - 20) + "px")
tooltip.style("top", (d3.event.pageY + 23) + "px")
}
function hideTooltip(d) {
// Hide tooltip
tooltip.style("opacity", 0)
}
});
</script>
Probando git. <a href="https://github.com/BilbaoDataLab/git-intro">Código disponible</a> basado en el <a href="https://github.com/BilbaoDataLab/workflow-datos/">diagrama de dispersión de Martín González hecho con d3</a>.
<a href="http://bilbaodatalab.wikitoki.org">Bilbao Data Lab</a>
<p>
<strong>Han colaborado</strong>:<br>
numeroteca<br>
haxier<br>
pgg<br>
alegriaheviacristian<br>
maria<br>
</p>
</body>