Skip to main content

Selection and Hit Testing

  • 5 minutes to read

The Selection allows end-users to select single or multiple points or series.

Selection example

How to: Handle chart selection changes

The code snippet below shows how to handle chart selection changes.

-(void) viewDidLoad { 
// ...
    self.chart.delegate = [ChartDelegate delegateWithOwner: self];
}
// ...

@interface ChartDelegate : NSObject <DXChartDelegate>
    +(id) delegateWithOwner: (ViewController *)owner;
@end

@implementation ChartDelegate {
    ViewController *owner;
}

+(id) delegateWithOwner: (ViewController *)owner {
    ChartDelegate *delegate = [[ChartDelegate alloc] init];
    delegate->owner = owner;
    return delegate;
}

- (void)chart:(DXChartBase*) chart seriesDidSelect:(NSInteger) seriesIndex {
    [self showAlertWithMessage: [NSString stringWithFormat: @"Series with index %ld was selected.", seriesIndex]];
}

- (void)chart:(DXChartBase*) chart seriesDidDeselect:(NSInteger) seriesIndex {
    [self showAlertWithMessage: [NSString stringWithFormat: @"Series with index %ld was deselected.", seriesIndex]];
}

-(void)showAlertWithMessage: (NSString *)message {
    UIAlertAction *okAction = [UIAlertAction actionWithTitle: @"OK"
                                                       style: UIAlertActionStyleDefault
                                                     handler: nil];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle: @"Series selection"
                                                                             message: message
                                                                      preferredStyle: UIAlertControllerStyleAlert];
    [alertController addAction:okAction];

    [owner presentViewController: alertController
                        animated: YES
                      completion: nil];
}
@end

The following symbols allow you to configure the selection:

Symbols Description
delegate Gets or sets the chart delegate.
DXChartDelegate A protocol that a class should adopt to handle selection changes.

How to: Select an individual point or whole series

The Charts’ selection mechanism allows you to specify a type of chart elements which should be selected - a series or a point.

The following code demonstrates how to do this:

self.chart.selectionKind = DXChartSelectionKindSeries;

The following classes and methods allow you to specify what chart element should be selected:

Symbols Description
DXChartBase.selectionKind Gets or sets the value indicating a type of chart elements which should be selected.
DXChartSelectionKind Lists all values indicating what chart element should be selected.

How to: Select several points (series) simultaneously

The Chart selection mechanism allows you to select one or more chart elements simultaneously.

The following code demonstrates how to configure the selection mechanism to accomplish this:

self.chart.selectionMode = DXChartSelectionModeMultiple;

The following classes and methods allow you to specify the selection mode:

Symbols Description
DXChartBase.selectionMode Gets or sets the value indicating how many chart elements can be selected simultaneously.
DXChartSelectionMode Lists all values indicating how many chart elements can be selected simultaneously.

How to: Get information about chart elements under the specified point

The Hit testing allows you to recognize which element is at the specified screen coordinates. For example, determining which chart element has been tapped by an end-user. The following example shows how to perform hit testing:

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView: self.chart];
    DXChartHitInfo *hitInfo = [self.chart calcHitInfo:location];

    BOOL useComma = NO;
    NSMutableString *text = [NSMutableString stringWithString: @"You tap"];

    if (hitInfo.inSeries){
        [text appendString:@" in a series"];
        useComma = YES;
    }
    if (hitInfo.inPoint){
        if (useComma) {
            [text appendString:@","];
        }
        [text appendString:@" in a point"];
        useComma = YES;
    }
    if (hitInfo.inLegend){
        if (useComma) {
            [text appendString:@","];
        }
        [text appendString:@" in the legend"];
    }
    [text appendString:@"."];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle: @"OK"
                                                       style: UIAlertActionStyleDefault
                                                     handler: ^(UIAlertAction* action){ }];

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle: @"Series hit-testing"
                                                                             message: text
                                                                      preferredStyle: UIAlertControllerStyleAlert];
    [alertController addAction:okAction];

    [self presentViewController:alertController
                       animated: YES
                     completion: nil];
}

The following methods and classes allows you to perform hit testing.

Symbols Description
DXChartBase::calcHitInfo: Returns information about chart elements at the specified point.
DXChartHitInfo The information about chart elements located in a specific point.