最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can color of data label be different inside and outside of the bar in Highchart - Stack Overflow

programmeradmin0浏览0评论

I'm wondering that the color of the text which is inside the bar chart(plotOptions.bar.dataLabels.color) can be different if the text doesn't fit to bar length. For instance:

Code is here:

$(function () {
$('#container').highcharts({
    chart: {
        type: 'bar',
        height: 700
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    plotOptions: {
        bar: {
            stacking: 'normal',
            pointPadding: 0,
            groupPadding: 0.2,
            dataLabels: {
                enabled: true,
                color: 'black',
                align: "right",
                format: '{y} M',
                inside: false,
                style: {
                    fontWeight: 'bold'
                },
                verticalAlign: "middle"
            },
        }
    },

    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]
    }]
});

});

Thanks in advance

I'm wondering that the color of the text which is inside the bar chart(plotOptions.bar.dataLabels.color) can be different if the text doesn't fit to bar length. For instance:

Code is here:

$(function () {
$('#container').highcharts({
    chart: {
        type: 'bar',
        height: 700
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    plotOptions: {
        bar: {
            stacking: 'normal',
            pointPadding: 0,
            groupPadding: 0.2,
            dataLabels: {
                enabled: true,
                color: 'black',
                align: "right",
                format: '{y} M',
                inside: false,
                style: {
                    fontWeight: 'bold'
                },
                verticalAlign: "middle"
            },
        }
    },

    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]
    }]
});

});

Thanks in advance

Share Improve this question asked Jun 3, 2014 at 9:29 Adem İlhanAdem İlhan 1,5003 gold badges16 silver badges27 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

Possible solution is to use formatter. Determine if value is lower than some level, and then change dataLabel color. For example: http://jsfiddle.net/Yrygy/176/

                formatter: function() {
                    var max = this.series.yAxis.max,
                        color =  this.y / max < 0.05 ? 'black' : 'white'; // 5% width
                    return '<span style="color: ' + color + '">' + this.y + ' M</span>';   
                },

You can also compare point's width with length of y-value string.

You can set the color to contrast, and it will change depending on whether the dataLabel is inside/outside the bar.

style: {
    color: 'contrast'
}

I use the "isInside" value like so:

 series: [{
   name: 'Percentage',
   data: this.calculateChartSeries(),
   dataLabels: {
     enabled: true,
     rotation: 0,
     align: 'right',
     borderWidth: 1,
     formatter: function() {
       if (this.point.isInside == true) {
         return '<span style="color: white">' + this.y + '%</span>';
       } else {
         return '<span style="color: black">' + this.y + '%</span>';
       }
     }
   }
 }]
 

But if that doesn't work for you, it's possible to use "this.point.shapeArgs.height" and check if that height is greater than the length of your text.

发布评论

评论列表(0)

  1. 暂无评论