|
|
@@ -0,0 +1,283 @@
|
|
|
+<script>
|
|
|
+/* eslint-disable no-unused-vars */
|
|
|
+import * as d3 from "d3";
|
|
|
+
|
|
|
+export default {
|
|
|
+ name: "Kg",
|
|
|
+ props: {
|
|
|
+ entities: {
|
|
|
+ required: true,
|
|
|
+ default: [],
|
|
|
+ },
|
|
|
+ relations: {
|
|
|
+ required: true,
|
|
|
+ default: [],
|
|
|
+ },
|
|
|
+ uniqueKey: {
|
|
|
+ required: true,
|
|
|
+ default: "",
|
|
|
+ }
|
|
|
+ },
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ i: 1,
|
|
|
+
|
|
|
+ kg: undefined,
|
|
|
+ svg: undefined,
|
|
|
+ g: undefined,
|
|
|
+ nodes: undefined,
|
|
|
+ links: undefined,
|
|
|
+ };
|
|
|
+ },
|
|
|
+ mounted() {
|
|
|
+ this.init();
|
|
|
+ this.paint();
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ init() {
|
|
|
+ this.kg = document.getElementById(`kg-${this.uniqueKey}`);
|
|
|
+ const w = this.kg.clientWidth;
|
|
|
+ const h = this.kg.clientHeight;
|
|
|
+ this.svg = d3.select(`#kg-${this.uniqueKey}`)
|
|
|
+ .append("svg")
|
|
|
+ .attr("id", `kg-${this.uniqueKey}-svg`)
|
|
|
+ .attr("viewBox", [0, 0, w, h]);
|
|
|
+ this.nodes = this.entities.map(d => {
|
|
|
+ return Object.create(d);
|
|
|
+ });
|
|
|
+ this.links = this.relations.map(d => {
|
|
|
+ return Object.create(d);
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ paint() {
|
|
|
+ const _this = this;
|
|
|
+ const w = _this.kg.clientWidth;
|
|
|
+ const h = _this.kg.clientHeight;
|
|
|
+ _this.g = _this.svg.append("g");
|
|
|
+ let g = _this.g;
|
|
|
+ let nodes = _this.nodes;
|
|
|
+ let links = _this.links;
|
|
|
+
|
|
|
+ let marker1 = g.append('g')
|
|
|
+ .append("marker")
|
|
|
+ .attr("id", 'arrowhead1')
|
|
|
+ .attr('viewBox', '-0 -5 10 10')
|
|
|
+ .attr('refX', 18)
|
|
|
+ .attr('refY', 0)
|
|
|
+ .attr('orient', 'auto')
|
|
|
+ .attr('markerWidth', 8)
|
|
|
+ .attr('markerHeight', 8)
|
|
|
+ .attr('xoverflow', 'visible')
|
|
|
+ .append('svg:path')
|
|
|
+ .attr('d', 'M 0,-5 L 10 ,0 L 0,5')
|
|
|
+ .attr("stroke-opacity", 0.8)
|
|
|
+ .attr('fill', 'darkgrey');
|
|
|
+
|
|
|
+ let marker2 = g.append('g')
|
|
|
+ .append("marker")
|
|
|
+ .attr("id", 'arrowhead2')
|
|
|
+ .attr('viewBox', '-0 -5 10 10')
|
|
|
+ .attr('refX', -8)
|
|
|
+ .attr('refY', 0)
|
|
|
+ .attr('orient', 'auto')
|
|
|
+ .attr('markerWidth', 8)
|
|
|
+ .attr('markerHeight', 8)
|
|
|
+ .attr('xoverflow', 'visible')
|
|
|
+ .append('svg:path')
|
|
|
+ .attr('d', 'M 10,-5 L 0 ,0 L 10,5')
|
|
|
+ .attr("stroke-opacity", 0.8)
|
|
|
+ .attr('fill', 'darkgrey');
|
|
|
+
|
|
|
+ let simulation = d3.forceSimulation(nodes)
|
|
|
+ .force("link", d3.forceLink(links).id(d => d.id))
|
|
|
+ .force("charge", d3.forceManyBody().strength(-50))
|
|
|
+ .force("center", d3.forceCenter(w / 2, h / 2));
|
|
|
+
|
|
|
+ let link = g.append("g")
|
|
|
+ .selectAll(".link")
|
|
|
+ .data(links)
|
|
|
+ .join("path")
|
|
|
+ .attr("stroke-width", 1.5)
|
|
|
+ .attr("stroke", "darkgrey")
|
|
|
+ .attr("stroke-opacity", 0.8)
|
|
|
+ .attr("id", function (d) {
|
|
|
+ return ("link-" + d.id)
|
|
|
+ })
|
|
|
+ .attr("class", "link")
|
|
|
+ .on("click", (e, d) => _this.$emit("LinkClick", d));
|
|
|
+
|
|
|
+ let node = g.append("g")
|
|
|
+ .selectAll("circle")
|
|
|
+ .data(nodes)
|
|
|
+ .join("circle")
|
|
|
+ .attr("r", 12)
|
|
|
+ .attr("fill", "#409EFF")
|
|
|
+ .attr("stroke", "#fff")
|
|
|
+ .attr("stroke-width", 1)
|
|
|
+ .attr("id", d => "node-" + d.id)
|
|
|
+ .attr("class", "node")
|
|
|
+ .on("click", (e, d) => _this.$emit("NodeClick", d))
|
|
|
+ .call(drag(simulation))
|
|
|
+
|
|
|
+ let defs = g.append("defs");
|
|
|
+ let circleTextFilter1 = defs
|
|
|
+ .append("filter")
|
|
|
+ .attr("id", "nodeTextBg1")
|
|
|
+ .attr("x", -0.05)
|
|
|
+ .attr("y", -0.05)
|
|
|
+ .attr("width", 1.1)
|
|
|
+ .attr("height", 1.1);
|
|
|
+
|
|
|
+ circleTextFilter1.append("feFlood")
|
|
|
+ .attr("flood-color", "#fff")
|
|
|
+ .attr("flood-opacity", "1");
|
|
|
+
|
|
|
+ circleTextFilter1.append("feComposite")
|
|
|
+ .attr("in", "SourceGraphic")
|
|
|
+ .attr("operator", "over");
|
|
|
+
|
|
|
+ // 节点名称显示
|
|
|
+ let nodeText = g.append("g")
|
|
|
+ .selectAll(".nodeText")
|
|
|
+ .data(nodes)
|
|
|
+ .join("text")
|
|
|
+ .text(d => d.name)
|
|
|
+ .attr("dx", function () {
|
|
|
+ return 0 - this.getBoundingClientRect().width / 2;
|
|
|
+ })
|
|
|
+ .attr("dy", 25)
|
|
|
+ .attr("font-size", 12)
|
|
|
+ .attr("id", d => "nodeText" + d.id)
|
|
|
+ .attr("filter", "url(#nodeTextBg1)")
|
|
|
+ .attr("class", "nodeText");
|
|
|
+
|
|
|
+ // 力导:节点、关系、节点文字、关系标签
|
|
|
+ simulation.on("tick", () => {
|
|
|
+ link
|
|
|
+ // .attr("marker-end", function (d) {
|
|
|
+ // if (d.source.x < d.target.x) {
|
|
|
+ // return "url(#arrowhead1)"
|
|
|
+ // } else {
|
|
|
+ // return null
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // .attr("marker-start", function (d) {
|
|
|
+ // if (d.source.x < d.target.x) {
|
|
|
+ // return null
|
|
|
+ // } else {
|
|
|
+ // return "url(#arrowhead2)"
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ .attr('d', function (d) {
|
|
|
+ if (d.source.x < d.target.x) {
|
|
|
+ return 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d.target.x + ' ' + d.target.y;
|
|
|
+ } else {
|
|
|
+ return 'M ' + d.target.x + ' ' + d.target.y + ' L ' + d.source.x + ' ' + d.source.y;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ node
|
|
|
+ .attr("cx", d => d.x)
|
|
|
+ .attr("cy", d => d.y);
|
|
|
+
|
|
|
+ nodeText
|
|
|
+ .attr("x", d => d.x)
|
|
|
+ .attr("y", d => d.y);
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ // 设置图谱的缩放
|
|
|
+ let zoom = d3.zoom()
|
|
|
+ .extent([[0, 0], [w, h]])
|
|
|
+ .scaleExtent([0.1, 8])
|
|
|
+ .on("zoom", zoomed);
|
|
|
+
|
|
|
+ this.svg.call(zoom)
|
|
|
+ .on("dblclick.zoom", null);
|
|
|
+
|
|
|
+ function drag(simulation) {
|
|
|
+ function dragstarted(event) {
|
|
|
+ if (!event.active) simulation.alphaTarget(0.3).restart();
|
|
|
+ event.subject.fx = event.subject.x;
|
|
|
+ event.subject.fy = event.subject.y;
|
|
|
+ }
|
|
|
+
|
|
|
+ function dragged(event) {
|
|
|
+ event.subject.fx = event.x;
|
|
|
+ event.subject.fy = event.y;
|
|
|
+ }
|
|
|
+
|
|
|
+ function dragended(event) {
|
|
|
+ if (!event.active) simulation.alphaTarget(0);
|
|
|
+ event.subject.fx = null;
|
|
|
+ event.subject.fy = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return d3.drag()
|
|
|
+ .on("start", dragstarted)
|
|
|
+ .on("drag", dragged)
|
|
|
+ .on("end", dragended);
|
|
|
+ }
|
|
|
+
|
|
|
+ function zoomed(event) {
|
|
|
+ g.attr("transform", event.transform);
|
|
|
+ if (event.transform.k >= 1) {
|
|
|
+ g.selectAll("circle")
|
|
|
+ .attr("r", 12 / event.transform.k)
|
|
|
+ .attr("stroke-width", 1 / event.transform.k);
|
|
|
+ g.selectAll("path")
|
|
|
+ .attr("stroke-width", 2 / event.transform.k);
|
|
|
+ g.selectAll(".nodeText")
|
|
|
+ .attr("font-size", 12 / event.transform.k)
|
|
|
+ .attr("dx", function () {
|
|
|
+ return (0 - this.getBoundingClientRect().width / 2) / event.transform.k
|
|
|
+ })
|
|
|
+ .attr("dy", 25 / event.transform.k);
|
|
|
+ g.selectAll(".linkText")
|
|
|
+ .attr("font-size", 10 / event.transform.k)
|
|
|
+ .attr("dy", -5 / event.transform.k);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ updateNode(entity) {
|
|
|
+ for (let i = 0; i < this.nodes.length; i++) {
|
|
|
+ if (this.nodes[i].id === entity.id) {
|
|
|
+ this.nodes[i] = entity;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.update();
|
|
|
+ },
|
|
|
+
|
|
|
+ updateLink(relation) {
|
|
|
+ for (let i = 0; i < this.links.length; i++) {
|
|
|
+ if (this.links[i].id === relation.id) {
|
|
|
+ this.links[i] = relation;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ this.update();
|
|
|
+ },
|
|
|
+
|
|
|
+ update() {
|
|
|
+ this.g.remove();
|
|
|
+ this.paint();
|
|
|
+ },
|
|
|
+ },
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<template>
|
|
|
+ <div v-if="entities && relations && uniqueKey"
|
|
|
+ :id="`kg-${uniqueKey}`"
|
|
|
+ class="kg">
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<style lang="less" scoped>
|
|
|
+.kg {
|
|
|
+ position: relative;
|
|
|
+ width: 100%;
|
|
|
+ height: 100%;
|
|
|
+}
|
|
|
+</style>
|