Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a full length background color for each bar in chartjs bar

How to set a full length background color for each bar in chartjs bar

pls refer below link i need same output https://drive.google.com/file/d/0B4X0e4vwH_UPVGxWN2NBdURyZ0E/view?usp=sharing

like image 353
Dhinakaran Avatar asked Sep 28 '22 03:09

Dhinakaran


People also ask

How do you change the color of a bar in ChartJS?

click(function(){ barChart. data. datasets[0]. backgroundColor = "rgba(192,192,192,1)"; createNumbers(); console.

How do I change the background color in ChartJS?

There is no built-in method to change background color, but you can use CSS. JSFiddle. If you want to fill exact area of chart and no whole div, you can write your own chart. js plugin.

How do you change the color of each column in a bar chart?

In a chart, click to select the data series for which you want to change the colors. On the Format tab, in the Current Selection group, click Format Selection. tab, expand Fill, and then do one of the following: To vary the colors of data markers in a single-series chart, select the Vary colors by point check box.

How do you change the color of a bar?

Click the Bar Chart in the Object Manager to select it. In the Property Manager, click on the Fill tab. Check the box next to Use color table. The bar chart will change to a variety of default colors.


1 Answers

You can adapt the other answer to show the background only for the bars - note that in this case you'll need the bars collection

Chart.types.Bar.extend({
    name: "BarAlt",
    initialize: function (data) {
        var self = this;
        var originalBuildScale = self.buildScale;
        self.buildScale = function () {
            originalBuildScale.apply(this, arguments);

            var ctx = self.chart.ctx;
            var scale = self.scale;
            var originalScaleDraw = self.scale.draw;
            var barAreaWidth = scale.calculateX(1) - scale.calculateX(0);
            var barAreaHeight = scale.endPoint - scale.startPoint;

            self.scale.draw = function () {
                originalScaleDraw.call(this, arguments);
                ctx.fillStyle = data.barBackground;
                self.datasets.forEach(function (dataset) {
                    dataset.bars.forEach(function (bar) {
                        ctx.fillRect(
                            bar.x - bar.width / 2,
                            scale.startPoint,
                            bar.width,
                            barAreaHeight);
                    })
                })
                ctx.fill();
            }
        }

        Chart.types.Bar.prototype.initialize.apply(this, arguments);
    }
});

with the chart data like so

var data = {
    barBackground: "rgba(221, 224, 229, 1)",
    ...

and the call like so (as before)

var ctx = document.getElementById("chart").getContext("2d");
var myBarChart = new Chart(ctx).BarAlt(data, {
    barValueSpacing: 15,
});

Fiddle - http://jsfiddle.net/ayy2vxsj/


enter image description here

like image 173
potatopeelings Avatar answered Oct 05 '22 08:10

potatopeelings