之前用echarts实现关系图后,就想着是不是可以通过点击某个按钮然后就把某一类数据隐藏。本来是想通过legend来实现的,毕竟看官网的例子,他们用legend就挺方便的,但是和我的需求有些不一样,所以就自己想了个法子去实现这样有一个功能。思路如下:定义多个数组,用它们来装某一类数据,当我们需要隐藏该类数据的时候就把这些数据从总的数据里面移到用来装该数据的数组中,但我们需要恢复数据时就可以把该分组的数据移到总数据中。总的来说就是修改data,然后重新画图,有点浪费空间和时间…
下面是例子:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"></script>
<body>
<div id="OdfMes" style="width: 1800px;height: 800px;float: right"></div>
<script>
var datas=[];//总的数据
var links=[];//连接情况
var cage0=[];//cage为0的数据
var cage1=[];//cage为1的数据
var cage2=[];//cage为2的数据
datas.push({
name: '校园大数据',
symbolSize: 120,
draggable: true,
category: 0,
itemStyle: {
normal: {
borderColor: '#04f2a7',
borderWidth: 6,
shadowBlur: 20,
shadowColor: '#04f2a7',
color: '#001c43',
}
}
},
{
name: '舆情大数据',
symbolSize: 100,
itemStyle: {
normal: {
borderColor: '#04f2a7',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#04f2a7',
color: '#001c43',
}
},
category: 1,
},
{
name: '用户分析',
symbolSize: 80,
category: 1,
itemStyle: {
normal: {
borderColor: '#04f2a7',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#04f2a7',
color: '#001c43',
}
},
},
{
name: '话题分析',
symbolSize: 80,
category: 1,
itemStyle: {
normal: {
borderColor: '#82dffe',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#04f2a7',
color: '#001c43',
}
},
},
{
name: '评论分析',
symbolSize: 80,
category: 1,
itemStyle: {
normal: {
borderColor: '#82dffe',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#04f2a7',
color: '#001c43',
}
},
},
{
name: '图书馆分析',
symbolSize:100,
category: 2,
itemStyle: {
normal: {
borderColor: '#82dffe',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#04f2a7',
color: '#001c43',
}
},
},
{
name: '借阅分析',
symbolSize:80,
category: 2,
itemStyle: {
normal: {
borderColor: '#b457ff',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#b457ff',
color: '#001c43'
}
},
},
{
name: '借阅排行',
symbolSize:80,
itemStyle: {
normal: {
borderColor: '#82dffe',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#04f2a7',
color: '#001c43'
}
},
category: 2,
},
{
name: '图书收录',
symbolSize:80,
itemStyle: {
normal: {
borderColor: '#82dffe',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#04f2a7',
color: '#001c43'
}
},
category: 2,
},
{
name: '图书分析',
symbolSize:80,
category: 2,
itemStyle: {
normal: {
borderColor: '#82dffe',
borderWidth: 4,
shadowBlur: 10,
shadowColor: '#04f2a7',
color: '#001c43'
}
},
});
links.push({
source: '校园大数据',
target: '舆情大数据'
},
{
source: '校园大数据',
target: '图书馆分析',
},
{
source: '舆情大数据',
target: '用户分析',
},
{
source: '舆情大数据',
target: '话题分析',
},
{
source: '舆情大数据',
target: '评论分析',
},
{
source: '校园大数据',
target: '图书馆分析',
},
{
source: '图书馆分析',
target: '图书分析',
},
{
source: '图书馆分析',
target: '借阅分析',
},
{
source: '图书馆分析',
target: '借阅排行',
value: 'DNA',
},{
source: '图书馆分析',
target: '图书收录'
});
$(function () {
var myChart = echarts.init(document.getElementById('OdfMes'));
option = {
backgroundColor: '#1a4377',
tooltip: {},
animationDurationUpdate: 1500,
animationEasingUpdate: 'quinticInOut',
color:['#83e0ff','#45f5ce','#b158ff'],
series: [
{
type: 'graph',
layout: 'force',
force:{
repulsion:1000,
edgeLength:50
},
roam: true,
label: {
normal: {
show: true
}
},
data: datas,
links: links,
lineStyle: {
normal: {
opacity: 0.9,
width: 5,
curveness: 0
}
},
categories:[
{name: '0'},
{name: '1'},
{name: '2'}
]
}
]
}
myChart.setOption(option);
//添加点击事件
//
//下面就开始隐藏和显示数据了
myChart.on('click', function (params) {
if(params.name=="图书收录"){
for(var i=0;i<datas.length;i++){
if(datas[i].category==2){
//alert(datas[i].name);
cage2.push(datas[i]);
datas.splice(i,1);
i=i-1;//ps,因为datas.splice(i,1)会重新排序数据,如果有两个数据相连并且都是category=2的话就会出现数据删不干净
// delete datas[i];
}
}
//修改数据后重新画关系图
myChart.setOption({
series: [{
data: datas,
links: links
}]
});
}
if(params.name=="用户分析"){
datas.push.apply(datas,cage2);
cage2.length=0;//清空数据,避免下次使用的时候出现重复数据
//修改数据后重新画关系图
myChart.setOption({
series: [{
data: datas,
links: links
}]
});
}
})
})
</script>
</body>
</html>
PS:使用了category值,通过category值来判断是否要移除。这只是个简单的demo,有需要的可以根据自身的需要去修改和添加功能。直接复制上面的代码就能跑起来。
个人学习心得,欢迎大佬指点。
本文介绍如何使用ECharts动态地隐藏和显示特定类别数据,通过修改数据集并重新绘制图表实现。提供了具体示例代码,展示了如何根据不同category值进行数据的增删操作。
677

被折叠的 条评论
为什么被折叠?



