d3.select method not working

后端 未结 3 1944
南笙
南笙 2020-12-06 10:26

I am new to datavis and the D3 library and I\'m trying to follow the tut here http://mbostock.github.com/d3/tutorial/bar-1.html

When I run the code, nothing displays

相关标签:
3条回答
  • 2020-12-06 10:45

    Using the inside the body makes it not only available to the tag or any of the but also executes it faster. Also as div is a tag u could create a class eg. one and then use it as d3.select(".one") so that it doesn't coincide.

    0 讨论(0)
  • 2020-12-06 10:59

    If you do not wish to put your <script> tags within the <body> element, you can also tell the browser to execute your d3 code (or any other JavaScript code) after the DOM is ready.

    Using a library such as jQuery, you can use:

    $( document ).ready(function() {
      // Your d3 code here
    });
    

    This will ensure that your scripts are executed after the whole DOM is ready, including the <body> element.

    For reference, examples and a shorter version of jQuery ready function, see http://learn.jquery.com/using-jquery-core/document-ready/.

    0 讨论(0)
  • 2020-12-06 11:10

    The problem is related to the position of your <script> .. </script> within the html document.

    No body element exists yet at the moment that your script is being executed. That means that d3.select("body") will be empty, and no div.chart is being appended.

    Try to move your <script> .. </script> inside the <body> .. </body> part. This will guarantee that the body element exists when your code is being executed.

    0 讨论(0)
提交回复
热议问题