具有水平和垂直组合布局的可折叠树

2024-05-05

我正在尝试在 D3 中创建一个可折叠树,它结合了水平(第一级和第二级)和垂直(3+级)布局。这里有一个jsfiddle http://jsfiddle.net/artemkolotilkin/z7tb23Lo/到目前为止我所得到的。除了一件事之外,它几乎就在那里。当两个同级部分展开时,它们之间的距离会增加得超过应有的距离。

我相信发生这种情况是因为 D3 认为子元素将以水平方式定位,因此它为它们分配更多空间,但我在代码中覆盖它们的位置,以便它们垂直显示。我觉得我现在需要重写计算父节点之间距离的函数,但无法弄清楚是哪一个。

这是我的 JS 代码:

var margin = {
    top: 20,
    right: 20,
    bottom: 20,
    left: 20
},
width = 1140 - margin.right - margin.left,
    height = 800 - margin.top - margin.bottom;

var i = 0,
    duration = 750;

var tree = d3.layout.tree()
    .size([height, width]);

var diagonal = d3.svg.diagonal()
    .source(function (d) {
    return {
        "x": d.source.x,
            "y": d.source.y
    };
})
    .target(function (d) {
    return {
        "x": d.target.x,
            "y": d.target.y
    };
});

var svg = d3.select("#body").append("svg")
    .attr("width", "100%")
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

root.x0 = height / 2;
root.y0 = 0;

function collapse(d) {
    if (d.children) {
        d._children = d.children;
        d._children.forEach(collapse);
        d.children = null;
    }
}

root.children.forEach(function (parent) {
    collapse(parent);
});

update(root);

function update(source) {

    // Compute the new tree layout.
    var nodes = tree.nodes(root),
        links = tree.links(nodes),
        position = [];

    nodes.forEach(function (d) {
        if (d.parent) {
            position[d.parent.id] = position[d.parent.id] || 1;
            d.position = position[d.parent.id]++;
            if (d.children && d.depth > 1) {
                d.children.forEach(function (entry) {
                    position[d.parent.id]++;
                });
            }
        }
        if (d.depth < 2) {
            d.y = d.depth * 60;
            if (d.parent) {
                columns = d.parent.children.length;
                //d.x = (d.position - 1 + columns / 12) * width / columns;
            } else {
                columns = d.children.length;
                //d.x = (width * (4 * columns - 1)) / (8 * columns);
                // d.x = width / 2;
            }
        } else {
            d.y = d.parent.y + d.position * 20;
            d.x = d.parent.x + 20;
        }
    });

    // Update the nodes…
    var node = svg.selectAll("g.node")
        .data(nodes, function (d) {
        return d.id || (d.id = ++i);
    });

    // Enter any new nodes at the parent's previous position.
    var nodeEnter = node.enter().append("g")
        .attr("class", "node")
        .attr("transform", function (d) {
        return "translate(" + source.x0 + "," + source.y0 + ")";
    })
        .on("click", click);

    nodeEnter.append("circle")
        .attr("r", 1e-6)
        .style("fill", function (d) {
        return d._children ? "lightsteelblue" : "#fff";
    });

    nodeEnter.append("text")
        .attr("x", function (d) {
        return 10;
    })
        .attr("dy", ".35em")
        .attr("text-anchor", function (d) {
        return "start";
    })
        .text(function (d) {
        //return d.name + " ("+d.x+","+d.y+")";
        return d.name;
    })
        .each(function (d) {
        d.width = this.getBBox().width;
    })
        .style("fill-opacity", 1e-6);

    // Transition nodes to their new position.
    var nodeUpdate = node.transition()
        .duration(duration)
        .attr("transform", function (d) {
        return "translate(" + d.x + "," + d.y + ")";
    });

    nodeUpdate.select("circle")
        .attr("r", 4.5)
        .style("fill", function (d) {
        return d._children ? "lightsteelblue" : "#fff";
    });

    nodeUpdate.select("text")
        .style("fill-opacity", 1);

    // Transition exiting nodes to the parent's new position.
    var nodeExit = node.exit().transition()
        .duration(duration)
        .attr("transform", function (d) {
        return "translate(" + source.x + "," + source.y + ")";
    })
        .remove();

    nodeExit.select("circle")
        .attr("r", 1e-6);

    nodeExit.select("text")
        .style("fill-opacity", 1e-6);

    // Update the links…
    var link = svg.selectAll("path.link")
        .data(links, function (d) {
        return d.target.id;
    });

    // Enter any new links at the parent's previous position.
    link.enter().insert("path", "g")
        .attr("class", "link")
        .attr("d", function (d) {
        var o = {
            x: source.x0,
            y: source.y0
        };
        return diagonal({
            source: o,
            target: o
        });
    });

    // Transition links to their new position.
    link.transition()
        .duration(duration)
        .attr("d", diagonal);

    // Transition exiting nodes to the parent's new position.
    link.exit().transition()
        .duration(duration)
        .attr("d", function (d) {
        var o = {
            x: source.x,
            y: source.y
        };
        return diagonal({
            source: o,
            target: o
        });
    })
        .remove();

    // Stash the old positions for transition.
    nodes.forEach(function (d) {
        d.x0 = d.x;
        d.y0 = d.y;
    });
}

