最近遇到的Vue项目设计图中出现了标签画像的需求,在以往接触的项目中基本都没遇到过,在参考了多个开源项目后,最后决定使用Echarts关系图来实现该需求,废话不多说,上代码
1、文件内引入echarts
// 引入echarts与echarts-gl
// 当前使用版本
// "echarts": "^5.0.2",
// "echarts-gl": "^2.0.8",
import "echarts-gl";
import * as echarts from 'echarts';
2、HTML
<template>
<div>
<div id="BQHX"></div>
</div>
<template>
3、Vue中 methods中添加方法,这里面添加了一个基本关系图型和三个描述模块,如下
// 标签画像
BQHX() {
// 基于准备好的dom,初始化echarts实例
let myChart = echarts.init(document.getElementById('BQHX'));
let option = {
animationDurationUpdate: 1000,
animationEasingUpdate: 'quinticInOut',
tooltip: {
trigger: 'item',
},
series: [
{
type: 'graph',
layout: 'none',
symbolSize: 50,
roam: false, // 是否可使用鼠标拖动、放大、缩小等
label: {
show: true,
formatter: (item) => {
// 各个关系节点内文字描述
return item.name === 'Node4'?'{font0|标签画像总数}\n{font1|6个}': '{font2| 1 }'
},
// 设置不同文字描述样式
rich: {
font0: {
fontSize: 18,
letterSpacing: 10,
fontWeight: 400,
lineHeight: 24,
},
font1: {
fontSize: 24,
fontWeight: 600,
align: 'center',
},
font2: {
fontSize: 32,
fontWeight: 400,
color: 'rgb(123,23,47)',
},
}
},
edgeSymbol: ['arrow', 'circle'],// 连接线两头的样式 圆和箭头可自由调整
edgeSymbolSize: [4, 10],
edgeLabel: {
fontSize: 20
},
data: [
{
name: 'Node1',
// 设置关系节点位置
x: 410,
y: 350,
symbol: 'image://图片base码', // 前缀 image:// + 图片的base64码
// 节点大小设置
symbolSize: 70,
},
{
name: 'Node2',
x: 465,
y: 270,
symbol: 'image://图片base码', // image://前缀 + 图片的base64码
symbolSize: 70,
},
{
name: 'Node3',
x: 550,
y: 250,
symbol: 'image://图片base码', // image://前缀 + 图片的base64码
symbolSize: 70,
},
{
name: 'Node4',
x: 550,
y: 400,
// 设置点类型为参考坐标点,其他点都是相对于该点进行的位置调整
type: 0,
symbol: 'image://图片base码', // image://前缀 + 图片的base64码
symbolSize: [250,150],
},
{
name: 'Node5',
x: 660,
y: 270,
symbol: 'image://图片base码', // image://前缀 + 图片的base64码
symbolSize: 70,
}
],
// 添加数据关联关系
links: [
{
source: 'Node4',
target: 'Node1'
},
{
source: 'Node4',
target: 'Node2'
},
{
source: 'Node4',
target: 'Node3'
},
{
source: 'Node4',
target: 'Node3'
},
{
source: 'Node4',
target: 'Node5'
},
{
source: 'Node4',
target: 'Node6'
},
],
// 关系图连接线样式设置
lineStyle: {
opacity: 1,
width: 2,
curveness: 0,
color: 'rgb(107,49,114)'
}
},
{
type: 'graph',
layout: 'none',
symbol: 'rect',
symbolSize: 0,
roam: false,
// 禁用默认的节点绘制
//symbolSize: 0,
itemStyle: {
normal: {
// 设置节点背景颜色为指定颜色
color: 'rgb(124,126,189)',
}
},
label: {
show: true,
formatter: (item) => {
let desc = '';
switch (item.name) {
case 'Node1':
desc =
'文字描述1';
break;
case 'Node2':
desc =
'文字描述2';
break;
case 'Node3':
desc =
'文字描述3';
break;
case 'Node5':
desc =
'文字描述5';
break;
case 'Node6':
desc =
'文字描述6';
break;
}
return '{font0|' + desc + '}'
},
rich: {
font0: {
fontSize: 12,
lineHeight: 24
}
}
},
data: [
{
name: 'Node1',
x: 310,
y: 405,
symbolSize: [165, 75],
},
{
name: 'Node2',
x: 300,
y: 280,
symbolSize: [115, 75]
},
{
name: 'Node3',
x: 520,
y: 220,
symbolSize: [175, 48]
},
{
name: 'Node4',
x: 435,
y: 400
},
{
name: 'Node5',
x: 600,
y: 290,
symbolSize: [115, 50]
},
{
name: 'Node6',
x: 590,
y: 405,
symbolSize: [172, 75],
}
]
},
{
type: 'graph',
layout: 'none',
symbol: 'rect',
symbolSize: 0,
roam: false,
// 禁用默认的节点绘制
//symbolSize: 0,
itemStyle: {
normal: {
// 设置节点背景颜色为透明
color: 'none',
}
},
label: {
show: true,
formatter: (item) => {
let desc = '{font0|描述}\n{font1| ';
switch (item.name) {
case 'Node1':
desc += Node1+'}';
break;
case 'Node2':
desc += Node2+'}';
break;
case 'Node3':
desc += Node3+'}';
break;
case 'Node4':
desc = '';
break;
case 'Node5':
desc += Node5+'}';
break;
case 'Node6':
desc += Node6+'}';
break;
default:
}
return desc;
},
rich: {
font0: {
color: 'rgb(255,255,255)',
fontSize: 9,
fontWeight: 400,
lineHeight: 16
},
font1: {
color: 'rgb(255,255,255)',
fontSize: 14,
fontWeight: 400,
align: 'center',
},
}
},
data: [
{
name: 'Node1',
x: 270,
y: 310,
},
{
name: 'Node2',
x: 285,
y: 230,
},
{
name: 'Node3',
x: 380,
y: 210,
},
{
name: 'Node4',
x: 435,
y: 400
},
{
name: 'Node5',
x: 600,
y: 240,
},
{
name: 'Node6',
x: 600,
y: 310,
}
]
},
{
type: 'graph',
layout: 'none',
symbol: 'rect',
symbolSize: 0,
roam: false,
// 禁用默认的节点绘制
//symbolSize: 0,
itemStyle: {
normal: {
// 设置节点背景颜色为无
color: 'none',
}
},
label: {
show: true,
formatter: (item) => {
let desc = '';
switch (item.name) {
case 'Node1':
desc = 'Node1';
break;
case 'Node2':
desc = 'Node2';
break;
case 'Node3':
desc = 'Node3';
break;
case 'Node5':
desc = 'Node5';
break;
case 'Node6':
desc = 'Node6';
break;
default:
}
return '{font0|'+desc+'}';
},
rich: {
font0: {
color: 'rgba(255,255,255,0.5)',
fontSize: 18,
fontWeight: 500,
},
}
},
data: [
{
name: 'Node1',
x: 290,
y: 405,
},
{
name: 'Node2',
x: 345,
y: 320,
},
{
name: 'Node3',
x: 435,
y: 300,
},
{
name: 'Node4',
x: 435,
y: 290
},
{
name: 'Node5',
x: 535,
y: 320,
},
{
name: 'Node6',
x: 590,
y: 405,
}
]
}
]
};
myChart.setOption(option)
},
大家有什么好的方法,可以互相交流学习
本文借鉴了其他网站的原型设计,以下是连接: