ECharts提供了多图表联动(connect)的功能,连接的多个图表可以共享组件事件并实现保存图片时的自动拼接。多图表联动支持直角系下tooltip的联动
。
分别设置每个ECharts对象为相同的group值,并通过在调用ECharts对象的connect方法时,传入group值,从而使用多个ECharts对象建立联动关系
`myChart1.group = 'group1'; //给第1个ECharts对象设置一个group值
myChart2.group = 'group1'; //给第2个ECharts对象设置一个相同的group值
myChart3.group = 'group1'; //给第3个ECharts对象设置一个相同的group值
myChart4.group = 'group1'; //给第4个ECharts对象设置一个相同的group值
echarts.connect('group1'); //调用ECharts对象的connect方法时,传入group值`
由图可知,共有四个图表建立了多图表联动,所以当鼠标滑过四个图表任意一个图表时,四个图表会同时显示详情提示框(tooltip)。
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>ECharts</title>
</head>
<body>
<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
<div id="main1" style="height:200px"></div>
<div id="main2" style="height:200px"></div>
<div id="main3" style="height:200px"></div>
<div id="main4" style="height:200px"></div>
<!-- ECharts单文件引入 -->
<script type="text/javascript" src='js/echarts.js'></script>
<script type="text/javascript">
// 基于准备好的dom,初始化echarts图表
var myChart1 = echarts.init(document.getElementById('main1'));
var myChart2 = echarts.init(document.getElementById('main2'));
var myChart3 = echarts.init(document.getElementById('main3'));
var myChart4 = echarts.init(document.getElementById('main4'));
var option1 = {
tooltip: { trigger: 'axis', },
legend: { data: ['Email'], left: 422, top: 8 },
xAxis: [{
data: ["2022-11-10 11", "2022-11-11 11", "2022-11-12 11", "2022-11-13 11", "2022-11-14 11", "2022-11-15 11",],
}],
yAxis: [
{
type: 'value' //y轴为值类型
}
],
series: [{ //配置第1个图表的数据系列
name: 'Email',
type: 'line',
data: [125, 62, 45, 56, 123, 205, 108, 128],
}]
}
var option2 = {
tooltip: { trigger: 'axis', },
legend: { data: ['Union Ads'], left: 422, top: 8 },
xAxis: [{
data: ["2022-11-10 11", "2022-11-11 11", "2022-11-12 11", "2022-11-13 11", "2022-11-14 11", "2022-11-15 11",],
}],
yAxis: [
{
type: 'value' //y轴为值类型
}
],
series: [{ //配置第2个图表的数据系列
name: 'Union Ads',
type: 'bar', barWidth: 40, //设置柱状图中每个柱子的宽度
data: [325, 98, 53, 48, 222, 256, 123, 111],
}]
}
var option3 = {
tooltip: { trigger: 'axis', },
legend: { data: ['Video Ads'], left: 422, top: 8 },
xAxis: [{
data: ["2022-11-10 11", "2022-11-11 11", "2022-11-12 11", "2022-11-13 11", "2022-11-14 11", "2022-11-15 11",],
}],
yAxis: [
{
type: 'value' //y轴为值类型
}
],
series: [{
name: 'Video Ads',
type: 'line',
smooth: true,
data: [20, 25, 30, 35, 38, 44, 46, 48, 53, 56]
}]
}
var option4 = {
tooltip: { trigger: 'axis', },
legend: { data: ['Direct', 'Search Engine'], left: 422, top: 8 },
xAxis: [{
data: ["2022-11-10 11", "2022-11-11 11", "2022-11-12 11", "2022-11-13 11", "2022-11-14 11", "2022-11-15 11",],
}],
yAxis: [
{
type: 'value' //y轴为值类型
}
],
series: [{
name: 'Direct',
type: 'line',
smooth: true,
data: [15, 15, 15, 15, 15, 15, 15, 15, 15, 15]
},
{
name: 'Search Engine',
type: 'line',
smooth: true,
data: [25, 25, 25, 25, 25, 25, 35, 25, 25, 35]
}]
}
// 为echarts对象加载数据
myChart1.setOption(option1);
myChart2.setOption(option2);
myChart3.setOption(option3);
myChart4.setOption(option4);
// //联动配置
// myChart1.connect([myChart2, myChart3, myChart4]);
// myChart2.connect([myChart1, myChart3, myChart4]);
// myChart3.connect([myChart2, myChart1, myChart4]);
// myChart4.connect([myChart2, myChart3, myChart1]);
//联动配置
myChart1.group = 'group1';
myChart2.group = 'group1';
myChart3.group = 'group1';
myChart4.group = 'group1';
echarts.connect('group1');
</script>
</body>
原文地址:https://blog.csdn.net/weixin_45653441/article/details/128624017
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_22964.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。