// Toggle children on click.
function click(d) {
    if (d.children) {
        d._children = d.children;
        d.children = null;
    } else {
        d.children = d._children;
        d._children = null;
    }
    update(d);
}

None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

具有水平和垂直组合布局的可折叠树 的相关文章

  • D3 围绕一组圆圈绘制船体

    我想用 d3 围绕分组力定向图构建绘制一个船体 我已经用圆圈构建了图表 但我现在想将圆的交点与路径 船体 连接起来 如果不连接交叉点 画一个围绕这组圆的船体就足够了 我尝试过具有凸包的力导向布局 http bl ocks org 29205
  • 当以编程方式触发 d3.behavior.zoom 事件时,如何设置平移和缩放的初始值?

    下面示例中的方块是具有初始平移和比例设置的 SVG 组的一部分 单击正方形会启动缩放过渡 但过渡设置的初始值与我的默认值不同 这一点从过渡开始时的不和谐就可以明显看出 如何在以编程方式启动的缩放过渡上设置平移和缩放的初始值 var svg
  • D3更新circle-pack数据新节点与现有节点重叠

    我正在关注一般更新模式 http bl ocks org mbostock 3808234但在分层方面存在问题 使用圆形包装布局 我pack新数据 update enter and exit圆形元素 然而 当新元素enter 它们重叠upd
  • 如何在 Angular 2 中实现 D3

    我想将这段代码从 d3 js 实现到 Angular 2 组件 但我不知道如何将 js 文件调用到组件 ts 文件中 我找到了一些折线图的代码 包括index html和lineChart js 我怎样才能调用javascriptngAft
  • D3js 从数组而不是文件中获取数据

    我发现了这个优秀的 d3js 图表here http bl ocks org Caged 6476579 但就我而言 我希望此图表从数组而不是 tsv 文件中获取值 我想让它从表 中获取值 我怎样才能做到这一点 因为它使用一个函数来实现这一
  • D3 js 链接在节点下面

    我创建了图形对象 稍后可以使用更多节点和链接来扩展图形对象 第一个创作看起来不错 然后 与add函数我添加了节点 4 和链接 as you can see above the link of between node 4 and 3 is
  • d3.js 强制布局是否允许动态 linkDistance?

    我使用力布局来表示有向未加权网络 我的灵感来自以下例子 http bl ocks org mbostock 1153292 http bl ocks org mbostock 1153292 我的问题是 在我的情况下 节点之间有更多的链接
  • 错误: 属性 d="MNaN,NaNA67.5,67.5 0 1,1 NaN,NaNL0,0Z" 的值无效

    我制作了一个饼图 当所有值都存在时 它工作正常 但是当所有值都设为 0 时 在控制台中我收到 600 多个错误 错误 属性translate translate NaN NaN 的值无效 错误 属性 d M4 133182947122317
  • LeafletJs只显示一个国家

    我使用 Leafletjs 和 D3 来显示地图 我只想在地图上显示英国 Leaflet和D3是否可以只显示英国 这当然是可能的 现在的解决方案取决于您是想使用 D3 绘制英国 还是想从 Tile Server 获取它 在后一种情况下 有一
  • d3 树 - 有相同孩子的父母

    我一直在将代码从 JIT 转换为 D3 并使用树布局 我已经复制了代码http mbostock github com d3 talk 20111018 tree html http mbostock github com d3 talk
  • 动画 D3 地球仪 (d3.geo.azimuthal)

    我对 d3 javascript 库有疑问 我想使用方位角 http mbostock github com d3 talk 20111018 azimuthal html地球 我想从地球上的经度和纬度坐标插入点 并使地球动画化 而无需使用
  • 如何在 d3 中使用SimulationLinkDatum和SimulationNodeDatum

    我在使用SimulationLinkDatum 类型时遇到问题 我创建了两个类 Node 和 Link 来实现SimulationNodeDatum 和SimulationLinkDatum 当我尝试使用SimulationLinkDatu
  • 设置 D3 力定向图

    致尊敬的读者 我对 javascript 相当陌生 我也遇到过这个问题 我正在尝试实现这个力导向图的修改版本 http mbostock github com d3 ex force html http mbostock github co
  • 调整发散堆积条形图以使用通用更新模式

    我一直在使用可用的堆积条形图示例here https bl ocks org mbostock b5935342c6d21928111928401e2c8608使用以下代码 var data month Q1 2016 apples 384
  • d3.js:在 SVG 线性渐变中过渡颜色?

    正如标题所说 使用D3 js 是否可以实现线性渐变的颜色过渡 例如 如果我有这个渐变 var gradient svg append svg defs append svg linearGradient attr id gradient a
  • 根据 D3 中的属性值对对象进行排序

    我有一系列在 D3 中使用的对象 例如 var cities city London country United Kingdom index 280 city Geneva country Switzerland index 259 ci
  • 请建议 D3.js 进行 CSV 数据导入

    我正在尝试使用 D3 js 导入 CSV 数据 var englishArray d3 csv data csv function d return d value function error data var englishArray
  • JS / d3.js:突出显示相邻链接的步骤

    再会 我之前对该项目的问题是 D3 js 根据相同的 json 值动态生成源和目标 https stackoverflow com questions 41138515 d3 js dynamically generate source a
  • Typescript 中未定义的 d3.scale

    我是 Typescript 的新手 2 周 我正在从事包装 d3 js 框架的项目 我在使用 d3 d ts 命名空间 导出模块 导入时遇到问题 我的问题 当我尝试使用 d3 scale linear 时 浏览器控制台中出现错误 TypeE
  • d3.js - 具有树形布局,如何更改 X 轴以使用 D3 中的时间刻度?

    我有这个树布局 需要它在 X 轴上使用时间刻度来将节点固定为日期 另外 我需要保留根节点 它有一个is rootJSON 数据中的属性 在时间范围之外 Here http codepen io anon pen kIJxo是具有树布局工作的

