我正在使用Mike Bostock世界旅游地图visualization,我想进行一些修改。循环过渡并按给定顺序显示国家
1)我希望在显示最后一个国家后重播动画。
2)未按名称排序DESC,我想定义tsv文件中行号的顺序。
我试过在转换中加入重播功能,但没有运气,我似乎无法真正理解它是如何工作的,所以我需要一些帮助。
function ready(error, world, names) {
if (error) throw error;
var globe = {type: "Sphere"},
land = topojson.feature(world, world.objects.land),
countries = topojson.feature(world, world.objects.countries).features,
borders = topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }),
i = -1,
n = countries.length;
countries = countries.filter(function(d) {
return names.some(function(n) {
//if (d.id == n.id) return d.name = n.name;
if (d.id == n.id) {
d.name = n.name;
d.org = n.org;
return d;
}
});
}).sort(function(a, b) {
return a.name.localeCompare(b.name);
});
(function transition() {
d3.transition()
.duration(1250)
.each("start", function() {
var data_pais = countries[i = (i + 1) % n]
title.text(data_pais.name);
subtitle.text(data_pais.org);
})
.tween("rotate", function() {
var p = d3.geo.centroid(countries[i]),
r = d3.interpolate(projection.rotate(), [-p[0], -p[1]]);
return function(t) {
projection.rotate(r(t));
c.clearRect(0, 0, width, height);
c.fillStyle = "#ccc", c.beginPath(), path(land), c.fill();
c.fillStyle = "#f00", c.beginPath(), path(countries[i]), c.fill();
c.strokeStyle = "#fff", c.lineWidth = .5, c.beginPath(), path(borders), c.stroke();
c.strokeStyle = "#000", c.lineWidth = 2, c.beginPath(), path(globe), c.stroke();
};
})
.transition()
.each("end", transition);
})();
}
嘿安德鲁,你是对的names.length位,谢谢!自定义排序不起作用,我删除了排序代码,并将tsv传递给csv文件以防万一,但它以字母顺序显示国家/地区。 – Rod0n
啊,是的,猜测我应该在测试的时候交换订单。我想我设法解决了这个问题;在一些有限的测试中,我管理了一个自定义命令。我已经用一些额外的代码更新了答案。 –
卓越的安德鲁,伟大的工程! – Rod0n