zoukankan      html  css  js  c++  java
  • CorePlot学习 点击scatterPlot中的symbol点时弹出相应的注释

    首先要引用委托方法:CPTScatterPlotDelegate、CPTPlotSpaceDelegate

    完成如下:

    #pragma mark -  
    #pragma mark CPTPlotSpaceDelegate methods  
    -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceCancelledEvent:(UIEvent *)event  
    {  
        return YES;  
    }  
    -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceUpEvent:(UIEvent *)event atPoint:(CGPoint)point  
    {  
        return YES;  
    }  
    -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(UIEvent *)event atPoint:(CGPoint)point  
    {  
        NSLog(@"you putdown at point:%@",[NSValue valueWithCGPoint:point]  
    );  
        return YES;  
    }  
    -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDraggedEvent:(UIEvent *)event atPoint:(CGPoint)point  
    {  
        return YES;  
    }  
      
    #pragma mark -   
    #pragma mark CPTScatterPlotDelegate  
    //当我们选择相应的点时,弹出注释:
    -(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)idx withEvent:(UIEvent *)event  
    {  

    if ( symbolTextAnnotation ) { [xyGraph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation]; symbolTextAnnotation = nil; } // Setup a style for the annotation CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle]; hitAnnotationTextStyle.color = [CPTColor greenColor]; hitAnnotationTextStyle.fontSize = 10.0f; hitAnnotationTextStyle.fontName = @"Helvetica-Bold"; // Determine point of symbol in plot coordinates NSNumber *x = [[datasForPlot objectAtIndex:idx] valueForKey:@"x"]; NSNumber *y = [[datasForPlot objectAtIndex:idx] valueForKey:@"y"]; NSString *date = [[datasForPlot objectAtIndex:idx]valueForKey:@"date"]; NSArray *anchorPoint = [NSArray arrayWithObjects:x, y, nil nil]; // Add annotation // First make a string for the y value NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; [formatter setMaximumFractionDigits:2]; NSString *yString = [formatter stringFromNumber:y]; NSString *myString = [NSString stringWithFormat:@"温度:%@ 日期:%@ 事件:%@",yString,date,nil]; // Now add the annotation to the plot area CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:myString/*yString*/ style:hitAnnotationTextStyle]; symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:xyGraph.defaultPlotSpace anchorPlotPoint:anchorPoint]; symbolTextAnnotation.contentLayer = textLayer; symbolTextAnnotation.displacement = CGPointMake(0.0, 20.0); [xyGraph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation]; // NSLog(@"index:%lu,event:%@",(unsigned long)idx,event); }

    千万别忘记了根据自己定义的设置相应的delegate = self;

    红色标注的就是我们实现该功能的重点

    对了,忘记说一点,该点非常重要,不然你手指点击不灵活。我们的symbol那么小,还要点击到它的中心才能触发下面的方法,这多难啊,有个参数设置一下就搞定了,设置它的触发范围:

        CPTScatterPlot *boundLinePlot = [[CPTScatterPlot alloc]init];  
        boundLinePlot.plotSymbolMarginForHitDetection = 5.0f;//设置symbol点的外沿范围,以用来检测手指的触摸  
  • 相关阅读:
    Atitit.兼具兼容性和扩展性的配置方案attilax总结
    Atitit.异步编程技术原理与实践attilax总结
    Atitit.异步编程技术原理与实践attilax总结
    Atitit. 获取cpu占有率的 java c# .net php node.js的实现
    Atitit. 获取cpu占有率的 java c# .net php node.js的实现
    Atitit.研发团队的管理原则---立长不立贤与按资排辈原则
    Atitit.研发团队的管理原则---立长不立贤与按资排辈原则
    Atitit.研发团队与公司绩效管理的原理概论的attilax总结
    Atitit.研发团队与公司绩效管理的原理概论的attilax总结
    Atitit selenium3 新特性
  • 原文地址:https://www.cnblogs.com/mins/p/4597700.html
Copyright © 2011-2022 走看看