d3.js根据年份切换json映射

d3.js根据年份切换json映射,json,maps,d3.js,geojson,Json,Maps,D3.js,Geojson,这是我在bl.ocks上的d3.js地图 在原始版本上,您可以使用箭头键或自动播放来循环显示年份数据,以显示随时间的变化 我的问题是。如何根据年份获得不同的world json文件以加载?1991年以前,旧苏联存在,但没有出现在数据中 我已经上传了两个JSON,一个有旧苏联,另一个没有 // The map var maptype; if ( parseInt(year) < 1991) maptype = world_countriesold; if ( parseInt(year)

这是我在bl.ocks上的d3.js地图

在原始版本上,您可以使用箭头键或自动播放来循环显示年份数据,以显示随时间的变化

我的问题是。如何根据年份获得不同的world json文件以加载?1991年以前,旧苏联存在,但没有出现在数据中

我已经上传了两个JSON,一个有旧苏联,另一个没有

// The map

var maptype;
if ( parseInt(year) < 1991) maptype = world_countriesold;
if ( parseInt(year) > 1991) maptype = world_countries;


var countries = d3.select("g#countries").empty() ?
chart.append("g").attr("id", "countries") : d3.select("g#countries"),
country = countries
.selectAll("path")
.data(maptype.features);
country
.enter()
.append("path")
.classed("country", true)
// changed { return d.id; } to { return d.ISO1AL3; } to look up 3-letter country code in c-shapes json
.attr("id", function(d,i) { return d.id; })
// changed { return d.properties.name; } to { return d.properties.CNTRY_NAME; } to return correct field in c-shapes json
.attr("title", function(d,i) { return d.properties.name; })
.attr("d", path);

我想我只是把if语句放错了位置,但我不知道如何强制触发…

在初始创建路径后,在传递新数据时需要更新现有路径。您的更新代码应该如下所示

country = countries.selectAll("path")
  .data(maptype.features);

country.enter()
  .append("path");
country.classed("country", true)
  .attr("id", function(d,i) { return d.id; })
  .attr("title", function(d,i) { return d.properties.name; })
  .attr("d", path);
country.exit().remove();
每次更改maptype时,即从if语句块内,都需要调用此代码块