Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data for sunburst charts with d3.hierarchy using D3js v4

Tags:

I am trying to get a sunburst chart using d3.hierarchy with d3js v4 . I did the same as an example here. But since the data I was getting as an array of objects and not the same as used in the example I did add a small function so that the data would be the same as from the example. Below is the function

const arrayToObject = (array) =>
    array.reduce((obj, item) => {
        obj[item.name] = item
        return obj
}, {})

Here is the link for the fiddle: https://jsfiddle.net/snt1/mbszu1u5/8/

Thank You.

like image 603
SNT Avatar asked Nov 15 '17 22:11

SNT


1 Answers

I'm not sure if this is an answer (I think it is) or I should post this as a comment BUT here's the thing with your code:

d3.hierarchy() looks for an object with "name" and "children" and manipulates the data which is then used by partition(root).

If you debug the code at https://bl.ocks.org/maybelinot/5552606564ef37b5de7e47ed2b7dc099, you'll see that d3.hierarchy() receives an object as {name: "flare", children: Array[15]}

In your code, if I just wrap the data array in an object, say: {name: "test", children: data}, it creates a sunburst with colors and appropriate titles.

Here's a FIDDLE with the changes. (btw I've commented arrayToObject)

Hope this helps. :)

like image 112
Shashank Avatar answered Sep 22 '22 13:09

Shashank