mirror of
https://gitee.com/antv/g6.git
synced 2024-12-02 19:58:46 +08:00
35 lines
875 B
JavaScript
35 lines
875 B
JavaScript
/**
|
|
* @fileOverview clean data
|
|
* @author huangtonger@aliyun.com
|
|
*/
|
|
|
|
const G6 = require('@antv/g6');
|
|
const Util = G6.Util;
|
|
|
|
const dataCleaner = {
|
|
clearData(data) {
|
|
const nodeMap = {};
|
|
const invalidEdges = [];
|
|
data.nodes.forEach(node => {
|
|
nodeMap[node.id] = node;
|
|
});
|
|
data.edges.forEach(edge => {
|
|
// console.log(edge, nodeMap[edge.source], nodeMap[edge.target]);
|
|
if (!nodeMap[edge.source]) {
|
|
console.warn('remove ', edge, 'because there is no source node');
|
|
invalidEdges.push(edge);
|
|
return;
|
|
}
|
|
if (!nodeMap[edge.target]) {
|
|
console.warn('remove ', edge, 'because there is no target node');
|
|
invalidEdges.push(edge);
|
|
return;
|
|
}
|
|
});
|
|
data.edges = data.edges.filter(edge => {
|
|
return invalidEdges.indexOf(edge) === -1;
|
|
});
|
|
}
|
|
};
|
|
Util.mix(Util, dataCleaner);
|