vue中使用echarts以及簡單關(guān)系圖的點擊事件方式
1.安裝
在Vue項目中打開終端執(zhí)行命令:
npm install echarts --save
下載后在package.json文件中可以看到下載的Echarts版本:

2.導(dǎo)入
在需要使用Echarts圖表的頁面中導(dǎo)入:
import * as echarts from "echarts";
如果多個地方使用的話可以通過全局引入:
import * as echarts from 'echarts' // 掛載到Vue實例 Vue.prototype.$echarts = echarts
3.繪制靜態(tài)圖表
在需要用到echarts的地方設(shè)置一個有寬高的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, // 點擊次數(shù)
};
},
在methods中定義實例化echarts對象的方法,在mounted生命周期中調(diào)用(確保dom元素已經(jīng)掛載到頁面當(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);
},
},
啟動項目,在頁面中看到如下效果:

4.關(guān)系圖節(jié)點點擊事件
上面只是展示了靜態(tài)的關(guān)系圖,如節(jié)點數(shù)據(jù)太多,各節(jié)點關(guān)系復(fù)雜,就可只展示主要數(shù)據(jù),其他可通過點擊節(jié)點查看各節(jié)點關(guān)系
需求:新建一個Node5,Node5和Node2有關(guān)系,點擊Node2展示Node5節(jié)點
在上面原本的getEchartData()方法中,添加關(guān)系圖的節(jié)點點擊事件
通過事件參數(shù)param中的dataType屬性值確認(rèn)點擊的對象是關(guān)系圖節(jié)點還是節(jié)點之間的邊緣,值為node時點擊的是節(jié)點,值為edge時點擊的是邊緣
通過param中的dataIndex值確定點擊的節(jié)點元素
完整代碼如下:
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圖表的點擊事件,可通過param參數(shù)確認(rèn)點擊的對象
that.mychart.on("click", function (param) {
if (param.dataType == "node") {
// Node2的 param.dataIndex 值為1
if (param.dataIndex == 1) {
// 判斷點擊的次數(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("點擊了邊", param);
}
});
// option數(shù)據(jù)放入圖表中
this.mychart.setOption(option);
},
最終效果如下:

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue+elementui 實現(xiàn)新增和修改共用一個彈框的完整代碼
Element-Ul是餓了么前端團(tuán)隊推出的一款基于Vue.js 2.0 的桌面端UI框架,手機(jī)端有對應(yīng)框架是Mint UI ,今天給大家普及vue+elementui 實現(xiàn)新增和修改共用一個彈框的完整代碼,一起看看吧2021-06-06
關(guān)于vue2強(qiáng)制刷新,解決頁面不會重新渲染的問題
今天小編就為大家分享一篇關(guān)于vue2強(qiáng)制刷新,解決頁面不會重新渲染的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10
Vue使用localStorage存儲數(shù)據(jù)的方法
這篇文章主要為大家詳細(xì)介紹了Vue使用localStorage存儲數(shù)據(jù)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
vue3中echarts圖表在頁面刷新后無法自適應(yīng)改變大小的解決過程
優(yōu)化ECharts圖表開發(fā)需獨立變量、創(chuàng)建顯示方法,并通過添加和移除窗口大小監(jiān)聽實現(xiàn)響應(yīng)式刷新,確保性能與資源管理2025-09-09

