d3 piechart from local json variable

2020-06-13 16:29发布

问题:

I am trying to create a donut chart using json from a local variable (rather than an external file) I have seen this post but am having a hard time adjusting my code in the same way

In the above example

d3.json("flare.json", function(error, root) {

was replaced with

root = JSON.parse( myjson );

which does not fix my problem

Check out the fiddle below where my data is loaded as variable titled myJson. How can I use this data for the donut?

Fiddle

回答1:

  1. Rename your JSON variable to data.
  2. Delete the d3.json call (but keep the code within it).

Complete jsfiddle here. You also need to append your SVG to "body" and not "#body".



标签: json d3.js