Map Animation in JavaScript

How to make an animated map with Plotly JS


Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Try Plotly Studio now.

d3.csv("https://raw.githubusercontent.com/plotly/datasets/master/gapminder_with_codes.csv", function(err, rows){

  function filter_and_unpack(rows, key, year) {
  return rows.filter(row => row['year'] == year).map(row => row[key])
  }

  var frames = []
  var slider_steps = []

  var n = 11;
  var num = 1952;
  for (var i = 0; i <= n; i++) {
    var z = filter_and_unpack(rows, 'lifeExp', num)
    var locations = filter_and_unpack(rows, 'iso_alpha', num)
    frames[i] = {data: [{z: z, locations: locations, text: locations}], name: num}
    slider_steps.push ({
        label: num.toString(),
        method: "animate",
        args: [[num], {
            mode: "immediate",
            transition: {duration: 300},
            frame: {duration: 300}
          }
        ]
      })
    num = num + 5
  }

var data = [{
      type: 'choropleth',
      locationmode: 'world',
      locations: frames[0].data[0].locations,
      z: frames[0].data[0].z,
      text: frames[0].data[0].locations,
      zauto: false,
      zmin: 30,
      zmax: 90

}];
var layout = {
    title: {
      text: 'World Life Expectency<br>1952 - 2007'
    },
    geo:{
       scope: 'world',
       countrycolor: 'rgb(255, 255, 255)',
       showland: true,
       landcolor: 'rgb(217, 217, 217)',
       showlakes: true,
       lakecolor: 'rgb(255, 255, 255)',
       subunitcolor: 'rgb(255, 255, 255)',
       lonaxis: {},
       lataxis: {}
    },
    updatemenus: [{
      x: 0.1,
      y: 0,
      yanchor: "top",
      xanchor: "right",
      showactive: false,
      direction: "left",
      type: "buttons",
      pad: {"t": 87, "r": 10},
      buttons: [{
        method: "animate",
        args: [null, {
          fromcurrent: true,
          transition: {
            duration: 200,
          },
          frame: {
            duration: 500
          }
        }],
        label: "Play"
      }, {
        method: "animate",
        args: [
          [null],
          {
            mode: "immediate",
            transition: {
              duration: 0
            },
            frame: {
              duration: 0
            }
          }
        ],
        label: "Pause"
      }]
    }],
    sliders: [{
      active: 0,
      steps: slider_steps,
      x: 0.1,
      len: 0.9,
      xanchor: "left",
      y: 0,
      yanchor: "top",
      pad: {t: 50, b: 10},
      currentvalue: {
        visible: true,
        prefix: "Year:",
        xanchor: "right",
        font: {
          size: 20,
          color: "#666"
        }
      },
      transition: {
        duration: 300,
        easing: "cubic-in-out"
      }
    }]
};

Plotly.newPlot('myDiv', data, layout).then(function() {
    Plotly.addFrames('myDiv', frames);
  });
})
</script>\n\t<script src="/?originalUrl=https%3A%2F%2Fcdnjs.cloudflare.com%2Fajax%2Flibs%2Fd3%2F3.5.17%2Fd3.min.js"></script>\n</head>\n\n<body>\n\t<div id='myDiv'><!-- Plotly chart will be drawn inside this DIV --></div>\n</body>","js":"d3.csv(\"https://raw.githubusercontent.com/plotly/datasets/master/gapminder_with_codes.csv\", function(err, rows){\n\n function filter_and_unpack(rows, key, year) {\n return rows.filter(row => row['year'] == year).map(row => row[key])\n }\n\n var frames = []\n var slider_steps = []\n\n var n = 11;\n var num = 1952;\n for (var i = 0; i <= n; i++) {\n var z = filter_and_unpack(rows, 'lifeExp', num)\n var locations = filter_and_unpack(rows, 'iso_alpha', num)\n frames[i] = {data: [{z: z, locations: locations, text: locations}], name: num}\n slider_steps.push ({\n label: num.toString(),\n method: \"animate\",\n args: [[num], {\n mode: \"immediate\",\n transition: {duration: 300},\n frame: {duration: 300}\n }\n ]\n })\n num = num + 5\n }\n\nvar data = [{\n type: 'choropleth',\n locationmode: 'world',\n locations: frames[0].data[0].locations,\n z: frames[0].data[0].z,\n text: frames[0].data[0].locations,\n zauto: false,\n zmin: 30,\n zmax: 90\n\n}];\nvar layout = {\n title: {\n text: 'World Life Expectency<br>1952 - 2007'\n },\n geo:{\n scope: 'world',\n countrycolor: 'rgb(255, 255, 255)',\n showland: true,\n landcolor: 'rgb(217, 217, 217)',\n showlakes: true,\n lakecolor: 'rgb(255, 255, 255)',\n subunitcolor: 'rgb(255, 255, 255)',\n lonaxis: {},\n lataxis: {}\n },\n updatemenus: [{\n x: 0.1,\n y: 0,\n yanchor: \"top\",\n xanchor: \"right\",\n showactive: false,\n direction: \"left\",\n type: \"buttons\",\n pad: {\"t\": 87, \"r\": 10},\n buttons: [{\n method: \"animate\",\n args: [null, {\n fromcurrent: true,\n transition: {\n duration: 200,\n },\n frame: {\n duration: 500\n }\n }],\n label: \"Play\"\n }, {\n method: \"animate\",\n args: [\n [null],\n {\n mode: \"immediate\",\n transition: {\n duration: 0\n },\n frame: {\n duration: 0\n }\n }\n ],\n label: \"Pause\"\n }]\n }],\n sliders: [{\n active: 0,\n steps: slider_steps,\n x: 0.1,\n len: 0.9,\n xanchor: \"left\",\n y: 0,\n yanchor: \"top\",\n pad: {t: 50, b: 10},\n currentvalue: {\n visible: true,\n prefix: \"Year:\",\n xanchor: \"right\",\n font: {\n size: 20,\n color: \"#666\"\n }\n },\n transition: {\n duration: 300,\n easing: \"cubic-in-out\"\n }\n }]\n};\n\nPlotly.newPlot('myDiv', data, layout).then(function() {\n Plotly.addFrames('myDiv', frames);\n });\n})\n"}">