一、选取合适的图表类型
Echarts Graph是一款基于数据链接关系的可视化组件,支持包括关系图、树图、力导向图、桑基图等多种类型。在实际应用中,我们需要根据数据的特点和需求选取合适的图表类型。 例如,当数据节点之间存在复杂的关系链条时,可以使用关系图来展示数据之间的关系;而在需要展示数据流向时,可以使用桑基图。在选取图表类型时,我们需要充分考虑数据的特点和展示目的。
//示例代码:生成力导向图
var chart = echarts.init(document.getElementById('main'));
chart.setOption({
title: {
text: '力导向图示例'
},
tooltip: {},
animationDurationUpdate: 1500,
animationEasingUpdate: 'quinticInOut',
series : [
{
type: 'graph',
layout: 'force',
symbolSize: 50,
roam: true,
label: {
show: true
},
data: [
{name: '节点1'},
{name: '节点2'},
{name: '节点3'},
{name: '节点4'},
{name: '节点5'}
],
links: [
{source: '节点1', target: '节点2'},
{source: '节点1', target: '节点3'},
{source: '节点2', target: '节点4'},
{source: '节点3', target: '节点5'},
{source: '节点4', target: '节点5'}
],
emphasis: {
lineStyle: {
width: 2.5
}
},
force: {
repulsion: 1000,
edgeLength: 200
}
}
]
});
二、优化节点形状和颜色
一般情况下,节点的形状和颜色可以用来表示节点的重要程度或者属性值。为了让数据更加直观易懂,我们可以通过节点的形状、大小、颜色等属性来展示数据的不同属性。 例如,在展示地理位置分布时,可以将节点形状设置成各省份的地图轮廓,并通过节点大小、颜色来表示该省份的相关指标;在展示人物关系时,可以设置节点形状为人物头像,并通过节点的颜色和大小来表示人物的重要程度。
//示例代码:优化节点形状和颜色
var chart = echarts.init(document.getElementById('main'));
chart.setOption({
title: {
text: '节点形状和颜色示例'
},
series: [
{
type: 'graph',
layout: 'force',
symbolSize: 50,
roam: true,
label: {
show: true
},
data: [
{name: '节点1', itemStyle: {color: '#c23531'}},
{name: '节点2', itemStyle: {color: '#2f4554'}},
{name: '节点3', itemStyle: {color: '#61a0a8'}},
{name: '节点4', itemStyle: {color: '#d48265'}},
{name: '节点5', itemStyle: {color: '#91c7ae'}}
],
links: [
{source: '节点1', target: '节点2'},
{source: '节点1', target: '节点3'},
{source: '节点2', target: '节点4'},
{source: '节点3', target: '节点5'},
{source: '节点4', target: '节点5'}
],
emphasis: {
lineStyle: {
width: 2.5
}
},
force: {
repulsion: 1000,
edgeLength: 200
}
}
]
});
三、交互动画效果
与静态可视化相比,交互动画可以使数据更加生动、直观,提升用户体验。在Echarts Graph中,我们可以通过设置不同的动画效果来增强交互。 例如,在节点聚焦时可以设置节点的大小、颜色变化;在切换布局时可以设置切换动画等。
//示例代码:添加交互动画效果
var chart = echarts.init(document.getElementById('main'));
chart.setOption({
title: {
text: '交互动画效果示例'
},
tooltip: {},
animationDurationUpdate: 1500,
animationEasingUpdate: 'quinticInOut',
series : [
{
type: 'graph',
layout: 'force',
symbolSize: 50,
roam: true,
label: {
show: true
},
data: [
{name: '节点1', itemStyle: {color: '#c23531'}},
{name: '节点2', itemStyle: {color: '#2f4554'}},
{name: '节点3', itemStyle: {color: '#61a0a8'}},
{name: '节点4', itemStyle: {color: '#d48265'}},
{name: '节点5', itemStyle: {color: '#91c7ae'}}
],
links: [
{source: '节点1', target: '节点2'},
{source: '节点1', target: '节点3'},
{source: '节点2', target: '节点4'},
{source: '节点3', target: '节点5'},
{source: '节点4', target: '节点5'}
],
emphasis: {
lineStyle: {
width: 2.5
}
},
force: {
repulsion: 1000,
edgeLength: 200
}
}
]
});
//节点聚焦时大小变化动画效果
chart.on('mouseover', function (params) {
if (params.dataType === 'node') {
chart.dispatchAction({
type: 'highlight',
seriesIndex: 0,
dataIndex: params.dataIndex
});
chart.dispatchAction({
type: 'downplay',
seriesIndex: 0,
dataIndex: params.dataIndex
});
chart.dispatchAction({
type: 'emphasis',
seriesIndex: 0,
dataIndex: params.dataIndex
});
}
});
//切换布局时的动画效果
chart.on('click', function () {
chart.setOption({
series: [{
type: 'graph',
layout: layout === 'force' ? 'circular' : 'force',
force: {
repulsion: 1000,
edgeLength: 200
}
}]
});
});
四、加入工具箱和数据筛选功能
在数据量较大时,我们可以通过添加工具箱来方便用户进行数据筛选和查看。例如,在关系图中,我们可以将筛选条件设置为节点的类型或属性值,并通过工具箱来进行筛选。
//示例代码:加入工具箱和数据筛选功能
var chart = echarts.init(document.getElementById('main'));
chart.setOption({
title: {
text: '工具箱和数据筛选示例'
},
tooltip: {},
animationDurationUpdate: 1500,
animationEasingUpdate: 'quinticInOut',
toolbox: {
feature: {
dataView: {
readOnly: false
},
restore: {},
saveAsImage: {}
}
},
series : [
{
type: 'graph',
layout: 'force',
symbolSize: 50,
roam: true,
label: {
show: true
},
data: [
{name: '节点1', itemStyle: {color: '#c23531'}, category: 1},
{name: '节点2', itemStyle: {color: '#2f4554'}, category: 2},
{name: '节点3', itemStyle: {color: '#61a0a8'}, category: 1},
{name: '节点4', itemStyle: {color: '#d48265'}, category: 1},
{name: '节点5', itemStyle: {color: '#91c7ae'}, category: 2}
],
links: [
{source: '节点1', target: '节点2'},
{source: '节点1', target: '节点3'},
{source: '节点2', target: '节点4'},
{source: '节点3', target: '节点5'},
{source: '节点4', target: '节点5'}
],
categories: [
{name: '数据类别1'},
{name: '数据类别2'}
],
emphasis: {
lineStyle: {
width: 2.5
}
},
force: {
repulsion: 1000,
edgeLength: 200
}
}
]
});
//添加筛选条件
chart.on('legendselectchanged', function (params) {
var selected = params.selected;
chart.setOption({
series: [{
data: data.filter(function (dataItem) {
return selected[dataItem.category];
})
}]
});
});