I am stuck for some time at this problem. What I want to do is to get graph
data from -(BOOL)plotSpace:(CPTPlotSpace *)space shouldHandlePointingDeviceDownEvent:(id)event atPoint:(CGPoint)point
event. I do not want that user can only touch plotSymbol
with -(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index
. So what I want to do is to touch anywhere on my Scatter Plot
for example above the drawn line, get my point at my line that is below touch event for that touch point and draw that point on the screen. I've seen this: Using core-plot, how can you convert a touched point to the plot space? but is not working.
EDIT:
This is for plotSymbol touch. I want to touch anywhere on the graph and show the points on the graph. I do not want to touch the plot symbol only to show my value at that point. I want to be able to touch it above or below the drawn line on a graph and it should be able to recognise that there is a plot symbol for my CGPoint)point
.
-(void)scatterPlot:(CPTScatterPlot *)plot plotSymbolWasSelectedAtRecordIndex:(NSUInteger)index{
CPTXYGraph *graph = (self.graphs)[0];
if ( symbolTextAnnotation ) {
[graph.plotAreaFrame.plotArea removeAnnotation:symbolTextAnnotation];
symbolTextAnnotation = nil;
}
// Setup a style for the annotation
CPTMutableTextStyle *hitAnnotationTextStyle = [CPTMutableTextStyle textStyle];
hitAnnotationTextStyle.color = [CPTColor whiteColor];
hitAnnotationTextStyle.fontSize = 16.0;
hitAnnotationTextStyle.fontName = @"Helvetica-Bold";
// Determine point of symbol in plot coordinates
NSDictionary *dataPoint = plotData[index];
NSNumber *x = dataPoint[@(CPTScatterPlotFieldX)];
NSNumber *y = dataPoint[@(CPTScatterPlotFieldY)];
NSArray *anchorPoint = @[x, y];
// Add annotation
// First make a string for the y value
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
NSString *yString = [formatter stringFromNumber:y];
// Now add the annotation to the plot area
CPTTextLayer *textLayer = [[CPTTextLayer alloc] initWithText:yString style:hitAnnotationTextStyle] ;
CPTImage *background = [CPTImage imageForPNGFile:@"check"];
textLayer.fill = [CPTFill fillWithImage:background];
textLayer.paddingLeft = 2.0;
textLayer.paddingTop = 2.0;
textLayer.paddingRight = 2.0;
textLayer.paddingBottom = 2.0;
symbolTextAnnotation = [[CPTPlotSpaceAnnotation alloc] initWithPlotSpace:graph.defaultPlotSpace anchorPlotPoint:anchorPoint];
symbolTextAnnotation.contentLayer = textLayer;
symbolTextAnnotation.contentAnchorPoint = CGPointMake(0.5, 0.0);
symbolTextAnnotation.displacement = CGPointMake(0.0, 10.0);
[graph.plotAreaFrame.plotArea addAnnotation:symbolTextAnnotation];
}
In the plot space delegate, use the -dataIndexFromInteractionPoint:
method on the plot to find the index of the closest data point to the touched point. Once you have the data index, use the code you already have to create and display the annotation.