在Sankey图中显示总值 [英] Show total values in Sankey Diagram

查看:105
本文介绍了在Sankey图中显示总值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





例如,小提琴的波纹管显示水果>地方。我想知道有多少水果喜欢到这些地方以及有多少水果有联系。例如:

 芒果(3)
苹果(2)
菠萝(1)

所有三个都与A地点相关联,所以地点A应该是:

 (7)地点A 

( 1更多是因为葡萄)



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



更新:看起来像v42默认增加了重量。

解决方案

请看这个 jsfiddle





code

  data1 = [
['Apple' ,'Place A',1],
['Apple','Place A',1],
['Grape','Place A',1],
['Grape , 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);
$ b $ data1.forEach(function(d){
d [0] = d [0] +(+ sourceTotals [d [0]] +);
d [1] =(+ destTotals [d [1]] +)+ d [1];
})


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.

解决方案

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

这篇关于在Sankey图中显示总值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