-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
253 lines (217 loc) · 8.74 KB
/
index.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
<!DOCTYPE HTML>
<meta charset=UTF-8>
<title>Ages of US Presidents and 2024 Candidates</title>
<!--
Copyright 2019 L. David Baron
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<style>
.axis-line { stroke-width: 3px; stroke: black; }
.tick-line { stroke-width: 1px; stroke: gray; }
.age-line { stroke-width: 5px; stroke: purple; }
.age-line-label { font-size: 15px; }
.age-tick-label { text-anchor: end; dominant-baseline: middle; font-size: 12px; }
.age-dot { fill: blue }
.candidate-line { stroke-width: 3px; stroke: blue; }
.age-dot-label { dominant-baseline: middle; font-size: 12px; }
</style>
<h1>Ages of US Presidents and 2024 Candidates</h1>
<svg id="graph" viewBox="0 0 1000 600"></svg>
<script>
let gPresidents = [
{ start: "1933-03-04", name: "FDR", birth: "1882-01-30" },
{ start: "1945-04-12", name: "Truman", birth: "1884-05-08" },
{ start: "1953-01-20", name: "Eisenhower", birth: "1890-10-14" },
{ start: "1961-01-20", name: "Kennedy", birth: "1917-05-29" },
{ start: "1963-11-22", name: "Johnson", birth: "1908-08-27" },
{ start: "1969-01-20", name: "Nixon", birth: "1913-01-09" },
{ start: "1974-08-09", name: "Ford", birth: "1913-07-14" },
{ start: "1977-01-20", name: "Carter", birth: "1924-10-01" },
{ start: "1981-01-20", name: "Reagan", birth: "1911-02-06" },
{ start: "1989-01-20", name: "Bush, GHW", birth: "1924-06-12" },
{ start: "1993-01-20", name: "Clinton", birth: "1946-08-19" },
{ start: "2001-01-20", name: "Bush, GW", birth: "1946-07-06" },
{ start: "2009-01-20", name: "Obama", birth: "1961-08-04" },
{ start: "2017-01-20", name: "Trump", birth: "1946-06-14" },
{ start: "2021-01-20", name: "Biden", birth: "1942-11-20" },
];
function date_to_float_year(date) {
let y = date.getUTCFullYear();
let yearStart = Date.UTC(y, 0);
let yearEnd = Date.UTC(y+1, 0);
let yearFraction = (date.valueOf() - yearStart) / (yearEnd - yearStart);
return y + yearFraction;
}
const year_re = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
function datestr_to_utc(date_string) {
// This may throw if the input is bad; it assumes input is the text above.
let [m, year, month, day] = year_re.exec(date_string);
return new Date(year, Number(month) + 1, day, 17);
}
function datestr_to_float_year(date_string) {
return date_to_float_year(datestr_to_utc(date_string));
}
for (let index in gPresidents) {
let entry = gPresidents[index];
entry.start = datestr_to_float_year(entry.start);
entry.birth = datestr_to_float_year(entry.birth);
if (index != 0) {
let prev_entry = gPresidents[Number(index) - 1];
prev_entry.end = entry.start;
}
}
gPresidents[gPresidents.length - 1].end = date_to_float_year(new Date());
let gCandidates = [
{ name: "Harris", birth: "1964-10-20" },
{ name: "Trump", birth: "1946-06-14" },
{ name: "Vance", birth: "1984-08-02" },
];
for (let entry of gCandidates) {
entry.birth = datestr_to_float_year(entry.birth);
}
function transform_coord(year, age) {
// year space is 1930-2030
// age space is 30-90
let x = (year - 1930) * 10;
let y = (90 - age) * 10;
return {x, y};
}
function set_line_1(line, point) {
line.setAttribute("x1", point.x);
line.setAttribute("y1", point.y);
}
function set_line_2(line, point) {
line.setAttribute("x2", point.x);
line.setAttribute("y2", point.y);
}
function set_xy(svg_element, point) {
svg_element.setAttribute("x", point.x);
svg_element.setAttribute("y", point.y);
}
function set_circle_c(circle, point) {
circle.setAttribute("cx", point.x);
circle.setAttribute("cy", point.y);
}
const SVG_NS = "http://www.w3.org/2000/svg";
let gGraph = document.getElementById("graph");
{
let x_axis = document.createElementNS(SVG_NS, "line");
set_line_1(x_axis, transform_coord(1932, 35));
set_line_2(x_axis, transform_coord(2026, 35));
x_axis.setAttribute("class", "axis-line");
x_axis.setAttribute("id", "x-axis-line");
let y_axis = document.createElementNS(SVG_NS, "line");
set_line_1(y_axis, transform_coord(1932, 35));
set_line_2(y_axis, transform_coord(1932, 85));
y_axis.setAttribute("class", "axis-line");
y_axis.setAttribute("id", "y-axis-line");
gGraph.appendChild(x_axis);
gGraph.appendChild(y_axis);
}
for (let tick = 40; tick < 83; tick += 5) {
let line = document.createElementNS(SVG_NS, "line");
line.setAttribute("class", "tick-line");
set_line_1(line, transform_coord(1932, tick));
set_line_2(line, transform_coord(2025, tick));
gGraph.appendChild(line);
let text = document.createElementNS(SVG_NS, "text");
text.setAttribute("class", "age-tick-label");
text.textContent = tick;
set_xy(text, transform_coord(1931.5, tick));
gGraph.appendChild(text);
}
for (let president of gPresidents) {
let line = document.createElementNS(SVG_NS, "line");
let start_point = transform_coord(president.start, president.start - president.birth);
set_line_1(line, start_point);
set_line_2(line, transform_coord(president.end, president.end - president.birth));
line.setAttribute("class", "age-line");
line.setAttribute("data-name", president.name);
line.setAttribute("data-start", president.start);
line.setAttribute("data-end", president.end);
gGraph.appendChild(line);
let text = document.createElementNS(SVG_NS, "text");
text.setAttribute("class", "age-line-label");
text.textContent = president.name;
set_xy(text, {x: start_point.x, y: start_point.y - 6});
text.setAttribute("transform", `rotate(-45 ${start_point.x} ${start_point.y - 6})`);
gGraph.appendChild(text);
}
let gNextInauguration = datestr_to_float_year("2025-01-20");
let gYearAfterInaug = datestr_to_float_year("2026-01-20");
let gTextAfterInaug = datestr_to_float_year("2026-03-20");
let gTextsToSpread = [];
for (let candidate of gCandidates) {
/*
let circle = document.createElementNS(SVG_NS, "circle");
circle.setAttribute("class", "age-dot");
circle.setAttribute("r", "6");
let point = transform_coord(gNextInauguration, gNextInauguration - candidate.birth);
set_circle_c(circle, point);
gGraph.appendChild(circle);
let text = document.createElementNS(SVG_NS, "text");
text.setAttribute("class", "age-dot-label");
text.textContent = candidate.name;
set_xy(text, {x: point.x + 10, y: point.y });
gGraph.appendChild(text);
*/
let line = document.createElementNS(SVG_NS, "line");
line.setAttribute("class", "candidate-line");
set_line_1(line, transform_coord(gNextInauguration, gNextInauguration - candidate.birth));
set_line_2(line, transform_coord(gYearAfterInaug, gYearAfterInaug - candidate.birth));
gGraph.appendChild(line);
let text = document.createElementNS(SVG_NS, "text");
text.setAttribute("class", "age-dot-label");
text.textContent = candidate.name;
let text_point = transform_coord(gTextAfterInaug, gTextAfterInaug - candidate.birth);
set_xy(text, text_point);
text.setAttribute("transform", `rotate(-45 ${text_point.x} ${text_point.y})`);
gGraph.appendChild(text);
gTextsToSpread.push(text);
}
// Spread out the labels that are too close together. This just does a
// single pass from top to bottom, which isn't great, because it can
// move a label up into one that it didn't initially include in the
// group that needed to be spread out.
const gMinSpacing = 14;
gTextsToSpread.sort((t1, t2) => Number(t1.getAttribute("y")) - Number(t2.getAttribute("y")));
for (let i = 0; i < gTextsToSpread.length; ) {
let start_y = Number(gTextsToSpread[i].getAttribute("y"));
let end_y;
let i_after_group = i + 1;
while (true) {
if (i_after_group == gTextsToSpread.length) {
break;
}
let y = Number(gTextsToSpread[i_after_group].getAttribute("y"));
if ((y - start_y) / (i_after_group - i) > gMinSpacing) {
break;
}
end_y = y;
++i_after_group;
}
if (i_after_group > i + 1) {
// Respace this group of labels that is too close together.
let mid = (end_y + start_y) / 2;
let num_in_group = i_after_group - i;
let mid_index_in_group = (i + i_after_group - 1) / 2;
//console.log(i, i_after_group);
for (let i_in_group = i; i_in_group < i_after_group; ++i_in_group) {
let new_y = mid + (i_in_group - mid_index_in_group) * gMinSpacing;
let text = gTextsToSpread[i_in_group];
//console.log(text.textContent);
text.setAttribute("y", new_y);
text.setAttribute("transform", `rotate(-45 ${text.getAttribute("x")} ${new_y})`);
}
}
i = i_after_group;
}
</script>