-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
315 lines (262 loc) · 9.36 KB
/
main.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
311
312
313
314
315
var Bar = require('./lib/bar');
var DateCalculator = require('./lib/date.calculator');
var DataHelper = require('./lib/data.helper');
var Drag = require('./lib/drag');
var Scales = require('./lib/scales');
var Tooltip = require('./lib/tooltip');
function noop () {}
function TimelineChart (element, data, opts) {
function initialize (element, events, opts) {
opts = opts || {};
opts.minDate = opts.minDate || DateCalculator.minDate(events);
opts.maxDate = opts.maxDate || DateCalculator.maxDate(events);
opts.leftPad = opts.leftPad || 80;
opts.barHeight = opts.barHeight || 25;
opts.barPadding = opts.barPadding || 5;
opts.barRoundSize = opts.barRoundSize || 10;
opts.xAxisHeight = opts.xAxisHeight || 60;
opts.margin = { top: 200, right: 40, bottom: 200, left: 40 };
opts.width = element.clientWidth - opts.margin.left - opts.margin.right;
opts.onBarClicked = opts.onBarClicked || noop;
opts.onBarChanged = opts.onBarChanged || noop;
opts.onBarEdited = opts.onBarEdited || noop;
opts.onBarCreated = opts.onBarCreated || noop;
opts.onBrush = opts.onBrush || noop;
opts.onBrushEnd = opts.onBrushEnd || noop;
return opts;
}
opts = initialize(element, data.events, opts);
var labels = DataHelper.labels(data);
var events = data.events;
var brush = d3.svg.brush();
var chartHeight = labels.length * opts.barHeight;
var svgHeight = chartHeight + opts.xAxisHeight;
var baseSVG = d3.select(element)
.append('svg')
.attr('id', 'selectable-gantt-chart')
.attr('width', opts.width)
.attr('height', svgHeight);
var chartData = baseSVG.append('g').attr('id', 'chart-data');
var timeScale = d3.time
.scale()
.domain([opts.minDate, opts.maxDate])
.range([opts.leftPad, opts.width])
.clamp(true);
var labelsScale = d3.scale
.ordinal()
.domain(labels)
.rangeRoundBands([1, chartHeight]);
var scales = new Scales(timeScale, labelsScale, opts.barPadding);
function getBrushedLabel () {
var y = d3.mouse(d3.select('g.brush').node())[1];
var domain = labelsScale.domain();
var range = labelsScale.range();
return domain[d3.bisect(range, y) - 1];
}
function isBarClicked (obj) {
return obj.label === getBrushedLabel();
}
function updateBarSelection (brushStart, brushEnd) {
var rects = d3.selectAll('rect.bar');
rects.each(function (bar) {
bar.selected = false;
function brushStartInsideBar () {
return brushStart >= bar.startedAt && brushStart <= bar.endedAt;
}
function brushEndInsideBar () {
return brushEnd >= bar.startedAt && brushEnd <= bar.endedAt;
}
function barInsideBrush () {
return brushStart <= bar.startedAt && brushEnd >= bar.endedAt;
}
if (brushStartInsideBar() || brushEndInsideBar() || barInsideBrush()) {
if (brush.empty()) {
bar.selected = isBarClicked(bar);
} else {
bar.selected = true;
}
}
});
rects.classed('selected', function (bar) {
return bar.selected;
});
}
function updateCircleSelection (brushStart, brushEnd) {
var circles = d3.selectAll('circle.instance');
circles.each(function (circle) {
if (brush.empty()) {
// NOTE: do not allow "clicking" on an instance for now
circle.selected = false;
} else {
circle.selected = circle.at >= brushStart && circle.at <= brushEnd;
}
});
circles.classed('selected', function (circle) {
return circle.selected;
});
}
function brushed () {
var timeRange = brush.extent();
var brushStart = Math.floor(timeRange[0].getTime() / 1000);
var brushEnd = Math.floor(timeRange[1].getTime() / 1000);
updateBarSelection(brushStart, brushEnd);
updateCircleSelection(brushStart, brushEnd);
if (!brush.empty()) {
var selection = d3.selectAll('.selected');
opts.onBrush(timeRange, selection.data());
}
}
function brushEnded () {
if (brush.empty()) {
var selection = d3.selectAll('.selected');
if (selection.empty()) {
var label = getBrushedLabel();
if (!DataHelper.isEditable(label, data)) { return false; }
var barAttrs = {
startedAt: brush.extent()[0].getTime() / 1000,
endedAt: brush.extent()[1].getTime() / 1000 + 600,
label: label
};
var newBar = opts.onBarCreated(barAttrs);
events.push(newBar);
renderEvents(chartDataGroup, events);
rectClicked(newBar);
}
} else {
opts.onBrushEnd(brush.extent(), d3.selectAll('.selected').data());
}
}
function rectClicked (d) {
var rects = d3.selectAll('rect.bar');
rects.each(function (bar) {
bar.selected = bar === d;
});
rects.classed('selected', function (bar) {
return bar.selected;
});
Drag.disable();
removeBrush();
if (DataHelper.isEditable(d.label, data)) {
Drag.enable(chartData, opts, scales, d, events);
}
opts.onBarClicked(new Bar(d));
}
function addBrush () {
brush.x(timeScale)
.on('brush', brushed)
.on('brushend', brushEnded);
d3.select('#selectable-gantt-chart').insert('g', ':first-child')
.attr('class', 'brush')
.attr('opacity', '.3')
.call(brush)
.selectAll('rect')
.attr('height', chartHeight);
}
function removeBrush () {
brush.x(timeScale)
.on('brush', null)
.on('brushend', null);
var brushSelection = d3.select('#selectable-gantt-chart .brush');
brushSelection.call(brush.clear());
brushSelection.remove();
}
this.deleteSelectedBar = function deleteSelectedBar () {
var rects = d3.selectAll('rect.bar');
var selectedBar = rects.filter(function (bar) { return bar.selected; }).remove();
};
this.clearBrush = function clearBrush() {
d3.selectAll('.selected').classed('selected', false);
var brushSelection = d3.selectAll('#selectable-gantt-chart .brush');
brushSelection.call(brush.clear());
if (brushSelection.empty()) { addBrush(); }
Drag.disable();
};
function renderEvents (chartDataGroup, events) {
function intervals (d) {
return d.hasOwnProperty('startedAt');
}
function instances (d) {
return d.hasOwnProperty('at');
}
function rectClass (d) {
return 'bar ' + (DataHelper.isEditable(d.label, data) ? 'editable' : 'readonly');
}
var tip = Tooltip.create();
baseSVG.call(tip);
function propagateEventToBrush () {
var brushNode = baseSVG.select('.brush').node();
if (brushNode) {
var fakeMouseEvent = new Event('mousedown');
fakeMouseEvent.pageX = d3.event.pageX;
fakeMouseEvent.clientX = d3.event.clientX;
fakeMouseEvent.pageY = d3.event.pageY;
fakeMouseEvent.clientY = d3.event.clientY;
brushNode.dispatchEvent(fakeMouseEvent);
}
}
chartDataGroup
.selectAll('rect')
.data(events.filter(intervals))
.enter()
.append('rect')
.on('mousedown', propagateEventToBrush)
.on('click', rectClicked)
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.attr('class', rectClass)
.attr('x', function (d) {
return timeScale(new Date(d.startedAt * 1000));
})
.attr('y', scales.computeBarHeight)
.attr('rx', opts.barRoundSize)
.attr('ry', opts.barRoundSize)
.attr('height', opts.barHeight - (opts.barPadding * 2))
.attr('width', scales.computeBarWidth);
chartDataGroup
.selectAll('circle')
.data(events.filter(instances))
.enter()
.append('circle')
.on('mouseover', tip.show)
.on('mouseout', tip.hide)
.on('mousedown', propagateEventToBrush)
.attr('class', 'instance')
.attr('cx', function (d) {
return timeScale(new Date(d.at * 1000));
})
.attr('cy', function (d) {
return labelsScale(d.label) + opts.barHeight / 2;
})
.attr('r', 2);
}
function createChart (element, events, opts) {
var xAxisOffset = chartHeight + 10;
var xAxis = d3.svg.axis()
.ticks(d3.time.hours, 1)
.scale(timeScale)
.tickSize(xAxisOffset * -1, 0, 0);
var yAxis = d3.svg.axis()
.tickPadding([10])
.orient('right')
.scale(labelsScale);
addBrush();
chartData.append('g')
.attr('class', 'xaxis')
.attr('transform', 'translate(0,' + xAxisOffset + ')')
.call(xAxis);
chartData.append('g')
.attr('class', 'yaxis')
.attr('transform', 'translate(0, 0)')
.call(yAxis);
chartData.selectAll('.yaxis line')
.attr('stroke', 'black')
.attr('x1', 0)
.attr('x2', opts.width)
.attr('y1', opts.barHeight / 2)
.attr('y2', opts.barHeight / 2);
return chartData.append('g').attr('height', chartHeight);
}
var chartDataGroup = createChart(element, events, opts);
renderEvents(chartDataGroup, events);
}
module.exports = TimelineChart;