D3.js get the nearest X and Y coordinates for the area diagram

Hi I have an area chart with multiple values ​​of X (year) and Y (price). As I already found, there is an easy way to get X, Y matters for the chart, when the user clicks on one of the points on the line, but clicking on the outer line, i.e. the SVG / Chart-Body area can provide only X, Y, which are the coordinates of the planes and not the data.

enter image description here

Point on the chart:

circles = c.svg().selectAll 'circle.dot'
circles.on 'click', (d) ->
    console.log 'POINT', 'Datum:', d

O / P:

POINT Datum: 
{x: Fri Feb 01 1980 00:00:00 GMT+0000 (GMT Standard Time), y: 666}

Display external graph:

svg.on 'click', () ->
    console.log 'SVG', d3.mouse(@)

O / P:

SVG [605.5, 394.5]

Now can I get the coordinates of the nearest data when I click on SVG? for example SVG [605.5, 394.5] will be (something like the nearest [X, Y] )

{x: Fri Feb 01 1980 00:00:00 GMT+0000 (GMT Standard Time), y: 666}

or in any other way to translate SVG X, Y into X, Y data?

Source data are presented in the form

[
    {x: Fri Jan 01 1980 00:00:00 GMT+0000 (GMT Standard Time), y: 666},
    {x: Fri Feb 01 1980 00:00:00 GMT+0000 (GMT Standard Time), y: 668},
    {x: Fri Mar 01 1980 00:00:00 GMT+0000 (GMT Standard Time), y: 700},
    {x: Fri Apr 01 1980 00:00:00 GMT+0000 (GMT Standard Time), y: 750},
    .
    .
    .
    {x: Fri Dec 01 2010 00:00:00 GMT+0000 (GMT Standard Time), y: 2000},
    .
    .
    .
]
+4
1

http://bl.ocks.org/mikehadlow/93b471e569e31af07cd3

d3.bisector,

var mouse = d3.mouse(this);
var mouseDate = xScale.invert(mouse[0]);
var bisectDate = d3.bisector(function(d) { return d.x; }).left;
var i = bisectDate(data, mouseDate); // returns the index to the current data item

var d0 = data[i - 1]
var d1 = data[i];
// work out which date value is closest to the mouse
var d = mouseDate - d0[0] > d1[0] - mouseDate ? d1 : d0;

var x = xScale(d[0]);
var y = yScale(d[1]);
+11

All Articles