我在我的网站上有一张地图和一个匹配的图例。当用户从选择列表中选择不同的值时,地图被更新,并且在相同的功能中,图例应该用新值更新。由于地图实现工作正常,因此即使在控制台中记录了正确的值(如果我记录变量),图例的值也保持不变。D3没有更新标签
这是绘制图例中的功能:他们与旧的阈值彩色
color_domain = [wert1, wert2, wert3, wert4, wert5];
ext_color_domain = [0, wert1, wert2, wert3, wert4, wert5];
console.log(ext_color_domain);
legend_labels = ["< "+wert1, ""+wert1, ""+wert2, ""+wert3, ""+wert4, "> "+wert5];
color = d3.scale.threshold()
.domain(color_domain)
.range(["#85db46", "#ffe800", "#ffba00", "#ff7d73", "#ff4e40", "#ff1300"]);
var legend = svg.selectAll("g.legend")
.data(ext_color_domain)
.enter().append("g")
.attr("class", "legend");
var ls_w = 20, ls_h = 20;
legend.append("rect")
.attr("x", 20)
.attr("y", function(d, i){ return height - (i*ls_h) - 2*ls_h;})
.attr("width", ls_w)
.attr("height", ls_h)
.style("fill", function(d, i) { return color(d); })
.style("opacity", 0.7);
legend.append("text")
.attr("x", 50)
.attr("y", function(d, i){ return height - (i*ls_h) - ls_h - 4;})
.text(function(d, i){ return legend_labels[i]; });
console.log(legend_labels); //gives the right legend_labels but doesn't display them correctly
};
可悲的是,甚至在地图上与新颜色更新。这是地图的被染色方式:
svg.append("g")
.attr("class", "id")
.selectAll("path")
.data(topojson.feature(map, map.objects.immoscout).features)
.enter().append("path")
.attr("d", path)
.style("fill", function(d) {
return color(rateById[d.id]);
})
谢谢您的回答。我测试了你的代码,它只画出了图例的一个方面。你有什么想法,为什么这是? – maidi
@maidi,如果你创建了一个工作jsFiddle或PLunkr,我会看看。 – Mark
缺少'.merge(legend)'工作。这就是诀窍。 –