Skip to content

Commit c62adf8

Browse files
Node folding algorithm fix
1 parent c7c9698 commit c62adf8

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "iknow-entity-browser",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "Visualizer for iKnow entities",
55
"main": "gulpfile.babel.js",
66
"scripts": {

src/static/js/graph/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ export function update (reset = false) {
188188
node = nodes.selectAll(".node").data(graph.nodes, (d) => d.id);
189189
node.exit().remove();
190190
let nodeEnter = node.enter().append("g")
191-
.attr("class", d => `node ${ d.type || "unknown" }`)
191+
.attr("class", d => `node${ d.id === 0 ? " root" : "" } ${ d.type || "unknown" }`)
192192
.call(dragger)
193193
.on("dblclick", function (d) {
194194
d3.event.stopPropagation();

src/static/js/model/index.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { toggleLoader } from "../utils";
44

55
let SIZE_CRITERIA = "frequency",
66
FOLDING_CRITERIA = "frequency",
7-
MAX_CHILDREN = 20;
7+
MAX_CHILDREN = 20,
8+
MIN_RADIUS = 5;
89

910
function fold (tree) {
1011
if (!tree.children || tree.children.length <= MAX_CHILDREN)
@@ -25,8 +26,17 @@ function fold (tree) {
2526
return tree;
2627
}
2728

29+
/**
30+
* Converts flat input data into a tree using implicit parent-children relationships. This
31+
* function also sorts the children data by relevance.
32+
* @param graph
33+
* @param parent
34+
* @returns {*}
35+
*/
2836
function toTree (graph, parent) {
37+
2938
parent.children = [];
39+
3040
for (let edge of graph.edges) {
3141
if (edge.source !== parent.id)
3242
continue;
@@ -42,12 +52,29 @@ function toTree (graph, parent) {
4252
parent.children.push(toTree(graph, t));
4353
}
4454
}
45-
parent.children.sort((a, b) => a[FOLDING_CRITERIA] > b[FOLDING_CRITERIA] ? 1 : -1);
55+
56+
try {
57+
parent.children.sort(
58+
(a, b) => a.entities[0][FOLDING_CRITERIA] > b.entities[0][FOLDING_CRITERIA] ? -1 : 1
59+
);
60+
} catch (e) {
61+
console.error(`Error! Most likely, one of the graph nodes does not has any entities.`
62+
+ ` The folding criteria displayed on the diagram would be random.`);
63+
}
64+
4665
return parent;
66+
4767
}
4868

69+
/**
70+
* This function converts graph model data to internal application's format.
71+
* @param graph
72+
* @returns {{graph: *, tree: *, foldedTree: *}}
73+
*/
4974
function preprocess (graph) {
75+
5076
let zeroID = null;
77+
5178
graph.nodes.forEach(node => { if (!zeroID && node.id === 0) zeroID = node; });
5279
if (!zeroID) {
5380
graph.nodes.unshift(zeroID = {
@@ -56,23 +83,24 @@ function preprocess (graph) {
5683
type: "entity",
5784
entities: [{
5885
id: -1,
59-
value: getOption("seed")
86+
value: getOption("seed"),
87+
[SIZE_CRITERIA]: 10000
6088
}]
6189
});
6290
}
6391
graph.nodes.forEach(node =>
64-
node.radius = 5 + Math.sqrt(node.entities[0][SIZE_CRITERIA] / 4 || 25));
65-
console.log(graph);
66-
// fold(graph, zeroID);
92+
node.radius = MIN_RADIUS + Math.sqrt(node.entities[0][SIZE_CRITERIA] / 4 || 25));
93+
console.log(`Graph:`, graph);
6794
let tree = toTree(graph, zeroID),
6895
foldedTree = fold(tree);
69-
console.log(tree);
96+
console.log(`Tree:`, tree);
7097

7198
return {
7299
graph: graph,
73100
tree: tree,
74101
foldedTree: foldedTree
75102
};
103+
76104
}
77105

78106
let graph = preprocess(sampleData.graph);

0 commit comments

Comments
 (0)