mercredi 29 juin 2016

CorePlot - sync ranges of 2 graphs


I've created 2 separate plots and now I wanted to detect when user changes xRange of one of them (scrolling) and change the xRange of second plot.

I've been trying using plotSpace:willChangePlotRangeTo:forCoordinate: and posting notification with new range and plotID in userInfo. Parent viewController listens to the notification and changes xRange of the second plot, BUT: I receive lag, the second plot is "shaky" and very often it ends up with different range. When I do it very fast, shakiness is not observed (only lag).

How can I solve this?

In parentViewController:

 -(void)receivedNewRangeNotif:(NSNotification*)notification {
    NSDictionary* userInfo = notification.userInfo;
    NSString* identifier = [userInfo objectForKey:@"identifierKey"];
    CPTPlotRange* newRange = [userInfo objectForKey:@"newRangeKey"];

    NSLog(@"receivedNewRangeNotif: %@",identifier);
    if ([identifier isEqualToString:@"firstItemID"]) {
        _secondItem.postNotifications = NO;
        CPTXYPlotSpace *secondPlotSpace = (CPTXYPlotSpace *)_secondItem.graph.defaultPlotSpace;
        secondPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:newRange.location length:newRange.length];
        _secondItem.postNotifications = YES;
    }
    else if ([identifier isEqualToString:@"secondItemID"]) {
        _firstItem.postNotifications = NO;
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)_firstItem.graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:newRange.location length:newRange.length];
        _firstItem.postNotifications = YES;
    }

In plotItem:

- (CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate {
    if (_postNotifications) {
    NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:newRange, @"newRangeKey",
                                self.identifier, @"identifierKey", nil];

    NSLog(@"sendingNewRangeNotif");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TODOnotifRangeChanged" object:nil userInfo:userInfo];
 }


    return newRange;
}

Aucun commentaire:

Enregistrer un commentaire