HTMLcopy
1
<div id="container"></div>
CSScopy
6
1
html, body, #container {
2
width: 100%;
3
height: 100%;
4
margin: 0;
5
padding: 0;
6
}
JavaScriptcopy
x
1
anychart.onDocumentReady(function () {
2
3
// load json data
4
anychart.data.loadJsonFile("https://static.anychart.com/git-storage/word-press/data/choropleth-map-tutorial/data.json", function (data) {
5
6
// Variables
7
// go into the records section of the data
8
var geoData = data.records
9
10
// sum of all cases per country
11
var sumCases = 0;
12
13
// convert cases to numbers
14
var numC;
15
16
// create a new array with the resulting data
17
var data = [];
18
19
// Go through the initial data
20
for (var i = 0; i < geoData.length; i++) {
21
// convert strings to numbers and save them to new variables
22
numC = parseInt(geoData[i].cases);
23
24
// check if we are in the same country by comparing the geoId
25
// if the country is the same, add cases to the appropriate variables
26
if ((geoData[i + 1]) != null && (geoData[i].geoId == geoData[i + 1].geoId)) {
27
sumCases = sumCases + numC;
28
}
29
else {
30
31
// add last day cases of the same country
32
sumCases = sumCases + numC;
33
34
// insert the resulting data in the array using the AnyChart keywords
35
data.push({ id: geoData[i].geoId, value: sumCases, title: geoData[i].countriesAndTerritories })
36
37
// reset the variables to start over
38
sumCases = 0;
39
40
}
41
};
42
43
// connect the data with the map
44
var chart = anychart.map(data);
45
chart.geoData(anychart.maps.world);
46
47
// specify the chart type and set the series
48
var series = chart.choropleth(data);
49
50
// set the chart title
51
chart.title("COVID-19 Global Cases");
52
53
// color scale ranges
54
ocs = anychart.scales.ordinalColor([
55
{ less: 99 },
56
{ from: 100, to: 999 },
57
{ from: 1000, to: 9999 },
58
{ from: 10000, to: 29999 },
59
{ from: 30000, to: 39000 },
60
{ from: 40000, to: 59000 },
61
{ from: 60000, to: 99999 },
62
{ greater: 100000 }
63
]);
64
65
// set scale colors
66
ocs.colors(["rgb(252,245,245)", "rgb(241,219,216)", "rgb(229,190,185)", "rgb(211,152,145)", "rgb(192,117,109)", "rgb(178,93,86)", "rgb(152,50,48)", "rgb(150,33,31)"]);
67
68
// tell the series what to use as a colorRange (colorScale)
69
series.colorScale(ocs);
70
71
// set the container id
72
chart.container('container');
73
74
// draw the chart
75
chart.draw();
76
});
77
78
});