转:ECharts图表组件之简单关系图:如何轻松实现另类站点地图且扩展节点属性实现点击节点页面跳转

时间:2023-03-08 17:21:34

站点地图不外乎就是罗列一个网站的层次结构,提炼地讲就是一个关系结构图。那么我们如何巧用ECharts图表组件内的简单关系结构图来实现一个站点的地图结构呢?另外如何点击某个节点的时候实现页面跳转呢?

转:ECharts图表组件之简单关系图:如何轻松实现另类站点地图且扩展节点属性实现点击节点页面跳转

针对上述所提出的疑问,我琢磨了一下ECharts图表组件。找到官方这样的一个适合的简单关系图:http://echarts.baidu.com/doc/example/force1.html 通过观察ECharts图表组件的简单关系图的数据结构,得出我要实现一个站点地图需要做哪些工作:

1、引入echarts图表组件相关的js文件,主要用到esl.js和echarts.js两个文件

<script src="/js/esl.js" type="text/javascript" charset="utf-8"></script>
<script src="/js/echarts.js" charset="utf-8" type="text/javascript"></script>

2、页面内放置一个装载图表的容器

<div id="main" style="height: 600px; width: 900px; border: 1px solid #ccc; padding: 10px;"></div>

这个容器的height和width是必须要指定的,不指定会出不来图表的。

3、通过esl.js内的require()方法创建关系图

//定义一个全局的图表对象 便于后面使用
var myChart; require(
[
'echarts',
'echarts/chart/bar' //按需加载图表关于bar图的部分
],
DrawEChart
);

4、编写创建图表的核心代码DrawEChart

//创建ECharts图表
function DrawEChart(ec) {
//--- 折柱 ---
myChart = ec.init(document.getElementById('main'));
//图表显示提示信息
myChart.showLoading({
text: "站点关系图正在努力加载..."
});
myChart.hideLoading();
myChart.setOption({
title: {
text: 'STEP DAY:网站地图',
subtext: 'From:www.stepday.com',
x: 'right',
y: 'bottom'
},
tooltip: {
trigger: 'item',
formatter: '{a} : {b}'
},
legend: {
x: 'left',
data: []
},
series: [
{
type: 'force',
name: "点击访问",
categories: [
{
name: '点击访问',
itemStyle: {
normal: {
color: '#ff7f50'
}
}
}
],
itemStyle: {
normal: {
label: {
show: true,
textStyle: {
color: '#800080'
}
},
nodeStyle: {
brushType: 'both',
strokeColor: 'rgba(255,215,0,0.4)',
lineWidth:
}
}
},
minRadius: ,
maxRadius: ,
density: 0.05,
attractiveness: 1.2,
nodes: [
{ category: , name: '博客', value: , url: "http://www.stepday.com/blog/" },
{ category: , name: '论坛', value: , url: "http://www.stepday.com/post/" },
{ category: , name: '管理', value: , url: "http://www.stepday.com/list/?1" },
{ category: , name: "管理经验谈", value: , url: "http://www.stepday.com/list/?2" },
{ category: , name: '管理课堂', value: , url: "http://www.stepday.com/list/?3" },
{ category: , name: "技术", value: , url: "http://www.stepday.com/list/?6" },
{ category: , name: "技术积累", value: , url: "http://www.stepday.com/list/?7" },
{ category: , name: "技术讲座", value: , url: "http://www.stepday.com/list/?8" },
{ category: , name: "技术探讨", value: , url: "http://www.stepday.com/list/?9" },
{ category: , name: "创业", value: , url: "http://www.stepday.com/list/?10" },
{ category: , name: "思考的准备", value: , url: "http://www.stepday.com/list/?11" },
{ category: , name: "奋斗在路上", value: , url: "http://www.stepday.com/list/?12" },
{ category: , name: "经营的思考", value: , url: "http://www.stepday.com/list/?13" },
{ category: , name: "屌丝经济", value: , url: "http://www.stepday.com/list/?14" },
{ category: , name: "投资理财", value: , url: "http://www.stepday.com/list/?15" },
{ category: , name: "屌丝形象", value: , url: "http://www.stepday.com/list/?22" },
{ category: , name: "健康指数", value: , url: "http://www.stepday.com/list/?23" },
{ category: , name: "爱情季节", value: , url: "http://www.stepday.com/list/?24" },
{ category: , name: "感悟生活", value: , url: "http://www.stepday.com/list/?30" },
{ category: , name: "烦恼回收站", value: , url: "http://www.stepday.com/list/?32" },
{ category: , name: "工作浅谈", value: , url: "http://www.stepday.com/list/?34" },
{ category: , name: "神级吐槽", value: , url: "http://www.stepday.com/list/?35" },
{ category: , name: "专题", value: , url: "http://www.stepday.com/zhuanti/highcharts/" },
{ category: , name: "Highcharts API", value: , url: "http://www.stepday.com/zhuanti/highcharts/" },
{ category: , name: "博客精简模式", value: , url: "http://www.stepday.com/zhuanti/picBlog/" },
{ category: , name: "论坛精简模式", value: , url: "http://www.stepday.com/zhuanti/picPost/" },
{ category: , name: '登录', value: , url: "http://www.stepday.com/login/" },
{ category: , name: '注册', value: , url: "http://www.stepday.com/register/" },
{ category: , name: 'STEP DAY', value: , url: "http://www.stepday.com" }
],
links: [
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: },
{ source: , target: , weight: }
]
}
]
}); var ecConfig = require('echarts/config');
//实现节点点击事件
function focus(param) {
var option = myChart.getOption();
var data = param.data;
//判断节点的相关数据是否正确
if (data != null && data != undefined) {
if (data.url != null && data.url != undefined) {
//根据节点的扩展属性url打开新页面
window.open(data.url);
}
}
}
//绑定图表节点的点击事件
myChart.on(ecConfig.EVENT.CLICK, focus)
}

到这里一个简单的站点结构关系图就完成了。从上面的代码中不难看出来,结构关系图的重点就是两大块:

1)、nodes数组

这个内主要是用于定义结构图的所有节点,其中category是指当前节点属于categories数组内的哪一个分类。脚标从0开始;

name表示节点的名称,value表示大小;

url是我自己扩展的节点属性,用于存放当前节点对应的站点url地址。

2)、links数组

这个主要是用于定义nodes内节点之间相互的关系,只要这里建立关系后,展现出来的关系图节点相互之间会产生一条连接线进行相互之间的链接。在设置这个数组的时候需要注意一下几点:

a、source:表示节点位于nodes数组内的位置,脚标从0开始。这个数字的范围必须是[0,nodes.length];

b、target:表示链接的目标节点在nodes数组内的位置,脚标从0开始。这个数字的范围必须是[0,nodes.length];

c、weight:表示当前节点的权重,权重越大距离目标节点的位置越近,也表示越亲近。数字范围不限制,数字越小距离目标节点越远。

3)、为了能够让用户点击节点可以打开节点所代表的页面,所以我么还需要实现节点的点击事件

在Echarts图表组件内都是通过myChart.on(event,function(){});的形式来进行事件的绑定的,这里也不例外,可以从代码中很明了地看出在结尾的地方绑定了EVETN_CLICK事件,且在实现方法内通过param对象拿到data数据对象,从中获得扩展属性url,进行数据判断通过window.open(url)的方式新开页面。