0

I'd like to create a simple pie chart in d3.js. The request response that I'm working with looks like this:

{"record":[{"status":"request"},{"status":"requirements"},{"status":"request"}]};

From what I've seen in examples, I need to convert that object to this:

[{"label":"Request", "value":2}, {"label":"Requirements", "value":1}];

I'm happy to use d3 functions or underscore or straight javascript to accomplish this, I just haven't been able to crack it on my own. Any help would be much appreciated!

1 Answer 1

2

You can use d3.nest() to reformat your data:

var data = d3.nest()
    .key(function(d){ return d.status; })
    .rollup(function(leaves){ return leaves.length; })
    .entries(input.record)
    .map(function(d){ return {label: d.key, value: d.values}; });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply mdml! So using: var response = {"record":[{"status":"request"},{"status":"requirements"},{"status":"request"}]}; var data = d3.nest() .key(function(d){ return d.status; }) .rollup(function(leaves){ return leaves.length; }) .entries(response.record) .map(function(d){ return {label: d.key, value: d.values}; }); console.log(data); I'm getting an error: "Unexpected value translate(NaN,NaN) parsing transform attribute"
Glad to help. So my answer reformatted your data correctly, but there's an issue using that data for your plot? I can't tell what's going on without seeing your code. Maybe you can post another question with the code for your plot.
No, you nailed it. Stupid library error on my part. Thanks!!

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.