score:3
Accepted answer
It is possible to extend Chart.js functionality (see http://www.chartjs.org/docs/latest/developers/charts.html) drawing a vertical line and a second tooltip when hovering on a point:
Chart.defaults.MyLine = Chart.defaults.line;
Chart.controllers.MyLine = 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.scales['y-axis-0'].top,
bottomY = this.chart.scales['y-axis-0'].bottom;
// draw line
ctx.save();
ctx.beginPath();
ctx.moveTo(x, topY);
ctx.lineTo(x, bottomY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#000000';
ctx.stroke();
var value = this.chart.data.datasets[activePoint._datasetIndex].data[activePoint._index];
ctx.font = this.chart.options.tooltips.titleFontStyle + " " + this.chart.options.tooltips.titleFontSize + "px Arial";
var xPad = this.chart.options.tooltips.xPadding;
var yPad = this.chart.options.tooltips.yPadding;
var width = ctx.measureText(value).width + xPad * 2;
var height = this.chart.options.tooltips.titleFontSize + yPad * 2;
var radius = this.chart.options.tooltips.cornerRadius;
console.log(activePoint, topY, xPad, yPad, ctx.font);
ctx.fillStyle = this.chart.options.tooltips.backgroundColor;
ctx.lineWidth = this.chart.options.tooltips.borderWidth;
var y = topY;
x = x - width / 2;
// draw rect upper tooltip
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
ctx.fill();
// draw text
ctx.textBaseline = 'top';
ctx.fillStyle = this.chart.options.tooltips.titleFontColor;
ctx.fillText(value, x + xPad, topY + yPad);
}
}
});
Chart.Tooltip.positioners.custom = function(elements, eventPosition) {
var tooltip = this;
return {
x: eventPosition.x,
y: elements[0]._chart.height
};
}
var ctx = document.getElementById('chart').getContext('2d');
var chart = new Chart(ctx, {
type: 'MyLine',
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,
legend: {
display: false
},
animation: false,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
},
tooltips: {
// bottom tooltip
position: 'custom',
caretSize: 0,
callbacks: {
title: function(tooltipItem, data) {
return data['labels'][tooltipItem[0]['index']];
},
label: function(tooltipItem, data) {
return "";
},
afterLabel: function(tooltipItem, data) {
var dataset = data['datasets'][0];
return "";
}
},
backgroundColor: '#FF0000',
titleFontSize: 12,
titleFontColor: '#FFFFFF',
bodyFontColor: '#000',
titleMarginBottom: 2,
displayColors: false
}
}
});
#chart {
width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.1/Chart.min.js"></script>
<canvas id="chart"></canvas>
P.S.: check also for others chart libraries for the same functionality.
Source: stackoverflow.com
Related Query
- ChartJs - Double tooltip Top and Bottom on hover
- chartjs - top and bottom padding of a chart area
- ChartJS Line chart cut off at the top and bottom
- Change style of hover and tooltip in chartjs or ng2-charts
- ChartJs multiaxis chart show different label bottom and top
- Chartjs v2 - format tooltip for multiple data sets (bar and line)
- Change point size and color on hover in chartjs
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- Chartjs adding icon to tooltip and label
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- Points cut at half at the edges of top and bottom, at chartjs
- ChartJS - Moving vertical line is display on top of tooltip
- Custom text in tooltip and legend: ChartJs
- How to toggle between Custom tooltip and normal tooltip in chartjs and angular
- ChartJS - Horizontal Stacked Bar Chart Tooltip being cut off by Bottom of Canvas
- how to write labels along with data on top and bottom of each stack in bar chart
- ChartJS customize left and bottom axe border
- JavaScript ignore commas inside ChartJS data field pulled from csv that data has commas and is enclosed by double quotes
- Why this Tooltip callback for ChartJS works and this doesn't
- ChartJS hover tooltip colors not showing their correct color
- How to disable chartjs tooltip on mibile devices and small screens?
- How Can customize chartjs doughnut chart border and tooltip
- Is there any way to show tooltip by default (without hover pie chart) on chartjs
- How do you create rounded corners on the top and bottom of a bar chart and the between 2 part still draw like image?
- chart js double tooltip on hover
- Vue + chartjs : Nearest point on hover tooltip not working
- Chartjs Tooltip Line Breaks
- How to remove square label from tooltip and make its information in one line?
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- How to disable a tooltip for a specific dataset in ChartJS
More Query from same tag
- Chart.js - Add gradient to line chart background
- Showing only entire units on ChartJS
- Chart.js Line Chart x-Axes label rounding
- Change background transparency of Chart.js chart
- how can i use chartjs-plugin-annotation?
- How to increase space between label and chart area in chart.js
- How do I force Chart.js axes min and max with react-chartjs-2?
- Chart js padding isn't working for data labels
- How to add datalabels to chartJS on Primefaces
- Chart.js Scaling issue when initially loading the page
- chart.js API returns bad x and y points
- Creating a horizontal bar chart extension on Chart.js
- Charts.js - onComplete javascript function coming from JSON
- Mobile page loading in "zoomed" state
- chart.js number of labels equal to number of datapoints
- How to show data values in top of bar chart and line chart in chart.js 3
- Iterating over an IEnumerable in Javascript for Chartjs
- How to move the x-axis values and line points to the middle of two x-axis lines using chartjs?
- Angular-Chart-JS - Line chart with different fill colors according to points' range
- I want to put bar of different color by the values in chart.js
- Changing the Y axis unit in Chartjs
- Chartjs doughnut with multiple dataset issue in legend creation
- Adding X axis title causes Uncaught RangeError: minimumFractionDigits value is out of range in Chart.js
- Dynamic information in chart.js radar chart
- Chart.js onAnimationComplete doesn't work
- ChartJS hide labels on small screen sizes
- How I create a pie chart based on COUNT(php + mysql)?
- The bar that contains a low value is almost invisible on the chartjs
- Why is this "not defined"
- cannot view graph using toArray()