[英]Map + Legend on same svg
I've been trying for a week now to realize this Map+Legend 我已经尝试了一个星期,以了解这个Map + Legend
I still can't display correctly my legend 我仍然无法正确显示我的图例
when i follow this 当我跟随这个
var legend = svg.selectAll("g.legend")
.data(extent_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 h - (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.8);
legend.append("text")
.attr("x", 50)
.attr("y", function(d, i){ return h - (i*ls_h) - ls_h - 4;})
.text(function(d, i){ return legend_labels[i]; });
Legend is displayed first then map appears and overwrites it 图例首先显示,然后地图出现并覆盖它
I'd like to have map and legend displayed at the same time and legend displayed on the right-bottom corner of the map (where blank space is located) 我想同时显示地图和图例,并在地图的右下角(位于空白处)显示图例
I read a lot about z-index so i tried many things on the css part of my .legend but can't figure it out 我读了很多有关z-index的文章,所以我在.legend的CSS部分尝试了很多东西,但无法弄清楚
Thank you so much for your help and for explaining to me what my mistakes are 非常感谢您的帮助并向我解释我的错误是什么
First: You place each element individually. 首先:您分别放置每个元素。 I would not recommend that.
我不建议这样做。 You already use a
<g>
element to group the entries. 您已经使用
<g>
元素对条目进行分组。 So you could attach the positioning to that element. 因此,您可以将位置附加到该元素。
With that being said: Your x-positioning is currently to 50. Use something which moves the boxes to the right like w- 120
. 这么说:您的x位置当前为50。使用类似
w- 120
的框将框向右移动。
Implementing both yields: 实现两个收益:
var ls_w = 20, ls_h = 20;
var legend = svg.selectAll("g.legend")
.data(extent_color_domain)
.enter().append("g")
.attr("class", "legend")
.attr( 'transform', function(d,i) {
return 'translate( ' + (w - 120) + ' ' + (h - (i*ls_h) - ls_h) + ' )'
});
legend.append("rect")
.attr("x", 20)
.attr("y", -20 )
.attr("width", ls_w)
.attr("height", ls_h)
.style("fill", function(d, i) { return color(d); })
.style("opacity", 0.8);
legend.append("text")
.attr("x", 50)
.attr("y", -4 )
.text(function(d, i){ return legend_labels[i]; });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.