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

javascript - applying background-color in bar chart using chart.js - Stack Overflow

programmeradmin5浏览0评论
var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
    labels:cmpny_timing,
    datasets: [{
        label: 'Hourly Attendance',
        data: d,
        backgroundColor: [
            'rgba(255, 99, 132, 0.2)',
            'rgba(54, 162, 235, 0.2)',
            'rgba(255, 206, 86, 0.2)',
            'rgba(75, 192, 192, 0.2)',
            'rgba(153, 102, 255, 0.2)'
        ],
        borderColor: [
            'rgba(255,99,132,1)',
            'rgba(54, 162, 235, 1)',
            'rgba(255, 206, 86, 1)',
            'rgba(75, 192, 192, 1)',
            'rgba(153, 102, 255, 1)
          ],
        borderWidth: 1
    }]
},

is there anyway i can change the color of each bar according to the data....i want to make background colors according to the data in my data i have an array of length 15 so is there anyway i can create 15 colors bars dynamically rather than hardcoding the background color property of chart.js

var myChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
    labels:cmpny_timing,
    datasets: [{
        label: 'Hourly Attendance',
        data: d,
        backgroundColor: [
            'rgba(255, 99, 132, 0.2)',
            'rgba(54, 162, 235, 0.2)',
            'rgba(255, 206, 86, 0.2)',
            'rgba(75, 192, 192, 0.2)',
            'rgba(153, 102, 255, 0.2)'
        ],
        borderColor: [
            'rgba(255,99,132,1)',
            'rgba(54, 162, 235, 1)',
            'rgba(255, 206, 86, 1)',
            'rgba(75, 192, 192, 1)',
            'rgba(153, 102, 255, 1)
          ],
        borderWidth: 1
    }]
},

is there anyway i can change the color of each bar according to the data....i want to make background colors according to the data in my data i have an array of length 15 so is there anyway i can create 15 colors bars dynamically rather than hardcoding the background color property of chart.js

Share Improve this question asked Sep 20, 2016 at 10:37 Masooma AhmadMasooma Ahmad 631 gold badge1 silver badge7 bronze badges 6
  • First of all, seems that the closing are not proper. You need close like 'rgba(153, 102, 255, 1)' for last item in borderColor: – Saji Commented Sep 20, 2016 at 10:52
  • If the color is not important, you could perhaps fill an array with 15 random colors - npmjs./package/randomcolor – dmoo Commented Sep 20, 2016 at 10:54
  • You can change the color of each bar according to the data. I have put up an example here, Please check it at, jsfiddle/1cLapgty – David R Commented Sep 20, 2016 at 10:54
  • @DavidR leme check that – Masooma Ahmad Commented Sep 20, 2016 at 11:00
  • Cool. Let us know if it is working. – David R Commented Sep 20, 2016 at 11:10
 |  Show 1 more ment

1 Answer 1

Reset to default 6

Using Chart.js plugins, you can create your own backgroundColor & borderColor properties and then assign them to the chart :

var randomColorPlugin = {

    // We affect the `beforeUpdate` event
    beforeUpdate: function(chart) {
        var backgroundColor = [];
        var borderColor = [];

        // For every data we have ...
        for (var i = 0; i < chart.config.data.datasets[0].data.length; i++) {

            // We generate a random color
            var color = "rgba(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ",";

            // We push this new color to both background and border color arrays
            // .. a lighter color is used for the background
            backgroundColor.push(color + "0.2)");
            borderColor.push(color + "1)");
        }

        // We update the chart bars color properties
        chart.config.data.datasets[0].backgroundColor = backgroundColor;
        chart.config.data.datasets[0].borderColor = borderColor;
    }
};


You can see a working example on this jsFiddle. Every time you run the code, you'll get different colors.
Here is one of the result :

发布评论

评论列表(0)

  1. 暂无评论