SNT
SNT

Reputation: 1403

Data for sunburst charts with d3.hierarchy using D3js v4

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.

Upvotes: 5

Views: 897

Answers (1)

Shashank
Shashank

Reputation: 5660

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. :)

Upvotes: 4

Related Questions