Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are <a href="https://github.com/mbostock/d3/wiki/Gallery" rel="noreferrer">lots of examples</a> showing how to add labels to graph and tree visualizations, but I'd probably start with this one as the simplest:</p> <ul> <li><a href="http://bl.ocks.org/950642" rel="noreferrer">http://bl.ocks.org/950642</a></li> </ul> <p>You haven’t posted a link to your code, but I'm guessing that <code>node</code> refers to a selection of SVG circle elements. You can’t add text elements to circle elements because <strong>circle elements are not <a href="http://www.w3.org/TR/SVG/intro.html#TermContainerElement" rel="noreferrer">containers</a></strong>; adding a text element to a circle will be ignored.</p> <p>Typically you use a G element to group a circle element (or an image element, as above) and a text element for each node. The resulting structure looks like this:</p> <pre><code>&lt;g class="node" transform="translate(130,492)"&gt; &lt;circle r="4.5"/&gt; &lt;text dx="12" dy=".35em"&gt;Gavroche&lt;/text&gt; &lt;/g&gt; </code></pre> <p>Use a <a href="http://bost.ocks.org/mike/join/" rel="noreferrer">data-join</a> to create the G elements for each node, and then use <a href="https://github.com/mbostock/d3/wiki/Selections#wiki-append" rel="noreferrer">selection.append</a> to add a circle and a text element for each. Something like this:</p> <pre><code>var node = svg.selectAll(".node") .data(nodes) .enter().append("g") .attr("class", "node") .call(force.drag); node.append("circle") .attr("r", 4.5); node.append("text") .attr("dx", 12) .attr("dy", ".35em") .text(function(d) { return d.name }); </code></pre> <p>One downside of this approach is that you may want the labels to be drawn on top of the circles. Since SVG does not yet support z-index, elements are drawn in document order; so, the above approach causes a label to be drawn <em>above its circle</em>, but it may be drawn <em>under other circles</em>. You can fix this by using two data-joins and creating separate groups for circles and labels, like so:</p> <pre><code>&lt;g class="nodes"&gt; &lt;circle transform="translate(130,492)" r="4.5"/&gt; &lt;circle transform="translate(110,249)" r="4.5"/&gt; … &lt;/g&gt; &lt;g class="labels"&gt; &lt;text transform="translate(130,492)" dx="12" dy=".35em"&gt;Gavroche&lt;/text&gt; &lt;text transform="translate(110,249)" dx="12" dy=".35em"&gt;Valjean&lt;/text&gt; … &lt;/g&gt; </code></pre> <p>And the corresponding JavaScript:</p> <pre><code>var circle = svg.append("g") .attr("class", "nodes") .selectAll("circle") .data(nodes) .enter().append("circle") .attr("r", 4.5) .call(force.drag); var text = svg.append("g") .attr("class", "labels") .selectAll("text") .data(nodes) .enter().append("text") .attr("dx", 12) .attr("dy", ".35em") .text(function(d) { return d.name }); </code></pre> <p>This technique is used in the <a href="http://bl.ocks.org/1153292" rel="noreferrer">Mobile Patent Suits</a> example (with an additional text element used to create a white shadow).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload