score:7
the easiest way to do this is to use the chartjs-plugin-annotation
plugin and configure 3 box annotations bound to your y axis (each box would have a different color).
here is an example below (and you can see it in action with this codepen).
var ctx = document.getelementbyid("mychart");
var mychart = new chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'points',
data: [
{x: 0, y: 2},
{x: 1, y: 3},
{x: 2, y: 2},
{x: 1.02, y: 0.4},
{x: 0, y: -1}
],
backgroundcolor: 'rgba(123, 83, 252, 0.8)',
bordercolor: 'rgba(33, 232, 234, 1)',
borderwidth: 1,
fill: false,
}],
},
options: {
title: {
display: true,
text: 'chart.js - gridline background',
},
scales: {
xaxes: [{
type: 'linear',
position: 'bottom',
ticks: {
min: -1,
max: 8,
stepsize: 1,
fixedstepsize: 1,
},
gridlines: {
color: 'rgba(171,171,171,1)',
linewidth: 1
}
}],
yaxes: [{
afterupdate: function(scaleinstance) {
console.dir(scaleinstance);
},
ticks: {
min: -2,
max: 4,
stepsize: 1,
fixedstepsize: 1,
},
gridlines: {
color: 'rgba(171,171,171,1)',
linewidth: 0.5
}
}]
},
annotation: {
annotations: [{
type: 'box',
yscaleid: 'y-axis-0',
ymin: 1,
ymax: 4,
bordercolor: 'rgba(255, 51, 51, 0.25)',
borderwidth: 2,
backgroundcolor: 'rgba(255, 51, 51, 0.25)',
}, {
type: 'box',
yscaleid: 'y-axis-0',
ymin: -1,
ymax: 1,
bordercolor: 'rgba(255, 255, 0, 0.25)',
borderwidth: 1,
backgroundcolor: 'rgba(255, 255, 0, 0.25)',
}, {
type: 'box',
yscaleid: 'y-axis-0',
ymin: -2,
ymax: -1,
bordercolor: 'rgba(0, 204, 0, 0.25)',
borderwidth: 1,
backgroundcolor: 'rgba(0, 204, 0, 0.25)',
}],
}
}
});
it's important to note that the annotations are painted on top of the chart, so your annotation color needs to contain some transparency to see the stuff behind it.
there is no problem using this plugin with ng2-charts. just add the source in a <script>
in your app's html file and add the annotation
property into your options
object. here is an angular2 / ng2-charts example demonstrating how to use the annotations plugin.
Source: stackoverflow.com
Related Query
- how to set chart.js grid color for line chart
- How to use set the color for each bar in a bar chart using chartjs?
- How do you set x and y axis and Title for a line chart using charts.js?
- Chart.js - How to set a line chart dataset as disabled on load
- How set color family to pie chart in chart.js
- Chart.js how to show cursor pointer for labels & legends in line chart
- Chart.js line chart set background color
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How to set a full length background color for each bar in chartjs bar
- How to change grid line width for one specific line
- How to dynamically set ChartJs line chart width based on dataset size?
- How to set single color on each bar in angular chart js
- How to add area with break on line chart with fill color
- how to set color for each item in tooltip with ChartJS
- How to use segment property to color line / border color based on value in chart js?
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- How to set dynamic value for horizontal line in annotation plugin for Chart.js
- chart.JS i want to put different color for each Y axis value grid line color
- How to change background color of labels in line chart from 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?
- With Chart js, I am trying to color every 7th vertical (x axis) grid line (representing each week beginning visually)
- How do you set up a chart.js scatter line chart through angular-chart.js?
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- How to show tick marks and labels with grid line for the specific one on the X axis with Chart.js?
- react-chartjs-2 how to set multiple background levels within a line chart
- How to set the number of of rows in a line chart in chart.js?
- how to set the color of all points in a line graph to the same in chart.js?
- ng2-charts / chart.js - How to set doughnut/pie chart color on specific labels programatically?
More Query from same tag
- Chart.js - Why the chart cannot rendered in a child component but in the father component can?
- How to use Angular-Charts JS?
- Chart section is empty
- Chart.js is only displaying after f12 is pressed
- Avoiding Empty Curly Braces in MongoDB Aggregation
- Unable to Construct Stacked Vue-ChartJS Line Plot
- How to set 2 y-axis title (and/or label) on chartjs?
- chartjs update() not updating
- react-chartjs error Cannot read property 'Chart' of undefined
- How show data label in the graph on Chart.js?
- combo bar line chart with multiple grouped/stacked datasets
- ReactJS + Material Design: How to get theme colors outside component?
- How do I import chartjs as an es6 module?
- Draw data in graphics generated by Chart JS
- how to make chartjs columns smaller in width for bar chart?
- Does the chartjs supports localization?
- How to load page containing Chart.js using jQuery
- How to plot line chart with multiple datas, but only 2 labels?
- How to check if HTML5 canvas element contains something in given coordinate region using javascript
- chartjs is issue with large amount data
- How to create an array of objects dynamically in javascript for a pie chart
- Displaying line chart for multiple datasets using chart.js
- Type 'string' is not assignable to type 'ChartType'
- ReferenceError: Chart is not defined - chartjs
- Chart Js, Style some ticks on the axis differently
- Filtering labels to show user from Chart.js
- How to dynamically set backgroundColor and borderColor barchart unsing Chart.js
- Chart.JS Bar Chart Throwing Error About Missing )
- Use react-icons for pointStyles in Chart.js graph on Next.js app
- ChartJS - Issues with positioning and viewing various components of horizontal bar