Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to re-render an SVG in d3?
    primarykey
    data
    text
    <p>I am adapting the d3 example calendar located here: <a href="http://bl.ocks.org/4063318" rel="nofollow noreferrer">http://bl.ocks.org/4063318</a></p> <p><img src="https://i.stack.imgur.com/qdfns.jpg" alt="enter image description here"></p> <p>and I'm trying to make it so that each day in the calendar is hyperlinked.</p> <p>To do so, I added an anchor tag around each "rect", like so:</p> <pre><code>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); </code></pre> <p>This will link each rect to this website. However, I want the link to be data dependent. So, instead of the line above: </p> <pre><code> .attr("xlink:href", "http://stackoverflow.com") //my new line of code </code></pre> <p>I use: </p> <pre><code> .attr("class", "rectAnchor") </code></pre> <p>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:</p> <pre><code>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 }); }); </code></pre> <p>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" . 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!</p> <p><strong><em>addendum:</em></strong></p> <pre><code> $(".rectAnchor").attr("xlink:href", "http:/127.0.0.1/subdir/" + $(this).children("rect").attr("finer")); </code></pre> <p>generates:</p> <pre><code>&lt;a class="rectAnchor" xlink:href="http:/127.0.0.1/subdir/undefined"&gt; &lt;rect class="day" width="17" height="17" x="170" y="51" finer="group1" fill="#aec7e8"&gt; &lt;title&gt;2012-03-13: group1&lt;/title&gt; &lt;/rect&gt; &lt;/a&gt; </code></pre> <p>(Notice the undefined and the 'xlink:href' instead of just 'href')</p> <pre><code> $(".rectAnchor").attr("xlink:href", function(d) { return "http:/127.0.0.1/subdir/" + $(this).children("rect").attr("finer");}); </code></pre> <p>generates:</p> <pre><code> &lt;a class="rectAnchor" xlink:href="http:/127.0.0.1/subdir/group2"&gt; &lt;rect class="day" width="17" height="17" x="153" y="34" finer="group2" fill="#aec7e8"&gt; &lt;title&gt;2012-03-05: group2&lt;/title&gt; &lt;/rect&gt; &lt;/a&gt; </code></pre> <p>Neither are hyperlinked in the displayed svg (i.e. mouse pointer exhibits no difference and clicking does nothing.) I also changed 'xlink:href' to 'href' in the 2 cases. this outputted the same as above, but with the 'xlink:' missing. However, as before, nothing was hyperlinked. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload