score:0
In case you want the colored rectangles spread over the entire chart, you can draw individual boxes of different background color using chartjs-plugin-annotation.js.
new Chart(document.getElementById('canvas'), {
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Dataset',
data: [1, 2, 3, 4, 5, 6, 7],
fill: false,
backgroundColor: 'rgb(0, 0, 0)',
borderColor: 'rgb(0, 0, 0)'
}]
},
options: {
responsive: true,
scales: {
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'Colored scale'
}
}]
},
annotation: {
annotations: [{
drawTime: "beforeDatasetsDraw",
type: "box",
xScaleID: "x-axis-0",
yScaleID: "y-axis-0",
yMin: 4.5,
yMax: 8,
backgroundColor: "rgba(255, 0, 0, 1)",
borderWidth: 0
},
{
drawTime: "beforeDatasetsDraw",
type: "box",
xScaleID: "x-axis-0",
yScaleID: "y-axis-0",
yMin: 3.5,
yMax: 4.5,
backgroundColor: "rgba(255, 165, 0, 1)",
borderWidth: 0
},
{
drawTime: "beforeDatasetsDraw",
type: "box",
xScaleID: "x-axis-0",
yScaleID: "y-axis-0",
xMin: 0,
xMax: 100,
yMin: 0,
yMax: 3.5,
backgroundColor: "rgba(50, 250, 50, 1)",
borderWidth: 0
}
]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.js"></script>
<canvas id="canvas" height="90">
score:0
You can draw individual boxes directly on the canvas using the Plugin Core API. The API offers a range of hooks that may be used for performing custom code.
In below code snippet, I use the beforeDraw
hook to draw the rectangles of different background color each.
new Chart(document.getElementById('canvas'), {
type: 'line',
plugins: [{
beforeDraw: chart => {
var ctx = chart.chart.ctx;
var xAxis = chart.scales['x-axis-0'];
var yAxis = chart.scales['y-axis-0'];
ctx.save();
ctx.fillStyle = 'green';
ctx.beginPath();
var yGreen = yAxis.getPixelForValue(3.5);
ctx.fillRect(xAxis.left - 6, yGreen, 6, yAxis.bottom - yGreen);
ctx.stroke();
ctx.fillStyle = 'orange';
ctx.beginPath();
var yOrange = yAxis.getPixelForValue(4.5);
ctx.fillRect(xAxis.left - 6, yOrange, 6, yGreen - yOrange);
ctx.stroke();
ctx.fillStyle = 'red';
ctx.beginPath();
var yRed = yAxis.getPixelForValue(7);
ctx.fillRect(xAxis.left - 6, yRed, 6, yOrange- yRed);
ctx.stroke();
ctx.restore();
}
}],
data: {
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [{
label: 'Dataset',
data: [1, 2, 3, 4, 5, 6, 7],
fill: false
}]
},
options: {
responsive: true,
scales: {
yAxes: [{
gridLines: {
tickMarkLength: 15
},
ticks: {
padding: 6
},
scaleLabel: {
display: true,
labelString: 'Colored scale'
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="canvas" height="90">
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 different row background colors according to Y axis values
- Y-Axis with different colors
- Chart.js Adapting different X axis with same scale
- Angular-Chart-JS - Line chart with different fill colors according to points' range
- chartjs display data in one bar line with 3 different data sets in 3 different colors
- Having different colors per bar with angular charts chart.js v2
- different colors for bar chart with chart.js
- How to draw one line with different colors in chartjs2?
- chart.js - Second bar, with another label and different colors
- Same bar different color / One bar with many colors
- Charts.js Formatting Y Axis with both Currency and Thousands Separator
- Chartjs random colors for each part of pie chart with data dynamically from database
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Chart.js Line-Chart with different Labels for each Dataset
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- Chart.js 2.0 Formatting Y Axis with Currency and Thousands Separator
- Chart.js Mixed Bar and Line chart with different scales
- y axis Formatting with Metric prefix 1000=> 1k Chart.js
- chart.js not allowing y axis steps with logarithmic scale
- Style X and Y Axis (tick mark) with Chart.js?
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chart.js Dynamically Updating Chart with X Axis Time
- Obtain max value of y axis of line chart rendered with Chart.js
- ChartJS - Line Chart with different size datasets
- Line chart with large number of labels on X axis
- Chart js different background for y axis
- chart.js different x axis and tooltip format
- Chart.js with dual axis on bar and line graph
More Query from same tag
- Fetch more data for line chart onZoom/onPan issues
- ChartsJS bar chart with HH:MM:SS format
- ChartJS does not render correctly
- Implementing a choropleth chart using chartjs-chart-geo plugin with react-chartjs-2
- ChartJS show jittering on hover
- Chartjs tooltips disappear when calling Chart.update
- Setting Common labels and background color common for all the charts in ChartJs
- Set Y axis at the top of each bar using Chart.js library
- Doughnut spacing
- Chart.js and Angular 8 - Dynamically updating Chart.js labels and data from *ngfor
- responsive = false not working
- Prevent the wiping of an Array after routing Angular.js
- hh:mm in chart.js on X-axis and text labels on the Y-axis
- Chart js old chart data not clearing
- Chart.js how to get Combined Bar and line charts?
- Use Ternary Option with Chart.js and AngularJs
- Change chartType onclick ChartJS
- Make Chart.js horizontal bar labels multi-line
- How to display Labels on Doughnut chart in Chart.js
- ChartJS different background gradient depending on data (line graph)
- syntax error, unexpected ' ' Laravel 5.6 ConsoleTvs chart js
- Multiple charts rendering on top of each other
- Dynamically update the options of a chart in chartjs using Javascript
- Text in center of chartJS lead to infiniteloop
- is there a way to change the dispaly of a date
- chartjs - top and bottom padding of a chart area
- Trigger bar/pie graph highlight when hovering over an external element [chart.js / chart js] [AngularJS] [angular-chart.js]
- Chart.js stacked bar chart - sorting values in bar by value
- Line Chart: background of RangeSlider
- Animated Word Web in Javascript