HTMLcopy
x
1
<script src="https://cdn.anychart.com/releases/8.10.0/js/anychart-core.min.js"></script>
2
<script src="https://cdn.anychart.com/releases/8.10.0/js/anychart-map.min.js"></script>
3
4
<script src="https://cdn.anychart.com/geodata/latest/custom/world/world.js"></script>
5
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.15/proj4.js"></script>
6
7
<script src="https://cdn.anychart.com/releases/8.10.0/js/anychart-data-adapter.min.js"></script>
8
9
<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
179
1
anychart.onDocumentReady(function () {
2
anychart.data.loadJsonFile('https://gist.githubusercontent.com/shacheeswadia/a20ba5b62cef306ccc1a8e8857e5316a/raw/0337b16fa8dc4de97263bc0a4ededf935a529c35/migration-data.json', function (data) {
3
4
// create a connector map chart instance
5
var map = anychart.connector();
6
7
// include the world map geodata
8
map.geoData('anychart.maps.world');
9
10
// darken all regions
11
map
12
.unboundRegions()
13
.enabled(true)
14
.fill('#E1E1E1')
15
.stroke('#D2D2D2');
16
17
// set the map chart title
18
map
19
.title()
20
.enabled(true)
21
.useHtml(true)
22
.padding([0, 0, 40, 0])
23
.text(
24
'<span style="color:#212121;">Migrations to the USA from the top 15 countries</span><br/>' +
25
'<span style="font-size: 14px;">Majority of the migrants come from Mexico, Asia and South America</span>'
26
);
27
28
// display all labels even if there is an overlap
29
map.
30
overlapMode("allow-overlap");
31
32
// a helper function to create the series
33
// that will form the connector lines
34
var createSeries = function (name, data, color) {
35
36
// create and customize the connector series
37
var connectorSeries = map
38
.connector(data)
39
.name(name)
40
.fill(color)
41
.stroke({
42
color: color,
43
thickness: 2
44
});
45
46
// change the coloring of the connector line in the hovered state
47
connectorSeries
48
.hovered()
49
.stroke('1.5 #212121')
50
.fill('#212121');
51
52
// configure the arrow marker
53
connectorSeries
54
.markers()
55
.position('100%')
56
.fill(color)
57
.stroke({
58
color: color
59
})
60
.size(8);
61
62
// configure the arrow marker in the hovered state
63
connectorSeries
64
.hovered()
65
.markers()
66
.position('100%')
67
.size(10)
68
.fill('#212121')
69
.stroke('2 #455a64');
70
71
// set the labels for the source countries
72
connectorSeries
73
.labels()
74
.enabled(true)
75
.format(function () {
76
return this.getData('from');
77
})
78
.fontColor('#212121');
79
80
// set the thickness of the connector line based on the series
81
if (name === 'More than 50,000') {
82
connectorSeries.startSize(5).endSize(2);
83
} else if (name === '40,000 to 50,000') {
84
connectorSeries.startSize(3.5).endSize(1.5);
85
} else if (name === '20,000 to 40,000') {
86
connectorSeries.startSize(3).endSize(1);
87
} else if (name === '16,000 to 20,000') {
88
connectorSeries.startSize(2).endSize(0.5);
89
} else {
90
connectorSeries.startSize(1).endSize(0);
91
}
92
93
// configure the settings for the legend items
94
connectorSeries
95
.legendItem()
96
.iconType('square')
97
.iconFill(color)
98
.iconStroke(false);
99
100
// configure the tooltip setting for the series
101
connectorSeries
102
.tooltip()
103
.useHtml(true)
104
.format(function () {
105
return (
106
'<h4 style="font-size:14px; font-weight:400; margin: 0.25rem 0;">From:<b> ' + this.getData('from') + '</b></h4>' +
107
'<h4 style="font-size:14px; font-weight:400; margin: 0.25rem 0;">Total Migrants::<b> ' + this.getData('total').toLocaleString() + '</b></h4>'
108
);
109
});
110
111
};
112
113
// create a dataset from the data
114
var dataSet = anychart.data.set(data).mapAs();
115
116
// create five series filtering the data
117
// by the absolute values of the migration numbers
118
createSeries(
119
'Less than 16,000',
120
dataSet.filter('total', filterFunction(0, 16000)),
121
'#fed693'
122
);
123
createSeries(
124
'16,000 to 20,000',
125
dataSet.filter('total', filterFunction(16000, 20000)),
126
'#f5ad52'
127
);
128
createSeries(
129
'20,000 to 40,000',
130
dataSet.filter('total', filterFunction(20000, 40000)),
131
'#3fb8c5'
132
);
133
createSeries(
134
'40,000 to 50,000',
135
dataSet.filter('total', filterFunction(40000, 50000)),
136
'#1792c0'
137
);
138
createSeries(
139
'More than 50,000',
140
dataSet.filter('total', filterFunction(50000, 1000000)),
141
'#1c5eaa'
142
);
143
144
// set up the legend for the sample
145
map
146
.legend()
147
.enabled(true)
148
.position('bottom')
149
.padding([0, 0, 20, 0])
150
.fontSize(10);
151
152
map
153
.legend()
154
.title()
155
.enabled(true)
156
.fontSize(13)
157
.padding([0, 0, 5, 0])
158
.text('Number of Migrants (in the year 2019)');
159
160
// set the container
161
map.container('container');
162
163
// draw the map
164
map.draw();
165
166
});
167
});
168
169
// a helper function to bind the data field to the local var.
170
function filterFunction(val1, val2) {
171
if (val2) {
172
return function (fieldVal) {
173
return val1 <= fieldVal && fieldVal < val2;
174
};
175
}
176
return function (fieldVal) {
177
return val1 <= fieldVal;
178
};
179
}