-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
223 lines (198 loc) · 8.6 KB
/
main.js
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
import * as d3 from "https://cdn.jsdelivr.net/npm/d3@7/+esm";
const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
async function main() {
const songs = (await d3.json("beatles_songs.json"))
.filter(song => song.yendor !== undefined && song.yendor.year >= 1962 && song.yendor.year <= 1970);
// Declare the chart dimensions and margins.
const width = 640;
const height = 400;
const marginTop = 80;
const marginRight = 20;
const marginBottom = 30;
const marginLeft = 40;
function graph(id, title, format, songs, xFn, yFn) {
if (id === "foo") {
console.log(songs);
}
// Declare the x (horizontal position) scale.
const x = d3.scaleLinear()
.domain([d3.min(songs, xFn) - 1, d3.max(songs, xFn)])
.range([marginLeft, width - marginRight]);
// Declare the y (vertical position) scale.
const y = d3.scaleLinear()
.domain([0, d3.max(songs, yFn)])
.range([height - marginBottom, marginTop]);
const xAxis = d3.axisBottom(x).tickFormat(d3.format(format));
const yAxis = d3.axisLeft(y);
// Find or create the SVG container.
let svg = d3.select("#" + id);
if (svg.empty()) {
svg = d3.select("body")
.append("svg")
.attr("id", id)
.attr("width", width)
.attr("height", height);
svg.append("text")
.attr("x", width / 2)
.attr("y", marginTop - 20)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text(title);
// Add the x-axis.
svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.attr("class", "x-axis");
// Add the y-axis.
svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.attr("class", "y-axis");
// Area for standard deviation
svg.append("path")
.attr("class", "stddev-area")
.attr("fill", "steelblue")
.attr("stroke", "none")
.style("opacity", 0.1);
// Line for mean.
svg.append("path")
.attr("class", "mean-line")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.style("opacity", 0.3);
// Add the graphed data.
svg.append("g")
.attr("class", "data-points")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("fill", "transparent");
}
// Update axes.
svg.selectAll(".x-axis")
.transition()
.call(xAxis);
svg.selectAll(".y-axis")
.transition()
.call(yAxis);
// Update data points.
svg.select(".data-points")
.selectAll("circle")
.data(songs, song => song.title)
.join(
enter => enter.append("circle")
.attr("cx", song => x(xFn(song)))
.attr("cy", song => y(yFn(song)))
.on("mouseover", function(e, song) {
d3.select(this).transition()
.duration(100)
.attr("r", 7);
tooltip.transition()
.duration(100)
.style("opacity", 1);
tooltip.html(song.title)
.style("left", (e.pageX + 10) + "px")
.style("top", (e.pageY - 15) + "px");
})
.on("mouseout", function() {
d3.select(this).transition()
.duration(100)
.attr("r", 3);
tooltip.transition()
.duration(100)
.style("opacity", 0);
})
.transition()
.attr("r", 3),
update => update,
exit => exit
.transition()
.attr("r", 0)
.remove());
// Map from year to { mean, stdev } object.
const stats = d3.rollups(songs,
d => ({ mean: d3.mean(d, yFn), stddev: d3.deviation(d, yFn) }),
song => xFn(song))
.map(d => ({ year: d[0], mean: d[1].mean, stddev: d[1].stddev ?? 0 }))
.sort((a, b) => a.year - b.year);
svg.select(".stddev-area")
.data([stats])
.transition()
.attr("d", d3.area()
.x(d => x(d.year))
.y0(d => y(d.mean - d.stddev))
.y1(d => y(d.mean + d.stddev)));
svg.select(".mean-line")
.data([stats])
.transition()
.attr("d", d3.line()
.x(d => x(d.year))
.y(d => y(d.mean)));
}
function graphByYear(id, title, songs, yFn) {
graph(id, title, "4d", songs, song => song.yendor.year, yFn);
}
function refresh(doFiltering) {
let filteredSongs = songs;
if (doFiltering) {
filteredSongs = filteredSongs.filter(song => song.yendor.songwriter === "Lennon");
}
graphByYear("duration",
"Duration (Seconds)",
filteredSongs,
song => song.yendor.duration);
graphByYear("number_of_chords",
"Number of Chords (Originals)",
filteredSongs.filter(song => song.isophonics?.chordlab !== undefined && song.pannell?.album?.Original_songs === 1),
song => new Set(song.isophonics.chordlab.map(cl => cl.chord).filter(chord => chord !== "N")).size);
graphByYear("number_of_takes",
"Number of Takes",
filteredSongs.filter(song => song.pannell?.album !== undefined),
song => song.pannell.album.Takes);
graphByYear("tempo",
"Tempo (BPM)",
filteredSongs.filter(song => song.TheHoleGotFixed !== undefined),
song => song.TheHoleGotFixed.tempos[0]);
graph("paul_billboard",
"Paul Authorship (vs John) vs. Billboard",
".1f",
filteredSongs.filter(song => song.pannell !== undefined &&
song.yendor["top.50.billboard"] !== -1 &&
Math.abs(song.pannell.album.Composer_share_John + song.pannell.album.Composer_share_Paul - 1) < 0.01),
song => song.pannell.album.Composer_share_Paul,
song => song.yendor["top.50.billboard"]);
graphByYear("top50",
"Top 50 Billboard",
filteredSongs.filter(song => song.yendor["top.50.billboard"] !== -1),
song => song.yendor["top.50.billboard"]);
graphByYear("acousticness",
"Acousticness",
filteredSongs.filter(song => song.chadwambles !== undefined),
song => song.chadwambles.acousticness);
graphByYear("danceability",
"Danceability",
filteredSongs.filter(song => song.chadwambles !== undefined),
song => song.chadwambles.danceability);
graphByYear("energy",
"Energy",
filteredSongs.filter(song => song.chadwambles !== undefined),
song => song.chadwambles.energy);
graphByYear("liveness",
"Liveness",
filteredSongs.filter(song => song.chadwambles !== undefined),
song => song.chadwambles.liveness);
graphByYear("speechiness",
"Speechiness",
filteredSongs.filter(song => song.chadwambles !== undefined),
song => song.chadwambles.speechiness);
graphByYear("valence",
"Valence",
filteredSongs.filter(song => song.chadwambles !== undefined),
song => song.chadwambles.valence);
}
refresh(false);
const lennonOnlyCheckbox = d3.select("#lennonOnly");
lennonOnlyCheckbox.on("change", () => refresh(lennonOnlyCheckbox.property("checked")));
}
main();