根据高图表中的列值更改数据标签颜色、旋转和对齐值



未求解高图表数据标签动态旋转作为列高的可能重复

柱形图示例的小提琴:http://jsfiddle.net/Yrygy/266/

有一些非常大的列和一个非常小的列。我想在大列中显示垂直旋转到 -90 度的白色标签,对于较小的列,我想在列的顶部显示深灰色标签,旋转 0 度。

经过一番混乱,我实现了这个:http://jsfiddle.net/Yrygy/267/我可以根据格式化程序函数中的值更改标签的颜色,但是使用全局变量来正确对齐和旋转是行不通的。

这方面的任何帮助都将非常有帮助。我无法使用重复问题中建议的解决方案,因为我需要保持 Y 轴均匀并且无法设置 MinPointLength。

最终代码:

var GlobalRotation = -90;
var GlobalAlign = 'right';
var X;
var Y;
$(function () {
 $('#container').highcharts({
    chart: {
        type: 'column',
        height: 700
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    plotOptions: {
        column: {
            stacking: 'normal',
            pointPadding: 0,
            groupPadding: 0.2,
            dataLabels: {
                enabled: true,
                inside: false,
                style: {
                    fontWeight: 'bold'
                },
                formatter: function() {
                    var max = this.series.yAxis.max,
                        color =  this.y / max < 0.05 ? 'black' : 'white';
                        //GlobalRotation = this.y / max < 0.05 ? 0 : -90;
                        //GlobalAlign = this.y / max < 0.05 ? 'left' : 'right';
                        //X = this.y / max < 0.05 ? 4 : 4;
                        //Y = this.y / max < 0.05 ? 0 : 5;
                    return '<span style="color: ' + color + '">' + this.y + ' </span>';   
                },
                verticalAlign: "top",
                rotation : GlobalRotation,
                align: GlobalAlign
            }
        }
    },
    series: [{
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 2.33]
    }]
    });
});

可以更改Highcharts.Series.prototype.drawDataLabels,绘制dataLabels的函数:

Highcharts.Series.prototype.drawDataLabels = (function (func) {
    return function () {
        func.apply(this, arguments);
        if (this.options.dataLabels.enabled || this._hasPointLabels) realignLabels(this);
    };
}(Highcharts.Series.prototype.drawDataLabels));
realignLabels函数是

检查较短的列,并在其中更改该特定dataLabelrotationxy

function realignLabels(serie) {
    $.each(serie.points, function (j, point) {
        if (!point.dataLabel) return true;
        var max = serie.yAxis.max,
            labely = point.dataLabel.attr('y'),
            labelx = point.dataLabel.attr('x');
            if (point.y / max < 0.05) {
                point.dataLabel.attr({
                    y: labely - 20,
                    x: labelx + 5,
                    rotation: 0
                });
            }
    });
};

http://jsfiddle.net/Yrygy/270/

最新更新