HTMLcopy
1
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-core.min.js" type="text/javascript"></script>
2
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-circular-gauge.min.js"></script>
3
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-data-adapter.min.js"></script>
4
5
<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
3
anychart.data.loadJsonFile('https://gist.githubusercontent.com/shacheeswadia/9e4598a73bb86180f1726daf9de8062f/raw/b258e93e1f9db37da6847eb05a230afcb64446d5/circularGaugeData.json', function (data) {
4
5
// Get the names of the records
6
var recordNames = data.map(function(d){
7
return d.Record;
8
});
9
10
// Get the percent values
11
var recordPercentValue = data.map(function(d){
12
return d.percent;
13
});
14
15
// Add the 100% value to signify the max value for each of the radial ring
16
var initialLength = recordPercentValue.length;
17
recordPercentValue.length *= 2;
18
recordPercentValue.fill(100, initialLength);
19
20
// Set the data to indicate the percent values
21
var dataSet = anychart.data.set(recordPercentValue);
22
23
// Create the angular gauge
24
var gauge = anychart.gauges.circular();
25
26
// Set the data for the angular gauge
27
gauge.data(dataSet);
28
29
// Set the colors based on the designated palette
30
var palette = anychart.palettes
31
.distinctColors()
32
.items([
33
'#593d04', '#6c4304', '#966905', '#a77b0a', '#cf9f20', '#ddb22e', '#f1c84d', '#ffdc73'
34
]);
35
36
// Specify the attributes of the angular gauge chart
37
gauge
38
.fill('#fff')
39
.stroke(null)
40
.padding(25, 100, 25, 100)
41
.margin(0)
42
.startAngle(0)
43
.sweepAngle(270);
44
45
// Create the angular gauge axis
46
var axis = gauge.axis().radius(100).width(1).fill(null);
47
48
// Set the minimum and maximum values for the axis
49
axis
50
.scale()
51
.minimum(0)
52
.maximum(100);
53
54
// Hide the axis labels and ticks
55
axis.labels().enabled(false);
56
axis.ticks().enabled(false);
57
axis.minorTicks().enabled(false);
58
59
// Create a function to make radial bars for each data point
60
var makeBarWithBar = function (gauge, radius, i, width) {
61
62
// Create the radial bars based on the data
63
gauge
64
.bar(i)
65
.dataIndex(i)
66
.radius(radius)
67
.width(width)
68
.fill(palette.itemAt(i))
69
.stroke(null)
70
.zIndex(5);
71
72
// Create the background radial bars indicating the total value
73
gauge
74
.bar(i + 100)
75
.dataIndex(initialLength + i)
76
.radius(radius)
77
.width(width)
78
.fill('#F5F4F4')
79
.stroke(null)
80
.zIndex(4);
81
82
// Set the labels based on each data point
83
gauge
84
.label(i)
85
.text(recordNames[i]);
86
87
// Align the labels
88
gauge
89
.label(i)
90
.hAlign('center')
91
.vAlign('middle')
92
.anchor('right-center')
93
.padding(0, 10)
94
.height(width / 2 + '%')
95
.offsetY(radius + '%')
96
.offsetX(0);
97
98
return gauge.bar(i);
99
100
};
101
102
// Angular gauge tooltip
103
gauge.tooltip().useHtml(true);
104
105
// Format the information of the tooltip
106
gauge
107
.tooltip()
108
.format(function(){
109
var index = this.index;
110
if (index >= initialLength)
111
index -= initialLength;
112
return "<div style='font-size:15px; font-weight:400; margin: 0.2rem 0; color: #1db954; padding:0.15rem'>Record: <span style='color:#fff;'>" + data[index]['Record'] + "</span></div><div style='font-size:15px; font-weight:400; margin: 0.2rem 0; color: #1db954; padding:0.15rem'>Artist: <span style='color:#fff;'>" + data[index]['Artist'] + "</span></div><div style='font-size:15px; font-weight:400; margin: 0.2rem 0; color: #1db954; padding:0.15rem'>Number of Spotify Streams: <span style='color:#fff;'>" + Intl.NumberFormat().format(data[index]['StreamsSpotify']) + "</span></div><div style='font-size:15px; font-weight:400; margin: 0.2rem 0; color: #1db954; padding:0.15rem'>Comparitive Popularity: <span style='color:#fff;'>" + data[index]['percent'] + "%</span></div>";
113
});
114
115
// Call the function to create the radial bars with specifications
116
for(var i=0, j=100; i<data.length, j>=12.5; i++, j-=12.5){
117
makeBarWithBar(gauge, j, i, 12);
118
}
119
120
// Add the angular gauge chart title
121
gauge
122
.title()
123
.useHtml(true)
124
.text(
125
'<span style = "color: #4c2b04; font-size:20px;">Popularity of 2021 GRAMMYs Record of the Year Nominations</span>' +
126
'<br/><span style="color:#1db954; font-size: 18px;">(Based on the number of Spotify Streams)</span>'
127
);
128
129
gauge
130
.title()
131
.enabled(true)
132
.hAlign('center')
133
.padding(0)
134
.margin([0, 0, 10, 0]);
135
136
// Drawing the resulting angular gauge
137
gauge.container('container');
138
gauge.draw();
139
140
});
141
142
});