echarts小技巧:多图共用图例

今天带来三种多图共用图例的方法

第一种:单容器------使用dataset

dataset数据集是专门用于管理数据的组件。在echarts中,我们可以给系列单独赋值,也可以使用数据集进行统一管理。

配置如下

js 复制代码
    var option = {
        legend: {},
        tooltip: {},
        dataset: {
            source: [
                ['product', '2012', '2013', '2014', '2015', '2016', '2017'],
                ['Milk Tea', 86.5, 92.1, 85.7, 83.1, 73.4, 55.1],
                ['Matcha Latte', 41.1, 30.4, 65.1, 53.3, 83.8, 98.7],
                ['Cheese Cocoa', 24.1, 67.2, 79.5, 86.4, 65.2, 82.5],
                ['Walnut Brownie', 55.2, 67.1, 69.2, 72.4, 53.9, 39.1]
            ]
        },
        series: [
            {
                type: 'pie',
                radius: '20%',
                center: ['25%', '30%']
                // No encode specified, by default, it is '2012'.
            },
            {
                type: 'pie',
                radius: '20%',
                center: ['75%', '30%'],
                encode: {
                    itemName: 'product',
                    value: '2013'
                }
            },
            {
                type: 'pie',
                radius: '20%',
                center: ['25%', '75%'],
                encode: {
                    itemName: 'product',
                    value: '2014'
                }
            },
            {
                type: 'pie',
                radius: '20%',
                center: ['75%', '75%'],
                encode: {
                    itemName: 'product',
                    value: '2015'
                }
            }
        ]
    };

第二种:单容器------使用gridIndex、xAxisIndex和yAxisIndex

1、 设置多个grid以便图形可以分开展示

2、 为同属于一个图例的系列加上相同的name

最终得到结果如下

附代码

js 复制代码
        var option = {
        grid: [
            { left: '7%', top: '7%', width: '38%', height: '38%' },
            { right: '7%', top: '7%', width: '28%', height: '38%' },
            { left: '7%', bottom: '7%', width: '38%', height: '38%' },
            { right: '7%', bottom: '7%', width: '38%', height: '38%' }
        ],
        xAxis: [
            {
                type: 'category',
                gridIndex: 0,
                data: ['1', '2', '3', '4']
            },
            {
                type: 'category',
                gridIndex: 1,
                data: ['1', '2', '3', '4']
            },
            {
                type: 'category',
                gridIndex: 2,
                data: ['1', '2', '3', '4']
            },
            {
                type: 'category',
                gridIndex: 3,
                data: ['1', '2', '3', '4']
            }
        ],
        yAxis: [
            {
                type: 'value',
                gridIndex: 0,
            },
            {
                type: 'value',
                gridIndex: 1,
            },
            {
                type: 'value',
                gridIndex: 2,
            },
            {
                type: 'value',
                gridIndex: 3,
            }
        ],
        legend: {},
        tooltip: {},
        series: [
            {
                type: 'bar',
                name: '111',
                yAxisIndex: 0,
                xAxisIndex: 0,
                data: new Array(4).fill(0).map(() => (Math.random() * 100).toFixed(2)),
            },
            {
                type: 'bar',
                name: '222',
                data: new Array(4).fill(0).map(() => (Math.random() * 100).toFixed(2)),
                yAxisIndex: 1,
                xAxisIndex: 1,
            },
            {
                type: 'line',
                name: '111',
                yAxisIndex: 2,
                xAxisIndex: 2,
                data: new Array(4).fill(0).map(() => (Math.random() * 100).toFixed(2)),
            },
            {
                type: 'line',
                name: '222',
                yAxisIndex: 3,
                xAxisIndex: 3,
                data: new Array(4).fill(0).map(() => (Math.random() * 100).toFixed(2)),
            }
        ]
    };

第三种:多容器------利用API,为legend绑定事件

  1. 首先必须了解知道,修改legend对象中的seleted属性可以改变图例选中状态
  2. 随后为其中一个容器绑定legend切换事件legendselectchanged,在事件回调中手动修改另一个容器图例选中状态,即可同步操作

值得注意的是容器的echarts实例的渲染顺序,绑定事件的图表必须先渲染。并需要保证被修改图例状态的实例存在。

代码如下

js 复制代码
    var e1 = echarts.init(document.querySelector('#e1'));
    var e2 = echarts.init(document.querySelector('#e2'));

    var option = {
        grid: [
            { left: '7%', top: '7%', width: '38%', height: '38%' },
            { right: '7%', top: '7%', width: '28%', height: '38%' },
            { left: '7%', bottom: '7%', width: '38%', height: '38%' },
            { right: '7%', bottom: '7%', width: '38%', height: '38%' }
        ],
        xAxis: [
            {
                type: 'category',
                gridIndex: 0,
                data: ['1', '2', '3', '4']
            },
            {
                type: 'category',
                gridIndex: 1,
                data: ['1', '2', '3', '4']
            },
            {
                type: 'category',
                gridIndex: 2,
                data: ['1', '2', '3', '4']
            },
            {
                type: 'category',
                gridIndex: 3,
                data: ['1', '2', '3', '4']
            }
        ],
        yAxis: [
            {
                type: 'value',
                gridIndex: 0,
            },
            {
                type: 'value',
                gridIndex: 1,
            },
            {
                type: 'value',
                gridIndex: 2,
            },
            {
                type: 'value',
                gridIndex: 3,
            }
        ],
        legend: {},
        tooltip: {},
        series: [
            {
                type: 'bar',
                name: '111',
                yAxisIndex: 0,
                xAxisIndex: 0,
                data: new Array(4).fill(0).map(() => (Math.random() * 100).toFixed(2)),
            },
            {
                type: 'bar',
                name: '222',
                data: new Array(4).fill(0).map(() => (Math.random() * 100).toFixed(2)),
                yAxisIndex: 1,
                xAxisIndex: 1,
            },
            {
                type: 'line',
                name: '111',
                yAxisIndex: 2,
                xAxisIndex: 2,
                data: new Array(4).fill(0).map(() => (Math.random() * 100).toFixed(2)),
            },
            {
                type: 'line',
                name: '222',
                yAxisIndex: 3,
                xAxisIndex: 3,
                data: new Array(4).fill(0).map(() => (Math.random() * 100).toFixed(2)),
            }
        ]
    };
    e1.setOption(option);
    e2.setOption(option);
    e1.off().on('legendselectchanged', function (params) {
        var tempOption = {
            legend: {
                selected: params.selected
            }
        }
        e2.setOption(tempOption)
    });

总结

前两种情况可以覆盖大部分场景,但对于数据的处理比较繁琐,必须搞明白数据与坐标系之间的对应关系。最后一个方法也有不足之处,虽然一劳永逸,但与执行顺序和实例自身状态关联较深。可根据需要选择最佳方式~

相关推荐
Uyker34 分钟前
从零开始制作小程序简单概述
前端·微信小程序·小程序
EndingCoder5 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客6 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro6 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom7 小时前
javaweb -html -CSS
前端·javascript·html
打小就很皮...7 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡8 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜059 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx9 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing99910 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序