gpt4 book ai didi

javascript - 根据 Highcharts 中的列值更改数据标签颜色、旋转和对齐值

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:34:02 25 4
gpt4 key购买 nike

Unsolved highcharts datalabel dynamic rotation as column height 可能重复

示例柱形图的 Fiddle: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 将是检查较短列的函数,并在其中更改 rotationxy 那个特定的 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
});
}
});
};

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

关于javascript - 根据 Highcharts 中的列值更改数据标签颜色、旋转和对齐值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com