其他分享
首页 > 其他分享> > 解决 Highcharts 中 yAxis 的 max 设置无效的问题

解决 Highcharts 中 yAxis 的 max 设置无效的问题

作者:互联网

问题场景

pic01

    $(function () {
        Highcharts.chart('container', {
            title: {
                text: 'line'
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                max: 350,
            },
            series: [{
                data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 250.4, 294.1, 95.6, 54.4]
            }]
        });
    });

解决方案

pic02

    $(function () {
        Highcharts.chart('container', {
            title: {
                text: 'line'
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                max: 350,
                tickAmount:8,
            },
            series: [{
                data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 250.4, 294.1, 95.6, 54.4]
            }]
        });
    });

解决方案2

  1. 还可以使用 tickPositioner 属性来手动设置坐标轴刻度

pic03

    $(function () {
        Highcharts.chart('container', {
            title: {
                text: 'line'
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                tickPositioner: function () {
                    var positions = [0,100,200,300,350];
                    return positions;
                }
            },
            series: [{
                data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 250.4, 294.1, 95.6, 54.4]
            }]
        });
    });
  1. 你也可以使用函数自动计算出合适的间隙

pic04

    $(function () {
        $('#container').highcharts({
            title: {
                text: 'line'
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                tickPositioner: function () {
                    var positions = [],
                        tick = Math.floor(this.dataMin),
                        increment = Math.ceil((this.dataMax - this.dataMin) / 6);
                    for (tick; tick - increment <= this.dataMax; tick += increment) {
                        positions.push(tick);
                    }
                    positions.push(350);
                    return positions;
                }
            },
            series: [{
                data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 250.4, 294.1, 95.6, 54.4]
            }],
        });
    });

参考

tickAmount API
tickPositioner API

标签:function,yAxis,max,tickAmount,text,350,Highcharts
来源: https://www.cnblogs.com/baimeishaoxia/p/11871273.html