11👍
✅
I finally just gathered the information and did the math. Here is just a snippet to show you how if anyone else ever has this problem.
$(document).ready(function() {
var ctx = $("#graph_1");
var dataArray = [ {x:1,y:1},{x:2,y:3},{x:3,y:5},{x:4,y:8},{x:5,y:7},{x:6,y:4},{x:7,y:2},{x:8,y:1} ];
var myChart = new Chart(ctx, {
type: 'scatter',
data: {
datasets: [{
label: "test",
fill: false,
data: dataArray
}]
},
options: {
title: {
display: true,
text: 'Test Graph'
},
animation: {
duration: 0,
}, // general animation time
hover: {
animationDuration: 0,
}, // duration of animations when hovering an item
responsiveAnimationDuration: 0, // animation duration after a resize
scales: {
xAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'x axis label'
}
}],
yAxes: [{
display: true,
scaleLabel: {
display: true,
labelString: 'y axis label'
}
}]
},
tooltips: {
mode: 'index',
intersect: false,
callbacks: {
// Use the footer callback to display the sum of the items showing in the tooltip
footer: function(tooltipItems, data) {
//return 'x:' + this._eventPosition.x + ' y:' + this._eventPosition.y;
},
},
footerFontStyle: 'normal'
},
}
});
ctx.mousemove(function(evt) {
//console.log(evt.offsetX + "," + evt.offsetY);
var ytop = myChart.chartArea.top;
var ybottom = myChart.chartArea.bottom;
var ymin = myChart.scales['y-axis-1'].min;
var ymax = myChart.scales['y-axis-1'].max;
var newy = '';
var showstuff = 0;
if (evt.offsetY <= ybottom && evt.offsetY >= ytop) {
newy = Math.abs((evt.offsetY - ytop) / (ybottom - ytop));
newy = (newy - 1) * -1;
newy = newy * (Math.abs(ymax - ymin)) + ymin;
showstuff = 1;
}
var xtop = myChart.chartArea.left;
var xbottom = myChart.chartArea.right;
var xmin = myChart.scales['x-axis-1'].min;
var xmax = myChart.scales['x-axis-1'].max;
var newx = '';
if (evt.offsetX <= xbottom && evt.offsetX >= xtop && showstuff == 1) {
newx = Math.abs((evt.offsetX - xtop) / (xbottom - xtop));
newx = newx * (Math.abs(xmax - xmin)) + xmin;
}
if (newy != '' && newx != '') {
//console.log(newx + ',' + newy);
$("#graph_coords").html('Mouse Coordinates: ' + newx.toFixed(2) + ',' + newy.toFixed(2));
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<SCRIPT src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></SCRIPT>
<DIV id="graph_coords"></DIV>
<DIV class="chart-container" style="position: relative; height:40vh; width:80vw;">
<CANVAS id="graph_1" style="background-color: #CBCBCB;"></CANVAS>
</DIV>
1👍
Halfhoot’s answer can be simplified a bit using the built in functions in the scales.
ctx.mousemove(function(evt) {
let x = chart.scales['x-axis-1'].getValueForPixel(evt.offsetX);
let y = chart.scales['y-axis-1'].getValueForPixel(evt.offsetY);
console.log(x, y);
});
There is a description of the Scales interface on the New Scales page in the API.
Source:stackexchange.com