omer akkoseoglu
omer akkoseoglu

Reputation: 117

VEGA Sunburst using table based raw data

I am working on a Sunburst that would eventually take input data without a configured parent-child structure but a column based data so I employed a nest transform instead of a stratify as in the given example:

https://vega.github.io/editor/#/gist/c1eeb9142fd7f611513f5a4edf7e180e/spec.json

The problem with it, is that it generates internal nodes even if some fields are empty as long as my data object got value available for leaf nodes.

How can i transform my data to get a visual as in the following example

https://vega.github.io/editor/#/gist/112c45cfed3b9c9013ea0b63a318292f/spec.json

Upvotes: 2

Views: 253

Answers (1)

omer akkoseoglu
omer akkoseoglu

Reputation: 117

The idea is to "transform" the column based data into a parent, child structure in order to apply a Stratify transform.

Starting data:

     {
          "value": 140,
          "level1": "France",
          "level2": "Reins",
          "level3": "Jean Monnet",
          "level4": "rue 3 ",
          "level5": "no",
          "level6": "a level"
        }

First group by all the levels or depth of the sunburst according to your value or quantity.

    "transform": [
        {
          "type": "aggregate",
          "groupby": [
            "level1",
            "level2",
            "level3",
            "level4",
            "level5",
            "level6"
          ],
          "fields": [
            "value"
          ],
          "ops": [
            "sum"
          ],
          "as": [
            "value"
          ]
        }
      ]

Secondly, we need to have a root note for stratify, so based on available data we can transform our data as following to define our root node having level1 elements as child nodes:

     {
      "name": "isolatedLevel1",
      "source": [
        "mainData"
      ],
      "transform": [
        {
          "type": "project",
          "fields": [
            "level1",
            "currentNode",
            "value"
          ],
          "as": [
            "currentNode",
            "parent",
            "value"
          ]
        },
        {
          "type": "aggregate",
          "groupby": [
            "parent",
            "currentNode"
          ],
          "fields": [
            "value"
          ],
          "ops": [
            "sum"
          ],
          "as": [
            "value"
          ]
        },
        {
          "type": "formula",
          "as": "parent",
          "expr": "!isDefined(datum.parent)?'rootID':datum.parent"
        },
        {
          "type": "project",
          "fields": [
            "currentNode",
            "parent",
            "value"
          ],
          "as": [
            "currentNode",
            "parent",
            "value"
          ]
        },
        {
          "type": "filter",
          "expr": "datum.currentNode"
        }
      ]
    },
    {
      "name": "rootNode",
      "source": "isolatedLevel1",
      "transform": [
        {
          "type": "project",
          "fields": [
            "parent",
            "level2"
            
          ],
          "as": [
            "currentNode",
            "parent"
          ]
        },
        {
  "type": "aggregate",
  "groupby": ["currentNode","parent"]
},
{
          "type": "project",
          "fields": [
            "currentNode",
            "parent"
            
          ],
          "as": [
            "currentNode",
            "parent"
          ]
        }
      ]
    },

    {
      "name": "isolatedLevel1MergedWithParent",
      "source": [
        "isolatedLevel1",
        "rootNode"
      ]
    },

Then, the idea is to determine for the upcoming levels, the parent and child between 2 levels. We can apply this data transform and repeat it till the last level of our data.

 {
  "name": "isolatedLevel2",
  "source": "mainData",
  "transform": [
    {
      "type": "project",
      "fields": [
        "level1",
        "level2",
        "value"
      ],
      "as": [
        "parent",
        "currentNode",
        "value"
      ]
    },
    {
      "type": "aggregate",
      "groupby": [
        "parent",
        "currentNode"
      ],
      "fields": [
        "value"
      ],
      "ops": [
        "sum"
      ],
      "as": [
        "value"
      ]
    },
    {
      "type": "project",
      "fields": [
        "currentNode",
        "parent",
        "value"
      ],
      "as": [
        "currentNode",
        "parent",
        "value"
      ]
    },
    {
      "type": "filter",
      "expr": "datum.currentNode"
    },
    {
      "type": "filter",
      "expr": "datum.parent"
    }
  ]
}

Finally, we merge all these "decomposed" transforms all together to get back our data, transformed into a parent child hierarchy, with values associated to its nodes only. This is a way to suit your data to a Stratify VEGA transform.

{
  "name": "mergedlevelsFinal",
  "source": [
    "isolatedLevel1",
    "isolatedLevel2",
    "isolatedLevel3",
    "isolatedLevel4",
    "isolatedLevel5",
    "isolatedLevel6",
    "rootNode"
  ],
  "transform": [
    {
      "type": "formula",
      "as": "value",
      "expr": "!indata('parents','parent',datum.currentNode)? datum.value:''"
    }
  ]
}

The hard part gone, we can now apply our stratify to the transformed data as we want for our sunburst.

  {
  "name": "tree",
  "source": [
    "mergedlevelsFinal"
  ],
  "transform": [
    {
      "type": "stratify",
      "key": "currentNode",
      "parentKey": "parent"
    },
    {
      "type": "partition",
      "field": "value",
      "sort": {
        "field": "value"
      },
      "size": [
        {
          "signal": "2 * PI"
        },
        {
          "signal": "width / 3"
        }
      ],
      "as": [
        "a0",
        "r0",
        "a1",
        "r1",
        "depth",
        "children"
      ]
    },
    {
      "type": "formula",
      "expr": "!datum.children?datum.value:''",
      "as": "value"
    }
  ]
}


    

Full Spec Solution available here https://vega.github.io/editor/#/gist/0f66d06894b61f8ba131f13f564e8c1f/spec.json

Upvotes: 1

Related Questions