{"id":1467,"date":"2022-08-30T15:16:46","date_gmt":"2022-08-30T15:16:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/19\/how-to-re-render-an-svg-in-d3-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:46","modified_gmt":"2022-08-30T15:16:46","slug":"how-to-re-render-an-svg-in-d3-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-to-re-render-an-svg-in-d3-collection-of-common-programming-errors\/","title":{"rendered":"How to re-render an SVG in d3?-Collection of common programming errors"},"content":{"rendered":"<p>I am adapting the d3 example calendar located here: http:\/\/bl.ocks.org\/4063318<\/p>\n<p>and I&#8217;m trying to make it so that each day in the calendar is hyperlinked.<\/p>\n<p>To do so, I added an anchor tag around each &#8220;rect&#8221;, like so:<\/p>\n<pre><code>var rect = svg.selectAll(\".day\")\n  .data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })\n  .enter()\n  .append(\"a\")                                   \/\/my new line of code\n  .attr(\"xlink:href\", \"http:\/\/stackoverflow.com\") \/\/my new line of code\n  .append(\"rect\")\n  .attr(\"class\", \"day\")\n  .attr(\"width\", cellSize)\n  .attr(\"height\", cellSize)\n  .attr(\"x\", function(d) { return week(d) * cellSize; })\n  .attr(\"y\", function(d) { return day(d) * cellSize; })\n  .datum(format);\n<\/code><\/pre>\n<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>\n<pre><code>  .attr(\"xlink:href\", \"http:\/\/stackoverflow.com\") \/\/my new line of code\n<\/code><\/pre>\n<p>I use:<\/p>\n<pre><code>  .attr(\"class\", \"rectAnchor\")\n<\/code><\/pre>\n<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>\n<pre><code>d3.csv(\"dji.csv\", function(error, csv) {\n  var data = d3.nest()\n    .key(function(d) { return d.Date; })\n    .rollup(function(d) { return (d[0].Close - d[0].Open) \/ d[0].Open; })\n    .map(csv);\n\n  rect.filter(function(d) { return d in data; })\n      .attr(\"class\", function(d) { return \"day \" + color(data[d]); })\n      .attr(\"dyanmiclinktext\", function(d) { return data[d]; })  \/\/my new line of code\n      .select(\"title\")\n      .text(function(d) { return d + \": \" + percent(data[d]); });\n\n\n  $(\".rectAnchor\")                                       \/\/my new line\n  .attr(\"xlink:href\", function(){                             \/\/my new line\n     return \"http:\/127.0.0.1\/subdir\/\" + $(this).children(\"rect\").attr(\"dynamiclinktext\"); \/\/my new line\n  });\n\n});\n<\/code><\/pre>\n<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&#8221;URL&#8221; instead of href:&#8221;URL&#8221; . Secondly, if I change the line .attr(&#8220;xlink:href&#8221;, function(){ to .attr(&#8220;href&#8221;, function(){ , it still doesn&#8217;t work. So, I&#8217;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&#8217;m missing? Any help is appreciated. Thanks!<\/p>\n<p><em><strong>addendum:<\/strong><\/em><\/p>\n<pre><code>   $(\".rectAnchor\").attr(\"xlink:href\", \"http:\/127.0.0.1\/subdir\/\" + $(this).children(\"rect\").attr(\"finer\"));\n<\/code><\/pre>\n<p>generates:<\/p>\n<pre><code>\n\n2012-03-13: group1\n\n\n<\/code><\/pre>\n<p>(Notice the undefined and the &#8216;xlink:href&#8217; instead of just &#8216;href&#8217;)<\/p>\n<pre><code> $(\".rectAnchor\").attr(\"xlink:href\", function(d) { return \"http:\/127.0.0.1\/subdir\/\" + $(this).children(\"rect\").attr(\"finer\");});\n<\/code><\/pre>\n<p>generates:<\/p>\n<pre><code> \n \n 2012-03-05: group2\n \n \n<\/code><\/pre>\n<p>Neither are hyperlinked in the displayed svg (i.e. mouse pointer exhibits no difference and clicking does nothing.) I also changed &#8216;xlink:href&#8217; to &#8216;href&#8217; in the 2 cases. this outputted the same as above, but with the &#8216;xlink:&#8217; missing. However, as before, nothing was hyperlinked. Thanks.<\/p>\n<ol>\n<li>\n<p>Where you&#8217;re using <code>$(\".rectAnchor\")<\/code>, you&#8217;re now in jQuery world &#8211; not d3 world.<\/p>\n<p>The <code>attr()<\/code> function in jQuery doesn&#8217;t work with functions, the way d3&#8217;s <code>attr()<\/code>.<\/p>\n<p>You need simply:<\/p>\n<pre><code>$(\".rectAnchor\").attr(\n  \"xlink:href\",\n  \"http:\/127.0.0.1\/subdir\/\" + $(this).children(\"rect\").attr(\"dynamiclinktext\")\n);\n<\/code><\/pre>\n<p>Assuming there are no other issues, this should work.<\/p>\n<p><strong>EDIT:<\/strong><\/p>\n<p>Actually, I didn&#8217;t notice <code>$(\".rectAnchor\")<\/code> yields multiple elements. You need a hybrid of your previous attempt and my suggestion above:<\/p>\n<pre><code>$(\".rectAnchor\").each(function(i, element) {\n    var $el = $(element);\/\/ $(this) would work too\n    $el.attr(\"xlink:href\", \"http:\/\/127.0.0.1\/subdir\/\" + $el.children(\"rect\").attr(\"dynamiclinktext\"));\n});\n<\/code><\/pre>\n<p>Note that where you have <code>http:\/127...<\/code> you actually need <code>http:\/\/127....<\/code> (i.e. you&#8217;re missing a slash).<\/p>\n<p>Finally, are you sure that wrapping SVG elements with tags actually works for making them links? It may, but I&#8217;ve never tried it. If you&#8217;re not sure, you should try it out as a standalone test (in, say, jsFiddle) with manually generated SVG (i.e. no javascript).<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-19 13:18:42. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I am adapting the d3 example calendar located here: http:\/\/bl.ocks.org\/4063318 and I&#8217;m trying to make it so that each day in the calendar is hyperlinked. To do so, I added an anchor tag around each &#8220;rect&#8221;, like so: var rect = svg.selectAll(&#8220;.day&#8221;) .data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1467","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1467"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1467\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}