echarts圖表tootip顯示不同圖表信息

先貼圖

image.png

需求是顯示三個獨(dú)立的圖表,在其中一個顯示tooltip時其他圖表的信息同時顯示。

一、思路:在echarts里并沒有提供不同繪制區(qū)域同時顯示圖表tooltip的方法,那就只能將這幾個圖表繪制在同一的區(qū)域里面

二、方法:配置3個網(wǎng)格布局

    // 配置3個網(wǎng)格布局
    const gridWidth = 27; // 每個圖表占27%寬度
    const grids = [];
    for (let i = 0; i < 3; i++) {
        grids.push({
            left: i === 0 ? '5%' : i === 1 ? `38%` : '70%',
            width: `${gridWidth}%`,
            top: '1%',
            height: '92%',
            bottom: '0%'
        });
    }

填充圖表數(shù)據(jù)及圖表所使用的網(wǎng)格信息

const series = [];
    for (let index = 0; index < 3; index++) {
        series.push({
            name: '',
            type: 'line',
            xAxisIndex: index,
            yAxisIndex: index,
            data: [],
            symbol: 'none',
            lineStyle: {
                width: 2,
                color: '#91cc75',
            },
            animation: false, // 關(guān)閉動畫以提高性能
            smooth: false, // 不使用平滑曲線
        });
    }

配置各個網(wǎng)格的XY軸信息

    // 配置X軸(數(shù)值軸)
    const xAxes = [];
    for (let i = 0; i < 3; i++) {
        xAxes.push({
            type: 'value',
            gridIndex: i,
            axisLine: {
                onZero: false  // 解決反轉(zhuǎn)Y軸后X軸回跑到頂部的問題
            }
        });
    }

    // 配置Y軸(時間軸)
    const yAxes = [];
    for (let i = 0; i < 3; i++) {
        yAxes.push({
            type: 'value',
            gridIndex: i,
            inverse: true, // 從上往下顯示
        });
    }

配置option

const option = {
        tooltip: {
            trigger: 'axis', //橫向顯示tooltip
            axisPointer: { //線的樣式
                type: 'line',
                axis: 'y',
                lineStyle: {
                    type: 'solid',
                    color: 'red',
                },
            },
            formatter(params: any) {
                if (!params || params.length === 0) {
                    return '';
                }
                let result = ''
                for (let index = 0; index < params.length; index++) {
                    const item = params[index]
                    result += `<div style="font-weight:600;margin-bottom:6px;"> ${item.seriesName}:${item.value[1]}</div>`;
                }
                return result
            },
        },
        axisPointer: {
            link: [
                {
                    yAxisIndex: [0, 1, 2]  // 三條線同時顯示
                }
            ],
        },
        grid: grids,
        xAxis: xAxes,
        yAxis: yAxes,
        series,
    };
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容