HTMLcopy
1
<button id="collapseAllButton" onclick="collapseAll()">Collapse All</button>
2
<button id="expandAllButton" onclick="expandAll()">Expand All</button>
3
<button id="collapseTaskButton" onclick="collapseTask()">Collapse Task</button>
4
<button id="expandTaskButton" onclick="expandTask()">Expand Task</button>
5
<label for="idSelect">id: </label>
6
<select id="idSelect" onchange="updateNameLabel()">
7
<option value="1">1</option>
8
<option value="2" selected>2</option>
9
</select>
10
<label id="nameLabel">(PR Campaign)</label>
11
<div id="container"></div>
CSScopy
19
1
html, body {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
7
button {
8
margin: 10px 0 0 10px;
9
}
10
label {
11
display: inline-block;
12
margin: 10px 0 0 10px;
13
}
14
#container {
15
position: absolute;
16
width: 100%;
17
top: 40px;
18
bottom: 0;
19
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create data
4
var data = [
5
{
6
id: "1",
7
name: "Development",
8
actualStart: "2018-01-25",
9
actualEnd: "2018-03-10",
10
children: [
11
{
12
id: "1_2",
13
name: "Analysis",
14
actualStart: "2018-01-25",
15
actualEnd: "2018-02-08"
16
},
17
{
18
id: "1_3",
19
name: "Design",
20
actualStart: "2018-02-04",
21
actualEnd: "2018-02-14"
22
},
23
{
24
id: "1_4",
25
name: "Meeting",
26
actualStart: "2018-02-15",
27
actualEnd: "2018-02-15"
28
},
29
{
30
id: "1_5",
31
name: "Implementation",
32
actualStart: "2018-02-15",
33
actualEnd: "2018-02-27"
34
},
35
{
36
id: "1_6",
37
name: "Testing",
38
actualStart: "2018-02-28",
39
actualEnd: "2018-03-10"
40
}
41
]},
42
{
43
id: "2",
44
name: "PR Campaign",
45
actualStart: "2018-02-15",
46
actualEnd: "2018-03-22",
47
children: [
48
{
49
id: "2_1",
50
name: "Planning",
51
actualStart: "2018-02-15",
52
actualEnd: "2018-03-10"
53
},
54
{
55
id: "2_2",
56
name: "Promoting",
57
actualStart: "2018-03-11",
58
actualEnd: "2018-03-22"
59
}
60
]}
61
];
62
63
// create a data tree
64
treeData = anychart.data.tree(data, "as-tree");
65
66
// create a chart
67
chart = anychart.ganttProject();
68
69
// set the data
70
chart.data(treeData);
71
72
// configure the scale
73
chart.getTimeline().scale().maximum("2018-03-27");
74
75
// set the container id
76
chart.container("container");
77
78
// initiate drawing the chart
79
chart.draw();
80
81
// fit elements to the width of the timeline
82
chart.fitAll();
83
});
84
85
// collapse all tasks
86
function collapseAll() {
87
chart.collapseAll();
88
}
89
90
// expand all tasks
91
function expandAll() {
92
chart.expandAll();
93
}
94
95
// collapse the given task
96
function collapseTask() {
97
var id = document.getElementById("idSelect").value;
98
chart.collapseTask(id);
99
}
100
101
// collapse the given task
102
function expandTask() {
103
var id = document.getElementById("idSelect").value;
104
chart.expandTask(id);
105
}
106
107
// update the text of the label
108
function updateNameLabel() {
109
var label = document.getElementById("nameLabel");
110
var id = document.getElementById("idSelect").value;
111
var item = treeData.search("id", id);
112
var itemName = item.get("name");
113
label.innerHTML = "(" + itemName + ")";
114
}