来自本地 json 变量的 d3 饼图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19716671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:59:53  来源:igfitidea点击:

d3 piechart from local json variable

jsond3.js

提问by jotamon

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

我正在尝试使用来自局部变量(而不是外部文件)的 json 创建一个甜甜圈图我看过这篇文章,但我很难以同样的方式调整我的代码

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?

查看下面的小提琴,其中我的数据被加载为名为 myJson 的变量。我如何将这些数据用于甜甜圈?

Fiddle

小提琴

回答by Lars Kotthoff

  1. Rename your JSON variable to data.
  2. Delete the d3.jsoncall (but keep the code within it).
  1. 将您的 JSON 变量重命名为data.
  2. 删除d3.json调用(但保留其中的代码)。

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

在这里完成 jsfiddle 。您还需要将 SVG 附加到“body”而不是“#body”。