score:10
Accepted answer
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>
score: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: stackoverflow.com
Related Query
- chart.js 2.0 current mouse coordinates tooltip
- How to customize tooltip on mouse hover of a polar area chart of Angular Charts
- How to get access to Chart instance related to current tooltip callback in Chart.js
- ChartJs (ver: 2.8.0): Custom tooltip does not hide if clicked (or mouse pointer is moved) outside the chart canvas area
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- Chart JS custom tooltip option?
- chart.js scatter chart - displaying label specific to point in tooltip
- show label in tooltip but not in x axis for chartjs line chart
- Chart.js doughnut chart tooltip size?
- ChartJS add tooltip to a grouped bar chart
- Chart JS Show HTML in Tooltip
- chart js tooltip how to control the data that show
- Chart js v2 tooltip callback line breaks
- How to disable Chart JS tooltip when there is no value?
- Chart.js tooltip hover customization for mixed chart
- Add all data in the tooltip of Chart JS
- Draw a horizontal and vertical line on mouse hover in chart js
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- Chart Js Show the old data on mouse hover
- Get X, Y onClick chart coordinates in ChartJS
- Why Chart.js's tooltip appears in wrong position when mouse hover?
- Chart js shows old data on mouse hover
- Charts.js tooltip overlapping text on chart
- ChartJS: Show all labels of a mixed chart in the tooltip
- Mouse over on line chart data active other data-set in Chart.js
- Chart.js line chart tooltip shows wrong label when line doesn't start at first label
- Position tooltip based on mouse position
- Chart JS custom tooltip not showing
- Chartjs - Insert additional data into chart tooltip
- Add a text as tooltip for each point on a line chart
More Query from same tag
- Charts.js how to have 2 different size y-axis
- How to update a chart using VueJS and ChartJS
- How to display grouped bar chart using chart.js?
- Chart.js - x-axis and tooltip label shows "undefined" in Chrome when browser language is NOT English
- Add MQTT topics and data to chartjs
- How to create multiple x-axis datasets labels by using chart.js
- Adding an additional datasets to chartJS
- Chart.js creating a line graph using dates
- How to update data from ajax call (chart.js)
- how to change display and styles of the labels in react-chartjs-2
- Error ReferenceError: CanvasGradient is not defined
- Create a arc like doughnut chart with Chart js plugins
- Chart.js x-axis label duplicating on hover
- Data is not display in correct order after update
- Chart.js bar chart : Grid color and hide label
- How to loop tooptip additional data in chart.js
- charts.js update global variables
- How to make multiple stacked charts to share the same yAxis?
- Add data to dynamically created Chart.js dataset
- chart.js v2 setting the y scale options seem to getting ignored
- Chart JS + Laravel - How to add 0 to array
- I am using chart.js I sent my time values throw my api as timestamp know i need to show them in the chart as date format
- How to have more controls over the borderColor per arc segment for chart.js pie charts
- ChartJs. How to stretch up line graph to the edges?
- Array of objects condensed into array of unique objects with nested array
- How to fill my chart (chart.js) with my own data from API
- Update ChartJS with MySQL Data
- OffsetWidth / offsetHeight is zero when template loaded by ngRoute
- chart js - Apply different color for each x-axes label
- Cannot read property 'labels' of undefined