繁体   English   中英

使用d3.js的Sankey图 - 添加链接标题的信息

[英]Sankey diagram using d3.js - Add information to link title

我正在尝试实现这个sankey示例: http ://bl.ocks.org/d3noob/c9b90689c1438f57d649

它使用CSV文件输入数据:

来源 - 目标 - 价值

链接标题显示为: 在此输入图像描述

我想在数据输入中添加另一个字段:

来源 - 目标 - 价值 - 信息

哪个应添加到链接标题中,如下所示: 在此输入图像描述

您是否看到了添加此信息的方法? 我试过改变:

<!-- language: lang-js -->

// load the data (using the timelyportfolio csv method)
d3.csv("sankey.csv", function(error, data) {

//set up graph in same style as original example but empty
graph = {"nodes" : [], "links" : []};

data.forEach(function (d) {
  graph.nodes.push({ "name": d.source });
  graph.nodes.push({ "name": d.target });
  graph.links.push({ "source": d.source,
                     "target": d.target,
                     "value": +d.value,
                     **"information": +d.information**} });
 });

// add the link titles
link.append("title")
    .text(function(d) {
        return d.source.name + " → " + 
            d.target.name + "\n" + format(d.value) + **d.information**; });

遗憾的是,这不起作用。

你没告诉我们它究竟是如何起作用的。 所以,我猜这个问题是使用字符串的一元加运算符......如果我的假设是正确的,你可能会在标题中看到一个NaN

无论如何,这些是步骤:

首先,在CSV中添加字段:

source,target,value,info
Barry,Elvis,2,foo
Frodo,Elvis,2,bar
Frodo,Sarah,2,baz
Barry,Alice,2,foobar
Elvis,Sarah,2,foobaz
Elvis,Alice,2,barbar
Sarah,Alice,4,barbaz

然后,在没有一元加号的情况下推送链接中的属性:

data.forEach(function(d) {
    graph.nodes.push({
        "name": d.source
    });
    graph.nodes.push({
        "name": d.target
    });
    graph.links.push({
        "source": d.source,
        "target": d.target,
        "value": +d.value,
        "information": d.info
    });
});

最后,获取标题中的属性:

link.append("title")
    .text(function(d) {
        return d.source.name + " → " +
            d.target.name + "\n" + format(d.value) + ", Information: " + d.information
    });

这是更新的bl.ocks: http ://bl.ocks.org/anonymous/5652b4a53cd73f0b3a493b400ec4e1b3/2cdf75a83dc79946722f975144c0ce892836a15a

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM