建立可以使用d3折叠/展开的树节点

问题描述

因此,前几天我使该树可视化工作了,现在我打算在此基础上构建并向其添加折叠节点。我的代码基于以下项目:(向Curran拍摄,精彩视频)https://vizhub.com/curran/4f92c793909f48d28012e43ddab716df?edit=files

这是当前的代码

传递给initfiletree()的“ item”包含json格式的数据,与提供的链接完全相同。

initfiletree(item) {

  const svg = select('svg');
  svg.selectAll('*').remove();
  const width = document.body.clientWidth;
  const height = document.body.clientHeight;
  const margin = { top: 0,right: 0,bottom: 0,left: 0};
  const innerWidth = width - margin.left - margin.right;
  const innerHeight = height - margin.top - margin.bottom;
  const treeLayout = tree().nodeSize([innerHeight/50,innerWidth]);


  const g = svg
      .attr('width',width)
      .attr('height',height)
    .append('g')
      .attr('transform',`translate(${margin.left},${margin.top})`);

  svg.call(zoom().on('zoom',() => {
    g.attr('transform',event.transform);
  }));

  const root = hierarchy(item);
  const links = treeLayout(root).links();
  const linkPathGenerator = linkHorizontal()
    .x(d => d.y)
    .y(d => d.x);

  g.selectAll('path')
    .data(links).enter()
    .append('path')
    .attr('d',linkPathGenerator);


  g.selectAll('text').data(root.descendants())
    .enter().append('text')
    .attr('x',d => d.y)
    .attr('y',d=> d.x)
    .attr('dy','0.32em')
    .attr('text-anchor',d => d.children ? 'middle' : 'start' )
    .attr('font-size',d => 1.5 - d.data.depth + 'em')
    .text(d => d.data.data.id);
  }

所以对于我的问题,d3中是否有任何内置函数可以集成到此代码中以使节点折叠/展开?还是需要对该函数进行大量重写?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)