随机推荐

  • 如何使用 angularjs 更改 iframe src

    p Your browser does not support iframes p 如何修改iframe的src 你还需要 sce trustAsResourceUrl否则它不会打开 iframe 内的网站 JSFiddle http js
  • 泛型函数的不同实例是否可以具有不同的静态变量?

    当我在泛型函数中使用静态变量时 泛型函数的每个实例中的变量实体都是相同的 例如 在这段代码中 fn foo
  • 为什么索引操作不调用 __getattr__ ?

    我的问题 看起来 getattr 不调用索引操作 即我不能使用 getattr 在课堂上A提供A 是否有一个原因 或者一种绕过它的方法 getattr 可以提供该功能而无需显式定义 getitem setitem 等A 最小示例 假设我定义
  • 读取txt文件中的每一行并使用windows dos命令分配变量

    我通过使用 Beyond Compare 命令行比较这 2 个文件夹 将文件从一个路径复制到 svn 工作副本 使用 Beyond Compare 进行比较后将生成报告 如果右侧存在任何额外文件 则应从 svn 存储库中删除 所以我使用下面
  • 通用 Property.GetSetMethod 的委托

    我正在尝试创建一个委托来设置泛型的属性值 但出现错误 Error binding to target method当我尝试执行以下代码时 Action
  • Ruby on Rails - 独特性

    我有一个关于唯一性验证的问题 From http guides rubyonrails org active record validations callbacks html uniqueness http guides rubyonra
  • 如何将 python 字典与多处理同步

    我正在使用 Python 2 6 和用于多线程的多处理模块 现在我想要一个同步字典 其中我真正需要的唯一原子操作是值上的 运算符 我应该用 multiprocessing sharedctypes synchronized 调用包装字典吗
  • 与恶霸算法相比,高级主选举算法有什么好处?

    我读过当前的主选举算法 如 Raft Paxos 或 Zab 如何在集群上选举主节点 但不明白为什么他们使用复杂的算法而不是简单的恶霸算法 我正在开发一个集群库并使用 UDP 多播来发送心跳消息 每个节点加入一个多播地址 并定期向该地址发送
  • Flask 会话不持久(Postman 有效,Javascript 无效)

    我正在开发一个 Flask 服务器 用于通过网络在一些后端 Python 功能和 Javascript 客户端之间进行通信 我正在尝试利用 Flask 的session变量来存储用户在与应用程序交互过程中的特定数据 我已经删除了下面大部分应
  • WebUSB 和 RFID 读取器

    我想知道是否有人有让 RFID 读取器通过 WebUSB 工作的经验 我使用的阅读器是https www parallax com product 28340 https www parallax com product 28340 根据我
  • ng-cloak 对于 Angular ui-router 在模板解析时隐藏元素没有帮助

    我正在使用角度用户界面路由器 我想展示一些东西如果 div 当模板下载并立即显示时 我们可以在控制器加载之前看到 div 的闪烁 scope total 有人会认为 scope total在开始时未定义 因此 div 将被隐藏 但我认为模板
  • Nginx 中 uwsgi_pass 和 proxy_pass 的区别?

    我在 Nginx 后面运行 uWSGI 并一直在使用proxy pass让 Nginx 访问 uWSGI 切换到有什么好处吗uwsgi pass 如果是这样 那是什么 uwsgi pass使用一个uwsgi协议 proxy pass使用普通
  • Android fill_parent 到 match_parent

    引入 match parent 和弃用 fill parent 背后的原因是什么 因为两者含义相同 此更改不会妨碍向后兼容性吗 使用 match parent 而不是 fill parent 不会使生成的 APK 在旧版本中无法运行 因为在
  • gwt 谷歌应用引擎 HTTP 错误 404

    我在 Eclipse 中使用 google 应用程序引擎创建了一个新的 gwt 项目 但是当我运行该项目时 在浏览器中我收到以下消息 HTTP 错误 404 访问 Test html 时出现问题 原因 NOT FOUND 由码头提供动力 现
  • 多维数组(如 C/C++ 中的数组)是不规则数组的特殊情况吗? [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我和一个哥们讨论了C 和C多维数组是否是不规则数组的特例 一种观点是 多维数组不是参差不齐的数组 因为多维数组的每个元素具有相同的大小 在参差不齐的数
  • IE11 中的 JavaScript 给我脚本错误 1003

    我有一个带有手风琴和一些 javascript 的网站 在 Firefox 中一切正常 但在 IE11 中出现错误 SCRIPT1003 应为 我将其范围缩小到 js 文件中的这段代码 var nmArray new Array funct
  • 如何(如果可能)更改 eclipse(月出主题)中突出显示的搜索结果的颜色选项?

    我已将 Eclipse Kepler SR2 主题更改为 Moonrise 0 8 9 现在 突出显示对象的颜色使结果几乎不可见 我没有找到任何选项来修改文本颜色或背景颜色 我已附加搜索视图 要更改突出显示的行搜索结果 请转到 Window
  • 如何在节点红色中进行http重定向?

    我需要重定向到另一个网址 我正在使用nodered请求一个url 然后获取用户名和密码 在cloudant数据库中检查它们 在发现它们存在之后 我需要重定向到另一个 home 是否有一个节点可以重定向 我尝试使用 http 请求节点 但它只
  • 如何使用 Apache HttpClient 4 获取文件上传的进度条?

    我有以下用于使用 Apache 的 HTTP Client org apache http client 上传文件的代码 public static void main String args throws Exception String
  • 具有水平和垂直组合布局的可折叠树

    我正在尝试在 D3 中创建一个可折叠树 它结合了水平 第一级和第二级 和垂直 3 级 布局 这里有一个jsfiddle http jsfiddle net artemkolotilkin z7tb23Lo 到目前为止我所得到的 除了一件事之