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
160
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('Migrations to the USA from the top 15 countries');
20
21
// display all labels even if there is an overlap
22
map.
23
overlapMode("allow-overlap");
24
25
// a helper function to create the series
26
// that will form the connector lines
27
var createSeries = function (name, data, color) {
28
29
// create and customize the connector series
30
var connectorSeries = map
31
.connector(data)
32
.name(name)
33
.fill(color)
34
.stroke({
35
color: color,
36
thickness: 2
37
});
38
39
// change the coloring of the connector line in the hovered state
40
connectorSeries
41
.hovered()
42
.stroke('1.5 #212121')
43
.fill('#212121');
44
45
// configure the arrow marker
46
connectorSeries
47
.markers()
48
.position('100%')
49
.fill(color)
50
.stroke({
51
color: color
52
})
53
.size(8);
54
55
// configure the arrow marker in the hovered state
56
connectorSeries
57
.hovered()
58
.markers()
59
.position('100%')
60
.size(10)
61
.fill('#212121')
62
.stroke('2 #455a64');
63
64
// set the labels for the source countries
65
connectorSeries
66
.labels()
67
.enabled(true)
68
.format(function () {
69
return this.getData('from');
70
});
71
72
// set the thickness of the connector line based on the series
73
if (name === 'More than 50,000') {
74
connectorSeries.startSize(5).endSize(2);
75
} else if (name === '40,000 to 50,000') {
76
connectorSeries.startSize(3.5).endSize(1.5);
77
} else if (name === '20,000 to 40,000') {
78
connectorSeries.startSize(3).endSize(1);
79
} else if (name === '16,000 to 20,000') {
80
connectorSeries.startSize(2).endSize(0.5);
81
} else {
82
connectorSeries.startSize(1).endSize(0);
83
}
84
85
// configure the settings for the legend items
86
connectorSeries
87
.legendItem()
88
.iconType('square')
89
.iconFill(color)
90
.iconStroke(false);
91
92
};
93
94
// create a dataset from the data
95
var dataSet = anychart.data.set(data).mapAs();
96
97
// create five series filtering the data
98
// by the absolute values of the migration numbers
99
createSeries(
100
'Less than 16,000',
101
dataSet.filter('total', filterFunction(0, 16000)),
102
'#fed693'
103
);
104
createSeries(
105
'16,000 to 20,000',
106
dataSet.filter('total', filterFunction(16000, 20000)),
107
'#f5ad52'
108
);
109
createSeries(
110
'20,000 to 40,000',
111
dataSet.filter('total', filterFunction(20000, 40000)),
112
'#3fb8c5'
113
);
114
createSeries(
115
'40,000 to 50,000',
116
dataSet.filter('total', filterFunction(40000, 50000)),
117
'#1792c0'
118
);
119
createSeries(
120
'More than 50,000',
121
dataSet.filter('total', filterFunction(50000, 1000000)),
122
'#1c5eaa'
123
);
124
125
// set up the legend for the sample
126
map
127
.legend()
128
.enabled(true)
129
.position('center')
130
.padding([20, 0, 20, 0])
131
.fontSize(10);
132
133
map
134
.legend()
135
.title()
136
.enabled(true)
137
.fontSize(13)
138
.padding([0, 0, 5, 0])
139
.text('Number of Migrants (in the year 2019)');
140
141
// set the container
142
map.container('container');
143
144
// draw the map
145
map.draw();
146
147
});
148
});
149
150
// a helper function to bind the data field to the local var.
151
function filterFunction(val1, val2) {
152
if (val2) {
153
return function (fieldVal) {
154
return val1 <= fieldVal && fieldVal < val2;
155
};
156
}
157
return function (fieldVal) {
158
return val1 <= fieldVal;
159
};
160
}