How to set Highcharts xAxis position on the yAxis zero value (negative/positive chart)?

点点圈 提交于 2019-12-24 06:57:05

问题


I would like to have my xAxis not below my chart but in the middle (on the yAxis zero value).

And I would like also to have the xAxis labels :

  • above the xAxis when it is a negavite value
  • below the xAxis when it is a positive value

Here is what I have : jsFiddle

$(function () {
    $('#container').highcharts({
        xAxis: {
                    showFirstLabel : true,
                    showLastLabel : true,
                    type : "category",
                    tickLength : 0,
                    lineWidth : 2
            },

        series: [{
            data: [
                {name : 'T1', y: 123},{name : 'T2', y: 152},{name : 'T3', y: -120},{name : 'T4', y: 0},{name : 'T5', y: 142},{name : 'T6', y: 212}
            ],
            type : 'column'
        }]
    });
});
  • Does it exist an easy way to do this ?
  • If not, how to do this ?

Thanks.


Solution :

Thanks to Pawel Fus (thanks, thanks, thanks), here is the solution for my problem : jsFiddle.

$(function () {
    $('#container').highcharts({
        chart: {
            renderTo: 'container',
            type: 'column',
            events: {
                load: function () {
                                        var xAxis = this.xAxis[0];
                    var serie = this.series[0];

                    for (var current_tick in xAxis.ticks) {
                        var tick = xAxis.ticks[current_tick];

                        if(serie.data[current_tick]){
                            if (serie.data[current_tick].y > 0) {
                                tick.label.attr({
                                    y: tick.label.y + 18
                                });
                            }
                        }
                    }
                }
            }
        },
        xAxis: {
                    showFirstLabel : true,
                    showLastLabel : true,
                    type : "category",
                    tickLength : 0,
                    crossing:0,
                    opposite:true,
                    lineWidth : 2
            },

        series: [{
            data: [
                {name : 'T1', y: 123},{name : 'T2', y: 152},{name : 'T3', y: -120},{name : 'T4', y: 0},{name : 'T5', y: 142},{name : 'T6', y: 212}
            ],
            type : 'column'
        }]
    });
});

回答1:


Answers for you:

  1. Yes, this is possible by using Highcharts plugin for that.

  2. This is not supported, you can use chart.events.load to loop over all labels in xAxis and update their position according to value in first series for respective category



来源:https://stackoverflow.com/questions/20683223/how-to-set-highcharts-xaxis-position-on-the-yaxis-zero-value-negative-positive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!