It is important to note when converting Pixels to Data Coordinates and vice versa that pixels are measured from the top-left inside corner of the chart. So, let’s see correspondence of the pixel coordinate and data-values in the table below:
let date = NSDate(year: 2011, month: 5, day: 1)
let coordinate = xAxis.getCoordinate(date)
// Convert back:
let dataValue = xAxis.getDataValue(coordinate)
var date = new DateTime(2011, 5, 1);
var coordinate = xAxis.GetCoordinate(date.FromComparable());
// Convert back:
var dataValue = xAxis.GetDataValue(coordinate).ToComparable();
let date = NSDate(year: 2011, month: 10, day: 5)
let coordinate = xAxis.getCoordinate(date)
// Convert back:
let dataValue = xAxis.getDataValue(coordinate)
var date = new DateTime(2011, 10, 5);
var coordinate = xAxis.GetCoordinate(date.FromComparable());
// Convert back:
var dataValue = xAxis.GetDataValue(coordinate).ToComparable();
Getting a CoordinateCalculator instance
There is a ISCIAxisCore.currentCoordinateCalculator property, which is readonly, and which provides a coordinate calculator instance which is valid for the current render pass.
As you might guess, converting Pixels to Data-Coordinates and vise versa slightly differs for different axis types due to difference in underlying data-types. In particular the following ones:
let calculator = xAxis.currentCoordinateCalculator!
let coordinate = calculator.getCoordinate(1.2)
// Convert back:
let dataValue = calculator.getDataValue(coordinate)
var calculator = xAxis.CurrentCoordinateCalculator;
var coordinate = calculator.GetCoordinate(1.2);
// Convert back:
var dataValue = calculator.GetDataValue(coordinate);
NOTE: Since the ISCICoordinateCalculator works with double representation of Date in timeIntervalSince1970, you will need to do all the needed conversions on your own. See the code below:
let calculator = xAxis.currentCoordinateCalculator!
let date = NSDate(year: 2011, month: 5, day: 1)!
let coordinate = calculator.getCoordinate(date.timeIntervalSince1970)
// Convert back:
let intervalSince1970 = calculator.getDataValue(coordinate)
let dateValue = Date(timeIntervalSince1970: intervalSince1970)
var calculator = xAxis.CurrentCoordinateCalculator;
var date = new DateTime(2011, 5, 1);
var coordinate = calculator.GetCoordinate(date.ToUnixTime());
// Convert back:
var intervalSince1970 = calculator.GetDataValue(coordinate);
var dateValue = NSDate.FromTimeIntervalSince1970(intervalSince1970).FromDate();
NOTE: Conversion from Date to Index and vice versa can be performed using the ISCICategoryLabelProvider. See the code below:
id<ISCICoordinateCalculator> calculator = xAxis.currentCoordinateCalculator;
// get ISCICategoryLabelProvider to convert Date value to Index
id<ISCICategoryLabelProvider> labelProvider = (id<ISCICategoryLabelProvider>)xAxis.labelProvider;
NSDate *date = [NSDate dateWithYear:2011 month:10 day:5];
NSInteger index = [labelProvider transformDataToIndex:date];
float coordinate = [calculator getCoordinateFrom:231];
// Convert back:
index = (NSInteger)[calculator getDataValueFrom:coordinate];
// use the ISCICategoryLabelProvider instance to convert index to Date value
date = [labelProvider transformIndexToData:index];
let calculator = xAxis.currentCoordinateCalculator!
// get ISCICategoryLabelProvider to convert Date value to Index
let labelProvider = xAxis.labelProvider as! ISCICategoryLabelProvider
var date = NSDate(year: 2011, month: 10, day: 5) as Date?
var index = labelProvider.transformDataToIndex(date)
let coordinate = calculator.getCoordinate(Double(index))
// Convert back:
index = Int(calculator.getDataValue(coordinate))
// use the ISCICategoryLabelProvider instance to convert index to Date value
date = labelProvider.transformIndexToData(index)
var calculator = xAxis.CurrentCoordinateCalculator;
// get ISCICategoryLabelProvider to convert Date value to Index
var labelProvider = (IISCICategoryLabelProvider)xAxis.LabelProvider;
var date = new DateTime(2011, 10, 5);
var index = labelProvider.TransformDataToIndex(date.ToDate());
var coordinate = calculator.GetCoordinate(index);
// Convert back:
index = (int)calculator.GetDataValue(coordinate);
// use the ISCICategoryLabelProvider instance to convert index to Date value
date = labelProvider.TransformIndexToData(index).FromDate();
NOTE: The exact data-values and coordinates might differ depending on your visibleRange, viewport etc…
Transforming Pixels to the Inner Viewport
If you register a UIGestureRecognizer to recognize gestures or touch events on a SciChartSurface, the touch coordinates you will receive will be relative to the SCIChartSurface itself. Before the Coordinate Transformation API can be used, it’s important to ensure that such coordinates are transformed relative to the viewport (the central area).
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
// the touch point relative to the SCIChartSurface
CGPoint location = [recognizer locationInView:recognizer.view.superview];
// translate the touch point relative to RenderableSeriesArea (or ModifierSurface)
CGPoint hitTestPoint = [self.surface translatePoint:location hitTestable:self.surface.renderableSeriesArea];
// the translated point can be used for looking for data values, hit-test or something else.
…
}
@objc fileprivate func handleSingleTap(_ recognizer: UITapGestureRecognizer) {
// the touch point relative to the SCIChartSurface
let location = recognizer.location(in: recognizer.view!.superview)
// translate the touch point relative to RenderableSeriesArea (or ModifierSurface)
let hitTestPoint = surface.translate(location, hitTestable: surface.renderableSeriesArea)
// the translated point can be used for looking for data values, hit-test or something else.
…
}
private void HandleSingleTap(UITapGestureRecognizer recognizer)
{
// the touch point relative to the SCIChartSurface
var location = recognizer.LocationInView(recognizer.View.Superview);
// translate the touch point relative to RenderableSeriesArea (or ModifierSurface)
var hitTestPoint = Surface.TranslatePoint(location, Surface.RenderableSeriesArea);
// the translated point can be used for looking for data values, hit-test or something else.
…
}