使用Vue-neo4j實現(xiàn)繪制三國人物圖譜關系
前言
發(fā)現(xiàn)一個很有用的Vue插件:https://www.npmjs.com/package/vue-neo4j
這個能在前端直連neo4j服務器啦,下圖就是測試效果繪制三國人物圖譜關系

版本說明
- vue
3.0版本 - vue-neo4j
0.4.8版本 - neo4j服務端版本
4.3.6版本,可以直接去官網下載。 - echarts
5.22版本
開發(fā)說明
一、windows服務器部署neo4j
1、部署jdk11, 因為neo4j 4.3.6使用的版本是jdk11以上,否則啟動不了哦
2、下載neo4j,解壓
3、到 .\conf目錄修改 neo4j.conf文件修改default_listen_address為0.0.0.0,因為不改的話,會導致啟動后只能本機訪問

4、cmd到.\bin目錄執(zhí)行 noe4j console. 這樣就完成啟動了

5、瀏覽器打開網址 http://127.0.0.1:7474
默認密碼為neo4j/neo4j 首次進入會提示修改密碼
二、vue3.0 版本前端工程說明
1、引入依賴 yarn add vue-neo4j
2、由于vue-neo4j 0.4.8 該版本只適配vue2.0版本,所以要改下源碼,把 Vue.property.$neo4j 改為 Vue.config.globalProperties.$neo4j

3、main.js引入
import VueNeo4j from 'vue-neo4j'; let app = createApp(App); app.use(VueNeo4j );

4、邏輯編寫
<template>
<div class="neo4jMain">
<div class="addContent">
<el-form :inline="true" :model="formInline" :rules="rules" ref="neo4jform">
<el-form-item label="開始節(jié)點名稱" prop="startNode">
<el-input v-model="formInline.startNode" placeholder="請輸入開始節(jié)點名稱"></el-input>
</el-form-item>
<el-form-item label="關系名稱" prop="relationName">
<el-input v-model="formInline.relationName" placeholder="請輸入關系名稱"></el-input>
</el-form-item>
<el-form-item label="結束節(jié)點名稱" prop="endNode">
<el-input v-model="formInline.endNode" placeholder="請輸入結束節(jié)點名稱"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="onSubmit">提交</el-button>
<!-- <el-button type="primary" @click="onDelete">清空表</el-button>-->
</el-form-item>
</el-form>
</div>
<div class="echarts" ref="echarts">
</div>
</div>
</template>
<script>
import * as echarts from 'echarts'
export default {
name: "Main.vue",
mounted() {
this.query();
},
data() {
return {
formInline: {
startNode: '',
endNode: '',
relationName: ''
},
rules: {
startNode: [{required: true, trigger: 'blur'}],
endNode: [{required: true, trigger: 'blur'}],
relationName: [{required: true, trigger: 'blur'}]
},
protocol: 'bolt',
host: '127.0.0.1',
port: 7687,
username: 'neo4j',
password: '123456',
echartsData: [],
nodesRelation: []
}
},
methods: {
onDelete() {
this.connect();
const session = this.$neo4j.getSession();
let cypher = `match p=(n:Person)-[]->(m:Person) delete p `;
session.run(cypher);
cypher = `MATCH (n:Person) delete n`;
session.run(cypher).then(() => {
session.close()
});
this.query();
},
initEcharts() {
// 基于準備好的dom,初始化echarts實例
let myChart = echarts.init(this.$refs.echarts)
// 繪制圖表
myChart.setOption({
title: {
text: 'Neo4j 圖譜關系'
},
tooltip: {},
animationDurationUpdate: 1500,
animationEasingUpdate: 'quinticInOut',
series: [
{
type: 'graph',
layout: 'force',
force: {
edgeLength: 40,
repulsion: 50,
gravity: 0.1
},
symbolSize: 50,
roam: true,
label: {
show: true
},
edgeSymbol: ['circle', 'arrow'],
edgeSymbolSize: [4, 10],
edgeLabel: {
fontSize: 20
},
data: this.echartsData,
// links: [],
links: this.nodesRelation,
lineStyle: {
opacity: 0.9,
width: 2,
curveness: 0
}
}
]
});
},
query() {
this.connect();
const session = this.$neo4j.getSession();
let cypher = `match p=(n:Person)-[]->(m:Person) return p limit 1000`;
session.run(cypher).then(res => {
let records = res.records;
let nodes = new Set();
this.nodesRelation = [];
for (let i = 0; i < records.length; i++) {
nodes.add(records[i]._fields[0].segments[0].start.properties.name);
nodes.add(records[i]._fields[0].segments[0].end.properties.name);
this.nodesRelation.push({
source: records[i]._fields[0].segments[0].start.properties.name,
target: records[i]._fields[0].segments[0].end.properties.name,
lineStyle: {
curveness: 0
},
label: {
show: true,
formatter: function() {
return records[i]._fields[0].segments[0].relationship.type
}
}
});
}
let curveness = [0, 0.4, -0.4, 0.3, -0.3, 0.2, -0.2, 0.1, -0.1];
for (let j = 0; j < this.nodesRelation.length; j++) {
let repeatNumber = 0;
for (let s = j+1; s < this.nodesRelation.length; s++) {
let r1 = this.nodesRelation[j];
let r2 = this.nodesRelation[s];
if (r1.source === r2.source &&
r1.target === r2.target) {
repeatNumber = repeatNumber + 1;
}
else if (r1.target === r2.source &&
r1.source === r2.target) {
repeatNumber = repeatNumber + 1;
}
}
this.nodesRelation[j].repeatNumber = repeatNumber;
}
for (let j = 0; j < this.nodesRelation.length; j++) {
console.log(this.nodesRelation[j].repeatNumber);
this.nodesRelation[j].lineStyle.curveness = curveness[this.nodesRelation[j].repeatNumber];
}
this.echartsData = [];
nodes.forEach((e) => {
let index = Math.ceil(Math.random()*10);
let color = () => {
if (index%4===0) {
return '#228B22'
} else if (index%4===1) {
return '#FFFF00'
} else if (index%4===2) {
return '#20B2AA'
} else if (index%4===3) {
return '#FFB6C1'
}
return '#87CEFA';
}
this.echartsData.push({
name: e,
x: Math.random() * 100,
y: Math.random() * 100,
itemStyle: {
color: color()
}
});
})
this.initEcharts();
}).then(() => {
session.close()
});
},
onSubmit() {
this.$refs.neo4jform.validate((valid) => {
if (valid) {
this.connect();
const session = this.$neo4j.getSession();
let cypher = `Merge (n:Person{name: '${this.formInline.startNode}'})
Merge (m:Person{name: '${this.formInline.endNode}'}) Merge (n)-[r:${this.formInline.relationName}]->(m)`;
session.run(cypher).then(() => {
this.formInline = {
startNode: '',
endNode: '',
relationName: ''
};
this.query();
}).then(() => {
session.close()
});
}
})
},
connect() {
return this.$neo4j.connect(this.protocol, this.host, this.port, this.username, this.password);
},
driver() {
// Get a driver instance
return this.$neo4j.getDriver()
},
testQuery() {
// Get a session from the driver
const session = this.$neo4j.getSession()
// Or you can just call this.$neo4j.run(cypher, params)
session.run('MATCH (n) RETURN n')
.then(res => {
console.log(res)
// console.log(res.records[0].get('count'))
})
.then(() => {
session.close()
})
}
}
}
</script>
<style scoped lang="less">
.neo4jMain {
height: 100%;
display: flex;
flex-direction: column;
.addContent {
padding: 15px;
box-shadow: -10px 0 10px #D3D3D3, /*左邊陰影*/ 10px 0 10px #D3D3D3, /*右邊陰影*/
0 -10px 10px #D3D3D3, /*頂部陰影*/ 0 10px 10px #D3D3D3;
}
.echarts {
background-color: #333;
flex: 1;
flex-grow: 1;
}
}
</style>
心得:
- 優(yōu)點是前端可以直接操作數據庫,弊端是數據庫配置暴露了。
- 建議還是通過后端連接數據庫操作數據。
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
談談因Vue.js引發(fā)關于getter和setter的思考
最近因為公司的新項目決定使用Vue.js來做,但在使用的過程中發(fā)現(xiàn)了一個有趣的事情,因為發(fā)現(xiàn)的這個事情展開了一些對于getter和setter的思考,具體是什么下面通過這篇文章來一起看看吧,有需要的朋友們可以參考學習。2016-12-12
Vue中使用video.js實現(xiàn)截圖和視頻錄制與下載
這篇文章主要為大家詳細介紹了Vue中如何使用video.js實現(xiàn)截圖和視頻錄制與下載,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-03-03
基于Vue3和Element Plus實現(xiàn)可擴展的表格組件
在開發(fā)過程中,我們經常需要創(chuàng)建具有復雜功能的表格組件,本文將介紹如何使用 Vue 3 和 Element Plus 庫來構建一個可擴展的表格組件,文中有詳細的代碼示例供大家參考,需要的朋友可以參考下2024-07-07
vue3如何通過provide和inject實現(xiàn)多層級組件通信
這篇文章主要介紹了vue3如何通過provide和inject實現(xiàn)多層級組件通信,本文通過實例代碼給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-11-11

