Skip to content

just related edges in graph in sidebar #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ extra_css:
## Graph Javascript by Apache ECharts
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`.

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.

# Docker
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]`.

Expand Down
2 changes: 0 additions & 2 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.4'

services:
mkdocs:
build:
Expand Down
42 changes: 37 additions & 5 deletions docs/ObsidianVault/assets/javascripts/interactive_graph.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
// draw graph in sidebar, change global to true if prefered
function draw_graph_sidebar(myChart, global=false) {
draw_graph(myChart, global)
}

// draw graph in modal view
function draw_graph_modal(myChart, global=true) {
draw_graph(myChart, global)
}

// add graph button next to light/dark mode switch if activated, but before search
$('.md-search').before('<form class="md-header__option"> \
<label id="graph_button" class="md-header__button md-icon"> \
Expand Down Expand Up @@ -27,9 +37,14 @@ function init_graph(params) {

var myChart = init_graph();

function draw_graph(myChart) {
function draw_graph(myChart, global=true) {
var _option = $.extend(true, {}, option);
if(!global) {
_option.series[0].data = graph_nodes();
_option.series[0].links = graph_links();
}
// draw the graph
myChart.setOption(option);
myChart.setOption(_option);

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

var option;

function graph_links() {
id = option.series[0].data.find(it => it.value === window.location.pathname).id;
return option.series[0].links.filter(it => it.source === id || it.target === id);
}

function graph_nodes() {
id = option.series[0].data.find(it => it.value === window.location.pathname).id;
links = option.series[0].links.filter(it => it.source === id || it.target === id);
ids = [];
links.forEach(function (link) {
ids.push(link.source, link.target);
});
return option.series[0].data.filter(it => [...new Set(ids)].includes(it.id));
}

$.getJSON(document.currentScript.src + '/../graph.json', function (graph) {
myChart.hideLoading();

Expand Down Expand Up @@ -111,7 +141,8 @@ $.getJSON(document.currentScript.src + '/../graph.json', function (graph) {
}
]
};
draw_graph(myChart);
// initial draw in sidebar
draw_graph_sidebar(myChart);
});

$("#__palette_0").change(function(){
Expand All @@ -134,9 +165,10 @@ $('#graph_button').on('click', function (params) {
$('#modal_background').remove();
add_graph_div();
myChart = init_graph();
draw_graph(myChart);
// re-draw sidebar, e.g. switch back from modal view
draw_graph_sidebar(myChart);
}
});
myChart = init_graph();
draw_graph(myChart);
draw_graph_modal(myChart);
});
Loading