简体   繁体   English

修改d3.js甜甜圈图以从json数组读取

[英]modifying d3.js donut chart to read from json array

How can I modify this example to read from a JSON array instead of CSV file? 如何修改此示例以从JSON数组而不是CSV文件读取? I will have a static JSON string that I would like to use as "data" rather than the CSV. 我将有一个静态JSON字符串,我想将其用作“数据”而不是CSV。 Any pointers will be much appreciated. 任何指针将不胜感激。

var width = 960,
height = 500,
radius = Math.min(width, height) / 2;

var color = d3.scale.ordinal()
.range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

var arc = d3.svg.arc()
.outerRadius(radius - 10)
.innerRadius(radius - 70);

var pie = d3.layout.pie()
.sort(null)
.value(function(d) { return d.population; });

var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

d3.csv("data.csv", type, function(error, data) {
if (error) throw error;

var g = svg.selectAll(".arc")
  .data(pie(data))
.enter().append("g")
  .attr("class", "arc");

 g.append("path")
  .attr("d", arc)
  .style("fill", function(d) { return color(d.data.age); });

 g.append("text")
  .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
  .attr("dy", ".35em")
  .text(function(d) { return d.data.age; });
});

function type(d) {
d.population = +d.population;
return d;
}

Sample JSON data: 样本JSON数据:

[
{
"age": "<5",
"population": 2704659
},
{
"age": "5-13",
"population": 4499890
},
{
"age": "14-17",
"population": 2159981
},
{
"age": "18-24",
"population": 3853788
},
{
"age": "25-44",
"population": 14106543
},
{
"age": "45-64",
"population": 8819342
},
{
"age": "≥65",
"population": 612463
}
]

This is an example from the following link. 这是来自以下链接的示例。 Original Example 原始例子

Not a whole lot changes, really. 确实,变化不大。 Using the example you gave, just define a var called data and assign it your sample JSON data: 使用您提供的示例,只需定义一个名为data变量,然后将其分配为示例JSON数据:

var data = [
{
  "age": "<5",
  "population": 2704659
},
{
  "age": "5-13",
  "population": 4499890
 },
...etc

Then block out or remove the d3.csv() line at line # 53. And everything works just fine. 然后屏蔽掉或删除第53行的d3.csv()行。一切正常。

Here's a fiddle for you: https://jsfiddle.net/ej2s217f/ 这是为您准备的小提琴: https : //jsfiddle.net/ej2s217f/

Just use d3.json 只需使用d3.json

var data; // a global

d3.json("path/to/file.json", function(error, json) {
  if (error) return console.warn(error);
  data = json;
  visualizeit();
});

Here is more on d3 requests . 这是有关d3请求的更多信息。

Edit 编辑

If you don't want to load an external json here is a jsfiddle 如果您不想加载外部json,请使用jsfiddle

All you have to do is drop the d3.json call and declare the var data = [...] 您要做的就是删除d3.json调用并声明var data = [...]

Basically, what remains is: 基本上,剩下的是:

    var width  = 960,
        height = 500,
        radius = Math.min(width, height) / 2;

    var color = d3.scale.ordinal()
        .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]);

    var arc = d3.svg.arc()
        .outerRadius(radius - 10)
        .innerRadius(radius - 70);

    var pie = d3.layout.pie()
        .sort(null)
        .value(function (d) {
            return d.population;
        });

    var svg = d3.select("body").append("svg")
        .attr("width", width)
        .attr("height", height)
        .append("g")
        .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

    data = [
        {
            "age": "<5",
            "population": 2704659
        },
        {
            "age": "5-13",
            "population": 4499890
        },
        {
            "age": "14-17",
            "population": 2159981
        },
        {
            "age": "18-24",
            "population": 3853788
        },
        {
            "age": "25-44",
            "population": 14106543
        },
        {
            "age": "45-64",
            "population": 8819342
        },
        {
            "age": "≥65",
            "population": 612463
        }
    ];

    var g = svg.selectAll(".arc")
        .data(pie(data))
        .enter().append("g")
        .attr("class", "arc");

    g.append("path")
        .attr("d", arc)
        .style("fill", function (d) {
            return color(d.data.age);
        });

    g.append("text")
        .attr("transform", function (d) {
            return "translate(" + arc.centroid(d) + ")";
        })
        .attr("dy", ".35em")
        .text(function (d) {
            return d.data.age;
        });

    function type(d) {
        d.population = +d.population;
        return d;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM