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 gauge type
3
var gauge = anychart.gauges.linear();
4
var data = getData();
5
6
// Set credits
7
gauge
8
.credits()
9
.enabled(true)
10
.url(
11
'https://en.wikipedia.org/wiki/List_of_cities_proper_by_population'
12
)
13
.text(
14
'Data source: https://en.wikipedia.org/wiki/List_of_cities_proper_by_population'
15
)
16
.logoSrc('https://en.wikipedia.org/static/favicon/wikipedia.ico');
17
18
// Sets our data
19
gauge.data(data);
20
gauge.padding([0, 20, 0, 180]).layout('horizontal');
21
22
// Set Chart Title
23
gauge
24
.title()
25
.enabled(true)
26
.useHtml(true)
27
.padding([15, 0, 0, 0])
28
.text(
29
'Top 10 Largest Cities in the World<br>' +
30
'<span style="color: #212121; font-size: 12px">(According to Wikipedia.org)</span>'
31
);
32
33
// Set scale, 1 brick on Led series = 1 million people
34
var scale = anychart.scales.linear();
35
scale.minimum(0).maximum(25000000).ticks({ interval: 1000000 });
36
37
for (var i = 0; i < data.length; i++) {
38
// Set Led Pointer
39
var ledSeries = gauge.led(i);
40
ledSeries
41
.width('5%')
42
.offset(i * 9)
43
.scale(scale)
44
// Color settings
45
.dimmer(function () {
46
return '#EAEAEA';
47
})
48
// Set max number of bricks = 25 million
49
.size(null)
50
.count(25);
51
ledSeries.colorScale().colors(['#64b5f6', '#64b5f6']);
52
53
// Set Led Pointer Label
54
ledSeries
55
.labels()
56
.enabled(true)
57
.useHtml(true)
58
.position('center-bottom')
59
.offsetX(5)
60
.anchor('right-center')
61
.format(function () {
62
return (
63
this.value.toLocaleString() +
64
', <span style="color: #212121; font-weight: 600">' +
65
this.getData('city') +
66
'</span>'
67
);
68
});
69
ledSeries
70
.hovered()
71
.labels()
72
.enabled(true)
73
.fontColor('#212121')
74
.fontWeight('600');
75
ledSeries
76
.selected()
77
.labels()
78
.enabled(true)
79
.fontColor('#212121')
80
.fontWeight('600');
81
82
// Create bar pointer
83
gauge
84
.bar(i)
85
.offset(i * 9 - 1)
86
.width('0.5%')
87
.color('#1976d2')
88
.scale(scale);
89
}
90
91
// Set Tooltip
92
gauge.tooltip().format(function () {
93
return this.getData('city') + ', ' + this.getData('country');
94
});
95
gauge.tooltip().titleFormat('{%Value}{groupsSeparator: }');
96
97
// Set container id and initiate drawing
98
gauge.container('container');
99
gauge.draw();
100
});
101
102
// Create data
103
function getData() {
104
return [
105
{ city: 'Shanghai', value: 24256800, country: 'China' },
106
{ city: 'Karachi', value: 23500000, country: 'Pakistan' },
107
{ city: 'Bejing', value: 21516000, country: 'China' },
108
{ city: 'Delhi', value: 18686902, country: 'India' },
109
{ city: 'Lagos', value: 17060303, country: 'Nigeria' },
110
{ city: 'Tianjin', value: 15200000, country: 'China' },
111
{ city: 'Istambul', value: 14160467, country: 'Turkey' },
112
{ city: 'Tokyo', value: 13297629, country: 'Japan' },
113
{ city: 'Guangzhou', value: 13080500, country: 'China' },
114
{ city: 'Mumbai', value: 12442373, country: 'India' }
115
];
116
}