1
如何將圖例添加到圖表(see fiddle)?我嘗試按如下方式定義圖例,但圖表消失(請參閱this fiddle)。如何將圖例添加到圖表?
var height = 900, width = 900;
var gridSize = Math.floor(width/42);
var legendElementWidth = gridSize*2;
var legend = svg.selectAll(".legend")
.data([0].concat(colorScaleDomain.quantiles()), function(d) { return d; });
legend.enter().append("g")
.attr("class", "legend");
legend.append("rect")
.attr("x", height)
.attr("y", function(d, i) { return legendElementWidth * (i-0.5); })
.attr("width", gridSize/2)
.attr("height", legendElementWidth)
.style("fill", function(d, i) { return colors[i]; });
legend.append("text")
.attr("class", "mono")
.text(function(d) { return "≥ " + Math.round(d) + "%"; })
.attr("x", (height) + gridSize)
.attr("y", function(d, i) { return legendElementWidth*i; });
legend.exit().remove();
謝謝。如果我需要在圖例中顯示絕對數字,該怎麼辦?此外,'colorScale'已被定義爲分位數:'ar colorScale = d3.scale.quantile() .domain(colorScaleDomain) .range(colors);'。我稍微誤解了爲什麼需要在'.data([0] .concat(colorScale.quantiles()),function(d){return d;});''中重新運行'colorScale.quantiles() – Dinosaurius
'分位數()'是規模。 'quantiles()'返回分位數。他們不同。 –
哦,我不知道這個。抱歉。無論如何,如果你能指出如何在圖例中獲得絕對數字,我非常感激。 – Dinosaurius