vue中使用echarts以及簡(jiǎn)單關(guān)系圖的點(diǎn)擊事件方式
1.安裝
在Vue項(xiàng)目中打開終端執(zhí)行命令:
npm install echarts --save
下載后在package.json文件中可以看到下載的Echarts版本:
2.導(dǎo)入
在需要使用Echarts圖表的頁(yè)面中導(dǎo)入:
import * as echarts from "echarts";
如果多個(gè)地方使用的話可以通過全局引入:
import * as echarts from 'echarts' // 掛載到Vue實(shí)例 Vue.prototype.$echarts = echarts
3.繪制靜態(tài)圖表
在需要用到echarts的地方設(shè)置一個(gè)有寬高的div盒子
<div> <div ref="chart" style="width:800px;height:600px;margin: auto"> </div> </div>
定義echarts關(guān)系圖的數(shù)據(jù)
data() { return { data: [ { name: "Node 1", x: 300, y: 300, }, { name: "Node 2", x: 800, y: 300, }, { name: "Node 3", x: 550, y: 100, }, { name: "Node 4", x: 550, y: 500, }, ], links: [ { source: 0, target: 1, symbolSize: [5, 20], label: { show: true, }, lineStyle: { width: 5, curveness: 0.2, }, }, { source: "Node 2", target: "Node 1", label: { show: true, }, lineStyle: { curveness: 0.2, }, }, { source: "Node 1", target: "Node 3", }, { source: "Node 2", target: "Node 3", }, { source: "Node 2", target: "Node 4", }, { source: "Node 1", target: "Node 4", }, ], num: 0, // 點(diǎn)擊次數(shù) }; },
在methods中定義實(shí)例化echarts對(duì)象的方法,在mounted生命周期中調(diào)用(確保dom元素已經(jīng)掛載到頁(yè)面當(dāng)中)
mounted() { this.getEchartData(); }, methods: { getEchartData() { const chart = this.$refs.chart; // 初始化echarts this.mychart = echarts.init(chart); let that = this; // option就是需要引進(jìn)echarts關(guān)系圖中的代碼 let option = { title: { text: "Basic Graph", }, tooltip: {}, animationDurationUpdate: 1500, animationEasingUpdate: "quinticInOut", series: [ { type: "graph", layout: "none", symbolSize: 50, roam: true, label: { show: true, }, edgeSymbol: ["circle", "arrow"], edgeSymbolSize: [4, 10], edgeLabel: { fontSize: 20, }, lineStyle: { opacity: 0.9, width: 2, curveness: 0, }, // data: [] data: that.data, // links: [], links: that.links, }, ], }; // option數(shù)據(jù)放入圖表中 this.mychart.setOption(option); }, },
啟動(dòng)項(xiàng)目,在頁(yè)面中看到如下效果:
4.關(guān)系圖節(jié)點(diǎn)點(diǎn)擊事件
上面只是展示了靜態(tài)的關(guān)系圖,如節(jié)點(diǎn)數(shù)據(jù)太多,各節(jié)點(diǎn)關(guān)系復(fù)雜,就可只展示主要數(shù)據(jù),其他可通過點(diǎn)擊節(jié)點(diǎn)查看各節(jié)點(diǎn)關(guān)系
需求:新建一個(gè)Node5,Node5和Node2有關(guān)系,點(diǎn)擊Node2展示Node5節(jié)點(diǎn)
在上面原本的getEchartData()方法中,添加關(guān)系圖的節(jié)點(diǎn)點(diǎn)擊事件
通過事件參數(shù)param中的dataType屬性值確認(rèn)點(diǎn)擊的對(duì)象是關(guān)系圖節(jié)點(diǎn)還是節(jié)點(diǎn)之間的邊緣,值為node時(shí)點(diǎn)擊的是節(jié)點(diǎn),值為edge時(shí)點(diǎn)擊的是邊緣
通過param中的dataIndex值確定點(diǎn)擊的節(jié)點(diǎn)元素
完整代碼如下:
getEchartData() { const chart = this.$refs.chart; // 初始化echarts this.mychart = echarts.init(chart); let that = this; // option就是需要引進(jìn)echarts關(guān)系圖中的代碼 let option = { title: { text: "Basic Graph", }, tooltip: {}, animationDurationUpdate: 1500, animationEasingUpdate: "quinticInOut", series: [ { type: "graph", layout: "none", symbolSize: 50, roam: true, label: { show: true, }, edgeSymbol: ["circle", "arrow"], edgeSymbolSize: [4, 10], edgeLabel: { fontSize: 20, }, lineStyle: { opacity: 0.9, width: 2, curveness: 0, }, // data: [] data: that.data, // links: [], links: that.links, }, ], }; // echarts圖表的點(diǎn)擊事件,可通過param參數(shù)確認(rèn)點(diǎn)擊的對(duì)象 that.mychart.on("click", function (param) { if (param.dataType == "node") { // Node2的 param.dataIndex 值為1 if (param.dataIndex == 1) { // 判斷點(diǎn)擊的次數(shù),單數(shù)顯示Node5數(shù)據(jù),雙數(shù)隱藏 that.num++; if (that.num % 2 == 1) { that.data.push({ name: "Node 5", x: 900, y: 300, }); that.links.push({ source: "Node 2", target: "Node 5", }); that.mychart.setOption(option); } else { that.data.pop(); that.links.pop(); that.mychart.setOption(option); } } } else { console.log("點(diǎn)擊了邊", param); } }); // option數(shù)據(jù)放入圖表中 this.mychart.setOption(option); },
最終效果如下:
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用Echarts實(shí)現(xiàn)大屏可視化布局示例詳細(xì)講解
這篇文章主要介紹了Vue使用Echarts實(shí)現(xiàn)大屏可視化布局示例,本文通過實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究
Vue.js每天必學(xué)之內(nèi)部響應(yīng)式原理探究,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09Vue?2源碼閱讀?Provide?Inject?依賴注入詳解
這篇文章主要為大家介紹了Vue?2源碼閱讀?Provide?Inject?依賴注入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08使用Vue.js中的過濾器實(shí)現(xiàn)冪方求值的方法
這篇文章主要介紹了使用Vue.js中的過濾器實(shí)現(xiàn)冪方求值的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08Vue導(dǎo)入Echarts實(shí)現(xiàn)折線散點(diǎn)圖
這篇文章主要為大家詳細(xì)介紹了Vue導(dǎo)入Echarts實(shí)現(xiàn)折線散點(diǎn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10通過debug搞清楚.vue文件如何變成.js文件(案例詳解)
這篇文章主要介紹了通過debug搞清楚.vue文件如何變成.js文件,本文以@vitejs/plugin-vue舉例,通過debug的方式帶你一步一步的搞清楚vue文件是如何編譯為js文件的,需要的朋友可以參考下2024-07-07在Vue 3中使用OpenLayers讀取WKB數(shù)據(jù)并顯示圖形效果
WKB作為一種緊湊的二進(jìn)制格式,在處理和傳輸空間數(shù)據(jù)時(shí)具有明顯優(yōu)勢(shì),本文介紹了如何在Vue 3中使用OpenLayers讀取WKB格式的空間數(shù)據(jù)并顯示圖形,感興趣的朋友一起看看吧2024-12-12