You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Highcharts气泡图的四个象限各添加一条plot line?

Highcharts气泡图:四象限添加Plot Line实现方案

这个需求完全可以实现。Highcharts的x轴和y轴支持配置多条plotLines,通过设置不同的线条位置与标签偏移,就能把plot line放到四个象限中。

实现核心思路

  • X轴添加两条plot line,共享同一轴位置,通过调整标签的垂直偏移(label.y)分别放在上下区域
  • Y轴添加两条plot line,共享同一轴位置,通过调整标签的水平对齐与偏移(label.align、label.x)分别放在左右区域
  • 用zIndex保证plot line显示在气泡数据上方

修改后的完整代码

Highcharts.chart('container', {
    chart: {
        type: 'bubble',
        plotBorderWidth: 1,
    },
    legend: {
        enabled: false
    },
    accessibility: {
        point: {
            valueDescriptionFormat: '{index}. {point.name}, fat: {point.x}g, sugar: {point.y}g, obesity: {point.z}%.'
        }
    },
    xAxis: {
        gridLineWidth: 1,
        title: {
            text: 'Изменение'
        },
        labels: {
            format: '{value}'
        },
        plotLines: [
            // 右上象限X轴标线
            {
                color: 'black',
                dashStyle: 'dot',
                width: 2,
                value: 65,
                label: {
                    align: "left",
                    rotation: 0,
                    y: 15,
                    style: {
                        fontStyle: 'italic',
                        color: "#418744"
                    },
                    text: '右上象限标签'
                },
                zIndex: 3
            },
            // 右下象限X轴标线
            {
                color: 'black',
                dashStyle: 'dot',
                width: 2,
                value: 65,
                label: {
                    align: "left",
                    rotation: 0,
                    y: -15,
                    style: {
                        fontStyle: 'italic',
                        color: "#418744"
                    },
                    text: '右下象限标签'
                },
                zIndex: 3
            }
        ],
    },
    yAxis: {
        startOnTick: false,
        endOnTick: false,
        title: {
            text: 'Ускорение'
        },
        labels: {
            format: '{value}'
        },
        maxPadding: 0.2,
        plotLines: [
            // 左上象限Y轴标线
            {
                color: 'black',
                dashStyle: 'dot',
                width: 2,
                value: 75,
                label: {
                    align: 'right',
                    style: {
                        fontStyle: 'italic',
                        color: "#4B514C"
                    },
                    text: '左上象限标签',
                    x: -10
                },
                zIndex: 3
            },
            // 左下象限Y轴标线
            {
                color: 'black',
                dashStyle: 'dot',
                width: 2,
                value: 75,
                label: {
                    align: 'left',
                    style: {
                        fontStyle: 'italic',
                        color: "#4B514C"
                    },
                    text: '左下象限标签',
                    x: 0
                },
                zIndex: 3
            }
        ],
    },
    tooltip: {
        useHTML: true,
        headerFormat: '<table>',
        pointFormat: '<tr><th colspan="2"><h3>{point.country}</h3></th></tr>' +
            '<tr><th>Fat intake:</th><td>{point.x}g</td></tr>' +
            '<tr><th>Sugar intake:</th><td>{point.y}g</td></tr>' +
            '<tr><th>Obesity (adults):</th><td>{point.z}%</td></tr>',
        footerFormat: '</table>',
        followPointer: true
    },
    plotOptions: {
        series: {
            dataLabels: {
                enabled: true,
                format: '{point.name}'
            }
        }
    },
    series: [{
        data: [
            { x: 80, y: 102.9, z: 14.7, name: 'DE', country: 'Germany' },
            { x: 50, y: 55.9, z: 14.7, name: 'RU', country: 'Russia' },
        ],
        colorByPoint: true
    }]
});

关键调整说明

  • X轴两条标线共用value:65,通过label.y的正负值将标签分别定位到右上、右下象限
  • Y轴两条标线共用value:75,通过label.align和label.x将标签分别定位到左上、左下象限
  • 所有标线设置zIndex:3,确保线条和标签不会被气泡遮挡

内容的提问来源于stack exchange,提问作者Listopad02

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 11:45:08