Skip to content

Commit

Permalink
optimize selection-based graph layout using node distance (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
goliath-walker committed Feb 25, 2022
1 parent 01fcc33 commit d5dfde2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export interface Note {
title: string;
links: Set<string>;
linkedToCurrentNote?: boolean;
/**
* (Minimal) distance of this note to current/selected note in Joplin
* 0 => current note itself
* 1 => directly adjacent note
* x => ... and so on
*/
distanceToCurrentNote?: number;
}

interface JoplinNote {
Expand Down Expand Up @@ -174,6 +181,7 @@ async function getLinkedNotes(
for (const joplinNote of joplinNotes) {
// store note data to be returned at the end of the traversal
const note = buildNote(joplinNote);
note.distanceToCurrentNote = degree;
noteMap.set(joplinNote.id, note);

const allLinks = [
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface Node {
id: string;
title: string;
focused: boolean;
distanceToCurrentNote?: number;
}

interface GraphData {
Expand All @@ -23,6 +24,7 @@ interface GraphData {
nodeFontSize: number;
nodeDistanceRatio: number;
showLinkDirection: boolean;
maxDegree: number;
}

let data: GraphData;
Expand Down Expand Up @@ -208,7 +210,8 @@ async function fetchData() {
nodeFontSize: await joplin.settings.value("SETTING_NODE_FONT_SIZE"),
nodeDistanceRatio:
(await joplin.settings.value("SETTING_NODE_DISTANCE")) / 100.0,
showLinkDirection
showLinkDirection,
maxDegree
};

notes.forEach(function (note, id) {
Expand Down Expand Up @@ -245,6 +248,7 @@ async function fetchData() {
id: id,
title: note.title,
focused: note.linkedToCurrentNote,
distanceToCurrentNote: note.distanceToCurrentNote
});
});

Expand Down
32 changes: 23 additions & 9 deletions src/ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,31 @@ function buildGraph(data) {
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");

const forceLink = d3
.forceLink()
.distance(200)
.id(function (d) {
return d.id;
})

if (data.maxDegree > 0) {
// we are in selection-based graph
forceLink.strength((link) => {
const minDistance = Math.min(
link.source.distanceToCurrentNote,
link.target.distanceToCurrentNote
);
if (minDistance === 0) {
return 1;
} else if (minDistance === 1) {
return 0.5;
} else return 0.1;
});
}

simulation = d3
.forceSimulation()
.force(
"link",
d3
.forceLink()
.distance(200)
.id(function (d) {
return d.id;
})
)
.force("link", forceLink)
.force(
"charge",
d3.forceManyBody().strength(function () {
Expand Down

0 comments on commit d5dfde2

Please sign in to comment.