Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8893047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:09:08+00:00 2026-06-14T23:09:08+00:00

I am adapting the d3 example calendar located here: http://bl.ocks.org/4063318 and I’m trying to

  • 0

I am adapting the d3 example calendar located here: http://bl.ocks.org/4063318

enter image description here

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:

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” . 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!

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’)

 $(".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 (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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:09:09+00:00Added an answer on June 14, 2026 at 11:09 pm

    Where you’re using $(".rectAnchor"), you’re now in jQuery world – not d3 world.

    The attr() function in jQuery doesn’t work with functions, the way d3’s 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. 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.... (i.e. you’re missing a slash).

    Finally, are you sure that wrapping SVG elements with <a> tags actually works for making them links? 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 (i.e. no javascript).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many data analysts that I respect use version control. For example: http://github.com/hadley/ See comments
http://drupal.org/project/zend I read in that link, that you can add a zendframework plugin and
I'm adapting an example found here on StackOverflow for replacing the select component on
I'm adapting this progress bar:http://www.richardshepherd.com/tv/audio/ to work with my playlist code, but I can't
I am using the tutorial example code provided by the wikitude SDK and adapting
I'm building a custom calendar view for my android app that allows you to
I'm trying to group (or at least make unique) a column m_group_name unless the
I've scourged the internet for an example that would use both spring-json and annotated
I'm adapting an application that makes heavy use of generators to produce its results
I'm adapting this example , in particular, the client. I'll tel you what I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.