-
Notifications
You must be signed in to change notification settings - Fork 10
/
d3dateline.js
266 lines (234 loc) · 8.86 KB
/
d3dateline.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
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
function loadchart(div, json) {
// pass in id of div where the svg will live and name/url of json data
var dayWidth = 100,
height = 200,
margin = {
top: 40,
right: 40,
bottom: 0,
left: 40
},
radius = 10;
// ctlx and ctly are the offsets for the control points for the Bezier curve.
// ctly is subtracted from source and target, to place control point
// above the source/target
// ctlx is added to source and subtracted from target, to place control point
// so as to flatten the curve slightly
var ctlx = 10;
var ctly = 35;
d3.json(json, function (error, graph) {
var line = d3.svg.line()
var earliest = new Date(graph.nodes[0].date);
// TODO discover latest by looking rather than assuming the nodes are sorted
var latest = new Date(graph.nodes[graph.nodes.length - 1].date);
// number of days in the data set ...
var interval = (latest - earliest) / 1000 / 60 / 60 / 24 + 1;
// ... determines the width of the svg
var width = interval * dayWidth;
var svg = d3.select("#" + div)
.append("svg")
.attr("width", width)
.attr("height", height)
.attr('preserveAspectRatio', 'xMinYMin slice')
.append('g');
/************************
Scales and Axes
*************************/
var x = d3.time.scale()
.domain([earliest, d3.time.day.offset(latest, 1)])
.rangeRound([0, width - margin.left - margin.right]);
var xAxisDays = d3.svg.axis()
.scale(x)
.orient('bottom')
.ticks(d3.time.days, 1)
.tickFormat(d3.time.format('%a %-e'))
.tickSize(5)
.tickPadding(8);
var xAxisMonths = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(d3.time.months, 1)
.tickFormat(d3.time.format("%B %Y"))
.tickSize(5, 0);
svg.append('g')
.attr('class', 'x axis monthaxis')
.attr('transform', 'translate(0, ' + (height - margin.top - margin.bottom) + ')')
.attr('style', 'opacity: 0.1')
.call(xAxisMonths);
svg.append('g')
.attr('class', 'x axis dayaxis')
.attr('transform', 'translate(0, ' + (height - margin.top - margin.bottom) + ')')
.call(xAxisDays);
/************************
Links
*************************/
function curve(d) {
// Bezier curve
// we assume source is earlier than target or on same day
var c, upper, lower;
if (d.source.x == d.target.x) {
// same day - control points on right - need to start with upper
if (d.source.y < d.target.y) {
upper = d.source;
lower = d.target;
} else {
upper = d.target;
lower = d.source;
}
c = "M" + upper.x + "," + upper.y +
" C" + (upper.x + ctly) + "," + (upper.y - ctlx) +
" " + (lower.x + ctly) + "," + (lower.y + ctlx) +
" " + lower.x + "," + lower.y;
} else {
// different days - use ellipse
var dx = d.target.x - d.source.x,
dy = d.target.y - d.source.y,
dr = Math.sqrt(dx * dx + dy * dy);
c = "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " +
d.target.x + "," + d.target.y;
}
return c;
}
// Arrowheads
svg.append("svg:defs").selectAll("marker")
.data(["end"])
.enter().append("svg:marker")
.attr("id", String)
.attr("class", "arrowhead")
.attr("viewBox", "0 -5 10 10")
.attr("refX", 25)
.attr("refY", -1.5)
.attr("markerWidth", 8)
.attr("markerHeight", 8)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
var link = svg.selectAll(".link")
.data(graph.links)
.enter().append("svg:path")
.attr("d", function (d) {
return curve(d);
})
.attr("class", "link")
.attr("marker-end", "url(#end)");
/************************
Nodes
*************************/
// add fixed coords to nodes
var stackcounts = [];
graph.nodes.forEach(function (node) {
// x is always over the sortdate
// y is stacked on any letters already on that date if the date is precise,
// otherwise at top of graph to allow it to be pulled into position
node.x = x(new Date(node.date)) + (2 * radius);
if (node.type == "fixed") {
var previousLetters = (stackcounts['d' + node.date]) ? stackcounts['d' + node.date] : 0;
stackcounts['d' + node.date] = previousLetters + 1;
node.y = height - margin.bottom - margin.top - radius - 1 -
(previousLetters * radius * 2) - previousLetters;
node.fixed = true;
} else {
// TODO offset x slightly
node.y = 0;
}
});
var node = svg.selectAll(".node")
.data(graph.nodes)
.enter().append("g")
.attr("class", "node");
var circle = node.append("svg:circle")
.attr('id', function (d) {
return "n" + d.id;
})
.attr('class', function (d) {
return "letter d" + d.date + " from" + d.from + " " +
((d.type == "fixed") ? "precise" : "notprecise");
})
.attr('r', radius)
node.append("svg:title")
.text(function (d) {
return d.name;
});
// text, centered in node, with white shadow for legibility
node.append("text")
.attr("text-anchor", "middle")
.attr("dy", radius / 2)
.attr("class", "shadow")
.text(function(d) { return d.id });
node.append("text")
.attr("text-anchor", "middle")
.attr("dy", radius / 2)
.text(function(d) { return d.id });
// on click, do something with id
// implement this in a function outside this block
node.on("click", function (d) {
itemclick(d);
});
// Resolves collisions between d and all other circles.
function collide(node) {
var r = radius + 8,
nx1 = node.x - r,
nx2 = node.x + r,
ny1 = node.y - r,
ny2 = node.y + r;
return function (quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== node)) {
var x = node.x - quad.point.x,
y = node.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = radius + radius;
if (l < r) {
l = (l - r) / l * .5;
node.x -= x *= l;
node.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2 || x2 < nx1 || y1 > ny2 || y2 < ny1;
};
}
/************************
Force and Tick
*************************/
var force = self.force = d3.layout.force()
.nodes(graph.nodes)
.links(graph.links)
.gravity(0)
.charge(0.1)
.distance(40)
.size([width, height])
.start()
.on("tick", tick);
function tick(e) {
// artificial gravity, based on node type
var k = 20 * e.alpha;
graph.nodes.forEach(function (o, i) {
if (o.type == "isAnswer") // move right
o.x += k;
else if (o.type == "hasAnswer") // move left
o.x += -k;
else if (o.type == "free") // move up
o.y += -k;
});
// handle collisions
var q = d3.geom.quadtree(graph.nodes),
i = 0,
n = graph.nodes.length;
while (++i < n) {
q.visit(collide(graph.nodes[i]));
}
node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
// constrain to bounding box
node.attr("cx", function (d) {
return d.x = Math.max(15, Math.min(width - 15, d.x));
})
.attr("cy", function (d) {
return d.y = Math.max(15, Math.min(height - 15, d.y));
});
link.attr("d", function (d) {
return curve(d);
});
}
});
}