score:7
Chart.js clears the canvas before drawing (or redrawing) a chart.
We can jump in on this and draw our background once the chart is cleared. Just extend the Line chart and override the clear function in the initialize override.
Preview
Script
Chart.types.Line.extend({
name: "LineAlt",
initialize: function(data){
Chart.types.Line.prototype.initialize.apply(this, arguments);
// keep a reference to the original clear
this.originalClear = this.clear;
this.clear = function () {
this.originalClear();
// 1 x scale unit
var unitX = this.datasets[0].points[1].x - this.datasets[0].points[0].x;
var yTop = this.scale.startPoint;
var yHeight = this.scale.endPoint - this.scale.startPoint;
// change your color here
this.chart.ctx.fillStyle = 'rgba(100,100,100,0.8)';
// we shift it by half a x scale unit to the left because the space between gridline is actually a shared space
this.chart.ctx.fillRect(this.datasets[0].points[5].x - 0.5 * unitX, yTop, unitX * 5, yHeight);
this.chart.ctx.fillRect(this.datasets[0].points[15].x - 0.5 * unitX, yTop, unitX * 5, yHeight);
}
}
});
Then just use LineAlt instead of Line
var myNewChart = new Chart(ctx).LineAlt(data);
Fiddle - http://jsfiddle.net/oe2606ww/
score:0
I'd try a little work around,I'd draw an image with four line each one with width 1px and a different color; then in a CSS sheet define:
canvas {
background-image: url(backgroundimage.jpg);
background-size: contain;
}
score:2
I combined @potatopeelings's and @v25's solutions for a chart.js v2 solution. It utilizes the format of @potatopeelings's solution, allowing to use an alternate chart type (LineAlt), and the updated implementation from @v25's solution.
Chart.controllers.LineAlt = Chart.controllers.line.extend({
draw: function (ease) {
if (this.chart.config.options.chartArea && this.chart.config.options.chartArea.backgroundColor) {
var ctx = this.chart.chart.ctx;
var chartArea = this.chart.chartArea;
var meta = this.chart.getDatasetMeta(0);
var start = meta.data[1]._model.x;
var stop = meta.data[2]._model.x;
ctx.save();
ctx.fillStyle = this.chart.config.options.chartArea.backgroundColor;
ctx.fillRect(start, chartArea.top, stop - start, chartArea.bottom - chartArea.top);
ctx.restore();
}
// Perform regular chart draw
Chart.controllers.line.prototype.draw.call(this, ease);
}
});
Then you can use the custom chart type just as in @potatopeelings's solution:
var myNewChart = new Chart(ctx, {type: 'LineAlt', data: data});
score:6
Some people here have requested something that works for later versions, here's my hacked together solution that works on ChartJS 2.7.2 (EDIT: Apr 2020: Also 2.9.3) and could probably be adapted. Chart.types.Line.extend
used in the answer above, doesn't seem to be valid in v2.
I managed to figure this out with help from this thread to get the plugin code, and also found this thread useful for gathering co-ordinates of the data points.
With some work this fiddle should allow you to pass the label array keys as start/stop positions via the following code (where 0 and 1 are the keys):
var start = meta.data[0]._model.x;
var stop = meta.data[1]._model.x;
You could loop this, along with the ctx.fillRect
function to draw multiple rectangles.
Here's the working fiddle: http://jsfiddle.net/oe2606ww/436/
Source: stackoverflow.com
Related Query
- chart.js Line chart with different background colors for each section
- Chart.js - Line charts with different colors for each border lines
- Chartjs random colors for each part of pie chart with data dynamically from database
- Angular-Chart-JS - Line chart with different fill colors according to points' range
- Trouble with setting background color for Line chart at Chartjs version 3.5.1
- different colors for bar chart with chart.js
- Chart.js Line-Chart with different Labels for each Dataset
- Chart.js Mixed Bar and Line chart with different scales
- Different color for each column in angular-chartjs bar chart
- Can I specify a different font size for each row of text in Chart Title?
- ChartJS - Line Chart with different size datasets
- Chart js different background for y axis
- chart js - Apply different color for each x-axes label
- Add a text as tooltip for each point on a line chart
- chart.JS i want to put different color for each Y axis value grid line color
- Charts.js - Bar chart different colors for value intervals not working
- How to set X coordinate for each bar with react chart js 2?
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- angular-chart.js : issue with pointHoverBorderColor property for line chart
- How do I get a different label for each bar in a bar chart in ChartJS?
- With Chart js, I am trying to color every 7th vertical (x axis) grid line (representing each week beginning visually)
- ChartJS/High Charts Radar chart - Different radial axis labels for each category that appear on hover
- Legend color not working with randomly generated background colors in chartjs pie chart
- Chart.js 2.1.6 background color for each section
- How to assign different background colors to chart.js pie chart data set?
- chartjs display data in one bar line with 3 different data sets in 3 different colors
- 2 Line Chart with different labels | Chart.js
- Can chart.js display text associated with each point on a line chart that is permanently visible?
- Plugin for custom horizontal line Y-axis marker on grid of a Line chart with Chart.js
- Grouped bar chart having each group with different data using chart.js
More Query from same tag
- How to show xaxis lable o only data point and hide all others?
- How to make labels on both side from horizontal bar chart js
- Limit data in Chart.js with Flask/SQLAlchemy in python
- How do I get a chart.js chart to display data in a Node/React web application?
- chart.js version 2.4.0 does not display bars for me
- Using Chart.js with Gatsby markdown
- Add Y-Axis dynamically Chartjs
- Display point style on legend in chart.js
- Limit data points with chart js
- Filtering labels to show user from Chart.js
- ChartJs with try catch
- ChartJS tooltip scrollbar to prevent data overflow?
- Chart.js Customize tool tip to show more data
- How to make bar shape like bell curve in angular-chart?
- How to filter hover on a chart in ChartJS?
- Change Chart JS Bar Chart Border to Dotted Line
- How to add overlay color to chart.js pie chart segment?
- How to disable chartjs tooltip on mibile devices and small screens?
- ChartJS how to generate custom legend?
- Changing the opacity of the colour on the Polar Area chart
- I have 10 points inn x-axis, ranging [-25,-20,,-15,-10,0,10,20,30,40,50]. But I want my line chart to start from -15 of x-axis. How we can achieve?
- Chartjs hovering over one Chart, shows tooltip across all charts
- chartjs-plugin-annotation box in Django app
- How to display average line in chart.js
- Chart.js - Remove empty space in pie chart
- Angular Charts - not able to read data
- How to make bootstrap 4 column height the same as row height
- Vue-Chartjs reactive chart options passing
- Django and Chart.js: Line-Chart with multiple Datasets
- Chart.Js - Hightligh an area in the background