when the graph is unconnected, layout by mds. except that the unfocused connected subgraph contains only one node

This commit is contained in:
shiwu.wyy 2019-08-20 23:46:51 +08:00
parent 86947e46fb
commit 69899661b6
2 changed files with 16 additions and 5 deletions

View File

@ -101,8 +101,9 @@ class Radial extends Base {
// the graph-theoretic distance (shortest path distance) matrix // the graph-theoretic distance (shortest path distance) matrix
const adjMatrix = Util.getAdjMatrix(data, false); const adjMatrix = Util.getAdjMatrix(data, false);
const unconnected = self.handleAbnormalMatrix(adjMatrix, focusIndex); self.handleAbnormalMatrix(adjMatrix, focusIndex);
const D = Util.floydWarshall(adjMatrix); const D = Util.floydWarshall(adjMatrix);
const connected = Util.isConnected(D);
self.set('distances', D); self.set('distances', D);
// the shortest path distance from each node to focusNode // the shortest path distance from each node to focusNode
@ -140,7 +141,7 @@ class Radial extends Base {
nodes[i].y = p[1] + center[1]; nodes[i].y = p[1] + center[1];
}); });
// if the graph is connected, layout by radial layout and force nonoverlap // if the graph is connected, layout by radial layout and force nonoverlap
if (!unconnected) { if (connected) {
// move the graph to origin, centered at focusNode // move the graph to origin, centered at focusNode
positions.forEach(p => { positions.forEach(p => {
p[0] -= positions[focusIndex][0]; p[0] -= positions[focusIndex][0];
@ -274,8 +275,6 @@ class Radial extends Base {
let hasDis = true; let hasDis = true;
for (let j = 0; j < matrix[i].length; j++) { for (let j = 0; j < matrix[i].length; j++) {
if (!matrix[i][j]) hasDis = false; if (!matrix[i][j]) hasDis = false;
// if the graph is unconnected return false
if (matrix[i][j] === Infinity) return false;
} }
if (hasDis) { if (hasDis) {
matrix[i][focusIndex] = 1; matrix[i][focusIndex] = 1;
@ -293,7 +292,6 @@ class Radial extends Base {
value = 0; value = 0;
} }
} }
return true;
} }
} }
module.exports = Radial; module.exports = Radial;

View File

@ -237,6 +237,19 @@ const MathUtil = {
}); });
return matrix; return matrix;
}, },
/**
* if the graph about the shortest path matrix is connected
* @param {array} matrix shortest path matrix
* @return {boolean} connected
*/
isConnected(matrix) {
if (matrix.length > 0) {
for (let j = 0; j < matrix[0].length; j++) {
if (matrix[0][j] === Infinity) return false;
}
}
return true;
},
getEDistance(p1, p2) { getEDistance(p1, p2) {
return Math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0]) return Math.sqrt((p1[0] - p2[0]) * (p1[0] - p2[0])