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
// This sample uses 3rd party proj4 component
4
// that makes it possible to set coordinates
5
// for the points and labels on the map and
6
// required for custom projections and grid labels formatting.
7
// See https://docs.anychart.com/7.14.3/Maps/Map_Projections
8
9
// set the stage as a container
10
stage = anychart.graphics.create("container");
11
12
// create data set
13
var dataSet = anychart.data.set([
14
{'id': 'US.MA', 'value': 0},
15
{'id': 'US.MN', 'value': 1},
16
{'id': 'US.MT', 'value': 2},
17
{'id': 'US.ND', 'value': 3},
18
{'id': 'US.HI', 'value': 4},
19
{'id': 'US.ID', 'value': 5},
20
{'id': 'US.WA', 'value': 6},
21
{'id': 'US.AZ', 'value': 7},
22
{'id': 'US.CA', 'value': 8},
23
{'id': 'US.CO', 'value': 9},
24
{'id': 'US.NV', 'value': 10},
25
{'id': 'US.NM', 'value': 11},
26
{'id': 'US.OR', 'value': 12},
27
{'id': 'US.UT', 'value': 13},
28
{'id': 'US.WY', 'value': 14},
29
{'id': 'US.AR', 'value': 15},
30
{'id': 'US.IA', 'value': 16},
31
{'id': 'US.KS', 'value': 17},
32
{'id': 'US.MO', 'value': 18},
33
{'id': 'US.NE', 'value': 19},
34
{'id': 'US.OK', 'value': 20},
35
{'id': 'US.SD', 'value': 21},
36
{'id': 'US.LA', 'value': 22},
37
{'id': 'US.TX', 'value': 23},
38
{'id': 'US.CT', 'value': 24},
39
{'id': 'US.NH', 'value': 25},
40
{'id': 'US.RI', 'value': 26},
41
{'id': 'US.VT', 'value': 27},
42
{'id': 'US.AL', 'value': 28},
43
{'id': 'US.FL', 'value': 29},
44
{'id': 'US.GA', 'value': 30},
45
{'id': 'US.MS', 'value': 31},
46
{'id': 'US.SC', 'value': 32},
47
{'id': 'US.IL', 'value': 33},
48
{'id': 'US.IN', 'value': 34},
49
{'id': 'US.KY', 'value': 35},
50
{'id': 'US.NC', 'value': 36},
51
{'id': 'US.OH', 'value': 37},
52
{'id': 'US.TN', 'value': 38},
53
{'id': 'US.VA', 'value': 39},
54
{'id': 'US.WI', 'value': 40},
55
{'id': 'US.WV', 'value': 41},
56
{'id': 'US.DE', 'value': 42},
57
{'id': 'US.MD', 'value': 43},
58
{'id': 'US.NJ', 'value': 44},
59
{'id': 'US.NY', 'value': 45},
60
{'id': 'US.PA', 'value': 46},
61
{'id': 'US.ME', 'value': 47},
62
{'id': 'US.MI', 'value': 48},
63
{'id': 'US.AK', 'value': 49}
64
]);
65
66
// map the data
67
var dataSetForSeries = dataSet.mapAs({id: "id"});
68
69
// create map
70
map = anychart.map();
71
map.geoData(anychart.maps.united_states_of_america);
72
73
// set the map title
74
map.title("Move the map");
75
76
// set the choropleth series
77
map.choropleth(dataSetForSeries);
78
79
// set default zoom
80
map.zoom(1.2);
81
82
// put the map in the container
83
map.container(stage);
84
85
// draw a map
86
map.draw();
87
88
// up button
89
upButton = createButton("Up", 0, 50);
90
upButton.listen("click", function(){
91
map.move(0, 10);
92
});
93
94
// down button
95
downButton = createButton("Down", 0, 20);
96
downButton.listen("click", function(){
97
map.move(0, -10);
98
});
99
100
// left button
101
leftButton = createButton("Left", -50, 20);
102
leftButton.listen("click", function(){
103
map.move(10, 0);
104
});
105
106
// right button
107
rightButton = createButton("Right", 50, 20);
108
rightButton.listen("click", function(){
109
map.move(-10, 0);
110
});
111
112
// disable all default interactivity on the map
113
map.interactivity(false);
114
115
});
116
117
// helper function to create buttons
118
function createButton(text, offsetX, offsetY){
119
var button = anychart.standalones.label();
120
button.background({fill: "#1976d2"});
121
button.text(text);
122
button.fontColor("#fff");
123
button.padding(5);
124
button.position('centerBottom');
125
button.anchor('centerBottom');
126
button.offsetX(offsetX);
127
button.offsetY(offsetY);
128
button.container(stage);
129
button.draw();
130
button.listen("mouseOver", function(){
131
button.background().fill("#1976d2 0.5")
132
document.body.style.cursor = "pointer";
133
button.draw();
134
});
135
button.listen("mouseOut", function(){
136
button.background().fill("#1976d2")
137
document.body.style.cursor = "auto";
138
button.draw();
139
});
140
return button;
141
}