|
1 | 1 | ---
|
2 | 2 | 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 |
4 | 4 | language: plotly_js
|
5 | 5 | suite: map-animations
|
6 | 6 | order: 1
|
7 | 7 | sitemap: false
|
8 | 8 | arrangement: horizontal
|
9 | 9 | ---
|
10 | 10 |
|
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){ |
12 | 12 |
|
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]) |
15 | 15 | }
|
16 | 16 |
|
17 | 17 | var frames = []
|
18 |
| - var z = unpack(rows, 'pop') |
19 |
| - var locations = unpack(rows, 'abb') |
| 18 | + var slider_steps = [] |
20 | 19 |
|
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 |
33 | 37 | }
|
34 | 38 |
|
35 | 39 | var data = [{
|
36 | 40 | type: 'choropleth',
|
37 |
| - locationmode: 'USA-states', |
| 41 | + locationmode: 'world', |
38 | 42 | locations: frames[0].data[0].locations,
|
39 | 43 | z: frames[0].data[0].z,
|
40 | 44 | text: frames[0].data[0].locations,
|
41 | 45 | zauto: false,
|
42 |
| - zmin: 500000, |
43 |
| - zmax: 50000000 |
| 46 | + zmin: 30, |
| 47 | + zmax: 90 |
44 | 48 |
|
45 | 49 | }];
|
46 |
| - |
47 | 50 | var layout = {
|
48 |
| - title: 'USA State Population<br>2010 - 2016', |
| 51 | + title: 'World Life Expectency<br>1952 - 2007', |
49 | 52 | geo:{
|
50 |
| - scope: 'usa', |
| 53 | + scope: 'world', |
51 | 54 | countrycolor: 'rgb(255, 255, 255)',
|
52 | 55 | showland: true,
|
53 | 56 | landcolor: 'rgb(217, 217, 217)',
|
|
74 | 77 | duration: 200,
|
75 | 78 | },
|
76 | 79 | frame: {
|
77 |
| - duration: 500, |
78 |
| - redraw: false |
| 80 | + duration: 500 |
79 | 81 | }
|
80 | 82 | }],
|
81 | 83 | label: "Play"
|
|
89 | 91 | duration: 0
|
90 | 92 | },
|
91 | 93 | frame: {
|
92 |
| - duration: 0, |
93 |
| - redraw: false |
| 94 | + duration: 0 |
94 | 95 | }
|
95 | 96 | }
|
96 | 97 | ],
|
|
99 | 100 | }],
|
100 | 101 | sliders: [{
|
101 | 102 | 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, |
166 | 104 | x: 0.1,
|
167 | 105 | len: 0.9,
|
168 | 106 | xanchor: "left",
|
|
185 | 123 | }]
|
186 | 124 | };
|
187 | 125 |
|
188 |
| -Plotly.plot(myDiv, data, layout).then(function() { |
| 126 | +Plotly.newPlot(myDiv, data, layout).then(function() { |
189 | 127 | Plotly.addFrames('myDiv', frames);
|
190 | 128 | });
|
191 | 129 | })
|
0 commit comments