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 will be x-coord and d will be y-coord and if DateTimeAxis is representing the x-Axis then d 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);
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.