score:2
Just to help others who are thinking how to add multiple background colors: I modified a little bit @potatopeelings answer. First I checked how many columns there are
var columnCount = chart.data.datasets[0].data.length;
var width = chartArea.right - chartArea.left;
var columnWidth = width/columnCount;
and then fill every other of them with background color
while (startPoint < chartArea.right) {
ctx.fillRect(startPoint, chartArea.top, columnWidth, height);
startPoint += columnWidth * 2;
}
For my chart this works better but for this the July column is for some reason only part of it. Doesn't know how to fix that but hopefully you can get idea from here.
score:4
I'm not sure what you mean by "the background color of the main area of the graph that isn't occupied with the line chart", but you can set the background-color of the canvas via CSS:
#myChart {
background-color: #666;
}
Look at this fiddle to see how it affects the chart.
This will set the background for the whole canvas, if you only want to set it for the inner part, check out potatopeelings solution.
score:30
With v2.1 of Chart.js, you can write your own plugin to do this
Preview
Script
Chart.pluginService.register({
beforeDraw: function (chart, easing) {
if (chart.config.options.chartArea && chart.config.options.chartArea.backgroundColor) {
var helpers = Chart.helpers;
var ctx = chart.chart.ctx;
var chartArea = chart.chartArea;
ctx.save();
ctx.fillStyle = chart.config.options.chartArea.backgroundColor;
ctx.fillRect(chartArea.left, chartArea.top, chartArea.right - chartArea.left, chartArea.bottom - chartArea.top);
ctx.restore();
}
}
});
and then
...
options: {
chartArea: {
backgroundColor: 'rgba(251, 85, 85, 0.4)'
}
}
...
Fiddle - http://jsfiddle.net/rrcd60y0/
Source: stackoverflow.com
Related Query
- Background colour of line charts in chart.js
- Chart Js Change Label orientation on x-Axis for Line Charts
- Chart.js line chart set background color
- How to save Chart JS charts as image without black background using blobs and filesaver?
- chart.js Line chart with different background colors for each section
- Chart.js - Color specific parts of the background in a line chart
- How to add gradient background to Line Chart [vue-chart.js]
- How to add multiple background color in line charts
- Remove background color in chartjs line chart
- How to change background color of labels in line chart from chart.js?
- Chart JS Line Graph multitooltipkey background color issue
- Ng2 charts - Animation doesn't work on line chart onInit
- Chart.js - Add gradient to line chart background
- react-chartjs-2 how to set multiple background levels within a line chart
- data in charts of charts.js is changing when I click in the line chart
- How to change chart background colour when exporting to PDF with chart.js
- Trouble with setting background color for Line chart at Chartjs version 3.5.1
- Line chart Change background color of shaded region on hover (Chartjs)
- how to not repeat code while creating multiple charts in chart js
- Limit labels number on Chart.js line chart
- ChartJS Line Charts - remove color underneath lines
- Chart.js - How to set a line chart dataset as disabled on load
- Chart area background color chartjs
- Draw horizontal line on chart in chart.js on v2
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- Remove the vertical line in the chart js line chart
- Moving vertical line when hovering over the chart using chart.js
- create a multi line chart using Chart.js
- How to add an on click event to my Line chart using Chart.js
- line chart with {x, y} point data displays only 2 values
More Query from same tag
- Chartjs disable color change on hover withouth disabling tooltips
- Extend bar chart on Chart JS 2 into a new type of Chart
- Adding Vertical scroll bar in a bar-chart (chartjs)
- ChartJS width undefined in React Typescript
- ChartJS:align zeros on chart with multi axes
- How to only draw the graph and the Xaxis gridlines using chart.js
- chartJS generateLegend()
- How can I query data from my Cosmos DB in Angular 7?
- I want to put bar of different color by the values in chart.js
- Chart.js stacked bar chart in opposite direction
- Chart.js - draw an arbitrary point with tooltip
- How to get item what been clicked on Chart (primeNg chart + angular application)
- Chart JS - Change the color of the last element in a Bar Chart
- How to hide Fields and Strike-through Legends when the data is empty or Zero in Pie/Polar/Doughnut Chart?
- Uncaught ReferenceError : require is not defined - Chart.js
- How can I give the automatically generated y-axis values on bar charts in Chart.JS commas?
- Chartjs-plugin: How to add a different color to each label?
- Changing grid color in radar chart
- How to get multiple elements on a page using flask (tables, pie charts, etc.)
- yAxis labels are being cut off in ngEchars (Echarts)
- ChatJS 2.8.0 min/max axis issue and labels on the top
- Charts js showing only when positives data
- Show wrong information in chart js 2
- Line chart with combination of fill and line
- How to draw vertical axis in google charts
- (Angular Ionic) How can I stream my custom data with chartjs-plugin-streaming?
- ChartJS hide labels on small screen sizes
- How do I create multiple charts at the same page with angular?
- Make y-axis sticky when having horizontal scroll on chartJS and Angular
- ChartJs not working with this JSON Object