score:2
Accepted answer
this can be done with the plugin core api. the api offers different hooks that may be used for executing custom code. in your case, you can use the afterdraw
hook to draw the desired lines directly on the canvas using canvasrenderingcontext2d
.
plugins: [{
afterdraw: chart => {
var ctx = chart.chart.ctx;
var xaxis = chart.scales['x-axis-0'];
var yaxis = chart.scales['y-axis-0'];
ctx.save();
chart.data.datasets[0].reflines.foreach(r => {
var y = yaxis.getpixelforvalue(r.y);
ctx.strokestyle = r.color;
ctx.beginpath();
ctx.moveto(xaxis.left, y);
ctx.lineto(xaxis.right, y);
ctx.stroke();
});
ctx.restore();
}
}],
above code assumes that the reference lines are defined inside your dataset
through the following definition.
reflines: [
{ y: 45, color: '#0be059' },
{ y: 49, color: '#fc3503' }
]
please take a look at your amended code and see how it works.
new chart('canvas', {
type: 'line',
plugins: [{
afterdraw: chart => {
var ctx = chart.chart.ctx;
var xaxis = chart.scales['x-axis-0'];
var yaxis = chart.scales['y-axis-0'];
ctx.save();
chart.data.datasets[0].reflines.foreach(r => {
var y = yaxis.getpixelforvalue(r.y);
ctx.strokestyle = r.color;
ctx.beginpath();
ctx.moveto(xaxis.left, y);
ctx.lineto(xaxis.right, y);
ctx.stroke();
});
ctx.restore();
}
}],
data: {
datasets: [{
fill: false,
label: 'signal1',
backgroundcolor: 'rgba(75,192,192,0.4)',
pointradius: 5,
pointhoverradius: 7,
bordercolor: 'rgba(75,192,192,1)',
borderwidth: 1,
linetension: 0,
data: [
{ x: '2016-07-14', y: 44 },
{ x: '2016-07-15', y: 52 },
{ x: '2016-07-16', y: 45 },
{ x: '2016-07-17', y: 47 },
{ x: '2016-07-18', y: 35 },
{ x: '2016-07-19', y: 46 },
{ x: '2016-07-20', y: 50 },
{ x: '2016-07-21', y: 44 }
],
reflines: [
{ y: 45, color: '#0be059' },
{ y: 49, color: '#fc3503' }
]
}]
},
options: {
scales: {
yaxes: [{
gridlines: {
color: 'rgba(151, 151, 151, 0.2)',
display: false,
drawborder: false
}
}],
xaxes: [{
offset: true,
bounds: 'data',
type: 'time',
time: {
unit: 'day',
displayformats: {
day: 'd.m.yyyy',
}
},
gridlines: {
color: 'rgba(151, 151, 151, 0.2)',
zerolinecolor: '#979797',
zerolinewidth: 1,
tickmarklength: 5,
drawborder: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.4/chart.bundle.min.js"></script>
<canvas id="canvas" height="80"></canvas>
Source: stackoverflow.com
Related Query
- how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?
- How can I create a time series line graph in chart.js?
- How can I create a bar chart that has multiple yAxes scales?
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- Can Chart.js combines Line Chart and Bar Chart in one canvas
- Chart.js: How can a line series (out of many) change line color and thickness upon mouse hover?
- How to create a chart that uses strings for both the X and Y axes?
- how to reduce list chart to one and use select dropdown to show selection without refresh page?
- How to use chart.js to plot line chart with x axis as time stamp in seconds
- How can I create a stacked bar chart with Charts.JS that displays relative rather than absolute values?
- How to create multiple y-axis time series chart
- How to plot a line chart which has both the start and points outside the canvas area using Chart.js
- chart.js - how to draw and manage line when only one label present in chart js Linechart
- How to take data from an API and create a chart on that using Chart.js and Angular 8?
- how to plot multiple time series in chartjs where each time series has different times
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to display value of only one datapoint in line chart
- How to use PrimeNg Chart of Width and Height?
- 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 create a gradient fill line chart in latest Chart JS version (3.3.2)?
- how to create multiple chart on one component vue
- Chart js: how can I align the legend and the title
- Can we draw a Line Chart with both solid and dotted line in it?
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- Time series line chart is not displayed
- How to show data values in top of bar chart and line chart in chart.js 3
- Time Series Line chart js in react not working
- How can I make line on chart thinner?
- How can I datalabels and Sum display in the same time on a stacked bar
More Query from same tag
- Why is my data on chartjs not starting from the data that it should
- Convert decimal values in array to percentages and pass all values into graph
- drawing bar chart with Chart.js
- Old data flickering after hover on line graph Chart.js
- chart js put meter square / superscript on y axis
- angular-charts 1.0.0 not working like inner directive when element
- Angular Chart.js Multiple Canvas
- How to remove or color line/grid in polar chart area using Chart.js?
- What's the most effective way to implement a radar plot with 50 points at arbitrary locations using chart.js
- Chart.js not updating or receiving data from Flask python
- react-chartjs-2 time scale dates not formatting
- Uncaught Error: [$injector:modulerr] (Error came up when I included chart.js)
- How to disable canvas elements from hijacking mouse wheel when scrolling through a page?
- Need help adding Chart.js chart to a Modal window (I'm Using AngularJS)
- How can I add a label above just the last bar in a Chart.JS bar chart?
- ChartJS multiple annotations (vertical lines)
- Why is ChartJS combining two charts?
- How do I keep chart.js charts in one JS file, and not get errors when the ID from the JS file don't exist on one specific html page?
- ChartJS bar not showing up for simple data points
- Importing chart.js to aurelia causes page errors after bunldling
- "TypeError: chart_js__WEBPACK_IMPORTED_MODULE_9__.default is not a constructor"
- Incorrect chart displayed using chart js
- Angularjs - Can't call the value from form to graph
- How to put 2 labels and distinct tooltips from two bars
- Chart.js scale x values
- Adding responsive text inside chart in Charts Js
- Chart JS in React doesn't fetch data
- How to use Chart.js in Nodejs?
- chart js -data on chart in rotated form
- Responsive ChartJS in Bootstrap Table Cell