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
// set input dateTime format
3
anychart.format.inputDateTimeFormat('yyyy-MM-dd HH:mm');
4
5
// create data sets
6
var activeDataSet = anychart.data.set(activeData());
7
8
var idleDataSet = anychart.data.set(idleData());
9
10
var errorDataSet = anychart.data.set(errorData());
11
12
// map the data
13
var activeMapping = activeDataSet.mapAs({
14
x: 0,
15
low: 1,
16
high: 2
17
});
18
var idleMapping = idleDataSet.mapAs({
19
x: 0,
20
low: 1,
21
high: 2
22
});
23
var errorMapping = errorDataSet.mapAs({
24
x: 0,
25
low: 1,
26
high: 2
27
});
28
29
// get colors from theme
30
// !!! When you change chart theme, colors may not match the color coding of the chart
31
// For Monochrome theme idle and error colors have fallback, because theme contains only 5 colors
32
var themeColors = anychart.theme().length
33
? anychart.theme()[0].palette.items
34
: anychart.palettes.defaultPalette;
35
var activeColor = themeColors[0];
36
var idleColor = themeColors[5] || '#a5a5a5';
37
var errorColor = themeColors[6] || '#ff3e29';
38
39
// create a chart
40
var chart = anychart.bar();
41
42
// set formatter for the chart tooltip's title
43
chart.tooltip().titleFormat(function () {
44
return this.x + ' - ' + this.seriesName;
45
});
46
47
// set formatter for the chart tooltip's content
48
chart.tooltip().format(function () {
49
return 'From: ' + this.low + '\nTo: ' + this.high;
50
});
51
52
// create series with mapped data and set the series settings
53
chart
54
.rangeBar(activeMapping)
55
.xMode('scatter')
56
.name('Active')
57
.fill(activeColor)
58
.stroke(null);
59
60
chart
61
.rangeBar(idleMapping)
62
.xMode('scatter')
63
.name('Idle')
64
.fill(idleColor)
65
.stroke(null);
66
67
chart
68
.rangeBar(errorMapping)
69
.xMode('scatter')
70
.name('Error')
71
.fill(errorColor)
72
.stroke(null);
73
74
// set the padding between bars
75
chart.barsPadding(-1);
76
77
// create and adjust dateTime Y scale
78
var yScale = anychart.scales.dateTime();
79
chart.yScale(yScale);
80
chart.yScale().ticks().interval('h', 3);
81
82
// disable xAxis labels
83
chart.xAxis().labels(false);
84
85
// adjust Yaxis albels formatting
86
chart.yAxis().labels().format('{%tickValue}{dateTimeFormat:HH:mm}');
87
88
// enable Y grids
89
chart.yGrid().enabled(true);
90
91
// adjust grids appearance
92
chart.yGrid().stroke({
93
color: '#cecece',
94
dash: '10 5'
95
});
96
97
// enable chart legend
98
var legend = chart.legend();
99
legend.enabled(true);
100
101
// place the legend at the bottom of the chart
102
legend.position('bottom');
103
104
// disable legend item click
105
legend.listen('legendItemClick', function (event) {
106
event.preventDefault();
107
});
108
109
// set container and render the chart
110
chart.container('container').draw();
111
});
112
113
// functions, that return data for the chart's series
114
function activeData() {
115
return [
116
['Process 1', '2018-02-01 00:00', '2018-02-01 00:30'],
117
['Process 1', '2018-02-01 00:40', '2018-02-01 01:25'],
118
['Process 1', '2018-02-01 01:45', '2018-02-01 02:30'],
119
['Process 1', '2018-02-01 02:40', '2018-02-01 03:15'],
120
['Process 1', '2018-02-01 04:05', '2018-02-01 04:55'],
121
['Process 1', '2018-02-01 05:05', '2018-02-01 07:30'],
122
['Process 1', '2018-02-01 08:00', '2018-02-01 09:30'],
123
['Process 1', '2018-02-01 10:00', '2018-02-01 12:30'],
124
['Process 2', '2018-02-01 00:00', '2018-02-01 01:30'],
125
['Process 2', '2018-02-01 02:00', '2018-02-01 04:30'],
126
['Process 2', '2018-02-01 05:05', '2018-02-01 05:15'],
127
['Process 2', '2018-02-01 07:15', '2018-02-01 09:50'],
128
['Process 2', '2018-02-01 10:25', '2018-02-01 10:30'],
129
['Process 2', '2018-02-01 10:55', '2018-02-01 12:05']
130
];
131
}
132
133
function idleData() {
134
return [
135
['Process 1', '2018-02-01 00:30', '2018-02-01 00:35'],
136
['Process 1', '2018-02-01 01:25', '2018-02-01 01:45'],
137
['Process 1', '2018-02-01 03:15', '2018-02-01 04:05'],
138
['Process 1', '2018-02-01 07:30', '2018-02-01 08:00'],
139
['Process 1', '2018-02-01 09:45', '2018-02-01 10:00'],
140
['Process 2', '2018-02-01 01:30', '2018-02-01 01:55'],
141
['Process 2', '2018-02-01 04:30', '2018-02-01 05:05'],
142
['Process 2', '2018-02-01 05:25', '2018-02-01 07:15'],
143
['Process 2', '2018-02-01 10:05', '2018-02-01 10:25'],
144
['Process 2', '2018-02-01 10:35', '2018-02-01 10:55'],
145
['Process 2', '2018-02-01 12:10', '2018-02-01 12:30']
146
];
147
}
148
149
function errorData() {
150
return [
151
['Process 1', '2018-02-01 00:35', '2018-02-01 00:40'],
152
['Process 1', '2018-02-01 02:30', '2018-02-01 02:40'],
153
['Process 1', '2018-02-01 04:55', '2018-02-01 05:05'],
154
['Process 1', '2018-02-01 09:30', '2018-02-01 09:45'],
155
['Process 2', '2018-02-01 01:55', '2018-02-01 02:00'],
156
['Process 2', '2018-02-01 05:15', '2018-02-01 05:25'],
157
['Process 2', '2018-02-01 09:50', '2018-02-01 10:05'],
158
['Process 2', '2018-02-01 10:30', '2018-02-01 10:35'],
159
['Process 2', '2018-02-01 12:05', '2018-02-01 12:10']
160
];
161
}