score:3
There is no option to do this with chartjs. However you can write your own plugin and draw the background by yourself in the beforeDraw
hook for example.
var chart = new Chart(ctx, {
plugins: [{
beforeDraw: function(chart) {
//..
}
}]
});
You can get all the information to calculate the height of an y-axis-segment from the chart parameter. I've included a snippet below how this could be implemented. Note however that this is more a proof of concept than a proper implementation:
var canvas = document.getElementById('myChart');
window.chartColors = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(51, 204, 51)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(201, 203, 207)'
};
var myLineChart = new Chart(canvas,
{
type: 'line',
data: {
labels: ['1', '2', '3', '4', '5'],
datasets: [
{
label: '# of Votes',
fill: false,
backgroundColor: window.chartColors.blue,
borderColor: window.chartColors.blue,
data: [2, 5, 12.5, 9, 6.3]
}
]
},
options: {
responsive: true,
title: {
display: true,
text: 'Conditional Background'
},
backgroundRules: [{
backgroundColor: window.chartColors.green,
yAxisSegement: 6
}, {
backgroundColor: window.chartColors.grey,
yAxisSegement: 12
}, {
backgroundColor: window.chartColors.red,
yAxisSegement: Infinity
}],
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 1
}
}]
}
},
plugins: [{
beforeDraw: function (chart) {
var ctx = chart.chart.ctx;
var ruleIndex = 0;
var rules = chart.chart.options.backgroundRules;
var yaxis = chart.chart.scales["y-axis-0"];
var xaxis = chart.chart.scales["x-axis-0"];
var partPercentage = 1 / (yaxis.ticksAsNumbers.length - 1);
for (var i = yaxis.ticksAsNumbers.length - 1; i > 0; i--) {
if (yaxis.ticksAsNumbers[i] < rules[ruleIndex].yAxisSegement) {
ctx.fillStyle = rules[ruleIndex].backgroundColor;
ctx.fillRect(xaxis.left, yaxis.top + ((i - 1) * (yaxis.height * partPercentage)), xaxis.width, yaxis.height * partPercentage);
} else {
ruleIndex++;
i++;
}
}
}
}]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<canvas id="myChart" width="400" height="250"></canvas>
score:1
Shiffty's answer is right on point, however it only works if the background values are present on the yAxis, which is not always the case... depends on what fits. A more generic solution is to calculate the actual values:
var canvas = document.getElementById('myChart');
window.chartColors = {
red: 'rgb(255, 99, 132)',
orange: 'rgb(255, 159, 64)',
yellow: 'rgb(255, 205, 86)',
green: 'rgb(51, 204, 51)',
blue: 'rgb(54, 162, 235)',
purple: 'rgb(153, 102, 255)',
grey: 'rgb(201, 203, 207)'
};
var myLineChart = new Chart(canvas,
{
type: 'line',
data: {
labels: ['1', '2', '3', '4', '5'],
datasets: [
{
label: '# of Votes',
fill: false,
backgroundColor: window.chartColors.blue,
borderColor: window.chartColors.blue,
data: [2, 5, 12.5, 9, 6.3]
}
]
},
options: {
responsive: true,
title: {
display: true,
text: 'Conditional Background'
},
backgroundRules: [{
backgroundColor: window.chartColors.green,
yAxisSegement: 6
}, {
backgroundColor: window.chartColors.grey,
yAxisSegement: 12
}, {
backgroundColor: window.chartColors.red,
yAxisSegement: 999999
}],
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
stepSize: 1
}
}]
}
},
plugins: [{
beforeDraw: function (chart) {
var rules = chart.chart.options.backgroundRules;
var ctx = chart.chart.ctx;
var yAxis = chart.chart.scales["y-axis-0"];
var xaxis = chart.chart.scales["x-axis-0"];
for (var i = 0; i < rules.length; ++i) {
var yAxisSegement = (rules[i].yAxisSegement > yAxis.ticksAsNumbers[0] ? yAxis.ticksAsNumbers[0] : rules[i].yAxisSegement);
var yAxisPosStart = yAxis.height - ((yAxisSegement * yAxis.height) / yAxis.ticksAsNumbers[0]) + chart.chart.controller.chartArea.top;
var yAxisPosEnd = (i === 0 ? yAxis.height : yAxis.height - ((rules[i - 1].yAxisSegement * yAxis.height) / yAxis.ticksAsNumbers[0]));
ctx.fillStyle = rules[i].backgroundColor;
ctx.fillRect(xaxis.left, yAxisPosStart, xaxis.width, yAxisPosEnd - yAxisPosStart + chart.chart.controller.chartArea.top);
}
}
}]
});
Source: stackoverflow.com
Related Query
- Chart JS — Conditional horizontal row background colours
- 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?
- Horizontal stacked bar chart with chart.js
- Horizontal bar chart in chart.js
- 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
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- set background color to save canvas chart
- Draw a horizontal and vertical line on mouse hover in chart js
- Can I specify a different font size for each row of text in Chart Title?
- Creating a horizontal bar chart extension on Chart.js
- Chart js different background for y axis
- (Vue, ChartJS) Create gradient background for chart from child component canvas context
- How to draw Horizontal line on Bar Chart Chartjs
- Stacked horizontal bar chart along total count with chart.js
- Chart.js: Minimum value for x-axis at horizontal stacked bar chart
- Chart.js - Color specific parts of the background in a line chart
- two xAxes horizontal bar chart Charts.js
- How to add gradient background to Line Chart [vue-chart.js]
- Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar
- How to change Chart.js horizontal bar chart Width?
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- to increase space between x axis and first horizontal bar in chart js
- How to add background image in bar chart column in Chart.js?
- Draw a horizontal bar chart from right to left
- Vue ChartKick - different colours for bar chart
- Angular-chart / line chart with multiple horizontal lines (margins)
More Query from same tag
- How to create multi scale chart?
- How to show xaxis lable o only data point and hide all others?
- create a multi line chart using Chart.js
- Chartjs custom colors not displaying when dataset is changed
- Chart.js Radar Labels Coordinates
- chart.js chart-click pass parameter in Angular controller
- ChartJs beforeDraw method not getting called in release build
- Chart.js: hiding series by clicking on legend
- How to increase Chart.js yAxes' height to prevent overlapping
- Draw data in graphics generated by Chart JS
- nnnick chart.js - Custom Tooltip on Line Chart
- Changing the opacity of the colour on the Polar Area chart
- Ionic 3 Angular Chart.js update data
- Pass function elements to render in js
- Chart.js - Writing Labels Inside of Horizontal Bars?
- Chart.js time scale showing one of the dates wrong
- How to change the z order of a chartjs polar chart
- Create an interactive custom tooltip for chartjs
- Having problems displaying multiple charts on a page
- How to use icon as legend in Chart.js?
- Chart not rendering w/Chart.js on button click
- How to add background image in bar chart column in Chart.js?
- I want to change color of individual bar of bar graph
- Can't display chart.js after fadeOut the pageloader
- Chart.Mvc colliding with System.Web.Helpers.Chart
- How to make react-chartjs-2 responsive on mobile?
- d3.csv parseFloat will not parse correctly
- How do I incorporate Luxon timezone options with chart.js?
- Implementing Data Decimation in vue chartjs
- How to show gradient vertically on chart js grouped bar chart?