5

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.

Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
SNT
  • 1,283
  • 3
  • 32
  • 78

1 Answers1

4

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

Shashank
  • 5,570
  • 1
  • 11
  • 17