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 data for this sample
3
var fmFrequencies = [
4
{ value: 87.9, name: 'Forever FM' },
5
{ value: 92.1, name: 'Crucial FM' },
6
{ value: 103.1, name: 'Radio<br/>Roo' },
7
{ value: 107.8, name: 'Radio<br/>Norwich' }
8
];
9
var amFrequencies = [
10
{ value: 180, name: 'Galaxy<br/>News Radio' },
11
{ value: 571, name: 'The Dukes<br/>of Hazzard' },
12
{ value: 780, name: 'Frasier' },
13
{ value: 970, name: 'The<br/>Simpsons' }
14
];
15
var stationCount = Math.max(amFrequencies.length, fmFrequencies.length);
16
17
// Create gauge chart
18
var gauge = anychart.gauges.linear();
19
gauge.markerPalette().items(['v-line']);
20
21
// Set gauge title
22
gauge
23
.title()
24
.enabled(true)
25
.useHtml(true)
26
.padding([0, 0, 15, 0])
27
.text(
28
'Radio Scale<br>' +
29
'<span style="color: #212121; font-size: 12px">' +
30
'Use AM and FM labels to switch between them</span>'
31
);
32
33
// Set gauge layout, padding and turn tooltip off
34
gauge.layout('horizontal').padding([30, 100, 0, 100]).tooltip(false);
35
36
// Set bar point as background
37
var bar = gauge.bar(stationCount - 1);
38
bar.width('3.5%').offset('-13.5%').fill('#EAEAEA').stroke('5 #fff');
39
bar
40
.hovered()
41
.stroke(function () {
42
return this.sourceColor;
43
})
44
.fill(function () {
45
return this.sourceColor;
46
});
47
bar
48
.selected()
49
.stroke(function () {
50
return this.sourceColor;
51
})
52
.fill(function () {
53
return this.sourceColor;
54
});
55
56
// Set points
57
for (var i = 0; i < stationCount; i++) {
58
var marker = gauge.marker(i);
59
marker
60
.enabled(true)
61
.stroke(null)
62
.width('2.6%')
63
.offset('-13%')
64
.fill('#212121');
65
marker.hovered().fill('#545f69');
66
marker.selected().fill('#dd2c00');
67
marker
68
.labels()
69
.enabled(true)
70
.position('center-bottom')
71
.useHtml(true)
72
.anchor('center-top')
73
.offsetX(5)
74
.offsetY(25)
75
.hAlign('center')
76
.format(function () {
77
return this.getData('name');
78
});
79
marker.hovered().labels().enabled(true).fontColor('#212121');
80
marker.selected().labels().enabled(true).fontColor('#dd2c00');
81
}
82
83
// Create axis
84
var axisTop = gauge.axis(0);
85
axisTop.minorTicks().enabled(true).stroke('2 #7c868e');
86
axisTop.ticks().stroke('2 #7c868e');
87
axisTop.stroke('2 #7c868e').width('1%').offset('-15.5%');
88
89
// Create another axis on bottom position
90
var axisBottom = gauge.axis(1);
91
axisBottom.minorTicks().enabled(true).stroke('2 #7c868e');
92
axisBottom
93
.stroke('2 #7c868e')
94
.ticks({ stroke: '2 #7c868e' })
95
.labels(false)
96
.width('1%')
97
.offset('-9%')
98
.orientation('bottom');
99
100
// Listen point click function
101
gauge.listen('pointClick', function (e) {
102
var name = e.iterator.get('name');
103
if (e.target !== bar && name) {
104
RSNLabel.text(
105
'You are listening to the<br/>' +
106
'<span style="font-weight: 600; color: #dd2c00">"' +
107
name.replace('<br/>', ' ') +
108
'' +
109
'"</span><br/>station now.'
110
);
111
} else {
112
RSNLabel.text('Click to choose station');
113
}
114
});
115
116
// Set helper function to update gauge
117
function updateGauge(data, labelsPostfix) {
118
// Create scale
119
var scale = gauge.scale();
120
scale.minimum(data[0].value).maximum(data[data.length - 1].value);
121
scale.ticks(
122
data.map(function (a) {
123
return a.value;
124
})
125
);
126
127
gauge
128
.axis(0)
129
.labels()
130
.format('{%Value} ' + labelsPostfix);
131
gauge.data(data);
132
return gauge;
133
}
134
135
// Create label for switching to FM
136
var FMLabel = gauge.label(0);
137
FMLabel.text('FM')
138
.position('center-top')
139
.anchor('center')
140
.padding(5)
141
.offsetX('-45px')
142
.offsetY('95px');
143
FMLabel.background()
144
.enabled(false)
145
.fill('#CECECE')
146
.corners(3)
147
.zIndex(-1);
148
FMLabel.listen('click', function () {
149
updateGauge(fmFrequencies, 'KHz');
150
AMLabel.fontColor('#7c868e').fontWeight(300);
151
FMLabel.fontColor('#dd2c00').fontWeight(600);
152
RSNLabel.text('Click to choose station').fontColor('#7c868e');
153
FMLabel.background(true);
154
AMLabel.background(false);
155
});
156
157
// Create label for switching to AM
158
var AMLabel = gauge.label(1);
159
AMLabel.text('AM').position('center-top').anchor('center').padding(5);
160
AMLabel.fontColor('#dd2c00').fontWeight(600);
161
AMLabel.offsetX('45px').offsetY('95px');
162
AMLabel.background()
163
.enabled(true)
164
.fill('#CECECE')
165
.corners(3)
166
.zIndex(-1);
167
168
AMLabel.listen('click', function () {
169
updateGauge(amFrequencies, 'MHz');
170
FMLabel.fontColor('#7c868e').fontWeight(300);
171
AMLabel.fontColor('#dd2c00').fontWeight(600);
172
RSNLabel.text('Click on point');
173
RSNLabel.fontColor('#7c868e');
174
AMLabel.background(true);
175
FMLabel.background(false);
176
});
177
178
// Create label for current radio station name
179
var RSNLabel = gauge.label(2);
180
RSNLabel.text('Click to choose station')
181
.position('center')
182
.anchor('center')
183
.hAlign('center');
184
RSNLabel.offsetY('150px').useHtml(true);
185
186
// Set up gauge as AM by default
187
updateGauge(amFrequencies, 'MHz');
188
189
// Set container id and initiate drawing
190
gauge.container('container');
191
gauge.draw();
192
});