score:4
Accepted answer
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/
score:0
You could extend the bar chart and override the scale draw event to shade the background, like so
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);
scale.xLabels.forEach(function (xLabel, i) {
ctx.fillStyle = data.labelBackgrounds[i];
ctx.fillRect(
scale.calculateX(i - (scale.offsetGridLines ? 0.5 : 0)),
scale.startPoint,
barAreaWidth,
barAreaHeight);
ctx.fill();
})
}
}
Chart.types.Bar.prototype.initialize.apply(this, arguments);
}
});
with the chart data like so
var data = {
labelBackgrounds: ["rgba(120,220,220,0.2)", "rgba(220,120,220,0.2)", "rgba(220,220,120,0.2)", "rgba(120,120,220,0.2)", "rgba(120,220,120,0.2)", "rgba(220,120,120,0.2)", "rgba(120,120,120,0.2)"],
labels: ...
Note - if you just want to color the bars, I believe there was already a question on SO for the same.
Fiddle - http://jsfiddle.net/x73rhq2y/
Source: stackoverflow.com
Related Query
- How to set a full length background color for each bar in chartjs bar
- how to set color for each item in tooltip with ChartJS
- How to use set the color for each bar in a bar chart using chartjs?
- How to set specific height for chartJs background color in terms of yAxis value
- Different color for each bar in a bar chart; ChartJS
- Gradient color for each bar in a bar graph using ChartJS
- How to set single color on each bar in angular chart js
- how to set color for each datapoint in chartjs-chart-treemap
- How to set X coordinate for each bar with react chart js 2?
- How can I set different colours for each bar in angular-chart.js v1.0.0?
- Set different color for each bars using ChartJS in Angular 4
- ChartJS How to set color to just one bar
- Different color for each bar in a bar graph in ChartJS and VueJS
- how to create bar chart with group and sam color for each group using chart.js?
- chartjs : how to set custom scale in bar chart
- Different color for each column in angular-chartjs bar chart
- Chart.js Pie Chart: How to set background image for segment
- how to set chart.js grid color for line chart
- Chart.js how to set cutoutPercentages for each dataset
- ChartJS click on bar and change it's background color
- How to make customized stepSize for each horizontal bar in Chart.js graph
- How to add background color between two lines in yAxis Chartjs
- How to remove bars for those bars with zero value in Chartjs bar chart?
- ChartJS How to set max labels for X axis?
- How to add background color for doughnut mid using chart,js
- Setting Common labels and background color common for all the charts in ChartJs
- How to add background color between two specific lines in Chartjs 3.1
- How to set static labels for ng2-charts bar charts?
- How to apply to different bground color for each area in Chart.js
- How to set the chartjs bar graph scale to the highest value in the result data
More Query from same tag
- Age pyramid: 3.5.1 chartjs
- How do I use ChartJS with a background color in the space between two line charts?
- Why tooltip position absolute does not apply using js?
- Problems displaying yLabels in charts.js
- How to show data values or index labels in ChartJs (Latest Version)
- Chart.js bubble chart changing dataset labels
- chart.js increase value on click
- ChartJs - Double tooltip Top and Bottom on hover
- Programmatically creating labels for Chart.js in angular?
- Vue Chart.js -- can I give a line chart a default value for missing data?
- Change <p> and <h> element when selecting something from dropdown
- How to fix bar width in chartjs?
- dataset not accepted in declaration of scatterChartData for Chartjs
- Chart.js removing first dataset and label
- Issues getting JSON string for chart.js data
- All the bar in bar chart stays at left with very little width
- How to create Stacked bar chart from JSON in chart.js
- How to use Selenium with "chart.js"
- Can I add some variables to my chart.js in page know that variables?
- how to programmatically make a line chart point active/highlighted
- Skipping the initial animation of a line chart
- Convert date label on Y-axis Line Chart to specific date format
- Chartjs multiple barcharts corresponding to one label
- chart.js onAnimationProgress progress percentage
- Chart.js V2.7.2 How to make Horizontal scroll bar without loosing Y-Axis
- How to display grouped data in ChartJs
- When I try to include the chart.js and angular chartjs files, I get: ReferenceError: require is not defined
- Chart.js with Node.js
- Unable to get Line Chart tooltip on ChartJS
- chartjs bar chart filter using datetimepicker