score:73
Accepted answer
Solution for ChartJS 2.6.0
ꜱᴄʀɪᴘᴛ (ᴇxᴛᴇɴᴅɪɴɢ ʟɪɴᴇ ᴄʜᴀʀᴛ)
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 = 2;
ctx.strokeStyle = '#07C';
ctx.stroke();
ctx.restore();
}
}
});
You would also need to set intersect: false
for tooltips.
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
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 = 2;
ctx.strokeStyle = '#07C';
ctx.stroke();
ctx.restore();
}
}
});
var chart = new Chart(ctx, {
type: 'LineWithLine',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul'],
datasets: [{
label: 'Statistics',
data: [3, 1, 2, 5, 4, 7, 6],
backgroundColor: 'rgba(0, 119, 204, 0.8)',
borderColor: 'rgba(0, 119, 204, 0.3)',
fill: false
}]
},
options: {
responsive: false,
tooltips: {
intersect: false
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx" height="200"></canvas>
score:6
Try this:
var ctx = this.$refs.canvas.getContext("2d");
// new Chart(ctx, config);
var originalLineDraw = Chart.controllers.line.prototype.draw;
Chart.helpers.extend(Chart.controllers.line.prototype, {
draw: function() {
originalLineDraw.apply(this, arguments);
var chart = this.chart;
var ctx = chart.chart.ctx;
if (this.chart.tooltip._active && this.chart.tooltip._active.length) {
var activePoint = this.chart.tooltip._active[0];
var ctx = this.chart.ctx;
var x = activePoint.tooltipPosition().x;
var topY = this.chart.scales['y-axis-0'].top;
var bottomY = this.chart.scales['y-axis-0'].bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 0.5;
ctx.strokeStyle = '#eeeeee';
ctx.stroke();
ctx.restore();
}
}});
This will definitely help you.
Source: stackoverflow.com
Related Query
- Moving vertical line when hovering over the chart using chart.js
- Moving vertical line when hovering over the chart using chart.js in v2.9.4
- ReactJS - Moving vertical line when hovering over Line chart using chart.js v3.7.1
- ChartJS: Highlight dataset in a stacked bar chart when hovering over the legend?
- Charts with previous data appear when hovering the cursor over the chart in Chart.js
- Remove the vertical line in the chart js line chart
- ChartJs line chart repaint glitch while hovering over
- Why can I not see a data value when hovering over a point on the radar chart?
- In Stacked horizontal bar chart how to remove the vertical line in Chart.js?
- Chartjs: Need help on drawing a vertical line when hovering cursor
- Chart.js vertical line when hovering and shadow on line
- canvas fill text vanishes when hovering over chartjs pie chart
- display vertical axis label in line chart using chart.js
- Highlight chart element when hovering over its corresponding legend Item
- Chart.js - Fill Text only appearing when hovering over one part of the doughnut
- vertical grid line not taking full hight of the canvas in chart js 3.3.2
- Hovering over line chart shows old chart data issue in chart.js
- How to plot a line chart which has both the start and points outside the canvas area using Chart.js
- data in charts of charts.js is changing when I click in the line chart
- 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
- When using chart.js to draw a line chart, if the peak is between two x values instead of "at a x value"
- Chart.js returns a console error and does not display the chart when using variables as data input
- Chart.js doughnut chart isn't showing tooltip when you hover over the left/right of the doughnut
- How to control the over riding of Y-axis values in Chart.js when more entries inserted using PHP?
- Chart.js - Line chart does not render all points when using Point[] format
- Trigger bar/pie graph highlight when hovering over an external element [chart.js / chart js] [AngularJS] [angular-chart.js]
- Show multiple line graphs on the same chart using chart JS
- How to add vertical line in bar chart using chartJs and ng2-charts?
- How do I increase the space between the Legend and the Chart when using angular-chart.js?
- Understanding the data flow when fetching chart data using AJAX
More Query from same tag
- ChartJS (Radar) - Set base tick position for "0" value
- Chart.js: changing tooltip template
- Add beforeDraw callback on chart JS v3
- Why can't I create monthly sales report with Chart.js in Codeigniter
- Why is chartjs treating these arrays differently?
- ChartJS executes a function multiple times
- Datetime picker in Chart.js
- Chart JS Tooltip - place it in a fixed location outside the canvas
- Chart.js - Make x-axis grouped by day
- Line chart is drawing before first point in Chart.js
- tc-angular-chartjs problems while assigning value to chart-data
- Chart JS: Donut/Doughnut Chart: Tooltip to be shown always for all the data. All tooltip is not shown when multiple data are with 0 data
- Chart.js, how to reduce space between labels and bars (horizontal bar chart)
- Cannot read property 'labels' of undefined
- moment.js is not loading before chart.js in Firefox extension
- Chart doesn't render in angular 2 app
- How implement Chart.js in Angular 2?
- How to add a vertical line on the end of the chart.js scatter graph
- How to make bars change color based on value in chartsjs
- Alter values of xAxis in chartjs
- ChartJS - tooltip of one element is covering point that user should be able to select
- Reactive charts with Meteor : d3charts , Hightcharts , ChartJS , other?
- Angular 8 and ng2 charts - Updating labels and data
- How can I get a point to be drown over the line?
- Uncaught TypeError: Cannot read property 'draw' of undefined : Angular chart js
- Creating a heart rate monitor
- Get last 6 month name in Array for Chartjs
- Charts.js scales yaxes ticks min max doesnt work
- Add Space Between Bars in Angularjs Chartjs
- Chart.js V2: Add prefix or suffix to tooltip label