Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30c2143

Browse files
authored
Merge pull request plotly#1370 from plotly/map-animation-fix
change redraw to true for choropleth map animation
2 parents 46210f9 + 75c0561 commit 30c2143

File tree

1 file changed

+31
-93
lines changed

1 file changed

+31
-93
lines changed
Lines changed: 31 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,56 @@
11
---
22
name: Map Animations
3-
plot_url: https://codepen.io/bcd/embed/rzrJjb/?height=500&theme-id=15263&default-tab=result
3+
plot_url: https://codepen.io/plotly/embed/WqZOVG/?height=500&theme-id=15263&default-tab=result
44
language: plotly_js
55
suite: map-animations
66
order: 1
77
sitemap: false
88
arrangement: horizontal
99
---
1010

11-
Plotly.d3.csv("https://raw.githubusercontent.com/bcdunbar/datasets/master/usa_pop_melted.csv", function(err, rows){
11+
Plotly.d3.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder_with_codes.csv", function(err, rows){
1212

13-
function unpack(rows, key) {
14-
return rows.map(function(row) { return row[key]; })
13+
function filter_and_unpack(rows, key, year) {
14+
return rows.filter(row => row['year'] == year).map(row => row[key])
1515
}
1616

1717
var frames = []
18-
var z = unpack(rows, 'pop')
19-
var locations = unpack(rows, 'abb')
18+
var slider_steps = []
2019

21-
var n = 7;
22-
var j = 51;
23-
var k = 0;
24-
var num = 2009
25-
for (var i = 0; i < n; i++) {
26-
k++
27-
num++
28-
j = 51
29-
j = j*k
30-
frames[i] = {data: [{z: [], locations: []}], name: num}
31-
frames[i].data[0].z = z.slice(0, j);
32-
frames[i].data[0].locations = locations.slice(0, j);
20+
var n = 11;
21+
var num = 1952;
22+
for (var i = 0; i <= n; i++) {
23+
var z = filter_and_unpack(rows, 'lifeExp', num)
24+
var locations = filter_and_unpack(rows, 'iso_alpha', num)
25+
frames[i] = {data: [{z: z, locations: locations, text: locations}], name: num}
26+
slider_steps.push ({
27+
label: num.toString(),
28+
method: "animate",
29+
args: [[num], {
30+
mode: "immediate",
31+
transition: {duration: 300},
32+
frame: {duration: 300}
33+
}
34+
]
35+
})
36+
num = num + 5
3337
}
3438

3539
var data = [{
3640
type: 'choropleth',
37-
locationmode: 'USA-states',
41+
locationmode: 'world',
3842
locations: frames[0].data[0].locations,
3943
z: frames[0].data[0].z,
4044
text: frames[0].data[0].locations,
4145
zauto: false,
42-
zmin: 500000,
43-
zmax: 50000000
46+
zmin: 30,
47+
zmax: 90
4448

4549
}];
46-
4750
var layout = {
48-
title: 'USA State Population<br>2010 - 2016',
51+
title: 'World Life Expectency<br>1952 - 2007',
4952
geo:{
50-
scope: 'usa',
53+
scope: 'world',
5154
countrycolor: 'rgb(255, 255, 255)',
5255
showland: true,
5356
landcolor: 'rgb(217, 217, 217)',
@@ -74,8 +77,7 @@
7477
duration: 200,
7578
},
7679
frame: {
77-
duration: 500,
78-
redraw: false
80+
duration: 500
7981
}
8082
}],
8183
label: "Play"
@@ -89,8 +91,7 @@
8991
duration: 0
9092
},
9193
frame: {
92-
duration: 0,
93-
redraw: false
94+
duration: 0
9495
}
9596
}
9697
],
@@ -99,70 +100,7 @@
99100
}],
100101
sliders: [{
101102
active: 0,
102-
steps: [{
103-
label: "2010",
104-
method: "animate",
105-
args: [["2010"], {
106-
mode: "immediate",
107-
transition: {duration: 300},
108-
frame: {duration: 300, "redraw": false}
109-
}
110-
]
111-
},{
112-
label: "2011",
113-
method: "animate",
114-
args: [["2011"], {
115-
mode: "immediate",
116-
transition: {duration: 300},
117-
frame: {duration: 300, "redraw": false}
118-
}
119-
]
120-
}, {
121-
label: "2012",
122-
method: "animate",
123-
args: [["2012"], {
124-
mode: "immediate",
125-
transition: {duration: 300},
126-
frame: {duration: 300, "redraw": false}
127-
}
128-
]
129-
}, {
130-
label: "2013",
131-
method: "animate",
132-
args: [["2013"], {
133-
mode: "immediate",
134-
transition: {duration: 300},
135-
frame: {duration: 300, "redraw": false}
136-
}
137-
]
138-
}, {
139-
label: "2014",
140-
method: "animate",
141-
args: [["2014"], {
142-
mode: "immediate",
143-
transition: {duration: 300},
144-
frame: {duration: 300, "redraw": false}
145-
}
146-
]
147-
}, {
148-
label: "2015",
149-
method: "animate",
150-
args: [["2015"], {
151-
mode: "immediate",
152-
transition: {duration: 300},
153-
frame: {duration: 300, "redraw": false}
154-
}
155-
]
156-
}, {
157-
label: "2016",
158-
method: "animate",
159-
args: [["2016"], {
160-
mode: "immediate",
161-
transition: {duration: 300},
162-
frame: {duration: 300, "redraw": false}
163-
}
164-
]
165-
}],
103+
steps: slider_steps,
166104
x: 0.1,
167105
len: 0.9,
168106
xanchor: "left",
@@ -185,7 +123,7 @@
185123
}]
186124
};
187125

188-
Plotly.plot(myDiv, data, layout).then(function() {
126+
Plotly.newPlot(myDiv, data, layout).then(function() {
189127
Plotly.addFrames('myDiv', frames);
190128
});
191129
})

0 commit comments

Comments
 (0)