I am a complete novice at d3.js or java in general. I am using the indented tree example from http://bl.ocks.org/1093025. It took me two hours to get this to work on my local computer, so that should give you an idea of my skill level.
I opened the flare.json file and started messing with it and was able to manipulate it successfully. It looks like this
{
"name": "Test D3",
"children": [
{
"name": "News",
"children": [
{
"name": "CNN",
"size": 1000
},
{
"name": "BBC",
"size": 3812
}
]
},
{
"name": "Blogs",
"children": [
{
"name": "Engaget",
"size": 3938
}
]
},
{
"name": "Search",
"children": [
{
"name": "Google",
"size": 3938
},
{
"name": "Bing",
"size": 3938
}
]
}
]
}
What I want to do now, is to try to add hyperlinks. For example, I want to be able to click on “CNN” and go to CNN.com. Is there a modification I can make to flare.json that will do that?
It is quite easy, just add some more “key” : “value” pairs. Example:
Of course, in your d3 code you then need to
append<svg:a>tags and set theirxlink:hrefattribute.Here is some html and d3-code that might be of help to you. First you need to import the xlink namespace in your html file:
Then in the d3 drawing code where you append nodes for each data element you wrap the element you want to be clickable links with an
svg:atag:You might want to remove the click handler (which is present in the original example) by deleting the .on(“click”, click) as it might interfere with the default behavior of SVG links.
Clicking on your
rects should now lead you to the appropriateurl.SVG links might not be fully implemented in all browsers.
Alternatively you could modify the
clickhandler to read the URL fromd.urland use that one to manually redirect the browser to that URL via JavaScript:window.location = d.url;. Then you do not need thesvg:atag and thexlinkcode. Though adding a real link (not a scripted one) has the benefit that the user/browser can decide what to do (e.g., open in new tab/page). It also helps if some of your users have JavaScript disabled.