score:4
You can just add a second draw block for the y coordinate that you get from the tooltip, first you move to the left of the chartArea that you can get the same way you got bottom and top and then you move to the right on the same Y
Chart.defaults.LineWithLine = Chart.defaults.line;
Chart.controllers.LineWithLine = Chart.controllers.line.extend({
draw: function(ease) {
Chart.controllers.line.prototype.draw.call(this, ease);
if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
var activePoint = this.chart.tooltip._active[0],
ctx = this.chart.ctx,
x = activePoint.tooltipPosition().x,
y = activePoint.tooltipPosition().y,
topY = this.chart.legend.bottom,
bottomY = this.chart.chartArea.bottom,
left = this.chart.chartArea.left,
right = this.chart.chartArea.right;
// Set line opts
ctx.save();
ctx.lineWidth = 1;
ctx.setLineDash([3, 3]);
ctx.strokeStyle = '#FF4949';
// draw vertical line
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.stroke();
// Draw horizontal line
ctx.beginPath();
ctx.moveTo(left, y);
ctx.lineTo(right, y);
ctx.stroke();
ctx.restore();
}
}
});
var options = {
type: 'LineWithLine',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
</body>
Edit:
You should use a custom plugin for this since you dont draw everytime you move the cursor and you can enforce this by using a custom plugin:
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
plugins: {
corsair: {
dash: [2, 2],
color: 'red',
width: 3
}
}
},
plugins: [{
id: 'corsair',
afterInit: (chart) => {
chart.corsair = {
x: 0,
y: 0
}
},
afterEvent: (chart, evt) => {
const {
chartArea: {
top,
bottom,
left,
right
}
} = chart;
const {
x,
y
} = evt;
if (x < left || x > right || y < top || y > bottom) {
chart.corsair = {
x,
y,
draw: false
}
chart.draw();
return;
}
chart.corsair = {
x,
y,
draw: true
}
chart.draw();
},
afterDatasetsDraw: (chart, _, opts) => {
const {
ctx,
chartArea: {
top,
bottom,
left,
right
}
} = chart;
const {
x,
y,
draw
} = chart.corsair;
if (!draw) {
return;
}
ctx.lineWidth = opts.width || 0;
ctx.setLineDash(opts.dash || []);
ctx.strokeStyle = opts.color || 'black'
ctx.save();
ctx.beginPath();
ctx.moveTo(x, bottom);
ctx.lineTo(x, top);
ctx.moveTo(left, y);
ctx.lineTo(right, y);
ctx.stroke();
ctx.restore();
}
}]
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
</body>
Edit:
Updated answer for v3
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
plugins: {
corsair: {
dash: [2, 2],
color: 'red',
width: 3
}
}
},
plugins: [{
id: 'corsair',
afterInit: (chart) => {
chart.corsair = {
x: 0,
y: 0
}
},
afterEvent: (chart, evt) => {
const {
chartArea: {
top,
bottom,
left,
right
}
} = chart;
const {
event: {
x,
y
}
} = evt;
if (x < left || x > right || y < top || y > bottom) {
chart.corsair = {
x,
y,
draw: false
}
chart.draw();
return;
}
chart.corsair = {
x,
y,
draw: true
}
chart.draw();
},
afterDatasetsDraw: (chart, _, opts) => {
const {
ctx,
chartArea: {
top,
bottom,
left,
right
}
} = chart;
const {
x,
y,
draw
} = chart.corsair;
if (!draw) {
return;
}
ctx.lineWidth = opts.width || 0;
ctx.setLineDash(opts.dash || []);
ctx.strokeStyle = opts.color || 'black'
ctx.save();
ctx.beginPath();
ctx.moveTo(x, bottom);
ctx.lineTo(x, top);
ctx.moveTo(left, y);
ctx.lineTo(right, y);
ctx.stroke();
ctx.restore();
}
}]
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js"></script>
</body>
score:2
I have done exactly this (but vertical line only) in a previous version of one of my projects. Unfortunately this feature has been removed but the older source code file can still be accessed via my github.
The key is this section of the code:
Chart.defaults.LineWithLine = Chart.defaults.line;
Chart.controllers.LineWithLine = Chart.controllers.line.extend({
draw: function(ease) {
Chart.controllers.line.prototype.draw.call(this, ease);
if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
var activePoint = this.chart.tooltip._active[0],
ctx = this.chart.ctx,
x = activePoint.tooltipPosition().x,
topY = this.chart.legend.bottom,
bottomY = this.chart.chartArea.bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 0.5;
ctx.strokeStyle = '#A6A6A6';
ctx.stroke();
ctx.restore();
}
}
});
Another caveat is that the above code works with Chart.js 2.8 and I am aware that the current version of Chart.js is 3.1. I haven't read the official manual on the update but my personal experience is that this update is not 100% backward-compatible--so not sure if it still works if you need Chart.js 3. (But sure you may try 2.8 first and if it works you can then somehow tweak the code to make it work on 3.1)
Source: stackoverflow.com
Related Query
- Draw a horizontal and vertical line on mouse hover in chart js
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- Can't Draw Horizontal Line on Graph Using ChartJS Annotation Plugin and Primevue Chart
- Draw horizontal line on chart in chart.js on v2
- ChartJS: Draw vertical line at data point on chart on mouseover
- How to draw Horizontal line on Bar Chart Chartjs
- Can we draw a Line Chart with both solid and dotted line in it?
- Chart Js V2 draw Horizontal Bar(Average) Over Vertical Bar
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- Chart.js - draw horizontal line in Bar Chart (type bar)
- Change horizontal line chart to vertical line
- Chartjs draw line chart where line go back and forth (by chronological order)
- Is there any way to remove extra space and Horizontal line from Bar chart of Chart.js?
- Draw line between starting point and Ending point in semi doughnut chart in chart js
- How to draw multiple color bars in a bar chart along a Horizontal Line using chart.js
- chart.js line chart and "correct" spacing between points? (i.e. horizontal position based on percent of width, not fixed)
- Drawing a horizontal line on a stacked bar chart on hover (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
- Adding vertical and horizontal scroll to Chart js Bar graph
- Draw stacked horizontal bar chart with Average line using ChartJS
- chart.js - how to draw and manage line when only one label present in chart js Linechart
- ChartJS: Is this horizontal stacked bar and line graph multiaxis chart even possible?
- Draw vertical and horizonal lines on the radar chart (like x-y axes)
- Plotting Dashed Vertical and Horizontal lines on line graph for single point
- How to add vertical line in bar chart using chartJs and ng2-charts?
- Change Vertical Line and Color bar chart in bar chart.js
- How can I create a horizontal scrolling Chart.js line chart with a locked y axis?
- Remove the vertical line in the chart js line chart
- Moving vertical line when hovering over the chart using chart.js
More Query from same tag
- chartJS line chart not plotting values that are less than minY
- Modifying the X-Axis Labels of a Bar chart in Chart.js 2
- Change List<Object> into List<List<int>> using LINQ
- Edit tooltips in ChartJS
- Chart.js Multiple dataset
- Adding a label to a doughnut chart in Chart.js shows all values in each chart
- Chart.js : How I change the x axes ticks labels alignment in any sizes?
- Chart.js - Adding Legend in a JSP Page
- Add space Between Columns in Bar chart. chartjs
- showing values or percentages with chartjs in react
- Chart JS, backgroundColor not showing
- ChartJS shows chart but no data in ASP net core
- Chart.JS TypeError: this.scale is undefined
- Trying to update background color on chart.js
- getBoundingClientRect gives incorrect value
- Chart.js: chart not displayed from modules despite no errors - JS
- Chartkick column_chart different colors not working
- Chart js - Line chart - How to hide the data label on the line?
- Chart.js - if there is not value show 0
- How to use percentage scale with Chart.js
- ChartJS - format datetime X axis when displaying data from MySql table
- Charts.js Formatting Y Axis with both Currency and Thousands Separator
- How to group smaller Pie Chart slices together to Improve Readability in chartjs
- Chart.js automatic scaling
- How to skip labels of a line in multiline graph in chartjs?
- How can I put my label on the right hand side of my chart in Chartjs
- Remove 0's (zeros) from x-axis of bar chart in Chart.js
- Chart.js modal window
- How do you limit the blur in a linear gradient on a canvas?
- How to loop inside javascript using php variable