Echarts 多个图表

本文介绍如何使用Echarts在一个Option中创建四个图表,并实现这些图表在页面上左右均匀分布,探讨多图表布局的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一个Option 四个均匀分布图表


var dataAll = [
    [
        [10.0, 8.04],
        [8.0, 6.95],
        [13.0, 7.58],
        [9.0, 8.81],
        [11.0, 8.33],
        [14.0, 9.96],
        [6.0, 7.24],
        [4.0, 4.26],
        [12.0, 10.84],
        [7.0, 4.82],
        [5.0, 5.68]
    ],
    [
        [10.0, 9.14],
        [8.0, 8.14],
        [13.0, 8.74],
        [9.0, 8.77],
        [11.0, 9.26],
        [14.0, 8.10],
        [6.0, 6.13],
        [4.0, 3.10],
        [12.0, 9.13],
        [7.0, 7.26],
        [5.0, 4.74]
    ],
    [
        [10.0, 7.46],
        [8.0, 6.77],
        [13.0, 12.74],
        [9.0, 7.11],
        [11.0, 7.81],
        [14.0, 8.84],
        [6.0, 6.08],
        [4.0, 5.39],
        [12.0, 8.15],
        [7.0, 6.42],
        [5.0, 5.73]
    ],
    [
        [8.0, 6.58],
        [8.0, 5.76],
        [8.0, 7.71],
        [8.0, 8.84],
        [8.0, 8.47],
        [8.0, 7.04],
        [8.0, 5.25],
        [19.0, 12.50],
        [8.0, 5.56],
        [8.0, 7.91],
        [8.0, 6.89]
    ]
];

var markLineOpt = {
    animation: false,
    label: {
        normal: {
            formatter: 'y = 0.5 * x + 3',
            textStyle: {
                align: 'right'
            }
        }
    },
    lineStyle: {
        normal: {
            type: 'solid'
        }
    },
    tooltip: {
        formatter: 'y = 0.5 * x + 3'
    },
    data: [[{
        coord: [0, 3],
        symbol: 'none'
    }, {
        coord: [20, 13],
        symbol: 'none'
    }]]
};

option = {
    title: {
        text: 'Anscombe\'s quartet',
        x: 'center',
        y: 0
    },
    grid: [
        /*38% 宽度图表刚好不接触*/
        /*左上*/
        {x: '7%', y: '7%', width: '38%', height: '38%'},
        /*右上*/
        {x2: '7%', y: '7%', width: '38%', height: '38%'},
        /*左下*/
        {x: '7%', y2: '7%', width: '38%', height: '38%'},
        /*右下*/
        {x2: '7%', y2: '7%', width: '38%', height: '38%'}
    ],
    tooltip: {
        formatter: 'Group {a}: ({c})'
    },
    xAxis: [
        {gridIndex: 0, min: 0, max: 20},
        {gridIndex: 1, min: 0, max: 20},
        {gridIndex: 2, min: 0, max: 20},
        {gridIndex: 3, min: 0, max: 20}
    ],
    yAxis: [
        {gridIndex: 0, min: 0, max: 15},
        {gridIndex: 1, min: 0, max: 15},
        {gridIndex: 2, min: 0, max: 15},
        {gridIndex: 3, min: 0, max: 15}
    ],
    series: [
        {
            name: 'I',
            type: 'scatter',
            xAxisIndex: 0,
            yAxisIndex: 0,
            data: dataAll[0],
            markLine: markLineOpt
        },
        {
            name: 'II',
            type: 'scatter',
            xAxisIndex: 1,
            yAxisIndex: 1,
            data: dataAll[1],
            markLine: markLineOpt
        },
        {
            name: 'III',
            type: 'scatter',
            xAxisIndex: 2,
            yAxisIndex: 2,
            data: dataAll[2],
            markLine: markLineOpt
        },
        {
            name: 'IV',
            type: 'scatter',
            xAxisIndex: 3,
            yAxisIndex: 3,
            data: dataAll[3],
            markLine: markLineOpt
        }
    ]
};

左右均匀分布

