score:9
Accepted answer
You can use annotation plugin and in particular Box annotations.
Here below there is an example with Scatter chart:
var randomScalingFactor = function() {
return (Math.random() > 0.5 ? 1.0 : -1.0) * Math.round(Math.random() * 100);
};
var randomColor = function(opacity) {
return 'rgba(' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + Math.round(Math.random() * 255) + ',' + (opacity || '.3') + ')';
};
var data = {
datasets: [{
label: "My First dataset",
data: [{
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}]
}, {
label: "My Second dataset",
data: [{
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}, {
x: randomScalingFactor(),
y: randomScalingFactor(),
}]
}]
};
data.datasets.forEach(function(dataset) {
dataset.borderColor = randomColor(0.4);
dataset.backgroundColor = randomColor(0.1);
dataset.pointBorderColor = randomColor(0.7);
dataset.pointBackgroundColor = randomColor(0.5);
dataset.pointBorderWidth = 1;
});
var ctx = document.getElementById("canvas").getContext("2d");
window.myScatter = new Chart(ctx, {
type: 'scatter',
data: data,
options: {
scales: {
xAxes: [{
position: 'bottom',
gridLines: {
zeroLineColor: "rgba(0,255,0,1)"
},
scaleLabel: {
display: true,
labelString: 'x axis'
},
}],
yAxes: [{
position: 'left',
gridLines: {
zeroLineColor: "rgba(0,255,0,1)"
},
scaleLabel: {
display: true,
labelString: 'y axis'
},
ticks: {
min: -100,
max: 100
}
}]
},
annotation: {
drawTime: "afterDraw",
events: ['dblclick'],
annotations: [{
id: 'low-box',
type: 'box',
xScaleID: 'x-axis-1',
yScaleID: 'y-axis-1',
xMin: -100,
xMax: 100,
yMin: -100,
yMax: -40,
backgroundColor: 'rgba(255, 0, 0, 0.3)',
//borderColor: 'rgb(255, 0, 0)',
borderWidth: 1
},{
id: 'hi-box',
type: 'box',
xScaleID: 'x-axis-1',
yScaleID: 'y-axis-1',
xMin: -100,
xMax: 100,
yMin: 100,
yMax: 40,
backgroundColor: 'rgba(255, 0, 0, 0.3)',
//borderColor: 'rgb(255, 0, 0)',
borderWidth: 1
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chartjs-plugin-annotation/0.5.7/chartjs-plugin-annotation.min.js"></script>
<canvas id="canvas"></canvas>
Source: stackoverflow.com
Related Query
- Chart.js - Color specific parts of the background in a line chart
- Chart.js line chart set background color
- Change the background color of tooltip dynamically using chart color
- Remove background color in chartjs line chart
- How to change background color of labels in line chart from chart.js?
- Background color of the chart area in chartjs not working
- Chart JS Line Graph multitooltipkey background color issue
- How can i give the full Legend a background color in chart js?
- How to make the background color of the chart as gradient using Chart.js
- Is it possible to set the background color of a sector in a radar chart in Chart.js?
- The colors of the bar chart in chart.js is not showing. I tried background color and fill color but none of it worked
- Chart.js - Give specific circles a background color in a bubble chart
- 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 do I use ChartJS with a background color in the space between two line charts?
- Chart area background color chartjs
- Remove the vertical line in the chart js line chart
- Moving vertical line when hovering over the chart using chart.js
- Chart.js line chart is cut off at the top?
- chart.js Line chart with different background colors for each section
- Unable to parse color in line chart (angular-chart.js)
- set background color to save canvas chart
- Chart looks only grey, does not show the color - Chartjs,discordjs
- Chartjs change the specific label color in x axis in callback function
- How to align Chart.JS line chart labels to the center
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- How can I remove extra whitespace from the bottom of a line chart in chart.js?
- how to set chart.js grid color for line chart
- ChartJS Line chart cut off at the top and bottom
- Rotate 90 degrees clockwise the scaleLabel (no ticks or labels) on a Chart.js line chart
More Query from same tag
- Dynamic dataset from MySQL query with Chart.js
- ChartJS with AngularJS
- Using ChartJs In a VueJs component
- trying to get minIndex and maxIndex from x-axis-0
- Reducing Y-axis in chart.js
- Chartjs draw line chart where line go back and forth (by chronological order)
- how to label axis within radar chart with chart.js
- Change color of line along labels Chart.js
- React-chartjs-2 update height dynamically
- Can we have both ajax and jquery scripts in one function?
- Chart JS add max value to legend
- ChartJS - how to display line chart with single element as a line? (not as a dot)
- chartjs-plugin-dragdata isn''t working with scatter charts
- Chart.js tooltip not showing on line chart
- colour sets for Chart.js border colour
- Chartjs: how to show only max and min values on y-axis
- how to add Thousand separator in pie chart tooltips of charts.js
- ChartJs multiaxis chart show different label bottom and top
- Parsing error: Unexpected token, expected ";" line 54 in .map function
- Where is radar.js?
- Creating dropdown options with Chart.js using select tag and multiple canvas
- Change the color of displayed values in pie chart
- Chartjs 2.0 alpha, how to have a static scale for y-axis
- How to add text inside the doughnut chart using Chart.js AngularJS 2.0?
- How to fix page layout with embedded frame
- Putting label array to the chartjs?
- ChartJS - why is there a visual difference when displaying the same dataset alone or with more datasets?
- Chart JS - How to output JSON objects into PIE variant
- ng-charts not updating labels when chart data is updated at same time
- angular-chart zero dimensions inside angular-strap panel