HTMLcopy
1
<script src="https://cdn.anychart.com/releases/8.11.0/js/anychart-core.min.js"></script>
2
<script src="https://cdn.anychart.com/releases/8.11.0/js/anychart-map.min.js"></script>
3
<script src="https://cdn.anychart.com/releases/8.11.0/geodata/countries/united_states_of_america/united_states_of_america.js"></script>
4
<script src="https://cdn.anychart.com/releases/8.11.0/js/anychart-data-adapter.min.js"></script>
5
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js"></script>
6
<script src="https://d3js.org/d3.v7.min.js"></script>
7
<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
// load the data
4
anychart.data.loadJsonFile(
5
"https://gist.githubusercontent.com/shacheeswadia/eead370d79e0f20df5148b79b6887393/raw/fba29e3dcf1806cab770e845546a136a924cf1d5/dataBubbleMap2.json",
6
function (data) {
7
8
// create a map chart
9
let map = anychart.map();
10
11
// add the u.s. geodata
12
map.geoData("anychart.maps.united_states_of_america");
13
14
// restrict the maximum size of the bubbles
15
map.maxBubbleSize("5%");
16
17
// set a custom coloring pattern
18
let fillColor = d3
19
.scaleQuantile()
20
.domain([20, 50])
21
.range(["#1b8366", "#77dd79", "#f7e975", "#fea43e", "#cf0011"]);
22
data.forEach(function (d) {
23
let diff = d.Petrol - d.PetrolFeb;
24
d.percentRise = ((diff * 100) / d.PetrolFeb).toFixed(2);
25
d.fill = fillColor(d.percentRise);
26
});
27
28
// create a bubble series
29
let series = map
30
.bubble(
31
anychart.data.set(data).mapAs({
32
size: "Petrol"
33
})
34
)
35
.stroke("0");
36
37
// enable and customize the series labels
38
series
39
.labels()
40
.enabled(true)
41
.anchor("left-center")
42
.position("right")
43
.fontColor("#353535")
44
.fontSize(11)
45
.offsetX(5);
46
47
// customize the tooltip
48
series
49
.tooltip()
50
.useHtml(true)
51
.format(
52
"<h6 style='font-size:14px; font-weight:400; margin: 0.2rem 0;'>Price in July: <b>{%Petrol}</b></h6><h6 style='font-size:14px; font-weight:400; margin: 0.2rem 0;'>Price in February: <b>{%PetrolFeb}</b></h6><h6 style='font-size:14px; font-weight:400; margin: 0.2rem 0;'>Change: <b>{%percentRise}%</b></h6>"
53
);
54
55
// customize the hovered state
56
series
57
.hovered()
58
.fill(function (d) {
59
return anychart.color.darken(d.sourceColor, 0.2);
60
})
61
.stroke(0);
62
63
// set the map's title
64
map
65
.title()
66
.enabled(true)
67
.useHtml(true)
68
.text(
69
'<span style = "color: #7c868e; font-size:18px;">Gasoline Prices in United States in July 2022</span>' +
70
'<br/><span style="font-size: 15px;">Color shows change from February 2022</span>'
71
);
72
73
// set the container id
74
map.container("container");
75
76
// initiate the map drawing
77
map.draw();
78
79
}
80
);
81
});