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 and return simple linear gauge
3
function drawGauge(value, settings, inverse) {
4
// Create gauge with settings
5
var gauge = anychart.gauges.linear();
6
gauge.data([value, settings.critical]);
7
gauge.layout('horizontal').padding(0).margin([0, 20, 0, 20]);
8
9
// Set scale for gauge
10
var scale = anychart.scales.linear();
11
scale.minimum(0).maximum(settings.maximum).ticks({ interval: 1 });
12
13
// Set axis for gauge
14
var axis = gauge.axis(0);
15
axis.width('1%').offset('43%').scale(scale).orientation('bottom');
16
axis.labels().format('{%Value} m');
17
18
// Create and set bar point
19
var barSeries = gauge.bar(0);
20
barSeries
21
.offset('3%')
22
.scale(scale)
23
.width('3%')
24
.fill('#545f69')
25
.stroke('#545f69');
26
27
// Create and set LED point
28
var ledPointer = gauge.led(1);
29
ledPointer
30
.offset('10%')
31
.width('30%')
32
.size(null)
33
.count(settings.count)
34
.scale(scale)
35
.gap(1)
36
.dimmer(function () {
37
return '#64b5f6';
38
});
39
ledPointer.colorScale().colors(['#ef6c00', '#ef6c00']);
40
41
if (inverse) {
42
ledPointer.dimmer(function () {
43
return '#ef6c00';
44
});
45
ledPointer.colorScale().colors(['#64b5f6', '#64b5f6']);
46
}
47
48
// Create and set label with actual data
49
var labelBar = barSeries.labels();
50
labelBar
51
.enabled(true)
52
.position('center')
53
.offsetY('-20px')
54
.fontColor('#455a64')
55
.fontSize('21px')
56
.format('${%Value} millions');
57
58
// Set gauge tooltip
59
gauge
60
.tooltip()
61
.useHtml(true)
62
.titleFormat('${%Value} millions')
63
.format(
64
'Acceptable: ${%Value} millions<br/>' +
65
'Maximum on scale: $' +
66
settings.maximum +
67
' millions'
68
);
69
70
return gauge;
71
}
72
73
// Create gauges
74
var revenueGauge = drawGauge(12.45, {
75
maximum: 20,
76
critical: 10,
77
count: 20
78
});
79
var expensesGauge = drawGauge(
80
8.86,
81
{ maximum: 10, critical: 8, count: 10 },
82
true
83
);
84
var profitGauge = drawGauge(3.5, { maximum: 5, critical: 3, count: 5 });
85
86
// Create stand alone legend
87
var legend = anychart.standalones.legend();
88
legend
89
.position('left')
90
.fontSize(14)
91
.items([
92
{ text: 'Actual', iconFill: '#545f69' },
93
{ text: 'Acceptable', iconFill: '#64b5f6' },
94
{ text: 'Critical', iconFill: '#ef6c00' }
95
]);
96
97
// Create table to place gauges
98
var layoutTable = anychart.standalones.table();
99
layoutTable
100
.hAlign('right')
101
.vAlign('middle')
102
.useHtml(true)
103
.fontSize(16)
104
.cellBorder(null);
105
106
// Put gauges into the layout table
107
layoutTable.contents([
108
[null, 'Key Metrics (Brick = $1 million)'],
109
[null, legend],
110
['Revenue', revenueGauge],
111
['Expenses', expensesGauge],
112
['Profit', profitGauge]
113
]);
114
115
// Set height for first row in layout table
116
layoutTable
117
.getRow(0)
118
.height(50)
119
.fontSize(22)
120
.hAlign('left')
121
.fontColor('#212121');
122
layoutTable.getRow(1).height(40).fontSize(16);
123
layoutTable.getCol(0).width(100);
124
125
// Set container id and initiate drawing
126
layoutTable.container('container');
127
layoutTable.draw();
128
});