根据highcharts中的Column值更改Datalabel Color,Rotation和Align值

Saa*_*ooq 4 javascript jquery highcharts

未解决的highcharts datalabel动态旋转可能重复为列高

小提琴样本柱形图: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]
    }]
    });

});
Run Code Online (Sandbox Code Playgroud)

Rae*_*emi 5

你可以改变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));
Run Code Online (Sandbox Code Playgroud)

realignLabels将是检查较短列的函数,并在其中更改rotation,x以及y特定的dataLabel:

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
                });
            }
    });
};
Run Code Online (Sandbox Code Playgroud)

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

  • 您可以像这样更改`style`:`style:'font-weight:bold; fill:#F00;'`.`fill`元素是你想要改变的元素:http://jsfiddle.net/Yrygy/272/ (2认同)