Reputation: 20204
My question is very similar to JS D3 textPath isn't displayed
The one difference is I want to attach the json that comes back to the links not just the nodes(well, for every node that has a link going to it's parent, I want that link to have the same json)...and ideally, I actually want to add a line from the root node out to the left as well going nowhere.
Then, I basically want to have text in the middle of my link and following the link line would be nice but not absolutely necessary.
My current non-working code is(and I am not sure, but do I need to add an id element to each link? but for to do so I would need the json!!!! plus I need the json to retrieve the text for each link as well) and then I think I use the id in the textPath to refer back to the path if I am not mistaken, right?
var width = $("#graph").width(),
height = 200;
var cluster = d3.layout.cluster()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var myDiv = document.getElementById('graph');
var svg = d3.select(myDiv).append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(40,0)");
d3.json("/datastreams/json/${_encoded}", 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 + ")"; })
link.append("text")
.style("font-size", function(d) { return d.selected ? "16px" : "10px"; })
.style("font-weight", function(d) { return d.selected ? "bold" : ""; })
.text(function(d) { return "SSSS="+d.name; });
node.append("circle")
.attr("r", function(d) { return d.selected ? 10 : 4.5; });
var refs = node.append("a")
.attr("xlink:href", function(d) { return d.root ? "start" : "end"; });
var text = refs.append("text")
.attr("dx", function(d) { return d.root ? 8 : -8; })
.attr("dy", -4)
.style("text-anchor", function(d) { return d.root ? "start" : "end"; });
text.append("tspan")
.style("font-size", function(d) { return d.selected ? "16px" : "10px"; })
.style("font-weight", function(d) { return d.selected ? "bold" : ""; })
.style("fill", function(d) { return "blue"; })
.text(function(d) { return d.name; });
});
d3.select(self.frameElement).style("height", height + "px");
</script>
Upvotes: 1
Views: 625
Reputation: 20204
ah, there we go as this seems to be working(well, I am not fully done but have it very close to what I want).
var textPath = path.each(function(l){
var aref = theG.append("a")
.attr("xlink:href", function(d) { return "http://databus.nrel.gov" });
var text = aref.append("text")
.style("fill", function(d) { return "blue"; });
var textPath = text.append("textPath")
.attr("startOffset", "50%")
.attr("xlink:href", "#path"+l.target.name)
.text("insert");
var aref2 = theG.append("a")
.attr("xlink:href", function(d) { return "http://databus.nrel.gov" });
var text2 = aref2.append("text")
.style("fill", function(d) { return "blue"; });
var textPath2 = text2.append("textPath")
.attr("startOffset", "60%")
.attr("xlink:href", "#path"+l.target.name)
.text("delete");
});
Upvotes: 1