localToData with DateTimeAxis

April 15, 2008

I was responding to one of the queries by Matt Horn, who is part of the Flex Documentation team on how to get the exact DataPoint for a series based on the mouse position which has one of its axis as a DateTimeAxis, thought let me share it here.

The usual code is

var d:Array = series1.localToData(p); // p is the point for which you want the data

if you notice d[0] will be x-coord and d[1] will be y-coord and if DateTimeAxis is representing the x-Axis then d[0] will be some huge number, basically it is the number of milliseconds from midnight of Jan 1 1970, so just create a Date object with that to get the actual data point.

To get the same data point which actually gets displayed on the Chart, just use the following code.

var d:Array = series1.localToData(p);
var da:Date = new Date(d[0]);
var str:String = daxis.formatForScreen(da); // daxis is the DataTimeAxis of the Chart.

formatForScreen is a public method which translates the date object to the one which is currently being displayed on the Chart based on the dataUnits that has been specified. Its a simple method, but nevertheless handy.

About these ads

2 Responses to “localToData with DateTimeAxis”

  1. Gilbert Says:

    I am trying to understand this, but I don’t get how I can get the p point.

    I am trying to do a linechart where the user can drag the datapoints and maybe this is useful.

  2. bishoponvsto Says:

    Hi Gilbert,

    This is two years too late, but anyhow:

    // Put the following inside a mouseMove listener

    var p:Point = new Point(chartID.mouseX,chartID.mouseY);

    Brian


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: