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

如何为Highcharts的Y轴标签gutter增加空间避免标签截断

解决Highcharts Y轴标签被竖线截断的问题

针对你遇到的Y轴标签被竖线截断、需要给标签区域增加40px空间的问题,可通过以下调整解决:

关键修改点

  • 移除yAxis.offset: -1:该负偏移会将Y轴向图表内部挤压,导致标签与轴重叠
  • 给Y轴标签添加x: 40:让标签向远离Y轴的方向偏移40px,避免被竖线截断
  • 增加chart.spacingRight: 60:给图表右侧预留足够空间,确保标签和后续注释不会超出容器

修改后的完整代码

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title></title>
  <style type="text/css">

    #container {
      height: 400px;
      width: 350px;
    }

    .highcharts-tick{display: none;}

    .highcharts-grid-line{opacity: 0.2}

    
  </style>
</head>
<body>

  <script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/data.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>
<script src="https://code.highcharts.com/stock/modules/export-data.js"></script>
<script src="https://code.highcharts.com/stock/modules/accessibility.js"></script>


<div id="container"></div>


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js" integrity="sha512-aVKKRRi/Q/YV+4mjoKBsE4x3H+BkegoM/em46NNlCqNTmUYADjBbeNefNxYV7giUp0VxICtqdrbqU7iVaeZNXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<div id=container2></div>


<script type="text/javascript">
  $(function() {
  
  const options = {
    chart: {
        backgroundColor: '#1B191B', // Background Color
        type: 'line',
        zoomType: "",
        spacingRight: 60 // 新增:增加右侧留白
    },
    rangeSelector : {
        enabled: false,
        selected : 2,
        inputEnabled: false
      },

    title: {
      text: 'APPLE INC', // Title of the Chart
      align: 'left',
      style: {
            color: '#dedbde',   // Custom CSS for the title
            fontWeight: 'bold'
        }
    },
    scrollbar: { enabled: false },
    exporting: {
      enabled: false 
    },
    yAxis: {
    lineWidth: 2,
    tickWidth: 1,
    labels: {
                style: {
                    color: '#dedbde' 
                },
                align: 'right',
                x: 40 // 新增:标签向右偏移40px
            },
         
    opposite: true, 
    // 移除offset: -1,避免轴挤压标签空间
  },

  xAxis: {
            labels: {
                style: {
                    color: '#dedbde' 
                }
            },
            gridLineWidth: 1,            
        },
     navigator: {
        enabled: false 
    },
    series: [{
      name: 'AAPL Stock Price',
      data: [],
      type: 'areaspline',
      threshold: null,
      color: '#5861B3', 
      tooltip: {
        valueDecimals: 2
      },
      
      fillColor: {
        linearGradient: {
          x1: 0,
          y1: 0,
          x2: 0,
          y2: 1
        },
        stops: [
          [0, Highcharts.getOptions().colors[0]],
          [1, Highcharts.color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
        ]
      }
    }]
  }
  const url = 'https://demo-live-data.highcharts.com/aapl-c.json'
  const chart = Highcharts.stockChart('container', options)
  $.getJSON(url, function(data) {
    chart.series[0].setData(data)
      // Execute callback
    if (chart.options.chart.events && chart.options.chart.events.dataLoad) {
      const dataLoad = chart.options.chart.events.dataLoad.bind(chart)
      dataLoad(data)
    }
  })
})

</script>


</body>
</html>

说明

  • yAxis.labels.x控制标签相对于Y轴的水平位置,设置为40正好满足你需要的40px间距要求
  • chart.spacingRight的值可根据你后续添加注释的大小调整,确保所有内容都能完整显示
  • 移除负偏移后,Y轴回到默认的右侧位置,不会再挤压标签空间

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 12:55:25