Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6848047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:49:12+00:00 2026-05-27T00:49:12+00:00

Original: http://mbostock.github.com/d3/ex/force.html In his example as provided in the above link, I’m simply trying

  • 0

Original: http://mbostock.github.com/d3/ex/force.html

In his example as provided in the above link, I’m simply trying to replace the circle nodes with their names instead. I don’t know much about D3 nor js/jquery but I’m trying to figure how it works.

I was able to replace the nodes with svg:text but when I do, they just “spawn” wherever they start and they don’t animate.

I don’t know if I should use groups here. If I do, teach me how.

So far, this is my modified code:

<div id="chart">
</div>

<script type="text/javascript">
<!--

var w = 960,
    h = 500,
    fill = d3.scale.category20();

var vis = d3.select("#chart").append("svg:svg")
    .attr("width", w)
    .attr("height", h);

d3.json("http://fourthdraft.com/ext/dataviz/miserables.json", function(json) {
  var force = d3.layout.force()
      .charge(-120)
      .linkDistance(70)
      .nodes(json.nodes)
      .links(json.links)
      .size([w, h])
      .start();

  var link = vis.selectAll("line.link")
      .data(json.links)
    .enter().append("svg:line")
      .attr("class", "link")
      .style("stroke-width", function(d) { return Math.sqrt(d.value); })
      .attr("x1", function(d) { return d.source.x; })
      .attr("y1", function(d) { return d.source.y; })
      .attr("x2", function(d) { return d.target.x; })
      .attr("y2", function(d) { return d.target.y; });

  var node = vis.selectAll("circle.node")
      .data(json.nodes) 
    .enter().append("svg:text")
      .attr("class", "node")
      .attr("x", function(d) { return d.x; })
      .attr("y", function(d) { return d.y; })
      .text(function(d) { return d.name; })
      .style("fill", function(d) { return fill(d.group); })
      .call(force.drag);

  node.append("svg:title")
      .text(function(d) { return d.name; });

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("cx", function(d) { return d.x; })
        .attr("cy", function(d) { return d.y; });
  });
});

//-->
</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:49:13+00:00Added an answer on May 27, 2026 at 12:49 am

    See this fiddle: http://jsfiddle.net/nrabinowitz/QMKm3/6/

    The main issue with your code above is that you correctly changed the cx and cy attributes (which are specific to the svg:circle element) to x and y in the part where you appended the svg:text elements, but you didn’t change them in the tick handler, which is where the iterative layout update happens:

    force.on("tick", function() {
        // snip
    
        node.attr("x", function(d) { return d.x; })
            .attr("y", function(d) { return d.y; });
    });
    

    You also should change the selection-and-appending from

    var node = vis.selectAll("circle.node")
      .data(json.nodes)
    .enter().append("svg:text")
    

    to

    var node = vis.selectAll("text.node")
      .data(json.nodes)
    .enter().append("svg:text")
    

    Even though I don’t think this makes any difference in the context of this code, it will eventually trip you up – in D3, as explained here, you generally use the pattern “select with a selector, add missing nodes that match this selector, remove extra nodes that match this selector”. In your code, the selector and the nodes you add don’t match, which is a conceptual issue even if it doesn’t have any ramifications for your code as written. (I should note that I still find this pattern a bit confusing and weird. But at a minimum, following it will make your code more legible to other developers.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have provided the test website link below: http://www.naughtyfancydress.com/html/outfit.html I am trying to integrate
The original URL is http://mysite.com/locations.aspx When someone clicks on Locations link somewhere and come
I'm trying a redirect on a page that goes like this: original URL: http://www.foo.com/products/
From http://items.sjbach.com/319/configuring-vim-right (2009 archive from original) I got that you were supposed to be
Example real site http://stackoverflow.com/ URL Masking http://masking.com/stackoverflow Question How to switch the URL masking
I'm trying to read a calendar feed from http://meetup.com/ , but it seems that
Original source: http://twitter.com/tobeytailor/status/8998006366 (x=[].reverse)() === window // true I've noticed that this behavior affects
This is an example of what I am trying to achieve. The original URL:
There's at least two Bcrypt implementations in C out there: The original: http://bcrypt.sourceforge.net/ Openwall's:
(The original question was asked there : http://www.ogre3d.org/phpBB2/viewtopic.php?t=44832 ) Someone asked : While I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.