Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
feat: decrease coupling between vue components
Browse files Browse the repository at this point in the history
  • Loading branch information
jobulcke committed Sep 27, 2023
1 parent cabbfbb commit 8f8ed1b
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 357 deletions.
11 changes: 8 additions & 3 deletions vue-frontend/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<script setup>
import LeafletMap from './components/map/LeafletMap.vue'
import {ref} from "vue";
import KnowledgeGraph from "@/components/graph/KnowledgeGraph.vue";
const selectedMember = ref(null);
</script>

<template>

<main>

<div>
<div style="display: flex; justify-content: space-between">
<div>
<KnowledgeGraph :member-id="selectedMember"></KnowledgeGraph>
</div>
<div>
<LeafletMap></LeafletMap>
<LeafletMap @marker-clicked="id => selectedMember = id"></LeafletMap>
</div>

</div>
Expand Down
182 changes: 9 additions & 173 deletions vue-frontend/src/components/graph/KnowledgeGraph.vue
Original file line number Diff line number Diff line change
@@ -1,183 +1,19 @@
<template>
<div>
<svg style="height:500px; width:100%"></svg>
<svg style="height:500px; width:600px"></svg>
</div>
</template>
<script>
import * as d3 from "d3";
import axios from 'axios'
import {triplesToGraph} from "@/components/graph/functions/triplesToGraph";
import {useTriplesFetching} from "@/components/graph/composables/useTriplesFetching";
export default {
methods:
{
async visualizeTriples(triples) {
const svg = d3.select("svg");
let graph = triplesToGraph(triples);
let force = d3.forceSimulation(graph.nodes);
function dragstart() {
d3.select(this).classed("fixed", true);
}
function clamp(x, lo, hi) {
return x < lo ? lo : x > hi ? hi : x;
}
function dragged(event, d) {
d.fx = clamp(event.x, 0, width);
d.fy = clamp(event.y, 0, height);
force.alpha(1).restart();
}
const drag = d3.drag().on("start", dragstart).on("drag", dragged);
svg
.append("svg:defs")
.selectAll("marker")
.data(["end"])
.enter()
.append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 30)
.attr("refY", -0.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("svg:polyline")
.attr("points", "0,-5 10,0 0,5");
var links = svg
.selectAll(".link")
.data(graph.links)
.enter()
.append("line")
.attr("marker-end", "url(#end)")
.attr("class", "link")
.attr("stroke-width", 1); //links
// ==================== Add Link Names =====================
var linkTexts = svg
.selectAll(".link-text")
.data(graph.links)
.enter()
.append("text")
.attr("class", "link-text")
.text(function (d) {
return d.predicate;
});
// ==================== Add Link Names =====================
var nodeTexts = svg
.selectAll(".node-text")
.data(graph.nodes)
.enter()
.append("text")
.attr("class", "node-text")
.text(function (d) {
return d.label;
});
// ==================== Add Node =====================
var nodes = svg
.selectAll(".node")
.data(graph.nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("r", 8)
.on("click", async function (d) {
let triples = await axios
.get('http://localhost:8080/'+d.name)
await this.visualizeTriples(triples.data);
// alert("You clicked on node " + d.name);
}.bind(this))
.call(drag);
function ticked() {
nodes
.attr("cx", function (d) {
return d.x;
})
.attr("cy", function (d) {
return d.y;
});
links
.attr("x1", function (d) {
return d.source.x;
})
.attr("y1", function (d) {
return d.source.y;
})
.attr("x2", function (d) {
return d.target.x;
})
.attr("y2", function (d) {
return d.target.y;
});
nodeTexts
.attr("x", function (d) {
return d.x + 12;
})
.attr("y", function (d) {
return d.y + 3;
});
linkTexts
.attr("x", function (d) {
return 4 + (d.source.x + d.target.x) / 2;
})
.attr("y", function (d) {
return 4 + (d.source.y + d.target.y) / 2;
});
}
force.on("tick", ticked);
force
.force(
"link",
d3.forceLink(graph.links).id((d) => d.id)
)
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 2, height / 2));
}
}, async mounted() {
let triples = [
{
subject: "ex:ThaiLand",
predicate: "ex:hasFood",
object: "ex:TomYumKung",
},
{
subject: "ex:TomYumKung",
predicate: "rdf:type",
object: "ex:SpicyFood",
},
{
subject: "ex:TomYumKung",
predicate: "ex:includes",
object: "ex:shrimp",
},
{
subject: "ex:TomYumKung",
predicate: "ex:includes",
object: "ex:chilly",
},
{
subject: "ex:TomYumKung",
predicate: "ex:includes",
object: "ex:lemon",
},
{subject: "ex:lemon", predicate: "ex:hasTaste", object: "ex:sour"},
{subject: "ex:chilly", predicate: "ex:hasTaste", object: "ex:spicy"},
];
// let triples = await axios
// .get('http://localhost:8080/start')
await this.visualizeTriples(triples);
props: {
memberId: String | null,
},
watch: {
memberId: function (newVal) {
useTriplesFetching(newVal)
}
},
};
</script>
Expand Down
146 changes: 146 additions & 0 deletions vue-frontend/src/components/graph/composables/useTriplesFetching.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import axios from "axios";
import * as d3 from "d3";
import {triplesToGraph} from "@/components/graph/functions/triplesToGraph";

