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
click(function(){ barChart. data. datasets[0]. backgroundColor = "rgba(192,192,192,1)"; createNumbers(); console.
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.
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.
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With