forked from DH-RSE-Summer-School/uoe_visualization_workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
207 lines (175 loc) · 5.5 KB
/
Copy pathindex.html
File metadata and controls
207 lines (175 loc) · 5.5 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Visualization</title>
<!-- Leaflet CSS -->
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
/>
<!-- D3 -->
<script src="https://d3js.org/d3.v7.min.js"></script>
<!-- Leaflet JS -->
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
font-family: Arial, sans-serif;
}
header {
padding: 1rem;
background: #f5f5f5;
border-bottom: 1px solid #ddd;
}
main {
display: grid;
grid-template-columns: 1fr 1fr;
height: calc(100vh - 70px);
}
/* Leaflet map */
#map {
height: 100%;
width: 100%;
}
/* D3 visualization area */
#viz {
padding: 1rem;
overflow: auto;
}
svg {
width: 100%;
height: 600px;
border: 1px solid #ddd;
}
</style>
</head>
<body>
<header>
<h1>Digital Humanities Visualization</h1>
<p>D3.js + Leaflet starter project</p>
</header>
<main>
<div id="map"></div>
<div id="viz">
<h2>D3 Visualization</h2>
<svg id="chart"></svg>
</div>
</main>
<script>
// add commented code for changing color/size/width etc.
// attach the leaflet map to the div with id "map"
const map = L.map("map").setView([55.9533, -3.1883], 5);
// add tiles/images to the map
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "© OpenStreetMap contributors",
}).addTo(map);
// attach d3 to the asvg element where you are going to draw the visualization
const svg = d3.select("#chart");
// give the svg width, height, and margins for the visualization
const width = 900;
const height = 600;
const margin = { top: 30, right: 30, bottom: 60, left: 60 };
// make the svg responsive to different screen sizes
svg.attr("viewBox", `0 0 ${width} ${height}`);
// load the data
d3.json("data/women_doctors_geo.json")
.then((data) => {
console.log(data);
// MAP
// go through every record in the data and add a marker to the map for each birthplace
data.forEach((element) => {
if (
element.source_data?.birthplace?.lat &&
element.source_data?.birthplace?.lon
) {
L.marker([
element.source_data.birthplace.lat,
element.source_data.birthplace.lon,
])
.addTo(map)
.bindPopup("University of Edinburgh");
}
});
// Timeline
// clean up the data. make sure years are considered as numbers and filter out any invalid values
const years = data
.map((d) => Number(d.source_data?.entry_year))
.filter((year) => Number.isFinite(year) && year > 0);
// grouping of the years and countinrg the number per year
const timelineData = d3
.rollups(
years,
(values) => values.length,
(year) => year,
)
.map(([year, count]) => ({ year, count }))
.sort((a, b) => a.year - b.year);
// x axis
const x = d3
.scaleLinear()
.domain(d3.extent(timelineData, (d) => d.year))
// .nice()
.range([margin.left, width - margin.right]);
// y axis
const y = d3
.scaleLinear()
.domain([0, d3.max(timelineData, (d) => d.count)])
// .nice()
.range([height - margin.bottom, margin.top]);
// append both axes to the svg
svg
.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickFormat(d3.format("d")));
svg
.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y));
// axis labels
svg
.append("text")
.attr("x", width / 2)
.attr("y", height - 15)
.attr("text-anchor", "middle")
.text("Entry Year");
svg
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height / 2)
.attr("y", 20)
.attr("text-anchor", "middle")
.text("Number of Records");
// line chart
const line = d3
.line()
.x((d) => x(d.year))
.y((d) => y(d.count));
svg
.append("path")
.datum(timelineData)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("d", line);
svg
.selectAll(".point")
.data(timelineData)
.join("circle")
.attr("class", "point")
.attr("cx", (d) => x(d.year))
.attr("cy", (d) => y(d.count))
.attr("r", 3)
.attr("fill", "steelblue");
// add slider for the timeline
})
.catch((error) => {
console.error("Error loading JSON:", error);
});
</script>
</body>
</html>