HTMLcopy
1
<div id="container"></div>
CSScopy
8
1
html,
2
body,
3
#container {
4
width: 100%;
5
height: 100%;
6
margin: 0;
7
padding: 0;
8
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
// The data used in this sample can be obtained from the CDN
3
// https://cdn.anychart.com/samples/gantt-charts/server-status-list/data.json
4
anychart.data.loadJsonFile(
5
'https://cdn.anychart.com/samples/gantt-charts/server-status-list/_data.json',
6
function (data) {
7
// create data tree on our data
8
var treeData = anychart.data.tree(data, 'as-table');
9
10
// create project gantt chart
11
var chart = anychart.ganttResource();
12
13
// set data for the chart
14
chart.data(treeData);
15
16
// set start splitter position settings
17
chart.splitterPosition(320);
18
19
// get chart data grid link to set column settings
20
var dataGrid = chart.dataGrid();
21
22
// hide first column
23
dataGrid.column(0).enabled(false);
24
25
// get chart timeline
26
var timeLine = chart.getTimeline();
27
// set base fill
28
timeLine.elements().fill(function () {
29
// get status from data item
30
var status = this.period.status;
31
32
// create fill object
33
var fill = {
34
// if this element has children, then add opacity to it
35
opacity: this.item.numChildren() ? 1 : 0.6
36
};
37
38
// set fill color by status
39
switch (status) {
40
case 'online':
41
fill.color = 'green';
42
break;
43
case 'maintenance':
44
fill.color = 'orange';
45
break;
46
case 'offline':
47
fill.color = 'red';
48
break;
49
default:
50
}
51
52
return fill;
53
});
54
55
// set base stroke
56
timeLine.elements().stroke('none');
57
// set select fill
58
timeLine.elements().selected().fill('#ef6c00');
59
60
// set first column settings
61
var firstColumn = dataGrid.column(1);
62
firstColumn.labels().hAlign('left');
63
firstColumn
64
.title('Server')
65
.width(140)
66
.labelsOverrider(labelTextSettingsOverrider)
67
.labels()
68
.format(function () {
69
return this.name;
70
});
71
72
// set first column settings
73
var secondColumn = dataGrid.column(2);
74
secondColumn.labels().hAlign('right');
75
secondColumn
76
.title('Online')
77
.width(60)
78
.labelsOverrider(labelTextSettingsOverrider)
79
.labels()
80
.format(function () {
81
return this.item.get('online') || '';
82
});
83
84
// set first column settings
85
var thirdColumn = dataGrid.column(3);
86
thirdColumn.labels().hAlign('right');
87
thirdColumn
88
.title('Maintenance')
89
.width(60)
90
.labelsOverrider(labelTextSettingsOverrider)
91
.labels()
92
.format(function () {
93
return this.item.get('maintenance') || '';
94
});
95
96
// set first column settings
97
var fourthColumn = dataGrid.column(4);
98
fourthColumn.labels().hAlign('right');
99
fourthColumn
100
.title('Offline')
101
.width(60)
102
.labelsOverrider(labelTextSettingsOverrider)
103
.labels()
104
.format(function () {
105
return this.item.get('offline') || '';
106
});
107
108
// set container id for the chart
109
chart.container('container');
110
111
// initiate chart drawing
112
chart.draw();
113
114
// zoom chart to specified date
115
chart.zoomTo(
116
Date.UTC(2008, 0, 31, 1, 36),
117
Date.UTC(2008, 1, 15, 10, 3)
118
);
119
}
120
);
121
});
122
123
function labelTextSettingsOverrider(label, item) {
124
switch (item.get('status')) {
125
case 'online':
126
label.fontColor('green').fontWeight('bold');
127
break;
128
case 'maintenance':
129
label.fontColor('orange').fontWeight('bold');
130
break;
131
case 'offline':
132
label.fontColor('red').fontWeight('bold');
133
break;
134
default:
135
}
136
}