HTMLcopy
1
<button onclick="filterDuration()">Filter Items</button>
2
<label>duration >=<input id="valueInput" value="16"> day(s)</label>
3
<div id="container"></div>
CSScopy
22
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
margin: 10px 0 0 10px;
12
}
13
input {
14
width: 30px;
15
margin: 10px 0 0 5px;
16
}
17
#container {
18
position: absolute;
19
width: 100%;
20
top: 35px;
21
bottom: 0;
22
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// create data
4
data = [
5
{
6
id: "1",
7
name: "Root",
8
actualStart: Date.UTC(2018, 0, 15),
9
actualEnd: Date.UTC(2018, 2, 10),
10
actual: {},
11
children: [
12
{
13
id: "1_1",
14
name: "Child 1",
15
actualStart: Date.UTC(2018, 0, 15),
16
actualEnd: Date.UTC(2018, 0, 25)
17
},
18
{
19
id: "1_2",
20
name: "Child 2",
21
actualStart: Date.UTC(2018, 0, 20),
22
actualEnd: Date.UTC(2018, 1, 4)
23
},
24
{
25
id: "1_3",
26
name: "Child 3",
27
actualStart: Date.UTC(2018, 1, 5),
28
actualEnd: Date.UTC(2018, 1, 5)
29
},
30
{
31
id: "1_4",
32
name: "Child 4",
33
actualStart: Date.UTC(2018, 1, 5),
34
actualEnd: Date.UTC(2018, 1, 24)
35
},
36
{
37
id: "1_5",
38
name: "Child 5",
39
actualStart: Date.UTC(2018, 1, 25),
40
actualEnd: Date.UTC(2018, 2, 10)
41
}
42
]}
43
];
44
45
// create a chart
46
chart = anychart.ganttProject();
47
48
// set the data
49
chart.data(data, "as-tree");
50
51
// configure the scale
52
chart.getTimeline().scale().maximum(Date.UTC(2018, 2, 15));
53
54
// set the chart title
55
chart.title().useHtml(true);
56
chart.title("Tree Data Model: filter()<br><br>" +
57
"<span style = 'color:#990000'>?</span>");
58
chart.title().padding(10);
59
60
// set the container id
61
chart.container("container");
62
63
// initiate drawing the chart
64
chart.draw();
65
66
// fit elements to the width of the timeline
67
chart.fitAll();
68
});
69
70
71
// search for items with duration equal or greater than a given one
72
function filterDuration() {
73
74
// reset the colors of the chart
75
resetColors();
76
77
// get the chart data as an instance of anychart.data.Tree
78
var treeData = chart.data();
79
80
// search for items
81
var input = (document.getElementById("valueInput").value) * 1000 * 3600 * 24;
82
var items = treeData.filter(function(item) {
83
return item.get("actualEnd") - item.get("actualStart") >= input;
84
});
85
86
/* get the names of the found items, add them to a string variable,
87
and set the fill colors of their nodes */
88
var text = "";
89
if (items.length == 0) {
90
text = "(none)";
91
} else {
92
for (var i = 0; i < items.length; i++) {
93
text += items[i].get("name") + ", ";
94
items[i].set("actual", {fill: "#dd2c00"});
95
}
96
text = text.substr(0, text.length - 2);
97
}
98
99
// update the chart title
100
chart.title("Tree Data Model: filter()<br><br><span style = 'color:#990000'>" +
101
text + "</span>");
102
103
}
104
105
// reset the colors of the chart
106
function resetColors() {
107
chart.data(data, "as-tree");
108
chart.fitAll();
109
}