0

这是我在 bl.ocks http://bl.ocks.org/textplusdata/f64f54dd8c1a0b632ed5上的 d3.js 地图

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

我的问题是这个。如何根据年份加载不同的世界 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”语句放在了错误的地方,但我不知道如何强制触发......

4

1 回答 1

0

在初始创建路径后,您需要在传入新数据时更新现有路径。您的更新代码应类似于

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语句块中。

于 2013-06-13T08:21:19.647 回答