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
// load json data
3
anychart.data.loadJsonFile('https://static.anychart.com/git-storage/word-press/data/choropleth-map-tutorial/data.json', function (data) {
4
5
// Variables
6
// go into the records section of the data
7
var geoData = data.records;
8
9
// sum of all cases per country
10
var sumCases = 0;
11
12
// sum of all deaths per country
13
var sumDeaths = 0;
14
15
// convert cases and deaths to numbers
16
var numC;
17
var numD;
18
19
// create a new array with the resulting data
20
var chartData = [];
21
22
// Go through the initial data
23
for (var i = 0; i < geoData.length; i++) {
24
// convert strings to numbers and save them to new variables
25
numC = parseInt(geoData[i].cases);
26
numD = parseInt(geoData[i].deaths);
27
28
// check if we are in the same country by comparing the geoId
29
// if the country is the same, add cases and deaths to the appropriate variables
30
if ((geoData[i + 1]) !== undefined && (geoData[i].geoId === geoData[i + 1].geoId)) {
31
sumCases = sumCases + numC;
32
sumDeaths = sumDeaths + numD;
33
} else {
34
35
// add last day cases and deaths of the same country
36
sumCases = sumCases + numC;
37
sumDeaths = sumDeaths + numD;
38
39
// insert the resulting data in the array using the AnyChart keywords
40
chartData.push({ id: geoData[i].geoId, value: sumCases, size: sumDeaths, title: geoData[i].countriesAndTerritories });
41
42
// reset the variables to start over
43
sumCases = 0;
44
sumDeaths = 0;
45
46
}
47
}
48
49
// connect the data with the map
50
var chart = anychart.map(chartData);
51
chart.geoData(anychart.maps.world);
52
53
// specify the chart type and set the series
54
var series = chart.choropleth(chartData);
55
56
// variable to store data that will be used for bubbles
57
var bubbleData = [];
58
59
// store only the countries that have at least 1 death
60
for (var j = 0; j < chartData.length; j++) {
61
if (data[j] && data[j].size > 0) {
62
bubbleData.push(chartData[j]);
63
}
64
}
65
66
// create a series for bubbles
67
var series_1 = chart.bubble(bubbleData);
68
69
// set the maximum size of the bubble
70
chart.maxBubbleSize(25);
71
72
// set the minimum size of the bubble
73
chart.minBubbleSize(3);
74
75
// set colors and stroke of bubbles
76
series_1.normal().fill('black', 0.3);
77
series_1.hovered().fill('black', 0.1);
78
series_1.selected().fill('black', 0.5);
79
series_1.normal().stroke('rgb(150,33,31)');
80
series_1.hovered().stroke('rgb(150,33,31)', 2);
81
series_1.selected().stroke('rgb(150,33,31)', 4);
82
83
// set the chart title
84
chart.title('COVID-19 Global Cases');
85
86
// color scale ranges
87
var ocs = anychart.scales.ordinalColor([
88
{ less: 99 },
89
{ from: 100, to: 999 },
90
{ from: 1000, to: 9999 },
91
{ from: 10000, to: 29999 },
92
{ from: 30000, to: 39000 },
93
{ from: 40000, to: 59000 },
94
{ from: 60000, to: 99999 },
95
{ greater: 100000 }
96
]);
97
98
// set scale colors
99
ocs.colors([
100
'rgb(252,245,245)',
101
'rgb(241,219,216)',
102
'rgb(229,190,185)',
103
'rgb(211,152,145)',
104
'rgb(192,117,109)',
105
'rgb(178,93,86)',
106
'rgb(152,50,48)',
107
'rgb(150,33,31)'
108
]);
109
110
// tell the series what to use as a colorRange (colorScale)
111
series.colorScale(ocs);
112
113
// enable the legend
114
chart.legend(true);
115
116
// set the source mode of the legend and add styles
117
chart.legend()
118
.itemsSourceMode('categories')
119
.position('right')
120
.align('top')
121
.itemsLayout('vertical')
122
.padding(50, 0, 0, 20)
123
.paginator(false);
124
125
// tooltip formatting
126
series.tooltip().format('Total Confirmed Cases: {%value}');
127
series_1.tooltip().format('Total Deaths: {%size}');
128
129
// set the container id
130
chart.container('container');
131
132
// draw the chart
133
chart.draw();
134
});
135
});