0

Following the Vega documentation example of tree layout I have basic tree structure with 3 levels. The data is ingested from ES and the model caters for 4000 child nodes and 63 middle tear nodes. enter image description here

In order to have a manageable view I would like to collapse child nodes to the middle tear and allow the user to expand or collapse a middle tear node at a time

I have tried adapting tie solution posted here How to implement tree nodes toggling in Vega JS? but my knowledge is to limited to get the functionality i need.

Could someone help with a simplified solution based on this sample? simple tree

phenning
  • 93
  • 1
  • 6

1 Answers1

2

Here is an example of how to "collapse child nodes to the middle tear and allow the user to expand or collapse a middle tear node at a time". This solution only adds a Vega signal to handle mouse event and a filter to the tree dataset.

Vega tree interactive

View in Vega online Editor

 {
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "An example of Cartesian layouts for a node-link diagram of hierarchical data.",
  "width": 600,
  "height": 600,
  "padding": 5,
  "signals": [
    {
      "name": "signal_selected_node_id",
      "init": 1,
      "on": [
        {
          "events": "symbol:mouseup",
          "update": "datum['depth'] == 1 ? datum['id']: signal_selected_node_id"
        }
      ]
    }
  ],
  "data": [
    {
      "name": "tree",
      "values": [
        {"id": 0, "parent": "", "type": "root"},
        {"id": 1, "parent": 0, "type": "H-VC"},
        {"id": 2, "parent": 0, "type": "H-VC"},
        {"id": 3, "parent": 1, "type": "L-VC"},
        {"id": 4, "parent": 2, "type": "L-VC"},
        {"id": 5, "parent": 1, "type": "L-VC"}
      ],
      "transform": [
        {"type": "stratify", "key": "id", "parentKey": "parent"},
        {
          "type": "tree",
          "method": "tidy",
          "size": [{"signal": "height"}, {"signal": "width - 100"}],
          "as": ["y", "x", "depth", "children"]
        },
        {
          "type": "filter",
          "expr": "datum['depth'] <= 1 || (datum['depth'] == 2 && datum['parent'] == signal_selected_node_id)"
        }
      ]
    },
    {
      "name": "links",
      "source": "tree",
      "transform": [
        {"type": "treelinks"},
        {"type": "linkpath", "orient": "horizontal", "shape": "diagonal"}
      ]
    }
  ],
  "scales": [
    {
      "name": "color",
      "type": "linear",
      "range": {"scheme": "magma"},
      "domain": {"data": "tree", "field": "depth"},
      "zero": true
    }
  ],
  "marks": [
    {
      "type": "path",
      "from": {"data": "links"},
      "encode": {
        "update": {"path": {"field": "path"}, "stroke": {"value": "#ccc"}}
      }
    },
    {
      "type": "symbol",
      "from": {"data": "tree"},
      "encode": {
        "enter": {"size": {"value": 100}, "stroke": {"value": "#fff"}},
        "update": {
          "x": {"field": "x"},
          "y": {"field": "y"},
          "fill": {"scale": "color", "field": "depth"}
        }
      }
    },
    {
      "type": "text",
      "from": {"data": "tree"},
      "encode": {
        "enter": {
          "text": {"field": "type"},
          "fontSize": {"value": 9},
          "baseline": {"value": "middle"}
        },
        "update": {
          "x": {"field": "x", "offset": 10},
          "y": {"field": "y", "offset": -10}
        }
      }
    }
  ]
}
Roy Ing
  • 724
  • 1
  • 2
  • 2