0

Please refer to think plunk: http://plnkr.co/edit/2lvNHxvLEsNpVqNqmDUX?p=preview

I was trying to draw a circle using D3JS using javascript. My script.js file has the circle defined.

    d3.select("body")
  .append("svg")
    .attr("width",50)
    .attr("height",50)
  .append("circle")
    .attr("cx",25)
    .attr("cy",25)
    .attr("r",25)
    .style("fill","purple");

But it doesn't show up in the html after the D3 circle header. Could someone tell me what I am doing wrong?

<!DOCTYPE html>
<html>

  <head>
    <script data-require="d3@*" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
    <title>D3JS Demo - Drawing Shapes</title>
  </head>

  <body>
    <h3>SVG Bar</h3>
    <svg>
      <rect width="50" height="200" style="fill:blue"></rect>
    </svg>

    <h3>D3 Bar</h3>
    <script>
      d3.select("body")
        .append("svg")
        .append("rect")
          .attr("widt ah",50)
          .attr("height",200)
            .style("fill","blue");
    </script>

    <h3>SVG Circle</h3>
    <svg width="50" height="50">
      <circle cx="25" cy="25" r="25" style="fill:blue"></circle>
    </svg>

    <h3>D3 Circle</h3>

  </body>

</html>

1 Answer 1

2

you need to add the script after the h3,instead of head, like this:

<h3>D3 Circle</h3>
<script src="script.js"></script>

No body element exists yet at the moment that your script is being executed. That means that d3.select("body") will be empty, if the script is run in head.

http://plnkr.co/edit/Xofci5Rm9XdpsP1zqu8a?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

that worked and i will accept your answer but could you please explain why this worked?
you just edited the post with the explanation. great! thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.