-
Notifications
You must be signed in to change notification settings - Fork 7
/
commitsbase.js
139 lines (112 loc) · 4.52 KB
/
commitsbase.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
// Find the element where the commit timeline should be drawn
var commits = d3.select("#commits");
var orgName = commits.attr("orgName") || "JavaPosseRoundup";
var startDate = commits.attr("startDate") || "";
var leftRightPadding = commits.attr("left-right-padding") || 20;
var topBottomPadding = commits.attr("top-bottom-padding") || 150;
var width = commits.attr("width") || $(document).width() - leftRightPadding;
var height = commits.attr("height") || $(window).height() - topBottomPadding;
var leftMargin = commits.attr("left-margin") || 180;
var rightMargin = commits.attr("right-margin") || 0;
var topMargin = commits.attr("top-margin") || 20;
var bottomMargin = commits.attr("bottom-margin") || 60;
var w = width - leftMargin - rightMargin,
h = height - topMargin - bottomMargin;
// Scales. Note the inverted domain for the y-scale: bigger is up!
var x = d3.time.scale().rangeRound([0, w]),
y = d3.scale.ordinal().rangePoints([0, h], .5),
colors = d3.scale.category20();
// Axes.
var xAxis = d3.svg.axis().scale(x).tickSubdivide(true);
var yAxis = d3.svg.axis().scale(y).tickSize(0).tickPadding(5).orient("left");
// Add an SVG element with the desired dimensions and margin.
var svg = commits.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + leftMargin + "," + topMargin + ")");
// Add the clip path.
svg.append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", w)
.attr("height", h);
// Need something in the background absorb the mouse events!
svg.append("rect")
.attr("width", w)
.attr("height", h)
.style("fill-opacity", ".0");
function drawChart(allCommits, timelines) {
var tickHeight = height / (timelines.length * 4 + 1);
var earliestCommitDate = d3.min(timelines, function(d) { return d.earliest; });
var start = d3.time.format("%Y-%m-%d").parse(startDate) || earliestCommitDate;
x.domain([d3.time.day.floor(start), d3.time.day.ceil(new Date())]);
y.domain(timelines.map(function(t) { return t.repo; }));
// Add the x-axis.
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + (h + topMargin + tickHeight) + ")")
.call(xAxis);
// Add the y-axis.
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.selectAll(".commit")
.data(allCommits)
.enter().append("line")
.attr("class", "commit")
.attr("clip-path", "url(#clip)")
.attr("x1", function(d) { return x(d.commitDate); })
.attr("y1", function(d) { return Math.floor(y(d.repo)) - tickHeight; })
.attr("x2", function(d) { return x(d.commitDate); })
.attr("y2", function(d) { return Math.floor(y(d.repo)) + tickHeight; })
.style("stroke", function(d) { return colors(d.committerEmail); })
.on("mouseover", showCommitInfo);
svg.selectAll(".timeline")
.data(timelines)
.enter().append("line")
.attr("class", "timeline")
.attr("clip-path", "url(#clip)")
.attr("x1", function(d) { return x(d.earliest); })
.attr("y1", function(d) { return Math.floor(y(d.repo)); })
.attr("x2", function(d) { return x(d.latest); })
.attr("y2", function(d) { return Math.floor(y(d.repo)); });
svg.call(d3.behavior.zoom().x(x).on("zoom", zoom));
function zoom() {
svg.select(".x.axis").call(xAxis);
svg.selectAll(".commit")
.data(allCommits)
.attr("x1", function(d) { return x(d.commitDate); })
.attr("x2", function(d) { return x(d.commitDate); });
svg.selectAll(".timeline")
.data(timelines)
.attr("x1", function(d) { return x(d.earliest); })
.attr("x2", function(d) { return x(d.latest); });
svg.selectAll(".committer, .message")
.attr("x", function(d) { return x(d.commitDate); });
}
function showCommitInfo(d, i) {
var committer = svg.selectAll(".committer")
.data([d], function(d) { return d.sha; });
committer.enter().append("text")
.attr("class", "committer")
.attr("x", x(d.commitDate))
.attr("y", y(d.repo))
.attr("dx", -2)
.attr("dy", 2 * tickHeight + 5)
.attr("text-anchor", "end")
.style("fill", colors(d.committerEmail))
.text(d.committerName);
committer.exit().remove();
var message = svg.selectAll(".message")
.data([d], function(d) { return d.sha; });
message.enter().append("text")
.attr("class", "message")
.attr("x", x(d.commitDate))
.attr("y", y(d.repo))
.attr("dx", 2)
.attr("dy", 2 * tickHeight + 5)
.text(d.message);
message.exit().remove();
}
}