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 drawLinearGauge(value) {
4
var gauge = anychart.gauges.linear();
5
gauge.data([value]).padding(10, 0, 30, 0);
6
gauge
7
.tooltip()
8
.useHtml(true)
9
.format(function () {
10
switch (this.pointer.id()) {
11
case '0':
12
return this.value + '°C';
13
case '1':
14
return (
15
this.value +
16
'°C' +
17
' (' +
18
(this.value * 1.8 + 32).toFixed(1) +
19
'°F)'
20
);
21
default:
22
}
23
});
24
25
// Set scale settings
26
var scale = gauge.scale();
27
scale.minimum(-25).maximum(25).ticks({ interval: 5 });
28
29
// Set axis and axis settings
30
var axis = gauge.axis();
31
axis.scale(scale).width('0.5%').offset('-1%');
32
33
// Set text formatter for axis labels
34
axis.labels().useHtml(true).format('{%Value}°');
35
36
return gauge;
37
}
38
39
// Create simple gauge
40
var simpleGauge = drawLinearGauge(12);
41
var simpleThermometer = simpleGauge.thermometer(0);
42
43
// Set simple thermometer settings
44
simpleThermometer
45
.name('Thermometer')
46
.id('0')
47
.fill('#64b5f6')
48
.stroke('#64b5f6');
49
50
// Create gauge with extra axis
51
var multiAxisGauge = drawLinearGauge(12);
52
var multiAxisThermometer = multiAxisGauge.thermometer(0);
53
multiAxisThermometer.name('Thermometer').id('1');
54
55
// Add left axis with custom labels
56
var axisLeft = multiAxisGauge.axis(0);
57
axisLeft.minorTicks(true);
58
axisLeft
59
.labels()
60
.useHtml(true)
61
.format(function () {
62
if (this.value > 0) {
63
return (
64
'<span style="color:#dd2c00;">' + this.value + '°</span>'
65
);
66
}
67
return (
68
'<span style="color: #1976d2;">' + this.value + '°</span>'
69
);
70
});
71
72
// Add custom right axis with custom labels
73
var axisRight = multiAxisGauge.axis(1);
74
axisRight.minorTicks(true);
75
axisRight
76
.labels()
77
.useHtml(true)
78
.format(function () {
79
if (this.value > 32) {
80
return '<span style="color:#dd2c00;">' + this.value + 'F</span>';
81
}
82
return '<span style="color: #1976d2;">' + this.value + 'F</span>';
83
});
84
axisRight.orientation('right').offset('3.5%');
85
86
// Set scale Fahrenheit for right axis
87
var Fscale = anychart.scales.linear();
88
Fscale.minimum(-13).maximum(77).ticks({ interval: 5 });
89
axisRight.scale(Fscale);
90
91
// Create table to place thermometers
92
var layoutTable = anychart.standalones.table();
93
layoutTable
94
.hAlign('center')
95
.vAlign('middle')
96
.useHtml(true)
97
.fontSize(16)
98
.cellBorder(null);
99
100
// Put thermometers into the layout table
101
layoutTable.contents([
102
['Thermometer Samples', null],
103
[
104
'Simple Thermometer',
105
'Thermometer with Custom<br/>Celsius and Fahrenheit Scales'
106
],
107
[simpleGauge, multiAxisGauge]
108
]);
109
110
// Set height for first row in layout table
111
layoutTable.getRow(0).height(40).fontSize(18);
112
layoutTable.getRow(1).height(80).fontSize(14);
113
114
// Merge cells in layout table where needed
115
layoutTable.getCell(0, 0).colSpan(3);
116
117
// Set container id and initiate drawing
118
layoutTable.container('container');
119
layoutTable.draw();
120
});