I need to calculate the distance between 2 points on a plot.
I am using the method below to achive this need. I tried the others methods but this is the only one that the point is actually plot. On others methods it always return 0.
-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
NSDecimal plotPoint[2];
CPTPlotSpace *thePlotSpace = plot.plotSpace;
CPTPlotArea *thePlotArea = plot.plotArea;
CPTCoordinate independentCoord = (NO ? CPTCoordinateY : CPTCoordinateX);
plotPoint[independentCoord] = [plot cachedDecimalForField:CPTBarPlotFieldBarLocation recordIndex:0];
CGPoint basePoint;
basePoint = [plot convertPoint:[thePlotSpace plotAreaViewPointForPlotPoint:plotPoint] fromLayer:thePlotArea];
plotPoint[independentCoord] = [plot cachedDecimalForField:CPTBarPlotFieldBarLocation recordIndex:1];
CGPoint basePoint2;
basePoint2 = [plot convertPoint:[thePlotSpace plotAreaViewPointForPlotPoint:plotPoint] fromLayer:thePlotArea];
int valor;
valor = basePoint2.x - basePoint.x;
The code above works almost always but i am getting EXC_BAD_ACESS that comes from this code above.
When i comment the code, the exc_bad_acess never occur
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000004
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 Foundation 0x32f7be56 NSIntegerDivide + 98
1 Foundation 0x32f7f9e0 NSDecimalDivide + 216
2 InfoTOS007 0x000e198c CPTDecimalDivide (CPTUtilities.m:496)
3 InfoTOS007 0x000fd0c8 -[CPTXYPlotSpace viewCoordinateForViewLength:linearPlotRange:plotCoordinateValue:] (CPTXYPlotSpace.m:384)
4 InfoTOS007 0x000fd66a -[CPTXYPlotSpace plotAreaViewPointForPlotPoint:] (CPTXYPlotSpace.m:449)
5 InfoTOS007 0x000bea38 -[GraficoEmbarqueDescarga dataLabelForPlot:recordIndex:] (GraficoEmbarqueDescarga.m:249)
6 InfoTOS007 0x000ce192 -[CPTPlot relabel] (CPTPlot.m:1016)
7 InfoTOS007 0x000cb868 -[CPTPlot layoutSublayers] (CPTPlot.m:402)
8 QuartzCore 0x34272f92 CA::Layer::layout_if_needed(CA::Transaction*) + 210
9 QuartzCore 0x34277114 CA::Context::commit_transaction(CA::Transaction*) + 220
10 QuartzCore 0x34276e50 CA::Transaction::commit() + 308
11 QuartzCore 0x3426ed7e CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 50
12 CoreFoundation 0x344deb44 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 12
13 CoreFoundation 0x344dcd80 __CFRunLoopDoObservers + 252
14 CoreFoundation 0x344dd0da __CFRunLoopRun + 754
15 CoreFoundation 0x344604d6 CFRunLoopRunSpecific + 294
16 CoreFoundation 0x3446039e CFRunLoopRunInMode + 98
17 GraphicsServices 0x30d4bfc6 GSEventRunModal + 150
18 UIKit 0x32a2873c UIApplicationMain + 1084
19 InfoTOS007 0x000b3468 main (main.m:16)
20 InfoTOS007 0x000b340c start + 32
It looks like the problem is that your never setting plotPoint[dependentCoord], so it is trying to convert an undefined value to plot coordinates. Just set it to a value within the range of your graph, if all your worried about is the X axis, then the Y axis value doesn’t matter. I haven’t tried compiling the following, but it looks right.