[英]How to re-render an SVG in d3?
I am adapting the d3 example calendar located here: http://bl.ocks.org/4063318我正在调整位于此处的 d3 示例日历: http : //bl.ocks.org/4063318
and I'm trying to make it so that each day in the calendar is hyperlinked.我正在努力使日历中的每一天都带有超链接。
To do so, I added an anchor tag around each "rect", like so:为此,我在每个“矩形”周围添加了一个锚标记,如下所示:
var rect = svg.selectAll(".day")
.data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
.enter()
.append("a") //my new line of code
.attr("xlink:href", "http://stackoverflow.com") //my new line of code
.append("rect")
.attr("class", "day")
.attr("width", cellSize)
.attr("height", cellSize)
.attr("x", function(d) { return week(d) * cellSize; })
.attr("y", function(d) { return day(d) * cellSize; })
.datum(format);
This will link each rect to this website.这会将每个矩形链接到该网站。 However, I want the link to be data dependent.但是,我希望链接依赖于数据。 So, instead of the line above:所以,而不是上面的行:
.attr("xlink:href", "http://stackoverflow.com") //my new line of code
I use:我用:
.attr("class", "rectAnchor")
I do this so that I can select the rectAnchor and then access their rect child, then set the xlink:href attribute, like so, in the following code:我这样做是为了我可以选择 rectAnchor,然后访问它们的 rect 子项,然后在以下代码中设置 xlink:href 属性,如下所示:
d3.csv("dji.csv", function(error, csv) {
var data = d3.nest()
.key(function(d) { return d.Date; })
.rollup(function(d) { return (d[0].Close - d[0].Open) / d[0].Open; })
.map(csv);
rect.filter(function(d) { return d in data; })
.attr("class", function(d) { return "day " + color(data[d]); })
.attr("dyanmiclinktext", function(d) { return data[d]; }) //my new line of code
.select("title")
.text(function(d) { return d + ": " + percent(data[d]); });
$(".rectAnchor") //my new line
.attr("xlink:href", function(){ //my new line
return "http:/127.0.0.1/subdir/" + $(this).children("rect").attr("dynamiclinktext"); //my new line
});
});
Now, when I do that, there are no working hyperlinks and another two undesirable things happen: First, the link inside the anchor tag says xlink:href"URL" instead of href:"URL" .现在,当我这样做时,没有可用的超链接,另外两个不受欢迎的事情发生了:首先,锚标记内的链接显示 xlink:href"URL" 而不是 href:"URL" 。 Secondly, if I change the line .attr("xlink:href", function(){ to .attr("href", function(){ , it still doesn't work. So, I'm wondering, is this because the svg has already been rendered and I need to re-render it with these new and improved anchor tags? Or is there something else I'm missing? Any help is appreciated. Thanks!其次,如果我将 .attr("xlink:href", function(){ 更改为 .attr("href", function(){ ,它仍然不起作用。所以,我想知道,这是因为svg 已经渲染,我需要用这些新的和改进的锚标签重新渲染它?或者还有什么我遗漏的?感谢任何帮助。谢谢!
addendum:附录:
$(".rectAnchor").attr("xlink:href", "http:/127.0.0.1/subdir/" + $(this).children("rect").attr("finer"));
generates:产生:
<a class="rectAnchor" xlink:href="http:/127.0.0.1/subdir/undefined">
<rect class="day" width="17" height="17" x="170" y="51" finer="group1" fill="#aec7e8">
<title>2012-03-13: group1</title>
</rect>
</a>
(Notice the undefined and the 'xlink:href' instead of just 'href') (注意 undefined 和 'xlink:href' 而不仅仅是 'href')
$(".rectAnchor").attr("xlink:href", function(d) { return "http:/127.0.0.1/subdir/" + $(this).children("rect").attr("finer");});
generates:产生:
<a class="rectAnchor" xlink:href="http:/127.0.0.1/subdir/group2">
<rect class="day" width="17" height="17" x="153" y="34" finer="group2" fill="#aec7e8">
<title>2012-03-05: group2</title>
</rect>
</a>
Neither are hyperlinked in the displayed svg (ie mouse pointer exhibits no difference and clicking does nothing.) I also changed 'xlink:href' to 'href' in the 2 cases.在显示的 svg 中也没有超链接(即鼠标指针没有区别,点击什么也不做。)我还在两种情况下将 'xlink:href' 更改为 'href'。 this outputted the same as above, but with the 'xlink:' missing.这输出与上面相同,但缺少“xlink:”。 However, as before, nothing was hyperlinked.但是,和以前一样,没有任何超链接。 Thanks.谢谢。
Where you're using $(".rectAnchor")
, you're now in jQuery world – not d3 world.在您使用$(".rectAnchor")
,您现在处于 jQuery 世界 - 而不是 d3 世界。
The attr()
function in jQuery doesn't work with functions, the way d3's attr()
. jQuery 中的attr()
函数不适用于函数,就像 d3 的attr()
。
You need simply:您只需要:
$(".rectAnchor").attr(
"xlink:href",
"http:/127.0.0.1/subdir/" + $(this).children("rect").attr("dynamiclinktext")
);
Assuming there are no other issues, this should work.假设没有其他问题,这应该可行。
EDIT:编辑:
Actually, I didn't notice $(".rectAnchor")
yields multiple elements.实际上,我没有注意到$(".rectAnchor")
产生多个元素。 You need a hybrid of your previous attempt and my suggestion above:您需要结合您之前的尝试和我上面的建议:
$(".rectAnchor").each(function(i, element) {
var $el = $(element);// $(this) would work too
$el.attr("xlink:href", "http://127.0.0.1/subdir/" + $el.children("rect").attr("dynamiclinktext"));
});
Note that where you have http:/127...
you actually need http://127....
(ie you're missing a slash).请注意,您有http:/127...
实际上需要http://127....
(即您缺少斜线)。
Finally, are you sure that wrapping SVG elements with <a>
tags actually works for making them links?最后,您确定用<a>
标签包装 SVG 元素实际上可以使它们链接吗? It may, but I've never tried it.可能会,但我从未尝试过。 If you're not sure, you should try it out as a standalone test (in, say, jsFiddle) with manually generated SVG (ie no javascript).如果您不确定,您应该使用手动生成的 SVG(即没有 javascript)将其作为独立测试(例如在 jsFiddle 中)进行尝试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.