D3.js get nearest X & Y coordinates for Area Chart
Asked Answered
B

1

6

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.

enter image description here

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},
    .
    .
    .
]
Baroque answered 18/8, 2014 at 16:33 Comment(6)
bl.ocks.org/mikehadlow/93b471e569e31af07cd3Illhumored
I already have crosshairs in place; essentially what I want is some way to get x, y (nearest/absolute) from original data set when clicked on area outside Line/Area chart.Baroque
Sorry I read it now. Only one problem I don't want 'Y' value for the cross-over point instead want to translate SVG's Y coordinate to Data relative Y.Baroque
What values for x should be used to compute the distance?Godfree
@Wh0RU Sorry, not sure what you mean. The example gets the closest data point to the cursor position, is that not what you're looking for?Illhumored
The example shows only for X-axis where quantization is possible however I also need Y-axis value with absolute index. e.g if user clicks on area between X=1 Jan 2010 and X=3 Jan 2010 and if there is no data then X (for clicked point) should be 1 Jan 2010. On the Y-axis the value should be absolute e.g. if it is clicked between 500 and 502 then take nearest ABS value but not of X say where, X=1 Jan 2010, Y=500 X=3 Jan 2010, Y=502Baroque
B
17

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

Using 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]);
Baroque answered 21/8, 2014 at 9:0 Comment(1)
Hi! Do you know how to implement it for dc.js?Chromyl

© 2022 - 2024 — McMap. All rights reserved.