Skip to content

Commit a69f52e

Browse files
committed
add obsidian graph plugin
1 parent 6c1fd55 commit a69f52e

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// add graph button next to light/dark mode switch if activated, but before search
2+
$('.md-search').before('<form class="md-header__option"> \
3+
<label id="graph_button" class="md-header__button md-icon"> \
4+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 171 146"> \
5+
<path d="M171,100 C171,106.075 166.075,111 160,111 C154.016,111 149.158,106.219 149.014,100.27 L114.105,83.503 C111.564,86.693 108.179,89.18 104.282,90.616 L108.698,124.651 C112.951,126.172 116,130.225 116,135 C116,141.075 111.075,146 105,146 C98.925,146 94,141.075 94,135 C94,131.233 95.896,127.912 98.781,125.93 L94.364,91.896 C82.94,90.82 74,81.206 74,69.5 C74,69.479 74.001,69.46 74.001,69.439 L53.719,64.759 C50.642,70.269 44.76,74 38,74 C36.07,74 34.215,73.689 32.472,73.127 L20.624,90.679 C21.499,92.256 22,94.068 22,96 C22,102.075 17.075,107 11,107 C4.925,107 0,102.075 0,96 C0,89.925 4.925,85 11,85 C11.452,85 11.895,85.035 12.332,85.089 L24.184,67.531 C21.574,64.407 20,60.389 20,56 C20,48.496 24.594,42.07 31.121,39.368 L29.111,21.279 C24.958,19.707 22,15.704 22,11 C22,4.925 26.925,0 33,0 C39.075,0 44,4.925 44,11 C44,14.838 42.031,18.214 39.051,20.182 L41.061,38.279 C49.223,39.681 55.49,46.564 55.95,55.011 L76.245,59.694 C79.889,52.181 87.589,47 96.5,47 C100.902,47 105.006,48.269 108.475,50.455 L131.538,27.391 C131.192,26.322 131,25.184 131,24 C131,17.925 135.925,13 142,13 C148.075,13 153,17.925 153,24 C153,30.075 148.075,35 142,35 C140.816,35 139.678,34.808 138.609,34.461 L115.546,57.525 C117.73,60.994 119,65.098 119,69.5 C119,71.216 118.802,72.884 118.438,74.49 L153.345,91.257 C155.193,89.847 157.495,89 160,89 C166.075,89 171,93.925 171,100"> \
6+
</path> \
7+
</svg> \
8+
</label> \
9+
</form>');
10+
11+
// add a div to html in which the graph will be drawn
12+
function add_graph_div(params) {
13+
$('.md-sidebar--secondary').each(function() {
14+
$(this).contents().append('<div id="graph" class="graph"></div>');
15+
});
16+
};
17+
18+
add_graph_div();
19+
20+
function init_graph(params) {
21+
var myChart = echarts.init(document.getElementById('graph'), null, {
22+
renderer: 'canvas',
23+
useDirtyRect: false
24+
});
25+
return myChart;
26+
};
27+
28+
var myChart = init_graph();
29+
30+
function draw_graph(myChart) {
31+
// draw the graph
32+
myChart.setOption(option);
33+
34+
// add click event for nodes
35+
myChart.on('click', function (params) {
36+
if(params.dataType == "node") {
37+
window.location = params.value;
38+
}
39+
});
40+
41+
// redraw on resize
42+
window.addEventListener('resize', myChart.resize);
43+
};
44+
45+
var option;
46+
47+
$.getJSON(document.currentScript.src + '/../graph.json', function (graph) {
48+
myChart.hideLoading();
49+
50+
// an offset of 5, so the dot/node is not that small
51+
graph.nodes.forEach(function (node) {
52+
node.symbolSize += 5;
53+
});
54+
55+
// special feature, if u want to have long note titles, u can use this ' •'
56+
// to cut everything behind in graph view
57+
graph.nodes.forEach(function (node) {
58+
node.name = node.name.split(' •')[0];
59+
});
60+
graph.links.forEach(function (link) {
61+
link.source = link.source.split(' •')[0];
62+
link.target = link.target.split(' •')[0];
63+
});
64+
65+
option = {
66+
tooltip: {
67+
show: false,
68+
},
69+
legend: [ // categories not supported yet
70+
//{
71+
// data: graph.categories.map(function (a) {
72+
// return a.name;
73+
// })
74+
//}
75+
],
76+
darkMode: "auto",
77+
backgroundColor: $("body").css("background-color"),
78+
series: [
79+
{
80+
name: 'Interactive Graph',
81+
type: 'graph',
82+
layout: 'force',
83+
data: graph.nodes,
84+
links: graph.links,
85+
categories: [],
86+
zoom: 2,
87+
roam: true,
88+
draggable: false,
89+
label: {
90+
show: true,
91+
position: 'right',
92+
formatter: '{b}'
93+
},
94+
emphasis: {
95+
focus: 'adjacency', // gray out not related nodes on mouse over
96+
label: {
97+
fontWeight: "bold"
98+
}
99+
},
100+
labelLayout: {
101+
hideOverlap: false // true could be a good idea for large graphs
102+
},
103+
scaleLimit: {
104+
min: 0.5,
105+
max: 5
106+
},
107+
lineStyle: {
108+
color: 'source',
109+
curveness: 0 // 0.3, if not 0, link an backlink will have 2 lines
110+
}
111+
}
112+
]
113+
};
114+
draw_graph(myChart);
115+
});
116+
117+
$("#__palette_0").change(function(){
118+
option.backgroundColor = $("body").css("background-color");
119+
myChart.setOption(option);
120+
});
121+
$("#__palette_1").change(function(){
122+
option.backgroundColor = $("body").css("background-color");
123+
myChart.setOption(option);
124+
});
125+
126+
$('#graph_button').on('click', function (params) {
127+
$("body").css({ overflow: "hidden", position: "fixed" });
128+
$('#graph').remove();
129+
$('<div id="modal_background"><div id="graph" class="modal_graph"></div></div>').appendTo('body');
130+
$('#modal_background').on('click', function (params) {
131+
if(params.target === this) {
132+
$("body").css({ overflow: "", position: "" });
133+
$('#graph').remove();
134+
$('#modal_background').remove();
135+
add_graph_div();
136+
myChart = init_graph();
137+
draw_graph(myChart);
138+
}
139+
});
140+
myChart = init_graph();
141+
draw_graph(myChart);
142+
});

mkdocs.yml

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ plugins:
4444
enable_creation_date: true
4545
timezone: Canada/Eastern
4646
type: datetime
47+
- obsidian-interactive-graph
48+
extra_javascript:
49+
- https://fastly.jsdelivr.net/npm/jquery/dist/jquery.min.js
50+
- https://fastly.jsdelivr.net/npm/echarts/dist/echarts.min.js
51+
- assets/javascripts/interactive_graph.js
52+
extra_css:
53+
- assets/stylesheets/interactive_graph.css
4754
markdown_extensions:
4855
- def_list
4956
- pymdownx.tasklist:

0 commit comments

Comments
 (0)