I am trying to add some text into circle. I have been following example from http://mbostock.github.com/d3/tutorial/circle.html but wasn't able to get the right output.
The code snippet is:
var data;
var code;
d3.json("/json/trace.json", function(json) {
data = json;
console.log(data);
// get code for visualization
code = data["code"];
alert(code);
var mainSVG = d3.select("#viz")
.append("svg")
.attr("width", 900)
.attr("height", 900);
mainSVG.append("circle")
.style("stroke", "gray")
.style("fill", "white")
.attr("r", 100)
.attr("cx", 300)
.attr("cy", 300);
circle = mainSVG.selectAll("circle").data([code]);
}) ;
Any suggestions how to get this work ? Thanks a lot!
Here's a way that I consider easier: The general idea is that you want to append a text element to a circle element then play around with its "dx" and "dy" attributes until you position the text at the point in the circle that you like. In my example, I used a negative number for the dx since I wanted to have text start towards the left of the centre.
Here is an example showing some text in circles with data from a json file: http://bl.ocks.org/4474971. Which gives the following:
The main idea behind this is to encapsulate the text and the circle in the same "
div
" as you would do in html to have the logo and the name of the company in the samediv
in a page header.The main code is:
and the json file is:
The resulting html code shows the encapsulation you want:
Extended the example above to fit the actual requirements, where circled is filled with solid background color, then with striped pattern & after that text node is placed on the center of the circle.
Output:
Below is the link to
See the Pen D3-Circle-Pattern-Text by Manish Kumar (@mkdudeja) on CodePen.codepen
also:Thanks, Manish Kumar
Perhaps, take a step back and drop the json until you have a grip on the theory.
http://tributary.io/inlet/4132672/ (live example, as presented by enjalot in this video. I strongly suggest checking the other d3 videos he has.