HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create data
4
var data = [
5
{
6
id: "1",
7
name: "Development",
8
actualStart: "2018-01-15",
9
actualEnd: "2018-03-10",
10
manager: "J. Doe",
11
children: [
12
{
13
id: "1_1",
14
name: "Analysis",
15
actualStart: "2018-01-15",
16
actualEnd: "2018-01-25",
17
progressValue: "75%"
18
},
19
{
20
id: "1_2",
21
name: "Design",
22
actualStart: "2018-01-20",
23
actualEnd: "2018-02-04",
24
progressValue: "100%",
25
custom_field: "!"
26
},
27
{
28
id: "1_3",
29
name: "Meeting",
30
actualStart: "2018-02-05",
31
actualEnd: "2018-02-05",
32
custom_field: "!"
33
},
34
{
35
id: "1_4",
36
name: "Implementation",
37
actualStart: "2018-02-05",
38
actualEnd: "2018-02-24",
39
progressValue: "60%"
40
},
41
{
42
id: "1_5",
43
name: "Testing",
44
actualStart: "2018-02-25",
45
actualEnd: "2018-03-10",
46
custom_field: "?"
47
}
48
]}
49
];
50
51
// create a data tree
52
var treeData = anychart.data.tree(data, "as-tree");
53
54
// create a chart
55
var chart = anychart.ganttProject();
56
57
// set the data
58
chart.data(treeData);
59
60
// set the text of the first data grid column
61
62
var column_1 = chart.dataGrid().column(0);
63
column_1.labels().fontWeight(600);
64
column_1.labels().useHtml(true);
65
column_1.labels().format(function() {
66
67
var children = this.item.numChildren();
68
var duration = this.actualEnd - this.actualStart;
69
var index = this.linearIndex;
70
71
var parentText = "<span style='color:#dd2c00'>" + index + ".</span>";
72
var milestoneText = "<span style='color:#ffa000'>" + index + ".</span>";
73
var taskText = "<span style='color:#64b5f6'>" + index + ".</span>";
74
75
// identify the task type and display the corresponding text
76
if (children > 0) {
77
return parentText;
78
} else {
79
if (duration == 0) {
80
return milestoneText;
81
} else {
82
return taskText;
83
}
84
}
85
86
});
87
88
// set the text of the second data grid column
89
90
var column_2 = chart.dataGrid().column(1);
91
column_2.labels().useHtml(true);
92
93
column_2.labels().format(function() {
94
95
var numChildren = this.item.numChildren();
96
var duration = (this.actualEnd - this.actualStart) / 1000 / 3600 / 24;
97
var progress = this.progress * 100 + "%";
98
var customField = "";
99
if (this.getData("custom_field")) {
100
customField = "<span style='font-weight:bold'>" +
101
this.getData("custom_field") + " </span>";
102
}
103
104
var parentText = "<span style='color:#dd2c00;font-weight:bold'>" +
105
this.name + ": " + duration + "d</span>";
106
var milestoneText = "<span style='color:#ffa000;font-weight:bold'>" +
107
customField + this.name + "</span";
108
var taskText = "<span style='color:#64b5f6'>" + customField +
109
this.name + ": " + progress + "</span>";
110
111
// identify the task type and display the corresponding text
112
if (numChildren > 0) {
113
return parentText;
114
} else {
115
if (duration == 0) {
116
return milestoneText;
117
} else {
118
return taskText;
119
}
120
}
121
122
});
123
124
// configure the scale
125
chart.getTimeline().scale().maximum("2018-03-20");
126
127
// set the container id
128
chart.container("container");
129
130
// initiate drawing the chart
131
chart.draw();
132
133
// fit elements to the width of the timeline
134
chart.fitAll();
135
});