javascript - How can I add links to dendrogram texts? -
i have applied clustered dendrogram show organization's structure. works well. in addition, want add links node texts.
i have added "url" keys json array each node
{ "name": "institutes", "children": [ { "name": "social", "url": "http://social.example.com/" }, { "name": "health", "url": "http://health.example.com/" }, { "name": "nature", "url": "http://nature.example.com/" }, { "name": "education", "url": "http://social.example.com/" } ] }
there 2 problems:
<a>
tags not wrap<text>
element. displayed<a></a><text></text>
. want<a><text></text></a>
.how can read
url
key json in order addhref
attribute's value (see line comment//???
) ?
d3.json("org_chart.json", function(error, root) { var nodes = cluster.nodes(root), links = cluster.links(nodes); var link = svg.selectall(".link") .data(links) .enter().append("path") .attr("class", "link") .attr("d", diagonal); var node = svg.selectall(".node") .data(nodes) .enter().append("g") .attr("class", "node") .attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; }); node.append("circle") .attr("r", 5.1); node.append("a") .attr("href", "#") //??? .attr("target","_blank"); node.append("text") .attr("dx", function(d) { return d.children ? -9 : 8; }) .attr("dy", 3) .style("text-anchor", function(d) { return d.children ? "end" : "start"; }) .text(function(d) { return d.name; }); });
one more thing:
i have tried
node.append("a").attr("href", function (d) { return d.url;});
and returns undefined
. have tried
node.append("a").attr("href", "http://example.com");
although looks like
<g class="node" transform="translate(0,428.74692874692875)"> <circle r="5.1"></circle> <a href="http://example.com/" target="_blank"> <text dx="-9" dy="3" style="text-anchor: end;"> education </text> </a> </g>
it not open http://example.com when click on it.
when you're using selection.append(name)
returns new selection containing appended elements, can append directly it, so:
node.append("a").append("text");
in order obtain url
property need use accessor function, so:
node.append("a").attr("href", function (d) { return d.url; });
edit:
for links might need import xlink namespace in html file:
<html xmlns:xlink="http://www.w3.org/1999/xlink">
and use namespacing:
.append("svg:a").attr("xlink:href", urlaccessor)
see relevant answer here.
Comments
Post a Comment