HTMLcopy
x
1
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-base.min.js"></script>
2
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-map.min.js"></script>
3
<script src="https://cdn.anychart.com/releases/8.9.0/geodata/countries/united_states_of_america/united_states_of_america.js"></script>
4
5
<script src="https://cdn.anychart.com/releases/8.9.0/js/anychart-data-adapter.min.js"></script>
6
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js"></script>
7
8
<script src="https://d3js.org/d3-scale.v3.min.js"></script>
9
<script src="https://d3js.org/d3-interpolate.v2.min.js"></script>
10
11
<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
91
1
anychart.onDocumentReady(function () {
2
anychart.data.loadJsonFile('https://gist.githubusercontent.com/shacheeswadia/70ec3d69e0e7a8bff7917607ea2926e4/raw/c3329fa81e86d9e637503b042becd17e68d9a718/bubbleMapData.json',
3
function (data) {
4
5
// Creates map chart
6
var map = anychart.map();
7
8
// Define settings for maps regions
9
map
10
.unboundRegions()
11
.enabled(true)
12
.fill('#E1E1E1')
13
.stroke('#D2D2D2');
14
15
// Set geodata using the script added
16
map.geoData('anychart.maps.united_states_of_america');
17
18
// Set Chart Title
19
map
20
.title()
21
.enabled(true)
22
.useHtml(true)
23
.text(
24
'<span style= "font-size:22px;">2020 US Election Results</span><br/><span style="color:#929292; font-size: 16px;">' +
25
'State-wise number of electoral votes and percentage of winner votes</span>'
26
);
27
28
// Set bubble min/max size settings
29
map.minBubbleSize('0.8%').maxBubbleSize('5%');
30
31
var opacity = d3.scaleLinear()
32
.domain([49, 70])
33
.range([0.4, 0.9]);
34
35
//Fill color based on winner
36
data.forEach(function(d){
37
var opacityVal = opacity(d.votepercent);
38
opacityVal = opacityVal.toFixed(2);
39
if(d.winner == "Democrats"){
40
d.fill = "#019bd8 " + opacityVal;
41
}else{
42
d.fill = "#d81c28 " + opacityVal;
43
}
44
});
45
46
// Charting the bubbles
47
var series = map.bubble(
48
anychart.data.set(data)
49
.mapAs({
50
size: 'electoralvotes'
51
})
52
);
53
54
// Enable HTML for labels
55
series.tooltip().useHtml(true);
56
57
// Customize tooltip text
58
series
59
.tooltip()
60
.format("<h6 style='font-size:14px; font-weight:400; margin: 0.2rem 0;'>Winning Party: <b>{%winner}</b></h6><h6 style='font-size:14px; font-weight:400; margin: 0.2rem 0;'>Electoral Votes: <b>{%electoralvotes}</b></h6><h6 style='font-size:14px; font-weight:400; margin: 0.2rem 0;'>% of votes won: <b>{%votepercent}%</b></h6>");
61
62
// Tooltip
63
series
64
.tooltip(true)
65
.stroke('2 #E1E1E1')
66
.selectionMode('none');
67
68
// Labels
69
series
70
.labels()
71
.enabled(true)
72
.anchor('left-center')
73
.position('right')
74
.fontColor('#353535')
75
.fontSize(11)
76
.offsetX(5);
77
78
// On hovering the bubbles
79
series
80
.hovered().fill(function(d){
81
return d.sourceColor.color;
82
}, 1)
83
84
// Set container id for the chart
85
map.container('container');
86
87
// Initiates chart drawing
88
map.draw();
89
90
});
91
});