【问题标题】:D3.js - merge two adjacency matrices in ones with D3/JSD3.js - 用 D3/JS 合并两个邻接矩阵
【发布时间】:2020-05-19 11:37:18
【问题描述】:

我想将 2 个邻接矩阵与 d3.js 合并。举个例子:

矩阵 1:

{
    "nodes":[
        {
            "id": a,
            "year": 1
        },{
            "id": b,
            "year": 1
        },
        {
            "id": c,
            "year": 1
        }
    ],
    "links":[
        {
            "source": a,
            "target": b
        },
        {
            "source": a,
            "target": c
        }
    ]
}

矩阵 2:

{
    "nodes":[
        {
            "id": a,
            "year": 2
        },{
            "id": b,
            "year": 2
        },
        {
            "id": d,
            "year": 2
        }
    ],
    "links":[
        {
            "source": a,
            "target": b
        },
        {
            "source": a,
            "target": d
        }
    ]
}

如您所见,一些 ID 出现在两个矩阵中,例如 a 和 b。我想在两个矩阵中合并这个单元格并改变这个单元格的颜色。 有人可以给我一些想法如何解决这个问题吗?

结果一定是这样的:

{
    "nodes":[
        {
            "id": a,
            "year": 3
        },{
            "id": b,
            "year": 3
        },
        {
            "id": c,
            "year": 1
        },
                {
            "id": d,
            "year": 2
        }
    ],
    "links":[
        {
            "source": a,
            "target": b
        },
        {
            "source": a,
            "target": c
        },
        {
            "source": a,
            "target": d
        }
    ]
}

【问题讨论】:

  • 发布预期的结果对象,以便清楚地了解您想要实现的目标。
  • 感谢您的提示!我将结果数组添加到我的问题中。
  • target 没有丢失。在某些单元格中,id 之间没有任何关系。
  • 您的预期输出没有来自任一输入对象的links 属性,是故意完成的(那么,这里的决策逻辑是什么?)还是错误?
  • 哦,对不起,我忘记了。我现在将它们添加到我的结果对象中。

标签: javascript d3.js adjacency-matrix


【解决方案1】:

所以,据我所知,您需要做的事情(合并和突出显示重复节点,基于重复的邻接,而不是 id),您可以这样做:

  • 遍历links 的组合数组以找出唯一/重复的条目(同时考虑反向source/target
  • 遍历nodes 的组合数组并用year:3 标记属于重复链接的那些节点
  • 建立linksnodes 的结果对象

const m1 = {"nodes":[{"id":'a',"year":1},{"id":'b',"year":1},{"id":'c',"year":1}],"links":[{"source":'a',"target":'b'},{"source":'a',"target":'c'}]},
      m2 = {"nodes":[{"id":'a',"year":2},{"id":'b',"year":2},{"id":'d',"year":2}],"links":[{"source":'a',"target":'b'},{"source":'a',"target":'d'}]},
      
      links = [...m1.links, ...m2.links].reduce((r,l) => {
        const dup = r.find(({source:s, target: t}) => 
          (s == l.source && t == l.target) || 
          (s == l.target && t == l.source))
        dup ? dup.dup = true : r.push(l)
        return r
      }, []),
      
      nodes = [...m1.nodes, ...m2.nodes].reduce((r,n) => {
        const dupL = links.find(l => 
          l.dup && (l.source == n.id || l.target == n.id)),
              dupN = r.find(({id}) => id == n.id)
        !dupN && r.push({...n, ...(dupL && {year: 3})})
        return r
      }, []),
      
      mergedMatrix = {links:links.map(({dup,...rest}) => rest),nodes}
     
console.log(mergedMatrix)
.as-console-wrapper{min-height:100%;}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多