echarts 树图点击展开关闭加数据!
来源:互联网 发布:echarts动态获取数据 编辑:程序博客网 时间:2024/06/10 01:54
想法:将1级2级3级.等分别存放单独的数组,将加入的点击事件将指定等级的数组加入特定数组中实现展开效果 和 和 合并效果。
echart 2.0 必须 3.0 没有
首先将echart 的容器加载出来 !
<body>
<div id="swdt" style="display:inline-block;width:100%;height:594px"></div></body>
在js 中的写法--》
<script type="text/javascript">
var myCharts = echarts.init(document.getElementById('swdt'));
//1级 数组 symbol 是点击的图片的样式,去掉默认是方块 ,为了更好的视觉效果可选择不同的样式
var arr1=[{"name":"爷爷",“children”:"","symbol":"image://+图片地址路径"}] ;
//首先解释一下这里的children 为 空 是为了加入点击事件给其添加数据
//2级菜单
var arr2 =[{"name":"爸爸",“children”:"","symbol":"image://+图片地址路径"},{"name":"叔叔",“children”:"","symbol":"image://+图片地址路径"}]
//3级菜单
var arr3 =[{"name":"我",“children”:"","symbol":"image://+图片地址路径"}]
option = {
title : {
text: '专项救治'
},
toolbox: {
show : true,
feature : {
mark : {show: true},
dataView : {show: true, readOnly: false},
restore : {show: true},
saveAsImage : {show: true}
}
},
series : [
{
name:'树图',
type:'tree',
orient: 'horizontal', // vertical horizontal
rootLocation: {x: 100,y: 230}, // 根节点位置 {x: 100, y: 'center'}
nodePadding: 8,
layerPadding: 200,
hoverable: false,
roam: true,
symbolSize: 6,
itemStyle: {
normal: {
color: '#4883b4',
label: {
show: true,
position: 'right',
formatter: "{b}",
textStyle: {
color: '#000',
fontSize: 5
}
},
lineStyle: {
color: '#ccc',
type: 'curve' // 'curve'|'broken'|'solid'|'dotted'|'dashed'
}
},
emphasis: {
color: '#4883b4',
label: {
show: false
},
borderWidth: 0
}
},
title : {
text: '专项救治'
},
toolbox: {
show : true,
feature : {
mark : {show: true},
dataView : {show: true, readOnly: false},
restore : {show: true},
saveAsImage : {show: true}
}
},
series : [
{
name:'树图',
type:'tree',
orient: 'horizontal', // vertical horizontal
rootLocation: {x: 100,y: 230}, // 根节点位置 {x: 100, y: 'center'}
nodePadding: 8,
layerPadding: 200,
hoverable: false,
roam: true,
symbolSize: 6,
itemStyle: {
normal: {
color: '#4883b4',
label: {
show: true,
position: 'right',
formatter: "{b}",
textStyle: {
color: '#000',
fontSize: 5
}
},
lineStyle: {
color: '#ccc',
type: 'curve' // 'curve'|'broken'|'solid'|'dotted'|'dashed'
}
},
emphasis: {
color: '#4883b4',
label: {
show: false
},
borderWidth: 0
}
},
//默认初始化加载1级菜单
data:arr1
}
]
};
data:arr1
}
]
};
myCharts.setOption(option);
//节点的点击事件
myCharts.on('click',function(params){
var aname =params.name;
//判断点击的节点名称
if(aname=="爷爷"){
//判断节点下的children是不是空 空展开即加入数据,不为空关闭
if(params.data.children==""){
arr1[0].children=arr2;
}else{
arr1[0].children="";
}
}else if(aname=="爸爸"){
if(params.data.children==""){
arr2[0].children=arr3;
}else{
arr2[0].children="";
}
myCharts.setOption(option,true);
}
//以此类推可扩展 有更好的点击判断节点的方法可留言
})
整体代码:
<!DOCTYPE html>
<html>
<!-- 引入 ECharts 文件 -->
<head>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>专项救治</title>
<script src="echarts-all.js"></script>
</head>
<body>
<div id="swdt" style="display:inline-block;width:100%;height:594px"></div>
</body>
<script type="text/javascript">
var myCharts = echarts.init(document.getElementById('swdt'));
var arr1=[{"name":"爷爷","children":""}] ;
var arr2=[{"name":"爸爸","children":""},{"name":"叔叔","children":""}];
var arr3=[{"name":"我","children":""}] ;
option = {
title : {
text: '专项救治'
},
toolbox: {
show : true,
feature : {
mark : {show: true},
dataView : {show: true, readOnly: false},
restore : {show: true},
saveAsImage : {show: true}
}
},
series : [
{
name:'专项救治',
type:'tree',
orient: 'horizontal', // vertical horizontal
rootLocation: {x: 100,y: 230}, // 根节点位置 {x: 100, y: 'center'}
nodePadding: 8,
layerPadding: 200,
hoverable: false,
roam: true,
symbolSize: 6,
itemStyle: {
normal: {
color: '#4883b4',
label: {
show: true,
position: 'right',
formatter: "{b}",
textStyle: {
color: '#000',
fontSize: 5
}
},
lineStyle: {
color: '#ccc',
type: 'curve' // 'curve'|'broken'|'solid'|'dotted'|'dashed'
}
},
emphasis: {
color: '#4883b4',
label: {
show: false
},
borderWidth: 0
}
},
data:arr1
}
]
};
myCharts.setOption(option);
myCharts.on('click',function(params){
var aname =params.name;
//判断点击的节点名称
if(aname=="爷爷"){
//判断节点下的children是不是空 空展开即加入数据,不为空关闭
if(params.data.children==""){
arr1[0].children=arr2;
}else{
arr1[0].children="";
}
}else if(aname=="爸爸"){
if(params.data.children==""){
arr2[0].children=arr3;
}else{
arr2[0].children="";
}
}
myCharts.setOption(option,true);
})
</script>
</html>
<html>
<!-- 引入 ECharts 文件 -->
<head>
<meta charset="UTF-8">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>专项救治</title>
<script src="echarts-all.js"></script>
</head>
<body>
<div id="swdt" style="display:inline-block;width:100%;height:594px"></div>
</body>
<script type="text/javascript">
var myCharts = echarts.init(document.getElementById('swdt'));
var arr1=[{"name":"爷爷","children":""}] ;
var arr2=[{"name":"爸爸","children":""},{"name":"叔叔","children":""}];
var arr3=[{"name":"我","children":""}] ;
option = {
title : {
text: '专项救治'
},
toolbox: {
show : true,
feature : {
mark : {show: true},
dataView : {show: true, readOnly: false},
restore : {show: true},
saveAsImage : {show: true}
}
},
series : [
{
name:'专项救治',
type:'tree',
orient: 'horizontal', // vertical horizontal
rootLocation: {x: 100,y: 230}, // 根节点位置 {x: 100, y: 'center'}
nodePadding: 8,
layerPadding: 200,
hoverable: false,
roam: true,
symbolSize: 6,
itemStyle: {
normal: {
color: '#4883b4',
label: {
show: true,
position: 'right',
formatter: "{b}",
textStyle: {
color: '#000',
fontSize: 5
}
},
lineStyle: {
color: '#ccc',
type: 'curve' // 'curve'|'broken'|'solid'|'dotted'|'dashed'
}
},
emphasis: {
color: '#4883b4',
label: {
show: false
},
borderWidth: 0
}
},
data:arr1
}
]
};
myCharts.setOption(option);
myCharts.on('click',function(params){
var aname =params.name;
//判断点击的节点名称
if(aname=="爷爷"){
//判断节点下的children是不是空 空展开即加入数据,不为空关闭
if(params.data.children==""){
arr1[0].children=arr2;
}else{
arr1[0].children="";
}
}else if(aname=="爸爸"){
if(params.data.children==""){
arr2[0].children=arr3;
}else{
arr2[0].children="";
}
}
myCharts.setOption(option,true);
})
</script>
</html>
阅读全文
1 0
- echarts 树图点击展开关闭加数据!
- 点击展开关闭效果
- easy-UI动态树点击文字实现展开关闭
- tree组件点击标签展开、关闭节点
- jquery树形 点击标题展开关闭
- 竖 导航栏 点击展开,别的关闭
- 最简单的点击展开关闭效果
- jquery树形 点击标题展开关闭
- echarts饼图点击事件
- Echarts折线图点击事件
- treeview无论点击哪个,只有一个节点展开,其它关闭。
- html中鼠标点击展开关闭层效果
- 通过ValueAnimator实现点击展开和关闭的效果
- 关于treegrid及tree点击文字展开或关闭
- 点击展开
- echarts学习笔记----堆积折线图,动态获取数据展示,以及点击跳转
- Echarts 饼图处理 option动态赋值 点击事件 一级钻取 自定义按钮 数据格式化
- echarts折线图实线加虚线
- linux 刻录光盘
- Java学习之StringBuffer 和 StringBuilder 类
- 昭阳k20-80拆机过程
- AES算法,DES算法,RSA算法JAVA实现
- stick
- echarts 树图点击展开关闭加数据!
- 怎么使用LINQ方法来比较自定义类型对象
- python基础数据类型(一):字符串
- Nginx安装过程中make出错fatal error: pcre.h: No such file or directory
- 移动端响应rem dpr viewport设置
- 火狐浏览器拒绝代理服务器连接
- SummerTraining——字典树(1)
- 蓝桥杯 最大算式
- UVa 10935