izik461 izik461 - 1 year ago 129
Objective-C Question

Coreplot - coordinates and size of axis label

I use date labels on x axis and want to highlight specific date using custom view. I am planning to use

, move the annotationFrame to labels position and scroll it (move annotation in
) when axis range is changed (simillar to core plot Framework - How to set custom UIView in CPTPlotSpaceAnnotation)

How Can I get coordinates of the label when called from plotSpacewillChangePlotRangeToForCoordinate?

enter image description here

I have successfully implemented moving annotation invoked by user selection: I save the _selectedLabel and send newFrame when range of plot changes. The are 2 problems with that:

  1. delay of annotation frame when changing range - see the movie: https://www.youtube.com/watch?v=R66ew6GAiJU&feature=youtu.be

  2. when annotated label leaves the screen, the _selectedLabel is "forgotten". When moving back to screen probably the new label is created and my _selectedLabel references to the old object->frame remains at x=0 (the point it left the screen). Maybe is it somehow possible to get coordinates of specific label (based on comparison with dateToAnnotate)?

    -(void)axis:(CPTAxis *)axis labelWasSelected:(CPTAxisLabel *)label {
    NSLog(@"labelWasSelected: %@",label);
    _selectedLabel = label;
    CGRect graphLabelFrame = [self adjustAnnotationFrameForLabel:_selectedLabel];
    [_delegate datesPlot:self didAnnotateFrame:graphLabelFrame animating:YES];

    -(CPTPlotRange *)plotSpace:(CPTPlotSpace *)space willChangePlotRangeTo:(nonnull CPTPlotRange *)newRange forCoordinate:(CPTCoordinate)coordinate {
    CGRect graphLabelFrame = [self adjustAnnotationFrameForLabel:_selectedLabel];
    [_delegate datesPlot:self didAnnotateFrame:graphLabelFrame animating:NO];

and in Delegate:

-(void)datesPlot:(datesPlot*) plot didAnnotateFrame:(CGRect)frame animating:(BOOL)animating{
if (animating) {
[UIView animateWithDuration:0.3 animations:^{
_annotationView.frame = [self.view convertRect:frame fromView:_datesHostingView];
else {
_annotationView.frame = [self.view convertRect:frame fromView:_datesHostingView];

[_annotationView setNeedsLayout];

Answer Source

The second parameter to the -axis:labelWasSelected: method is the axis label. The contentLayer of the label is a CALayer subclass that actually displays the label. Use the built-in coordinate conversion methods to convert the frame and/or bounds of the contentLayer to the coordinate space of the annotation's annotationHostLayer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download