我在我的網站上有一張地圖和一個匹配的圖例。當用戶從選擇列表中選擇不同的值時,地圖被更新,並且在相同的功能中,圖例應該用新值更新。由於地圖實現工作正常,因此即使在控制檯中記錄了正確的值(如果我記錄變量),圖例的值也保持不變。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)'工作。這就是訣竅。 –