d3.js & json - simple sample code?

前端 未结 2 861
故里飘歌
故里飘歌 2020-12-13 04:31

There are some examples to get data from external json file in d3.js. But these samples do not show the json, so I really want to see how it works.

I have this json

相关标签:
2条回答
  • 2020-12-13 04:56

    Try something like this

    d3.json("data.js", function(data) {
    alert(data.length)
    });
    

    where data.js or data.json or whatever you want to call it as long as it has js content is your json file. Also try reading: https://github.com/mbostock/d3/wiki/Requests. All your code that uses the json data will be called from the json callback function.

    0 讨论(0)
  • 2020-12-13 05:00

    You can also use Jquery JSON calls if you're more familiar with those. Or you can even just use a script tag that references a variable being assigned to JSON, like so:

    <script src="us-pres.json" type="text/javascript"></script>
    

    where us-pres.json starts like this:

    var dataset = {"state":"US",...
    

    As long as you get the JSON into a variable (collection), d3 doesn't really care how you do it. Once it's there, you just assign it using the d3 .data(dataset) call.

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