var dataAll = [389, 259, 262, 324, 232, 176, 196, 214, 133, 370];
var yAxisData = ['原因1', '原因2', '原因3', '原因4', '原因5', '原因6', '原因7', '原因8', '原因9', '原因10'];
var option = {
    backgroundColor: '#0f375f',
    title: [{
        text: "各渠道投诉占比",
        x: 'center',
        y: 0,
        textStyle: {
            color: "#fff",
            fontSize: "14"

        }
    }, ],
    grid: [{
            x: '7%',
            y: '7%',
            width: '38%'
        },
        {
            x2: '7%',
            y: '7%',
            width: '38%'
        },
    ],
    tooltip: {
        formatter: '{b} ({c})'
    },
    xAxis: [{
            gridIndex: 0,
            axisTick: {
                show: false
            },
            axisLabel: {
                show: false
            },
            splitLine: {
                show: false
            },
            axisLine: {
                show: false
            }
        },
        {
            gridIndex: 1,
            axisTick: {
                show: false
            },
            axisLabel: {
                show: false
            },
            splitLine: {
                show: false
            },
            axisLine: {
                show: false
            }
        },
    ],
    yAxis: [{
        gridIndex: 0,
        interval: 0,
        data: yAxisData.reverse(),
        axisTick: {
            show: false
        },
        axisLabel: {
            show: true
        },
        splitLine: {
            show: false
        },
        axisLine: {
            show: true,
            lineStyle: {
                color: "#6173a3"
            }
        },
    }, {
        gridIndex: 1,
        interval: 0,
        data: yAxisData.reverse(),
        axisTick: {
            show: false
        },
        axisLabel: {
            show: true
        },
        splitLine: {
            show: false
        },
        axisLine: {
            show: true,
            lineStyle: {
                color: "#6173a3"
            }
        },
    }],
    series: [

        {
            name: '投诉原因TOP10',
            type: 'bar',
            xAxisIndex: 0,
            yAxisIndex: 0,
            barWidth: '45%',
            itemStyle: {
                normal: {
                    color: '#86c9f4'
                }
            },
            label: {
                normal: {
                    show: true,
                    position: "right",
                    textStyle: {
                        color: "#9EA7C4"
                    }
                }
            },
            data: dataAll.sort(),
        }, {
            name: '各渠道投诉占比',
            type: 'bar',
            xAxisIndex: 1,
            yAxisIndex: 1,
            barWidth: '45%',
            itemStyle: {
                normal: {
                    color: '#86c9f4'
                }
            },
            label: {
                normal: {
                    show: true,
                    position: "right",
                    textStyle: {
                        color: "#9EA7C4"
                    }
                }
            },
            data: dataAll.sort(),
        },

    ]
};

### 实现多个图表的布局与交互 #### 使用 Grid 布局实现多图共存 为了在同一页面上显示多个 ECharts 图表,可以利用 `grid` 组件来定义每个图表的位置和大小。通过设置不同的 `left`, `right`, `top`, 和 `bottom` 属性,能够精确控制各个图表之间的相对位置[^1]。 ```javascript var chartDom1 = document.getElementById('main1'); var myChart1 = echarts.init(chartDom1); var option1; option1 = { grid: { // 设置第一个图表的网格参数 left: '5%', top: '10%', width: '45%', height: '80%' }, xAxis: {}, yAxis: {}, series: [{ type: 'bar' }] }; myChart1.setOption(option1); var chartDom2 = document.getElementById('main2'); var myChart2 = echarts.init(chartDom2); var option2; option2 = { grid: { // 设置第二个图表的网格参数 left: '55%', top: '10%', width: '40%', height: '80%' }, xAxis: {}, yAxis: {}, series: [{ type: 'line' }] }; myChart2.setOption(option2); ``` #### 利用 Timeline 进行多图切换互动 当希望提供一种时间轴式的浏览体验时,可采用 `timeline` 控件。此控件允许用户沿着预设的时间节点移动,并同步更新关联的所有图表的状态。每一个时间节点对应一组特定的数据集或视觉样式变化[^2]。 ```html <div id="container" style="height: 400px;"></div> <script src="https://cdn.jsdelivr.net/npm/echarts@latest/dist/echarts.min.js"></script> <script type="text/javascript"> var dom = document.getElementById("container"); var myChart = echarts.init(dom); var app = {}; option = null; option = { baseOption: { timeline: { axisType: 'category', autoPlay: true, playInterval: 3000, data: [ {value: '2023-01', symbolSize: 10}, {value: '2023-02'}, {value: '2023-03'} ] }, title : [{ text: '' }], tooltip : { trigger: 'axis' } }, options: [ { series : [ {name:'example',type:'pie',data:[...]} ] }, { series : [ {name:'example',type:'scatter',data:[...]} ] }, ... ] }; if (option && typeof option === "object") { myChart.setOption(option, true); } </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值