【问题标题】:How to set Highcharts xAxis position on the yAxis zero value (negative/positive chart)?如何在 yAxis 零值(负/正图表)上设置 Highcharts xAxis 位置?
【发布时间】:2013-12-19 13:37:50
【问题描述】:

我想让我的 xAxis 不在图表下方,而是在中间(在 yAxis 零值上)。

我也想要 xAxis 标签:

  • 在 xAxis 之上,当它是 negavite 值时
  • 低于 xAxis,当它是一个

这是我所拥有的: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'
        }]
    });
});
  • 是否有一种简单的方法可以做到这一点?
  • 如果没有,该怎么做?

谢谢。


解决办法:

感谢 Pawel Fus(谢谢,谢谢,谢谢),这是我的问题的解决方案: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'
        }]
    });
});

【问题讨论】:

    标签: javascript jquery highcharts


    【解决方案1】:

    给你答案:

    1. 是的,这可以通过使用Highcharts plugin 来实现。

    2. 不支持此功能,您可以使用chart.events.load 循环 xAxis 中的所有标签,并根据相应类别的第一个系列中的值更新它们的位置

    【讨论】:

    • 谢谢,我要试试这个插件。那我就在chart.events.load中更改标签位置,如果没有别的办法。
    • 在标签格式化程序中,您也可以尝试更新标签 y 位置,但我还没有尝试过。
    • 可以在标签格式化程序中更新标签 y 位置吗?我以为只是格式化文本。实际上,我只使用此功能来格式化日期。
    • 刚刚检查过,很抱歉您的评论,这是不可能的。关于我的第一个答案,请参阅如何更新 dataLabels:jsfiddle.net/9e8NS -axis.labels 可以轻松完成类似的操作。
    • 嗯.. 给你的例子:jsfiddle.net/9e8NS/1 - 如果有什么不清楚的地方请告诉我 ;)
    猜你喜欢
    • 2011-10-14
    • 1970-01-01
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多