-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgantt-chart-d3.ts
274 lines (235 loc) · 7.27 KB
/
gantt-chart-d3.ts
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
/**
* @author Dimitry Kudrayvtsev
* @version 2.1
*/
class Task {
constructor(
public startDate: Date,
public endDate: Date,
public taskName: string,
public taskType: string,
public status: string
){}
}
enum TimeDomainMode {
Fit,
Fixed
}
class gantt {
constructor(){}
private _margin = {
top : 20,
right : 100,
bottom : 20,
left : 100
};
private _selector = 'body';
private timeDomainStart = d3.timeDay.offset(new Date(),-3);
private timeDomainEnd = d3.timeHour.offset(new Date(),+3);
private _timeDomainMode = TimeDomainMode.Fit;// fixed or fit
private _taskTypes = ["running"];
private _taskStatusMap = {};// {[status:string]: string;};
private _height = document.body.clientHeight - this._margin.top - this._margin.bottom;
private _width = document.body.clientWidth - this._margin.right - this._margin.left;
private _tickFormat = "%H:%M";
private keyFunction(d: Task) {
return d.startDate + d.taskType + d.endDate;
}
private x = d3.scaleTime()
.domain([ this.timeDomainStart, this.timeDomainEnd ])
.range([ 0, this._width ])
.clamp(true);
private y = d3.scaleBand()
.domain(this._taskTypes)
.range([ 0, this._height - this._margin.top - this._margin.bottom ])
.round(true);
private xAxis = d3.axisBottom(this.x)
.tickFormat(d3.timeFormat(this._tickFormat))
.tickSize(8).tickPadding(8);
private yAxis = d3.axisLeft(this.y)
.tickSize(0);
private initTimeDomain(tasks: Task[]) {
if (this._timeDomainMode == TimeDomainMode.Fit) {
if (tasks === undefined || tasks.length < 1) {
this.timeDomainStart = d3.timeDay.offset(new Date(), -3);
this.timeDomainEnd = d3.timeHour.offset(new Date(), +3);
return;
}
tasks.sort(function(a, b) {
return a.endDate.valueOf() - b.endDate.valueOf();
});
this.timeDomainEnd = tasks[tasks.length - 1].endDate;
tasks.sort(function(a, b) {
return a.startDate.valueOf() - b.startDate.valueOf();
});
this.timeDomainStart = tasks[0].startDate;
}
}
private initAxis() {
this.x = d3.scaleTime()
.domain([ this.timeDomainStart, this.timeDomainEnd ])
.range([ 0, this._width ])
.clamp(true);
this.y = d3.scaleBand()
.domain(this._taskTypes)
.range([ 0, this._height - this._margin.top - this._margin.bottom ])
.round(true);
this.xAxis = d3.axisBottom(this.x)
.tickFormat(d3.timeFormat(this._tickFormat))
.tickSize(8).tickPadding(8);
this.yAxis = d3.axisLeft(this.y)
.tickSize(0);
}
private getTaskClass(task: Task): string {
return this._taskStatusMap[task.status];
}
public init(tasks: Task[]) {
this.initTimeDomain(tasks);
this.initAxis();
var g = this;
var svg = d3.select(this._selector)
.append("svg")
.attr("class", "chart")
.attr("width", this._width + this._margin.left + this._margin.right)
.attr("height", this._height + this._margin.top + this._margin.bottom)
.append("g")
.attr("class", "gantt-chart")
.attr("width", this._width + this._margin.left + this._margin.right)
.attr("height", this._height + this._margin.top + this._margin.bottom)
.attr("transform", "translate(" + this._margin.left + ", " + this._margin.top + ")");
var taskElements = svg.selectAll(".chart")
.data(tasks, this.keyFunction).enter()
.append("g")
.attr("transform", function(d) { return "translate(" + g.x(d.startDate.valueOf()) + "," + g.y(d.taskType) + ")"; });
taskElements
.append("rect")
.attr("rx", 5)
.attr("ry", 5)
.attr("class", function(d: Task){
if(g.getTaskClass(d) == null){
return "bar";
} else {
return g.getTaskClass(d);
}
})
.attr("y", 0)
.attr("height", function(d) { return g.y.bandwidth(); })
.attr("width", function(d) { return Math.max(1,(g.x(d.endDate) - g.x(d.startDate))); });
var fontsize=20;
taskElements
.append("text")
.attr("dx", fontsize*2)
.attr("dy", function(d) { return g.y.bandwidth() / 2 + fontsize/2;})
.attr("font-size", fontsize)
.attr("fill", "black")
.text(function(d: Task) {
return d.taskName;
});
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0, " + (this._height - this._margin.top - this._margin.bottom) + ")")
.call(this.xAxis);
svg.append("g")
.attr("class", "y axis")
.call(this.yAxis);
return this;
}
public redraw(tasks: Task[]) {
this.initTimeDomain(tasks);
this.initAxis();
var g = this;
var svg = d3.select(".chart");
var ganttChartGroup = svg.select(".gantt-chart");
var rect = ganttChartGroup.selectAll("rect").data(tasks, this.keyFunction);
rect.enter()
.insert("rect",":first-child")
.attr("rx", 5)
.attr("ry", 5)
.attr("class", function(d: Task){
if(g.getTaskClass(d) == null){
return "bar";
} else {
return g.getTaskClass(d);
}
})
.transition()
.attr("y", 0)
.attr("transform", function(d) { return "translate(" + g.x(d.startDate.valueOf()) + "," + g.y(d.taskType) + ")"; })
.attr("height", function(d) { return g.y.bandwidth(); })
.attr("width", function(d) {
return Math.max(1,(g.x(d.endDate) - g.x(d.startDate)));
});
rect.transition()
.attr("transform", function(d) { return "translate(" + g.x(d.startDate.valueOf()) + "," + g.y(d.taskType) + ")"; })
.attr("height", function(d) { return g.y.bandwidth(); })
.attr("width", function(d) {
return Math.max(1,(g.x(d.endDate) - g.x(d.startDate)));
});
rect.exit().remove();
svg.select(".x").transition();
//.call(xAxis);
svg.select(".y").transition();
//.call(yAxis);
return this;
}
public margin(value) {
if (!arguments.length)
return this._margin;
this._margin = value;
return this;
}
public timeDomain(value) {
if (!arguments.length)
return [ this.timeDomainStart, this.timeDomainEnd ];
this.timeDomainStart = value[0];
this.timeDomainEnd = value[1];
return this;
}
/**
* @param {string}
* vale The value can be "fit" - the domain fits the data or
* "fixed" - fixed domain.
*/
public timeDomainMode(value: TimeDomainMode = null) {
if (value == null)
return this._timeDomainMode;
this._timeDomainMode = value;
return this;
}
public taskTypes(value : string[] = null) {
if (value == null)
return this._taskTypes;
this._taskTypes = value;
return this;
}
public taskStatus(value: {[status:string]:string;} = null) {
if (value == null)
return this._taskStatusMap;
this._taskStatusMap = value;
return this;
}
public width(value: number = null) {
if (value == null)
return this._width;
this._width = value - this._margin.right - this._margin.left;
return this;
}
public height(value: number = null) {
if (value == null)
return this._height;
this._height = value;
return this;
}
public tickFormat(value: string = null) {
if (value == null)
return this._tickFormat;
this._tickFormat = value;
return this;
}
public selector(value: string = null) {
if (value == null)
return this._selector;
this._selector = value;
return this;
}
}