我正在使用核心图创建图形.I在日志中有错误
*由于未捕获异常'NSInvalidArgumentException‘而终止应用,原因:’positionRelativeToViewPoint:forCoordinate:inDirection:‘中的方向无效
我正在使用下面的代码来绘制轴
-(void)configureAxesDancis {
// 1 - Create styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor blackColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor blackColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 2.0f;
CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor blackColor];
tickLineStyle.lineWidth = 1.0f;
// 2 - Get axis set
CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet;
// 3 - Configure x-axis
CPTAxis *x = axisSet.xAxis;
x.title = @"Age In Days";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = -30.0f;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 2.0f;
x.tickDirection = CPTSignNegative;
// x.axisConstraints = [CPTConstraints constraintWithLowerOffset:15.0]; // x.axisConstraints = [CPTConstraints constraintWithLowerOffset:15.0];
NSInteger majorIncrementx = 5;
NSInteger minorIncrementx = 1;
CGFloat xMax = 20.0f; // should determine dynamically based on max price
NSMutableSet *xLabels = [NSMutableSet set];
NSMutableSet *xMajorLocations = [NSMutableSet set];
NSMutableSet *xMinorLocations = [NSMutableSet set];
// NSInteger m = 0;
for (NSInteger j = minorIncrementx; j <= xMax; j += minorIncrementx) {
NSUInteger mod = j % majorIncrementx;
if (mod == 0) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%li", (long)j] textStyle:x.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j-10);
// CGFloat location = m+5;
label.tickLocation = location;
label.offset = -x.majorTickLength+10 - x.labelOffset+10;
if (label) {
[xLabels addObject:label];
}
[xMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
} else {
[xMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
}
}
x.axisLabels = xLabels;
x.majorTickLocations = xMajorLocations;
x.minorTickLocations = xMinorLocations;
// 4 - Configure y-axis
CPTAxis *y = axisSet.yAxis;
y.title = @"Width";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = 40.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = -10.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 4.0f;
y.minorTickLength = 2.0f;
y.tickDirection = CPTSignNegative;
NSInteger majorIncrement = 1000;
NSInteger minorIncrement = 1000;
CGFloat yMax = 10000.0f; // should determine dynamically based on max price
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
NSMutableSet *yMinorLocations = [NSMutableSet set];
for (NSInteger j = minorIncrement; j <= yMax; j += minorIncrement) {
NSUInteger mod = j % majorIncrement;
if (mod == 0) {
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%li", (long)j] textStyle:y.labelTextStyle];
NSDecimal location = CPTDecimalFromInteger(j/3);
label.tickLocation = location;
label.offset = -y.majorTickLength - y.labelOffset;
if (label) {
[yLabels addObject:label];
}
[yMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:location]];
} else {
[yMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
}
}
y.axisLabels = yLabels;
y.majorTickLocations = yMajorLocations;
y.minorTickLocations = yMinorLocations;
}当调用此函数时,应用程序崩溃。我不understand.but前几天的代码工作正常,之后我在目标.but中做了一些更改,这是coming.please帮助解释为什么会出现这个错误。谢谢!
添加堆栈跟踪:
Uncaught exception: Invalid direction in positionRelativeToViewPoint:forCoordinate:inDirection:
2015-03-30 17:52:19.939 MyApp[4398:101971] Stack trace: (
0 CoreFoundation 0x0000000103de4f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000103a7dbb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000103de4e6d +[NSException raise:format:] + 205
3 MyApp 0x00000001003fa1be -[CPTAxisLabel positionRelativeToViewPoint:forCoordinate:inDirection:] + 1998
4 MyApp 0x00000001003b9423 -[CPTAxis updateMajorTickLabels] + 675
5 MyApp 0x00000001003bbc85 -[CPTAxis setAxisLabels:] + 1685
6 MyApp 0x00000001003065e0 -[MyAppBarChartViewController configureAxes] + 2496
7 MyApp 0x000000010030477c -[MyAppBarChartViewController initPlot] + 124
8 MyApp 0x0000000100304285 -[MyAppBarChartViewController viewDidLoad] + 533
9 UIKit 0x0000000102208a90 -[UIViewController loadViewIfRequired] + 738
10 UIKit 0x0000000102208c8e -[UIViewController view] + 27
11 UIKit 0x000000010224be80 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 360
12 UIKit 0x0000000102248069 -[UITabBarController _setSelectedViewController:] + 311
13 UIKit 0x000000010224b4e9 -[UITabBarController _tabBarItemClicked:] + 245
14 UIKit 0x00000001020e48be -[UIApplication sendAction:to:from:forEvent:] + 75
15 UIKit 0x00000001023b8b76 -[UITabBar _sendAction:withEvent:] + 451
16 UIKit 0x00000001020e48be -[UIApplication sendAction:to:from:forEvent:] + 75
17 UIKit 0x00000001021eb410 -[UIControl _sendActionsForEvents:withEvent:] + 467
18 UIKit 0x00000001023bcfd1 -[UITabBar(Static) _buttonUp:] + 103
19 UIKit 0x00000001020e48be -[UIApplication sendAction:to:from:forEvent:] + 75
20 UIKit 0x00000001021eb410 -[UIControl _sendActionsForEvents:withEvent:] + 467
21 UIKit 0x00000001021ea7df -[UIControl touchesEnded:withEvent:] + 522
22 UIKit 0x000000010212a308 -[UIWindow _sendTouchesForEvent:] + 735
23 UIKit 0x000000010212ac33 -[UIWindow sendEvent:] + 683
24 UIKit 0x00000001020f79b1 -[UIApplication sendEvent:] + 246
25 UIKit 0x0000000102104a7d _UIApplicationHandleEventFromQueueEvent + 17370
26 UIKit 0x00000001020e0103 _UIApplicationHandleEventQueue + 1961
27 CoreFoundation 0x0000000103d1a551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
28 CoreFoundation 0x0000000103d1041d __CFRunLoopDoSources0 + 269
29 CoreFoundation 0x0000000103d0fa54 __CFRunLoopRun + 868
30 CoreFoundation 0x0000000103d0f486 CFRunLoopRunSpecific + 470
31 GraphicsServices 0x000000010748a9f0 GSEventRunModal + 161
32 UIKit 0x00000001020e3420 UIApplicationMain + 1282
33 MyApp 0x00000001001f7075 main + 149
34 libdyld.dylib 0x0000000104a90145 start + 1发布于 2015-05-31 14:45:10
在使用核心图框架构建散点图时,我刚刚遇到了同样的错误。当图形试图更新滚动条标签时,您在主题中提到的相同的NSInvalidArgumentException将被抛出,如堆栈跟踪中要调用的最后四个方法所示(按调用时间按时间顺序显示在下面):
[MyAppBarChartViewController configureAxes]
[CPTAxis setAxisLabels:]
[CPTAxis updateMajorTickLabels]
[CPTAxisLabel positionRelativeToViewPoint:forCoordinate:inDirection:]最终导致该问题的原因是将x轴刻度方向设置为负值,如以下行所示:
x.tickDirection = CPTSignNegative;将tickDirection更改为CPTSignNone或CPTSignPositive为我解决了这个问题。不知道为什么,我还没有更深入的研究。希望这能有所帮助!
https://stackoverflow.com/questions/28084530
复制相似问题