|
| 1 | +"use strict"; |
| 2 | + |
1 | 3 | var data; |
| 4 | +var x_scale; |
| 5 | +var bar_y; |
| 6 | +var bar_height; |
| 7 | +var y_label_width; |
| 8 | +var x_axis; |
| 9 | +var width; |
2 | 10 |
|
3 | | -d3.json("doc.json", function(error, json) { |
4 | | - if (error) return console.warn(error); |
5 | | - data = json["rev_history"]; |
| 11 | +function offset(d, i) { |
| 12 | + // increase the y offset if the document name changed in this revision |
| 13 | + if (i > 0 && data[i - 1].name !== d.name || d.rev.match("rfc")) |
| 14 | + bar_y += bar_height; |
| 15 | + return "translate(" + x_scale(d.published) + ", " + bar_y + ")"; |
| 16 | +} |
6 | 17 |
|
7 | | - // make js dates out of publication dates |
8 | | - data.forEach(function(el) { el.published = new Date(el.published); }); |
9 | 18 |
|
10 | | - // add pseudo entry for beginning of year of first publication |
11 | | - var year = data[0].published.getFullYear(); |
12 | | - data.unshift({ name:'', rev: '', published: new Date(year, 0, 0)}); |
| 19 | +function bar_width(d, i) { |
| 20 | + if (i < data.length - 1) |
| 21 | + return x_scale(data[i + 1].published) - x_scale(d.published); |
| 22 | +} |
13 | 23 |
|
14 | | - // add pseudo entry at end of year of last revision |
15 | | - year = data[data.length - 1].published.getFullYear(); |
16 | | - data.push({ name:'', rev: '', published: new Date(year + 1, 0, 0)}); |
17 | 24 |
|
18 | | - draw_timeline(); |
19 | | -}); |
| 25 | +function scale_x() { |
| 26 | + width = $("#timeline").width(); |
20 | 27 |
|
| 28 | + // scale data to width of container minus y label width |
| 29 | + x_scale = d3.time.scale().domain([ |
| 30 | + d3.min(data, function(d) { return d.published; }), |
| 31 | + d3.max(data, function(d) { return d.published; }) |
| 32 | + ]).range([y_label_width, width]); |
21 | 33 |
|
22 | | -var xscale; |
23 | | -var y; |
24 | | -var bar_height; |
| 34 | + x_axis = d3.svg.axis() |
| 35 | + .scale(x_scale) |
| 36 | + // don't add a tick for the pseudo entry |
| 37 | + .tickValues(data.slice(0, -1).map(function(d) { return d.published; })) |
| 38 | + .tickFormat(d3.time.format("%b %Y")) |
| 39 | + .orient("bottom"); |
| 40 | +} |
25 | 41 |
|
26 | 42 |
|
27 | | -function offset(d, i) { |
28 | | - if (i > 1 && data[i - 1].name !== d.name || d.rev.match("rfc")) |
29 | | - y += bar_height; |
30 | | - return "translate(" + xscale(d.published) + ", " + y + ")"; |
| 43 | +function update_x_axis() { |
| 44 | + d3.select("#timeline svg .x.axis").call(x_axis) |
| 45 | + .selectAll("text") |
| 46 | + .style("text-anchor", "end") |
| 47 | + .attr("transform", "translate(-14, 2) rotate(-60)"); |
31 | 48 | } |
32 | 49 |
|
33 | 50 |
|
34 | | -function bar_width(d, i) { |
35 | | - if (i > 0 && i < data.length - 1) |
36 | | - return xscale(data[i + 1].published) - xscale(d.published); |
| 51 | +function update_timeline() { |
| 52 | + bar_y = 0; |
| 53 | + scale_x(); |
| 54 | + var chart = d3.select("#timeline svg").attr("width", width); |
| 55 | + var bar = chart.selectAll("g").data(data); |
| 56 | + bar.attr("transform", offset).select("rect").attr("width", bar_width); |
| 57 | + update_x_axis(); |
37 | 58 | } |
38 | 59 |
|
39 | 60 |
|
40 | 61 | function draw_timeline() { |
41 | | - var w = $("#timeline").width(); |
42 | | - // bar_height = parseFloat($("body").css('line-height')); |
43 | | - bar_height = 30; |
44 | | - |
45 | | - xscale = d3.time.scale().domain([ |
46 | | - d3.min(data, function(d) { return d.published; }), |
47 | | - d3.max(data, function(d) { return d.published; }) |
48 | | - ]).range([0, w]); |
49 | | - |
50 | | - y = 0; |
51 | | - var chart = d3.select("#timeline svg").attr("width", w); |
52 | | - var bar = chart.selectAll("g").data(data); |
53 | | - |
54 | | - // update |
55 | | - bar |
56 | | - .attr("transform", offset) |
57 | | - .select("rect") |
58 | | - .attr("width", bar_width); |
59 | | - |
60 | | - // enter |
61 | | - var g = bar.enter() |
62 | | - .append("g") |
63 | | - .attr({ |
64 | | - class: "bar", |
65 | | - transform: offset |
66 | | - }); |
67 | | - g.append("rect") |
68 | | - .attr({ |
69 | | - height: bar_height, |
70 | | - width: bar_width |
| 62 | + bar_height = parseFloat($("body").css('line-height')); |
| 63 | + // bar_height = 20; |
| 64 | + |
| 65 | + var div = $("#timeline"); |
| 66 | + if (div.is(":empty")) |
| 67 | + div.append("<svg></svg>"); |
| 68 | + var chart = d3.select("#timeline svg").attr("width", width); |
| 69 | + |
| 70 | + var gradient = chart.append("defs") |
| 71 | + .append("linearGradient") |
| 72 | + .attr("id", "gradient"); |
| 73 | + gradient.append('stop') |
| 74 | + .attr('class', 'stop-left') |
| 75 | + .attr('offset', '0'); |
| 76 | + gradient.append('stop') |
| 77 | + .attr('class', 'stop-right') |
| 78 | + .attr('offset', '1'); |
| 79 | + |
| 80 | + var y_labels = data |
| 81 | + .map(function(elem) { return elem.name; }) |
| 82 | + .filter(function(val, i, self) { return self.indexOf(val) === i; }); |
| 83 | + |
| 84 | + // calculate the width of the widest y axis label by drawing them off-screen |
| 85 | + // and measuring the bounding boxes |
| 86 | + y_label_width = 10 + d3.max(y_labels, function(l) { |
| 87 | + var lw; |
| 88 | + chart.append("text") |
| 89 | + .attr({ |
| 90 | + class: "y axis", |
| 91 | + transform: "translate(0, " + -bar_height + ")" |
| 92 | + }) |
| 93 | + .text(l) |
| 94 | + .each(function() { |
| 95 | + lw = this.getBBox().width; |
| 96 | + }) |
| 97 | + .remove().remove(); |
| 98 | + return lw; |
| 99 | + }); |
| 100 | + |
| 101 | + // update |
| 102 | + update_timeline(); |
| 103 | + |
| 104 | + // enter |
| 105 | + var bar = chart.selectAll("g").data(data); |
| 106 | + var g = bar.enter() |
| 107 | + .append("g") |
| 108 | + .attr({ |
| 109 | + class: "bar", |
| 110 | + transform: offset |
| 111 | + }); |
| 112 | + g.append("rect") |
| 113 | + .attr({ |
| 114 | + height: bar_height, |
| 115 | + width: bar_width |
| 116 | + }); |
| 117 | + g.append("text") |
| 118 | + .attr({ |
| 119 | + x: 3, |
| 120 | + y: bar_height/2 |
| 121 | + }) |
| 122 | + .text(function(d) { return d.rev; }); |
| 123 | + |
| 124 | + var y_scale = d3.scale.ordinal() |
| 125 | + .domain(y_labels) |
| 126 | + .rangePoints([0, bar_y]); |
| 127 | + |
| 128 | + var y_axis = d3.svg.axis() |
| 129 | + .scale(y_scale) |
| 130 | + .tickValues(y_labels) |
| 131 | + .orient("left"); |
| 132 | + |
| 133 | + chart.append("g").attr({ |
| 134 | + class: "x axis", |
| 135 | + transform: "translate(0, " + bar_y + ")" |
71 | 136 | }); |
72 | | - g.append("text") |
73 | | - .attr({ |
74 | | - x: 3, |
75 | | - y: bar_height/2 |
76 | | - }) |
77 | | - .text(function(d) { return d.rev; }); |
78 | | - |
79 | | - // exit |
80 | | - bar.exit().remove(); |
81 | | - |
82 | | - var xaxis = d3.svg.axis() |
83 | | - .scale(xscale) |
84 | | - .tickValues(data.slice(1, -1).map(function(d) { return d.published; })) |
85 | | - .tickFormat(d3.time.format("%b %Y")) |
86 | | - .orient("bottom"); |
87 | | - |
88 | | - var ids = data |
89 | | - .map(function(elem) { return elem.name; }) |
90 | | - .filter(function(val, i, self) { return self.indexOf(val) === i; }); |
91 | | - ids.shift(); // first one is pseudo entry (last one, too, but filtered above) |
92 | | - console.log(ids); |
93 | | - |
94 | | - var yaxis = d3.svg.axis() |
95 | | - .scale(d3.scale.ordinal().domain(ids).rangePoints([0, y - bar_height])) |
96 | | - .tickValues(ids) |
97 | | - .orient("left"); |
98 | | - |
99 | | - chart.append("g") |
100 | | - .attr({ |
101 | | - class: "x axis", |
102 | | - transform: "translate(0, " + y + ")" |
103 | | - }) |
104 | | - .call(xaxis) |
105 | | - .selectAll("text") |
106 | | - .style("text-anchor", "end") |
107 | | - .attr("transform", "translate(-18, 8) rotate(-90)"); |
108 | | - |
109 | | - chart.append("g") |
110 | | - .attr({ |
111 | | - class: "y axis", |
112 | | - transform: "translate(10, " + bar_height/2 + ")" |
113 | | - }) |
114 | | - .call(yaxis) |
115 | | - .selectAll("text") |
116 | | - .style("text-anchor", "start"); |
117 | | - |
118 | | - chart.attr('height', y); |
| 137 | + update_x_axis(); |
| 138 | + |
| 139 | + chart.append("g") |
| 140 | + .attr({ |
| 141 | + class: "y axis", |
| 142 | + transform: "translate(10, " + bar_height/2 + ")" |
| 143 | + }) |
| 144 | + .call(y_axis) |
| 145 | + .selectAll("text") |
| 146 | + .style("text-anchor", "start"); |
| 147 | + |
| 148 | + // set height of timeline |
| 149 | + var x_label_height; |
| 150 | + d3.select(".x.axis").each(function() { |
| 151 | + x_label_height = this.getBBox().height; |
| 152 | + }); |
| 153 | + chart.attr('height', bar_y + x_label_height); |
119 | 154 | } |
120 | 155 |
|
121 | 156 |
|
| 157 | +d3.json("doc.json", function(error, json) { |
| 158 | + if (error) return; // console.warn(error); |
| 159 | + data = json["rev_history"]; |
| 160 | + |
| 161 | + // make js dates out of publication dates |
| 162 | + data.forEach(function(d) { d.published = new Date(d.published); }); |
| 163 | + |
| 164 | + // add pseudo entry 185 days after last revision (when the ID will expire) |
| 165 | + var pseudo = new Date(data[data.length - 1].published.getTime() + |
| 166 | + 1000*60*60*24*185); |
| 167 | + data.push({ name: "", rev: "", published: pseudo}); |
| 168 | + draw_timeline(); |
| 169 | +}); |
| 170 | + |
| 171 | + |
122 | 172 | $(window).on({ |
123 | | - resize: function (event) { |
124 | | - draw_timeline(); |
| 173 | + resize: function() { |
| 174 | + update_timeline(); |
125 | 175 | } |
126 | 176 | }); |
0 commit comments