Simple Tree

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "usermeta": {
    "deneb": {
      "build": "1.0.0.157",
      "metaVersion": 1,
      "provider": "vega"
    },
    "interactivity": {
      "tooltip": true,
      "contextMenu": true,
      "selection": false,
      "dataPointLimit": 50
    },
    "information": {
      "name": "Tree",
      "description": "[No Description Provided]",
      "author": "Kerry Kolosko",
      "uuid": "321a61fa-f71a-4cd0-96b3-f5cf06457db1",
      "generated": "2022-02-02T11:30:35.340Z"
    },
    "dataset": [
      {
        "key": "__0__",
        "name": "id",
        "description": "",
        "type": "numeric",
        "kind": "column"
      },
      {
        "key": "__1__",
        "name": "name",
        "description": "",
        "type": "text",
        "kind": "column"
      },
      {
        "key": "__2__",
        "name": "children",
        "description": "",
        "type": "numeric",
        "kind": "measure"
      },
      {
        "key": "__3__",
        "name": "parentid",
        "description": "",
        "type": "numeric",
        "kind": "measure"
      }
    ]
  },
  "config": {
    "autosize": {
      "type": "fit",
      "contains": "padding"
    }
  },
  "data": [
    {
      "__1__": "dataset",
      "transform": [
        {
          "key": "__0__",
          "parentKey": "__3__",
          "type": "stratify"
        },
        {
          "method": "tidy",
          "separation": false,
          "as": [
            "y",
            "x",
            "depth",
            "__2__"
          ],
          "size": [
            {"signal": "height"},
            {"signal": "width - 100"}
          ],
          "type": "tree"
        }
      ]
    },
    {
      "__1__": "links",
      "source": "dataset",
      "transform": [
        {"type": "treelinks"},
        {
          "shape": "diagonal",
          "type": "linkpath",
          "orient": "horizontal"
        }
      ]
    }
  ],
  "padding": 5,
  "marks": [
    {
      "encode": {
        "update": {
          "stroke": {"value": "#ccc"},
          "path": {"field": "path"}
        }
      },
      "from": {"data": "links"},
      "type": "path"
    },
    {
      "encode": {
        "update": {
          "x": {"field": "x"},
          "fill": {
            "field": "depth",
            "scale": "color"
          },
          "y": {"field": "y"}
        },
        "enter": {
          "stroke": {"value": "#fff"},
          "size": {"value": 100}
        }
      },
      "from": {"data": "dataset"},
      "type": "symbol"
    },
    {
      "encode": {
        "update": {
          "align": {
            "signal": "datum.__2__ ? 'right' : 'left'"
          },
          "x": {"field": "x"},
          "dx": {
            "signal": "datum.__2__ ? -7 : 7"
          },
          "opacity": {"value": 1},
          "y": {"field": "y"}
        },
        "enter": {
          "fontSize": {"value": 9},
          "text": {"field": "__1__"},
          "baseline": {
            "value": "middle"
          }
        }
      },
      "from": {"data": "dataset"},
      "type": "text"
    }
  ],
  "scales": [
    {
      "__1__": "color",
      "zero": true,
      "range": {"scheme": "plasma"},
      "domain": {
        "data": "dataset",
        "field": "depth"
      },
      "type": "linear"
    }
  ]
}

5 comments

  1. Hello Kerry,
    I tried importing this json to Power BI desktop using “Import from Template”. After field matching I see lots of errors in a log.
    I would be grateful if you could check why importing of your template isn’t working.

  2. I have been looking for something JUST LIKE THIS for one of my use cases! I need to figure out whether I can adapt it to what I’m trying to visualize. Essentially, in JIRA you can have a variety of relationships between items. What I’m trying to work out is whether I can build something that shows the selected item in the center and then incoming relationships on the left and outgoing relationships on the right.
    If:
    A blocks M
    B blocks M
    C blocks M
    M blocks Y
    M blocks X
    M blocks Z

    Then I’d like to show something like:

    A —-> —-> X
    B —-> M —-> Y
    C —-> —-> Z

  3. Hello Kerry,
    Could buttons be added next to each name to open and collapse the tree?
    I’ve been trying, but when I program it, I can’t get it to perform the action when you click.

Leave a comment

Your email address will not be published. Required fields are marked *