Vega 中折叠和展开树节点的简单方法

Simple way in Vega to colapse and exspand a Tree node

根据 Vega 树布局文档示例,我有 3 个级别的基本树结构。数据是从 ES 中提取的,该模型适用于 4000 个子节点和 63 个中间撕裂节点。

为了有一个易于管理的视图,我想将子节点折叠到中间撕裂,并允许用户一次展开或折叠中间撕裂节点

我已经尝试调整此处发布的 tie 解决方案 How to implement tree nodes toggling in Vega JS? 但我的知识有限,无法获得我需要的功能。

有人可以根据此示例提供简化的解决方案吗? simple tree

这里是如何“将子节点折叠到中间撕裂并允许用户一次展开或折叠中间撕裂节点”的示例。此解决方案仅添加一个 Vega 信号来处理鼠标事件和一个过滤器到树数据集。

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}
        }
      }
    }
  ]
}