score:0
I was facing the same issue when using chartjs 3.0. I used the following code to get the horizontal line displayed
var horizonalLinePlugin = {
id : 'horizontalLine',
afterDraw: function (chartInstance) {
// var yScale = chartInstance.scales["y-axis-0"];
var yScale = chartInstance.scales['y'];
// var canvas = chartInstance.chart;
var canvas = chartInstance.canvas;
var ctx = chartInstance.ctx;
var index;
var line;
var style;
var yValue;
if (chartInstance.config._config.options.horizontalLine) {
for (index = 0; index < chartInstance.config._config.options.horizontalLine.length; index++) {
line = chartInstance.config._config.options.horizontalLine[index];
if (!line.style) {
style = "rgba(169,169,169, .6)";
} else {
style = line.style;
}
if (line.y) {
yValue = yScale.getPixelForValue(line.y);
} else {
yValue = 0;
}
ctx.lineWidth = 3;
if (yValue) {
ctx.beginPath();
ctx.moveTo(20, yValue);
// ctx.moveTo(0, yValue);
ctx.lineTo(canvas.width, yValue);
ctx.strokeStyle = style;
ctx.stroke();
}
if (line.text) {
ctx.fillStyle = style;
ctx.fillText(line.text, 0, yValue + ctx.lineWidth);
}
}
return;
};
}
};
Chart.register(horizonalLinePlugin);
score:3
chartInstance.chart is undefined. Not sure is that a migration issue to chart.js ver 3.0...
Try the following:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta.6/chart.js"></script>
<style>
.container{
position: relative;
width: 50vw;
}
</style>
</head>
<body>
<canvas id="myChart" class="container"></canvas>
<script>
var canvas = document.getElementById("myChart");
var ctx = canvas.getContext("2d");
var horizonalLinePlugin = {
id: 'horizontalLine',
afterDraw: function(chartInstance) {
var yScale = chartInstance.scales["y"];
// chartInstance.chart is undefined
//var canvas = chartInstance.chart;
// console.log(canvas);
// var ctx = canvas.ctx;
var index;
var line;
var style;
if (chartInstance.options.horizontalLine) {
for (index = 0; index < chartInstance.options.horizontalLine.length; index++) {
line = chartInstance.options.horizontalLine[index];
if (!line.style) {
style = "rgba(169,169,169, .6)";
} else {
style = line.style;
}
if (line.y) {
yValue = yScale.getPixelForValue(line.y);
} else {
yValue = 0;
}
ctx.lineWidth = 3;
if (yValue) {
ctx.beginPath();
ctx.moveTo(0, yValue);
ctx.lineTo(canvas.width, yValue);
ctx.strokeStyle = style;
ctx.stroke();
}
if (line.text) {
ctx.fillStyle = style;
ctx.fillText(line.text, 0, yValue + ctx.lineWidth);
}
}
return;
}
}
};
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "MyDataset01",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
data: [65, 59, 80, 81, 56, 55, 40],
},
]
};
//When uncommenting below line, graph is created without horizontal lines
Chart.register(horizonalLinePlugin);
var myChart = new Chart(ctx, {
type: 'line',
data: data,
options: {
"horizontalLine": [{
"y": 75.8,
"style": "#ff0000",
"text": "upper-limit"
}, {
"y": 62.3,
"style": "#00ff00",
"text": "avg"
}, {
"y": 48.8,
"style": "#0000ff",
"text": "lower-limit"
}],
}
});
</script>
here is the code above in Jsfiddle - working!
Source: stackoverflow.com
Related Query
- How to draw horizontal Lines using chart.js 3.x ? Cannot get it working
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- How to Draw Gantt chart using chart js or other libraries
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to draw Horizontal line on Bar Chart Chartjs
- How to draw outer labels for polar chart using ng2-charts and chart.js at fixed positions outside the rings?
- ChartJS / Chartjs-plugin-annotation How to draw multiple vertical lines using an array?
- How to draw multiple color bar in a bar chart using chart.js
- How to get Data from API to display chart using chartjs in Vuejs
- How to get the data attribute of the canvas chart created using chartjs
- Can't Draw Horizontal Line on Graph Using ChartJS Annotation Plugin and Primevue Chart
- Can't draw pie chart using chartjs - TypeError: Cannot create property '_meta' on number '16'
- How to Draw a line on chart without a plot point using chart.js
- How to draw a chart like below using chart js
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- how to draw Line chart using chart.js and datalabels should be shown
- Draw stacked horizontal bar chart with Average line using ChartJS
- How Can I Get An Instance of a ChartJS Bar Chart Using Angular
- How to run Chart.js samples using source code
- how to modify horizontal bar chart using chart js
- How to use canvasjs to draw column chart using text data
- How do I draw horizontal bars with a label using either ChartJS or D3?
- How to pass Arrays from backing bean to JavaScript to draw a chart using Chart.js in an Ajax call
- How to add text inside the doughnut chart using Chart.js?
- How to clear a chart from a canvas so that hover events cannot be triggered?
- 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?
- How can I make two of my lines in Chart JS thicker
- How to add an on click event to my Line chart using Chart.js
- Draw horizontal lines in Chart.js 2.0
More Query from same tag
- Change the Y-axis values from real numbers to integers in Chart.js
- Missing Tooltip for some data points using chartjs
- Show labels on each sector to polar chart using angular js chart
- Chartjs not reading data from vuex mapped state
- Adding multiple dataset and colors not being assigned in ng2-charts
- Chart js Datalabels styling
- How to plot Json data in Chart js
- Charts.js in Angular 7 - working with imported time for timeseries
- Chart.js animation triggered only on hover?
- Charts not rendering properly in Angular
- ChartJS and require: Chart is undefined
- Changing data when click button in chart.js/vue.js
- Angular ng2-charts Radarchart compile error for startAngle
- Bar Chart cannot read data and label in laravel
- Get last 6 month name in Array for Chartjs
- How to align legend in angular-chart.js
- How to plot dates in Chart.js
- Multidimensional Charts.js (array of arrays)
- Chart.JS value on top disappear when tooltip show up
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
- chart.js onClick event function, access angular component
- Javascript How to give color using conditional statement based on json array state value
- Chart.js How to align x-ticks?
- How to start the line graph from the left Y axis in a line/bar mixed chart (Chart.js)?
- using array values in chart.js data and label field
- How do I use NuGet to install ChartJS?
- Chart.JS Chart Not Being Generated
- Can't draw pie chart using chartjs - TypeError: Cannot create property '_meta' on number '16'
- Dynamicly update scatter/line chart in Chart.JS with multiple x/y grids
- Chart.js stacked bar ordering depending on value