Show total values in Sankey Diagram

余生长醉 提交于 2019-12-11 03:12:07

问题


Is it possible to show a total in sankey diagram?

For example, the fiddle bellow show Fruits > Place. I would like to know how many fruits are liked to the places and in places how many fruits are linked. So something like:

Mango (3)
Apple (2)
Pineapple (1)

All three are linked to Place A, so Place A should be:

(7) Place A

(1 more is because of Grape)

http://jsfiddle.net/Khrys/5c2urqbx/

UPDATE: Looks like the v42 added weight by default.


回答1:


Take a look at this jsfiddle

code

data1 = [
              ['Apple', 'Place A', 1],
              ['Apple', 'Place A', 1],
              ['Grape', 'Place A', 1],
              ['Grape', 'Place B', 2],
              ['Pineapple', 'Place A', 1],
              ['Strawberry', 'Place B', 4],
              ['Mango', 'Place A', 3]
];

var sourceTotals = {};

for (var i = 0; i < data1.length; i++) {
    var item = data1[i],
        key = item[0];
    if (sourceTotals.hasOwnProperty(key)) {
        sourceTotals[key] += item[2];
    }
    else {
        sourceTotals[key] = item[2];
    }
}

console.log(sourceTotals);

var destTotals = {};

for (var i = 0; i < data1.length; i++) {
    var item = data1[i],
        key = item[1];
    if (destTotals.hasOwnProperty(key)) {
        destTotals[key] += item[2];
    }
    else {
        destTotals[key] = item[2];
    }
}

console.log(destTotals);

data1.forEach( function(d) {
    d[0] = d[0] + " (" + sourceTotals[d[0]] + ")";
    d[1] = "(" + destTotals[d[1]] + ") " + d[1];
})



回答2:


If you use d3.js sankey, it will automatically sum the values and display it in the tooltip as per this example...http://bl.ocks.org/d3noob/c9b90689c1438f57d649



来源:https://stackoverflow.com/questions/28840141/show-total-values-in-sankey-diagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!