【问题标题】:highchart place x-axis labels in between ticks on a datetime axishighchart 在日期时间轴上的刻度之间放置 x 轴标签
【发布时间】:2016-02-18 21:12:07
【问题描述】:

使用 SO 中的各种帖子/问题作为参考,我创建了一个散点图 jsfiddle

xAxis: {
        opposite:true,
        type: 'datetime',
        gridLineWidth: 1,
        gridLineDashStyle: 'ShortDot',
        gridLineColor:'black',
        alternateGridColor: 'lightgrey',
        tickInterval: 3 * 30 * 24 * 3600 * 1000, // 1 quarter
        labels: {
            //align: "left",
            //padding:200,
            formatter: function () {
                var s = "";
                if (Highcharts.dateFormat('%b', this.value) == 'Jan') {
                    s = s + "Q1"
                };
                if (Highcharts.dateFormat('%b', this.value) == 'Apr') {
                    s = s + "Q2"
                };
                if (Highcharts.dateFormat('%b', this.value) == 'Jul') {
                    s = s + "Q3"
                };
                if (Highcharts.dateFormat('%b', this.value) == 'Oct') {
                    s = s + "Q4"
                };
                s = s + " " + Highcharts.dateFormat('%Y', this.value);
                return s;
            }
        },
        plotLines: [{
            color: 'red', // Color value
            value: now, // Value of where the line will appear
            width: 2, // Width of the line
            label: {
                text: 'Now',
                align: 'center',
                verticalAlign: 'bottom',
                y: +20,
                rotation: 0
            }
        }]
    },

但我对 X 轴标签位于刻度线附近感到震惊。 如何移动到网格的中间?

无论如何我可以实现以下目标吗?

我试过 align, padding 但没有帮助。当时间线增加时,我仍然应该将标签放在中间。 我应该对 tickInterval 做些什么吗?这可能是我缺少的一个简单属性。

我找到了这个链接 jsfiddle,它解决了我的问题,但有 2 个 x 轴,我正在从列表中填充数据。

【问题讨论】:

  • 使用类别轴,您可以使用 'tickMarkPlacement:between'。但由于您使用的是日期时间轴,您唯一的选择可能是设置标签的“x”属性,并将它们推到右侧或左侧

标签: javascript jquery highcharts data-visualization axis-labels


【解决方案1】:

我实现了 Christopher Cortez 的解决方案,发现 here

但是,也更改为触发 highcharts load 事件,而不是回调,并且我已将其更改为在触发 HighCharts redraw 事件时调用,以便它们在页面时保持对齐已调整大小。

$('#container').highcharts({
        chart: {
            defaultSeriesType: 'scatter',
            events: {
                 load: centerLabels,
                 redraw: centerLabels                    
            }
        },

        /* ... all the other options ...*/

 });

在哪里

function centerLabels(chart) {        
    var $container = $(chart.target.container); 
    var axes = chart.target.axes;

    var $labels = $container.find('.highcharts-axis-labels .timeline_label');
    var $thisLabel, $nextLabel, thisXPos, nextXPos, delta, newXPos;

    $labels.each(function () {
        $thisLabel = $(this).parent('span');
        thisXPos = parseInt($thisLabel.css('left'));

        $nextLabel = $thisLabel.next();
        // next position is either a label or the end of the axis
        nextXPos = $nextLabel.length ? parseInt($nextLabel.css('left')) : axes[0].left + axes[0].width;
        delta = (nextXPos - thisXPos) / 2.0;
        newXPos = thisXPos + delta;

        // remove the last label if it won't fit
        if ($nextLabel.length || $(this).width() + newXPos < nextXPos) {
            $thisLabel.css('left', newXPos + 'px');
        } else {
            $thisLabel.remove();
        }
    });
}

JSFiddle

【讨论】:

  • 它有效。但是当我尝试 export 时,标签没有在中间对齐。
  • 尝试使用load event而不是回调。
  • 这是个好主意,更干净。我编辑了我的答案。但是,它仍然没有解决导出问题。