简体   繁体   English

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

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

I am trying to implement this sankey example: http://bl.ocks.org/d3noob/c9b90689c1438f57d649 我正在尝试实现这个sankey示例: http ://bl.ocks.org/d3noob/c9b90689c1438f57d649

It uses a CSV file for data input with the fields: 它使用CSV文件输入数据:

source - target - value 来源 - 目标 - 价值

The link title is displayed as: 链接标题显示为: 在此输入图像描述

I would like to add another field to the data input: 我想在数据输入中添加另一个字段:

source - target - value - information 来源 - 目标 - 价值 - 信息

Which should be added to the link title as follows: 哪个应添加到链接标题中,如下所示: 在此输入图像描述

Do you see a way to add this information? 您是否看到了添加此信息的方法? I tried changing: 我试过改变:

<!-- 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**; });

Which is unfortunately not working. 遗憾的是,这不起作用。

You didn't tell us exactly how it is not working. 你没告诉我们它究竟是如何起作用的。 So, I'd guess the problem is the unary plus operator being used with strings... if my assumption is correct, you're probably seeing a NaN in the title. 所以,我猜这个问题是使用字符串的一元加运算符......如果我的假设是正确的,你可能会在标题中看到一个NaN

Anyway, these are the steps: 无论如何,这些是步骤:

First, add the field in the CSV: 首先,在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

Then, push the property in the links, without the unary plus: 然后,在没有一元加号的情况下推送链接中的属性:

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
    });
});

Finally, get the property in the title: 最后,获取标题中的属性:

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

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

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

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