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
// Create table to place gauges
3
var layoutTable = anychart.standalones.table();
4
layoutTable
5
.hAlign('center')
6
.vAlign('middle')
7
.useHtml(true)
8
.fontSize(16)
9
.cellBorder(null);
10
11
// Put gauges into the layout table
12
layoutTable.contents([
13
[null, 'Lemonade recipes', null, null, null],
14
[
15
null,
16
'Quick Lemonade',
17
'Strawberry Lemonade',
18
'Ginger Lemonade',
19
null
20
],
21
[
22
null,
23
createGauge([
24
{
25
name: 'Water',
26
volume: '1500',
27
low: '0',
28
high: '1500'
29
},
30
{
31
name: 'Lemon',
32
volume: '160',
33
low: '1500',
34
high: '1660'
35
},
36
{
37
name: 'Sugar',
38
volume: '350',
39
low: '1660',
40
high: '2010'
41
}
42
]),
43
createGauge([
44
{
45
name: 'Water',
46
volume: '1250',
47
low: '0',
48
high: '1250'
49
},
50
{
51
name: 'Lemon',
52
volume: '120',
53
low: '1250',
54
high: '1370'
55
},
56
{
57
name: 'Sugar',
58
volume: '250',
59
low: '1370',
60
high: '1620'
61
},
62
{
63
name: 'Strawberry',
64
volume: '400',
65
low: '1620',
66
high: '2020'
67
}
68
]),
69
createGauge([
70
{
71
name: 'Water',
72
volume: '2000',
73
low: '0',
74
high: '2000'
75
},
76
{
77
name: 'Lemon',
78
volume: '160',
79
low: '2000',
80
high: '2160'
81
},
82
{
83
name: 'Sugar',
84
volume: '150',
85
low: '2160',
86
high: '2310'
87
},
88
{
89
name: 'Ginger',
90
volume: '50',
91
low: '2310',
92
high: '2360'
93
}
94
]),
95
null
96
]
97
]);
98
99
// Set height for first row in layout table
100
layoutTable.getCol(0).width('10%');
101
layoutTable.getCol(4).width('10%');
102
layoutTable
103
.getRow(0)
104
.height(30)
105
.fontSize(16)
106
.fontColor('#212121')
107
.vAlign('bottom');
108
layoutTable.getRow(1).height(30).fontSize(13);
109
layoutTable.getCell(1, 1).border().bottom('3 #EAEAEA');
110
layoutTable.getCell(1, 2).border().bottom('3 #EAEAEA');
111
layoutTable.getCell(1, 3).border().bottom('3 #EAEAEA');
112
113
// Merge cells in layout table where needed
114
layoutTable.getCell(0, 1).colSpan(3).border().bottom('3 #EAEAEA');
115
116
// Set container id and initiate drawing
117
layoutTable.container('container');
118
layoutTable.draw();
119
120
// Helper function to define color
121
function defineColor(name) {
122
switch (name) {
123
case 'Water':
124
return '#64b5f6';
125
case 'Lemon':
126
return '#ffd740';
127
case 'Sugar':
128
return '#eeeeee';
129
case 'Strawberry':
130
return '#f48fb1';
131
case 'Ginger':
132
return '#ffccbc';
133
default:
134
}
135
}
136
137
// Helper function to create gauge
138
function createGauge(data) {
139
// Create new gauge
140
var gauge = anychart.gauges.linear();
141
gauge.data(anychart.data.set(data).mapAs({ value: 'high' }));
142
gauge.padding([0, 0, 15, 0]).tooltip(false);
143
gauge.scale().maximumGap(0.05).ticks([]);
144
145
// Create bar and marker points for gauge
146
for (var i = 0; i < data.length; i++) {
147
// Create bar point
148
var bar = gauge.rangeBar(i);
149
bar.width('30%').color(defineColor(data[i].name));
150
151
// Set label for bar point
152
bar
153
.labels()
154
.enabled(true)
155
.position('center')
156
.fontColor('#212121')
157
.anchor('center')
158
.format(function () {
159
return this.getData('volume') + ' ml.';
160
});
161
bar
162
.hovered()
163
.labels()
164
.enabled(true)
165
.fontColor('#212121')
166
.fontWeight('600');
167
bar
168
.selected()
169
.labels()
170
.enabled(true)
171
.fontColor('#212121')
172
.fontWeight('600');
173
174
// Create marker point
175
var marker = gauge.marker(i);
176
marker.color('#545f69').offset('31%').type('triangle-left');
177
178
// Set label for marker point
179
marker
180
.labels()
181
.enabled(true)
182
.position('right-center')
183
.anchor('left-center')
184
.format('{%Name}');
185
marker
186
.hovered()
187
.labels()
188
.enabled(true)
189
.fontColor('#212121')
190
.fontWeight('600');
191
marker
192
.selected()
193
.labels()
194
.enabled(true)
195
.fontColor('#212121')
196
.fontWeight('600');
197
}
198
199
return gauge;
200
}
201
});