forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_timeline.js
More file actions
269 lines (236 loc) · 7.61 KB
/
document_timeline.js
File metadata and controls
269 lines (236 loc) · 7.61 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
269
"use strict";
var data;
var x_scale;
var bar_y;
var bar_height;
var y_label_width;
var x_axis;
var width;
var now;
function expiration_date(d) {
return new Date(d.published.getTime() + 1000 * 60 * 60 * 24 * 185);
}
function max(arr) {
return Math.max.apply(null, Object.keys(arr)
.map(function (e) {
return arr[e];
}));
}
function offset(d) {
if (bar_y[d.name] === undefined) {
var m = Object.keys(bar_y)
.length === 0 ? -bar_height : max(bar_y);
bar_y[d.name] = m + bar_height;
}
return "translate(" + x_scale(d.published) + ", " + bar_y[d.name] + ")";
}
function bar_width(d, i) {
// check for next rev of this name, or published RFC
for (i++; i < data.length; i++) {
if (data[i].name === d.name || data[i].name.match(/^rfc/)) { break; }
}
var w;
if (d.name.match(/^draft-/)) {
// don't extend the bar past the expiration date of the document
w = i === data.length ? expiration_date(d) : data[i].published;
w = w > expiration_date(d) ? expiration_date(d) : w;
} else {
// documents other than drafts don't expire after 185 days
w = i === data.length ? new Date() : data[i].published;
}
return x_scale(w) - x_scale(d.published);
}
function scale_x() {
// scale data to width of container minus y label width
x_scale = d3.scaleTime()
.domain([
d3.min(data, function (d) { return d.published; }),
d3.max(data, function (d) { return d.published; })
])
.range([y_label_width, width]);
// if the end of the timeline is past the current date, show it
var tv = data.slice(0, -1);
now = Date.now();
if (tv[tv.length - 1].published > now) { tv.push(new Date(now)); }
// x label format
var format = d3.timeFormat("%b %Y");
// resort data by publication time to suppress some ticks if they are closer
// than 12px and have a different label from the one before; and don't add a
// tick for the final pseudo entry
tv = tv.sort(function (a, b) { return a.published - b.published; })
.map(function (d, i, arr) {
if (i === 0 ||
x_scale(d.published) > x_scale(arr[i - 1].published) + 12 &&
format(d.published) !== format(arr[i - 1].published)) {
return d.published;
}
})
.filter(function (d) { return d !== undefined; });
x_axis = d3.axisBottom(x_scale)
.tickValues(tv)
.tickFormat(function (d) {
if (d.getTime() < now) { return format(d); }
return "Now";
});
}
function update_x_axis() {
d3.select("#timeline svg .x.axis")
.call(x_axis)
.selectAll("text")
.style("text-anchor", "end")
.attr("transform", "translate(-14, 2) rotate(-60)");
}
function update_timeline() {
bar_y = {};
scale_x();
var chart = d3.select("#timeline svg")
.attr("width", width);
// enter data (skip the last pseudo entry)
var bar = chart.selectAll("g")
.data(data.slice(0, -1));
bar.attr("transform", offset)
.select("rect")
.attr("width", bar_width);
update_x_axis();
}
function draw_timeline() {
bar_height = parseFloat($("body")
.css("line-height"));
var div = $("#timeline");
div.addClass("my-3");
if (div.is(":empty")) {
div.append("<svg></svg>");
}
var chart = d3.select("#timeline svg")
.attr("width", width);
var defs = chart.append("defs");
var fade = defs.append("linearGradient")
.attr("id", "maskGradient");
fade.append("stop")
.attr("offset", 0.9)
.attr("stop-color", "white")
.attr("stop-opacity", 1);
fade.append("stop")
.attr("offset", 1)
.attr("stop-color", "white")
.attr("stop-opacity", 0);
var mask = defs.append("mask")
.attr("id", "fade")
.attr("maskContentUnits", "objectBoundingBox");
mask.append("rect")
.attr("height", 1)
.attr("width", 1)
.attr("fill", "url(#maskGradient)");
var y_labels = data
.map(function (d) { return d.name; })
.filter(function (val, i, self) { return self.indexOf(val) === i; });
// calculate the width of the widest y axis label by drawing them off-screen
// and measuring the bounding boxes
y_label_width = 10 + d3.max(y_labels, function (l) {
var lw;
var text = chart.append("text");
text
.attr("class", "y axis")
.attr("transform", "translate(0, " + -bar_height + ")");
text
.text(l)
.each(function () {
lw = this.getBBox()
.width;
})
.remove()
.remove();
return lw;
});
// update
update_timeline();
// re-order data by document name, for CSS background color alternation
var ndata = [];
y_labels.forEach(function (l) {
ndata = ndata.concat(data.filter(function (d) {
return d.name ===
l;
}));
});
data = ndata;
// enter data (skip the last pseudo entry)
var bar = chart.selectAll("g")
.data(data.slice(0, -1));
var g = bar.enter()
.append("g");
g.attr("class", "bar")
.attr("transform", offset);
var a = g.append("a");
a.attr("xlink:href", function (d) { return d.url; });
var rect = a.append("rect")
.attr("height", bar_height)
.attr("width", bar_width)
.attr("class", "btn")
.attr("type", "button")
.attr("mask", function (d, i) {
// apply gradient if the document is a draft and expired
if (d.name.match(/^draft-/) &&
bar_width(d, i) >= x_scale(expiration_date(d)) -
x_scale(d.published)) {
return "url(#fade)";
}
});
var text = g.append("text");
text.attr("x", 3)
.attr("y", bar_height / 2);
text.text(function (d) { return d.rev; });
var y_scale = d3.scalePoint()
.domain(y_labels)
.range([0, max(bar_y) + bar_height]);
var y_axis = d3.axisLeft(y_scale)
.tickValues(y_labels);
chart.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0, " + (max(bar_y) + bar_height) + ")");
update_x_axis();
var g = chart.append("g");
g
.attr("class", "y axis")
.attr("transform", "translate(10, " + bar_height / 2 + ")");
g
.call(y_axis)
.selectAll("text")
.style("text-anchor", "start");
// set height of timeline
var x_label_height;
d3.select(".x.axis")
.each(function () {
x_label_height = this.getBBox()
.height;
});
chart.attr("height", max(bar_y) + bar_height + x_label_height);
}
d3.json("doc.json")
.then(function (json) {
data = json.rev_history;
if (data.length) {
// make js dates out of publication dates
data.forEach(function (d) { d.published = new Date(d.published); });
// add pseudo entry when the ID will expire
data.push({
name: "",
rev: "",
published: expiration_date(data[data.length - 1])
});
width = $("#timeline")
.width();
draw_timeline();
}
});
$(window)
.on({
resize: function () {
var g = $("#timeline svg");
g.remove();
width = $("#timeline")
.width();
$("#timeline")
.append(g);
update_timeline();
}
});