Hi I have an area chart with several X (year) & Y (price) values. As I've found there is easy way to get X, Y coodinates value for chart when user clicks on one of the point on line however clicking outside line i.e. SVG/Chart-Body area can provide only X, Y which are coordinates of planes rather than data.
Point on 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}
Point outside chart:
svg.on 'click', () ->
console.log 'SVG', d3.mouse(@)
O/P:
SVG [605.5, 394.5]
Now is there any way I could get nearest data coordinates when clicked on SVG? e.g SVG [605.5, 394.5] would be (something like nearest [X, Y] using)
{x: Fri Feb 01 1980 00:00:00 GMT+0000 (GMT Standard Time), y: 666}
or some other way to translate SVG X, Y into Data X, Y?
Original data is in the form of,
[
{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},
.
.
.
]
x
should be used to compute the distance? – Godfree