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
children: [
11
{
12
id: "1_1",
13
name: "Analysis",
14
actualStart: "2018-01-15",
15
actualEnd: "2018-02-25",
16
progressValue: "75%"
17
},
18
{
19
id: "1_2",
20
name: "Design",
21
actualStart: "2018-01-20",
22
actualEnd: "2018-02-04",
23
progressValue: "100%"
24
},
25
{
26
id: "1_3",
27
name: "Meeting",
28
actualStart: "2018-02-05",
29
actualEnd: "2018-02-05",
30
custom_field: "1_2"
31
},
32
{
33
id: "1_4",
34
name: "Implementation",
35
actualStart: "2018-02-05",
36
actualEnd: "2018-02-24",
37
progressValue: "60%"
38
},
39
{
40
id: "1_5",
41
name: "Testing",
42
actualStart: "2018-02-25",
43
actualEnd: "2018-03-10"
44
}
45
]}
46
];
47
48
// create a data tree
49
var treeData = anychart.data.tree(data, "as-tree");
50
51
// create a chart
52
var chart = anychart.ganttProject();
53
54
// set the data
55
chart.data(treeData);
56
57
// access the timeline
58
var timeline = chart.getTimeline();
59
60
// configure labels of elements
61
timeline.elements().labels().fontWeight(600);
62
63
// configure labels of tasks
64
timeline.tasks().labels().useHtml(true);
65
timeline.tasks().labels().format(function() {
66
if (this.progress == 1) {
67
return "<span style='color:#64b5f6'>COMPLETE</span>";
68
} else {
69
return "<span style='color:#64b5f6'>" +
70
this.progress * 100 + "</span>%";
71
}
72
});
73
74
// configure labels of parent tasks
75
timeline.groupingTasks().labels().useHtml(true);
76
timeline.groupingTasks().labels().format(function() {
77
var duration = (this.actualEnd - this.actualStart) / 1000 / 3600 / 24;
78
return "<span style='color:#dd2c00'>" +
79
duration + "</span>d";
80
});
81
82
// configure labels of milestones
83
timeline.milestones().labels().useHtml(true);
84
timeline.milestones().labels().format(function() {
85
var relatedTaskId = this.getData("custom_field");
86
var relatedTaskItem = treeData.search("id", relatedTaskId);
87
var relatedTaskName = relatedTaskItem.get("name");
88
return "<span style='color:#ffa000'>Review:</span> " +
89
relatedTaskName;
90
});
91
92
// configure the scale
93
chart.getTimeline().scale().maximum("2018-03-15");
94
95
// set the container id
96
chart.container("container");
97
98
// initiate drawing the chart
99
chart.draw();
100
101
// fit elements to the width of the timeline
102
chart.fitAll();
103
});