-
Notifications
You must be signed in to change notification settings - Fork 5
/
d3-timeline.js
310 lines (267 loc) · 8.76 KB
/
d3-timeline.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
(function() {
d3.timeline = function() {
var DISPLAY_TYPES = ["circle", "rect"];
var hover = function () {},
click = function () {},
scroll = function () {},
orient = "bottom",
width = null,
height = null,
tickFormat = { format: d3.time.format("%I %p"),
tickTime: d3.time.years,
tickNumber: 1,
tickSize: 6 },
colorCycle = d3.scale.category20(),
display = "rect",
beginning = 0,
ending = 0,
margin = {left: 30, right:30, top: 30, bottom:30},
stacked = false,
rotateTicks = false,
itemHeight = 15,
itemMargin = 1
;
function timeline (gParent) {
var g = gParent.append("g");
var gParentSize = gParent[0][0].getBoundingClientRect();
var gParentItem = d3.select(gParent[0][0]);
var yAxisMapping = {},
maxStack = 1,
minTime = 0,
maxTime = 0;
setWidth();
// check how many stacks we're gonna need
// do this here so that we can draw the axis before the graph
if (stacked || (ending == 0 && beginning == 0)) {
g.each(function (d, i) {
d.forEach(function (datum, index) {
// create y mapping for stacked graph
if (stacked && Object.keys(yAxisMapping).indexOf(index) == -1) {
yAxisMapping[index] = maxStack;
maxStack++;
}
// figure out beginning and ending times if they are unspecified
if (ending == 0 && beginning == 0){
datum.times.forEach(function (time, i) {
if (time.starting_time < minTime || minTime == 0)
minTime = time.starting_time;
if (time.ending_time > maxTime)
maxTime = time.ending_time;
});
}
});
});
if (ending == 0 && beginning == 0) {
beginning = minTime;
ending = maxTime;
}
}
var scaleFactor = (1/(ending - beginning)) * (width - margin.left - margin.right);
// draw the axis
var xScale = d3.time.scale()
.domain([beginning, ending])
.range([margin.left, width - margin.right]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient(orient)
.tickFormat(tickFormat.format)
.ticks(tickFormat.tickTime, tickFormat.tickNumber)
.tickSize(tickFormat.tickSize);
g.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + 0 +","+(margin.top + (itemHeight + itemMargin) * maxStack)+")")
.call(xAxis);
// draw the chart
g.each(function(d, i) {
d.forEach( function(datum, index){
var data = datum.times;
var hasLabel = (typeof(datum.label) != "undefined");
var queColor = "#F88";
var itemHeightPlus = 0;
if (datum.cargoTipo == "legislativo")
{queColor = "#88F";
}else{
queColor = "#F88";
}
if (datum.cargo == "presidente")
{queColor = "red";
}
g.selectAll("svg").data(data).enter()
.append(display)
.attr('x', getXPos)
.attr("y", getStackPosition)
.attr("width", function (d, i) {
return (d.ending_time - d.starting_time) * scaleFactor;
})
.attr("cy", getStackPosition)
.attr("cx", getXPos)
.attr("r", itemHeight/2)
.attr("height", itemHeight)
.style("fill", queColor)
.on("mousemove", function (d, i) {
hover(d, index, datum);
})
.on("click", function (d, i) {
click(d, index, datum);
})
;
// add the label
if (hasLabel) {
gParent.append('text')
.attr("class", "timeline-label")
.attr("transform", "translate("+ 0 +","+ (itemHeight/2 + margin.top + (itemHeight + itemMargin) * yAxisMapping[index])+")")
.text(hasLabel ? datum.label : datum.id);
}
if (typeof(datum.icon) != "undefined") {
gParent.append('image')
.attr("class", "timeline-label")
.attr("transform", "translate("+ 0 +","+ (margin.top + (itemHeight + itemMargin) * yAxisMapping[index])+")")
.attr("xlink:href", datum.icon)
.attr("width", margin.left)
.attr("height", itemHeight);
}
function getStackPosition(d, i) {
if (stacked) {
return margin.top + (itemHeight + itemMargin) * yAxisMapping[index];
}
return margin.top;
}
});
});
if (width > gParentSize.width) {
var zoom = d3.behavior.zoom().x(xScale).on("zoom", move);
function move() {
var x = Math.min(0, Math.max(gParentSize.width - width, d3.event.translate[0]));
zoom.translate([x, 0]);
g.attr("transform", "translate(" + x + ",0)");
scroll(x*scaleFactor, xScale);
}
gParent
.attr("class", "scrollable")
.call(zoom);
}
if (rotateTicks) {
g.selectAll("text")
.attr("transform", function(d) {
return "rotate(" + rotateTicks + ")translate("
+ (this.getBBox().width/2+10) + "," // TODO: change this 10
+ this.getBBox().height/2 + ")";
});
}
var gSize = g[0][0].getBoundingClientRect();
setHeight();
function getXPos(d, i) {
return margin.left + (d.starting_time - beginning) * scaleFactor;
}
function setHeight() {
if (!height && !gParentItem.attr("height")) {
if (itemHeight) {
// set height based off of item height
height = gSize.height + gSize.top - gParentSize.top;
// set bounding rectangle height
d3.select(gParent[0][0]).attr("height", height);
} else {
throw "height of the timeline is not set";
}
} else {
if (!height) {
height = gParentItem.attr("height");
} else {
gParentItem.attr("height", height);
}
}
}
function setWidth() {
if (!width && !gParentSize.width) {
throw "width of the timeline is not set";
} else if (!(width && gParentSize.width)) {
if (!width) {
width = gParentItem.attr("width");
} else {
gParentItem.attr("width", width);
}
}
// if both are set, do nothing
}
}
timeline.margin = function (p) {
if (!arguments.length) return margin;
margin = p;
return timeline;
}
timeline.orient = function (orientation) {
if (!arguments.length) return orient;
orient = orientation;
return timeline;
};
timeline.itemHeight = function (h) {
if (!arguments.length) return itemHeight;
itemHeight = h;
return timeline;
}
timeline.itemMargin = function (h) {
if (!arguments.length) return itemMargin;
itemMargin = h;
return timeline;
}
timeline.height = function (h) {
if (!arguments.length) return height;
height = h;
return timeline;
};
timeline.width = function (w) {
if (!arguments.length) return width;
width = w;
return timeline;
};
timeline.display = function (displayType) {
if (!arguments.length || (DISPLAY_TYPES.indexOf(displayType) == -1)) return display;
display = displayType;
return timeline;
};
timeline.tickFormat = function (format) {
if (!arguments.length) return tickFormat;
tickFormat = format;
return timeline;
};
timeline.hover = function (hoverFunc) {
if (!arguments.length) return hover;
hover = hoverFunc;
return timeline;
};
timeline.click = function (clickFunc) {
if (!arguments.length) return click;
click = clickFunc;
return timeline;
};
timeline.scroll = function (scrollFunc) {
if (!arguments.length) return scroll;
scroll = scrollFunc;
return timeline;
}
timeline.colors = function (colorFormat) {
if (!arguments.length) return colorCycle;
colorCycle = colorFormat;
return timeline;
};
timeline.beginning = function (b) {
if (!arguments.length) return beginning;
beginning = b;
return timeline;
};
timeline.ending = function (e) {
if (!arguments.length) return ending;
ending = e;
return timeline;
};
timeline.rotateTicks = function (degrees) {
rotateTicks = degrees;
return timeline;
}
timeline.stack = function () {
stacked = !stacked;
return timeline;
};
return timeline;
};
})();