function visualizeTriples(triples) {
let children = d3.select("svg").selectAll("*")
children.remove();

const width = 600;
const height = 500;
let svg = d3.select("svg").attr("width", width).attr("height", height);
let graph = triplesToGraph(triples);
let force = d3.forceSimulation(graph.nodes);

function dragstart() {
d3.select(this).classed("fixed", true);
}

function clamp(x, lo, hi) {
return x < lo ? lo : x > hi ? hi : x;
}

function dragged(event, d) {
d.fx = clamp(event.x, 0, width);
d.fy = clamp(event.y, 0, height);
force.alpha(1).restart();
}

const drag = d3.drag().on("start", dragstart).on("drag", dragged);

svg
.append("svg:defs")
.selectAll("marker")
.data(["end"])
.enter()
.append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("refX", 30)
.attr("refY", -0.5)
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("svg:polyline")
.attr("points", "0,-5 10,0 0,5");

let links = svg
.selectAll(".link")
.data(graph.links)
.enter()
.append("line")
.attr("marker-end", "url(#end)")
.attr("class", "link")
.attr("stroke-width", 1); //links
// ==================== Add Link Names =====================
let linkTexts = svg
.selectAll(".link-text")
.data(graph.links)
.enter()
.append("text")
.attr("class", "link-text")
.text(function (d) {
return d.predicate;
});
// ==================== Add Link Names =====================
let nodeTexts = svg
.selectAll(".node-text")
.data(graph.nodes)
.enter()
.append("text")
.attr("class", "node-text")
.text(function (d) {
return d.label;
});
// ==================== Add Node =====================
let nodes = svg
.selectAll(".node")
.data(graph.nodes)
.enter()
.append("circle")
.attr("class", "node")
.attr("r", 8)
.call(drag);

function ticked() {
nodes
.attr("cx", function (d) {
return 2 * d.x;
})
.attr("cy", function (d) {
return 2 * d.y;
});

links
.attr("x1", function (d) {
return 2 * d.source.x;
})
.attr("y1", function (d) {
return 2 * d.source.y;
})
.attr("x2", function (d) {
return 2 * d.target.x;
})
.attr("y2", function (d) {
return 2 * d.target.y;
});

nodeTexts
.attr("x", function (d) {
return 2 * d.x + 12;
})
.attr("y", function (d) {
return 2 * d.y + 3;
});

linkTexts
.attr("x", function (d) {
return 4 + 2 * (d.source.x + d.target.x) / 2;
})
.attr("y", function (d) {
return 4 + 2 * (d.source.y + d.target.y) / 2;
});
}

force.on("tick", ticked);

return force
.force(
"link",
d3.forceLink(graph.links).id((d) => d.id)
)
.force("charge", d3.forceManyBody())
.force("center", d3.forceCenter(width / 5, height / 4));
}

export function useTriplesFetching(memberId) {
axios({
method: 'get',
url: 'http://localhost:5173/triples/' + memberId,
}).then((response) => {
console.log(response.data)
visualizeTriples(response.data)
});
}


Loading

0 comments on commit 8f8ed1b

Please sign in to comment.