D3地图转换失败

时间:2016-07-18 07:07:06

标签: javascript d3.js maps transition topojson

我使用D3将一张印度地图(India.json)转换为其地图(India_simple.json)。这些都是topojson格式。我的最终目标是实现像Max Galka的演示中所示的过渡 - http://metrocosm.com/how-to-make-cartograms-with-animation/。特别是我指的是美国地图转换演示 - view-source:http://metrocosm.com/shape-transition.html

但不幸的是,过渡功能无效。代码如下 -

<!DOCTYPE html>
<meta charset="utf-8">

<!-- Set a style for our worldshape-data -->
  <style>
  path {
    stroke: red;
    stroke-width: 0.5px;
    fill: grey;
  }
  </style>
<body>

  <!-- implementation of the hosted D3- and TopoJson js-libraries -->
  <script src="http://d3js.org/d3.v3.min.js"></script>
  <script src="http://d3js.org/topojson.v0.min.js"></script>

  <!-- map creation --> 
  <script>
  // canvas resolution
  var width = 1000,
      height = 600;

  // projection-settings for mercator    
  var projection = d3.geo.mercator()
      // where to center the map in degrees
      .center([100, 30 ])
      // zoomlevel
      .scale(700)
      // map-rotation
      .rotate([0,0]);

  // defines "svg" as data type and "make canvas" command
  var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

  // defines "path" as return of geographic features
  var path = d3.geo.path()
      .projection(projection);

  // group the svg layers 
  var g = svg.append("g");

  // load data and display the map on the canvas with country geometries
  d3.json("india.json", function(error, topology) {
    d3.json("india_simple.json", function(error2, topology2) {
      var map = g.selectAll("path")
        .data(topojson.object(topology, topology.objects.collection)
            .geometries)
      .enter()
        .append("path")
        .attr("d", path);



        setInterval(function(){                   
  map
    .data(topojson.object(topology2, topology2.objects.collection))
    .transition()
    .duration(3000)
    .attr("d", path);

    setTimeout(function(){
      map
        .data(topojson.object(topology, topology.objects.collection))
        .transition()
        .duration(3000)
        .attr("d", path);


    }, 3000)
}, 6000);      
  });
        });




  // zoom and pan functionality
 /* var zoom = d3.behavior.zoom()
      .on("zoom",function() {
          g.attr("transform","translate("+ 
              d3.event.translate.join(",")+")scale("+d3.event.scale+")");
          g.selectAll("path")  
              .attr("d", path.projection(projection)); 
    });

  svg.call(zoom)*/

  </script>
</body>
</html>

观察到的输出只是india_simple.json地图。 setInterval和setTimeout函数不起作用。我无法在控制台中看到任何错误。

india.json和india_simple.json文件放在dropbox中 - https://www.dropbox.com/sh/wrxyngyq4jdie9c/AACG2-dTzC79rRvlxlOC5poBa?dl=0 请帮我获得所需的过渡

0 个答案:

没有答案