-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.qmd
172 lines (138 loc) · 4.76 KB
/
index.qmd
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
---
title: "Mesas de ping pong en CABA 🏓"
format:
html:
page-layout: full
editor: visual
---
```{ojs}
//| echo: false
//| output: false
dataURL = "https://raw.githubusercontent.com/fer-aguirre/ping-pong-caba/main/data/pingpong-caba.csv"
fetch(dataURL)
.then(response => {
if (!response.ok) throw new Error(response.status);
return response.text();
});
data = d3.csv(dataURL)
barrio_geo = await FileAttachment("./data/ba_barrios.geo.json").json()
zone_geo = await FileAttachment("./data/zonas.geo.json").json()
d3 = require('d3@5', 'd3-array@^2.2')
import {select} from "@jashkenas/inputs"
import {svgGradientDefs} from "@luciyer/svg-gradients-chrome-safari"
```
```{ojs}
//| echo: false
viewof barrio_overlay = select(["Mostrar barrios", "Ocultar barrios"])
map = {
var style = {
height: 600,
margin: 20,
barrio_stroke: "#000",
barrio_stroke_width: 1,
barrio_stroke_opacity: 1,
zone_stroke: "#BBB",
zone_stroke_width: 0.5,
zone_stroke_opacity: 1
};
let projection = d3.geoMercator()
.fitSize([width - style.margin * 2,
style.height - style.margin * 2], barrio_geo);
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, style.height])
.attr('width', width)
.attr('height', style.height)
.on("click", function() {
tooltip.style("visibility", "hidden");
if (currentElement) {
d3.select(currentElement).style("opacity", 1);
currentElement = null;
}
});
const zone_map = svg.append('g')
.attr("class", "zone-map")
.attr("transform", `translate(${style.margin}, ${style.margin})`);
const tooltip = d3.select("body").append("div")
.attr("class", "svg-tooltip")
.style("position", "absolute")
.style("visibility", "hidden")
.style("font-size", "1rem")
.style("background", "rgba(255,255,255, 2555)")
.style("padding", "0.1rem 0.5rem")
.style("color", "#000")
.style("border-radius", "3px")
.style("border", "1px solid #000");
let mouseover = function (d) {
d3.select(this).style("opacity", 0.5);
}
let mouseout = function (d) {
d3.select(this).style("opacity", 1);
}
let currentElement = null;
zone_map.append("g")
.selectAll("path")
.data(zone_geo.features)
.enter()
.append("path")
.attr("fill", "#FFF")
.attr("stroke", style.zone_stroke)
.attr("stroke-opacity", style.zone_stroke_opacity)
.attr("stroke-width", style.zone_stroke_width)
.attr("d", d3.geoPath().projection(projection));
if(barrio_overlay === "Mostrar barrios") {
let barrio_map = svg.append("g")
.attr("class", "barrio-map")
.attr("transform", `translate(${style.margin}, ${style.margin})`);
barrio_map.append("g")
.selectAll("path")
.data(barrio_geo.features)
.enter()
.append("path")
.attr("fill", "none")
.attr("stroke", style.barrio_stroke)
.attr("stroke-opacity", style.barrio_stroke_opacity)
.attr("stroke-width", style.barrio_stroke_width)
.attr("d", d3.geoPath().projection(projection));
}
let points = svg.append("g")
.attr("class", "points")
.attr("transform", `translate(${style.margin}, ${style.margin})`);
points.selectAll("image")
.data(data)
.enter()
.append("image")
.attr("xlink:href", "https://github.com/fer-aguirre/ping-pong-caba/blob/main/assets/logo.png?raw=true")
.attr("x", function(d) { return projection([d.long, d.lat])[0]; })
.attr("y", function(d) { return projection([d.long, d.lat])[1]; })
.attr("width", "20px")
.attr("height", "20px")
.style("opacity", 1)
.on("mouseover", mouseover)
.on("mouseout", mouseout)
.on("click", function (d) {
d3.event.stopPropagation();
if (tooltip.style("visibility") === "visible") {
tooltip.style("visibility", "hidden");
d3.select(this).style("opacity", 1);
currentElement = null;
} else {
let tooltipText = "<b>" + d.plaza + "</b><br><i><a href='" + d.google_maps + "' target='_blank'>Haz clic para abrir la ubicación en Google Maps</a></i>";
if (d.direccion) tooltipText += "<br>Dirección: " + d.direccion;
if (d.comuna) tooltipText += "<br>" + d.comuna;
if (d.mesas) tooltipText += "<br>Mesas: " + Math.round(d.mesas);
tooltip
.style("visibility", "visible")
.style("top", (d3.event.pageY - 10) + "px")
.style("left", (d3.event.pageX + 10) + "px")
.html(tooltipText);
if (currentElement) {
d3.select(currentElement).style("opacity", 1);
}
d3.select(this)
.style("opacity", 0.5);
currentElement = this;
}
});
return svg.node();
}
```