0

How can I modify this example to read from a JSON array instead of CSV file? I will have a static JSON string that I would like to use as "data" rather than the 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:

[
{
"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

2 Answers 2

2

Not a whole lot changes, really. Using the example you gave, just define a var called data and assign it your sample JSON data:

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.

Here's a fiddle for you: https://jsfiddle.net/ej2s217f/

Sign up to request clarification or add additional context in comments.

Comments

0

Just use 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.

Edit

If you don't want to load an external json here is a jsfiddle

All you have to do is drop the d3.json call and declare the 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;
    }

2 Comments

I am NOT saving my file locally. I will have a json style array defined as above. so var data = [{"age" : "<5", "population" : "1211"}] etc
@konrad here you go :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.