Skip to content

Commit 3579a28

Browse files
authored
Merge pull request #4 from daxcore/feature/just-related-edges-graph
just related edges in graph in sidebar
2 parents f7ca39b + f794629 commit 3579a28

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ extra_css:
3232
## Graph Javascript by Apache ECharts
3333
A `interactive_graph.js` example can be downloaded from [here](https://raw.githubusercontent.com/daxcore/mkdocs-obsidian-interactive-graph-plugin/main/docs/ObsidianVault/assets/javascripts/interactive_graph.js) and must be located into the docs directory under `docs/YourSiteName/assets/javascripts/interactive_graph.js`.
3434

35+
Beginning from version 0.3.0 the default graph inside the sidebar was minimized to egdes related to the current page only. The previous behavior can be restored by setting `global` to `true` at line `draw_graph_sidebar(myChart, global=false)` at top of javascript file.
36+
3537
# Docker
3638
Adapt the `.env` and `mkdocs.yml` files to your needs. `DEV=ON` will rebuild the `mkdocs-obsidian-interactive-graph-plugin` from local files. If `DEV != ON` the upstream packages of PyPI will be used. Build and start the Docker container via `docker compose up --build [-d]`.
3739

compose.yml

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.4'
2-
31
services:
42
mkdocs:
53
build:

docs/ObsidianVault/assets/javascripts/interactive_graph.js

+37-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// draw graph in sidebar, change global to true if prefered
2+
function draw_graph_sidebar(myChart, global=false) {
3+
draw_graph(myChart, global)
4+
}
5+
6+
// draw graph in modal view
7+
function draw_graph_modal(myChart, global=true) {
8+
draw_graph(myChart, global)
9+
}
10+
111
// add graph button next to light/dark mode switch if activated, but before search
212
$('.md-search').before('<form class="md-header__option"> \
313
<label id="graph_button" class="md-header__button md-icon"> \
@@ -27,9 +37,14 @@ function init_graph(params) {
2737

2838
var myChart = init_graph();
2939

30-
function draw_graph(myChart) {
40+
function draw_graph(myChart, global=true) {
41+
var _option = $.extend(true, {}, option);
42+
if(!global) {
43+
_option.series[0].data = graph_nodes();
44+
_option.series[0].links = graph_links();
45+
}
3146
// draw the graph
32-
myChart.setOption(option);
47+
myChart.setOption(_option);
3348

3449
// add click event for nodes
3550
myChart.on('click', function (params) {
@@ -44,6 +59,21 @@ function draw_graph(myChart) {
4459

4560
var option;
4661

62+
function graph_links() {
63+
id = option.series[0].data.find(it => it.value === window.location.pathname).id;
64+
return option.series[0].links.filter(it => it.source === id || it.target === id);
65+
}
66+
67+
function graph_nodes() {
68+
id = option.series[0].data.find(it => it.value === window.location.pathname).id;
69+
links = option.series[0].links.filter(it => it.source === id || it.target === id);
70+
ids = [];
71+
links.forEach(function (link) {
72+
ids.push(link.source, link.target);
73+
});
74+
return option.series[0].data.filter(it => [...new Set(ids)].includes(it.id));
75+
}
76+
4777
$.getJSON(document.currentScript.src + '/../graph.json', function (graph) {
4878
myChart.hideLoading();
4979

@@ -111,7 +141,8 @@ $.getJSON(document.currentScript.src + '/../graph.json', function (graph) {
111141
}
112142
]
113143
};
114-
draw_graph(myChart);
144+
// initial draw in sidebar
145+
draw_graph_sidebar(myChart);
115146
});
116147

117148
$("#__palette_0").change(function(){
@@ -134,9 +165,10 @@ $('#graph_button').on('click', function (params) {
134165
$('#modal_background').remove();
135166
add_graph_div();
136167
myChart = init_graph();
137-
draw_graph(myChart);
168+
// re-draw sidebar, e.g. switch back from modal view
169+
draw_graph_sidebar(myChart);
138170
}
139171
});
140172
myChart = init_graph();
141-
draw_graph(myChart);
173+
draw_graph_modal(myChart);
142174
});

0 commit comments

Comments
 (0)