I am making a graph and I need to make it put a horizontal line at X point of it
this is the "script" of my graph;
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, ],
datasets: [{
label: 'Reparaciones x dia',
data: ['0', '4', '1', '3', '1', '0', '0', '1', '11', '4', '8', '7', '5', '1', '2', '5', '3', '8', '1', ],
backgroundColor: "rgba(0,255,51,0.5)"
}, {
label: 'Totales',
data: [0, 4, 5, 8, 9, 9, 9, 10, 21, 25, 33, 40, 45, 50, 51, 53, 58, 61, 69, 73],
backgroundColor: "rgba(0,153,255,0.5)"
}]
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<canvas id="myChart"></canvas>
I need a line for example at number 15 of the vertical axis...
You can create your own plugin for Chart.js, which will draw the line you want on the chart. For example, if you want the plugin to draw a horizontal line on the value of
y
, your code should follow the following algorithm:y
where you want to display the horizontal line (this would be done insideoptions
when you create the Chart)moveTo
)lineTo
)style
)stroke
)You could then extend that plugin to allow more than one horizontal line, label the line, customize colors, opacity, etc... But I'm going to limit myself to the most basic version: a single line of a fixed color.
Here you can see the code of this plugin working:
And as an extension that I put above, here I leave a somewhat more complex plugin that allows you to draw horizontal or vertical lines, and also allows you to choose a color or label (I have done it in a short time and it can be improved by reducing the code, then I update it when I have a bit more time):
You have to extend the library to paint the line.