在 Core-Plot 中设置 axisConstraints 崩溃并出现错误 'The orthogonalRange was nil in '

Setting axisConstraints in Core-Plot crashes with error 'The orthogonalRange was nil in '

我正在 Objective-C 中使用 Core-Plot 1.6 开发一个旧应用程序。我在 Y 上有左右轴。我正在尝试向 Right-Y 访问添加标签,但只有当我像这样设置 axisConstraint 属性 时它才会崩溃:

    rightY.axisConstraints =  [CPTConstraints constraintWithUpperOffset:0];

这是我要执行的操作的完整代码。

   CPTXYAxis *rightY = [[CPTXYAxis alloc] init];
// Configure the right y-axis
rightY.labelingPolicy = CPTAxisLabelingPolicyNone;
rightY.title = NSLocalizedStringFromTable(@"Time (Sec)", @"Scores", @"Time in seconds label for scores");
rightY.titleTextStyle = axisTitleStyle;
rightY.titleOffset = 23.0f;
rightY.axisLineStyle = axisLineStyle;
rightY.tickDirection = CPTSignPositive;
rightY.labelingPolicy = CPTAxisLabelingPolicyNone;
rightY.labelOffset = 4.0f;
rightY.majorTickLineStyle = axisLineStyle;
rightY.majorTickLength = -5.0f;
rightY.minorTickLength = -2.5f;
rightY.axisConstraints =  [CPTConstraints constraintWithUpperOffset:0];

NSInteger rightMajorIncrement = 1;
NSInteger rightMinorIncrement = 1;


CGFloat rightYMax = 12; // should determine dynamically based on max price
NSMutableSet *rightYLabels = [NSMutableSet set];
NSMutableSet *rightYMajorLocations = [NSMutableSet set];
NSMutableSet *rightYMinorLocations = [NSMutableSet set];
for (NSInteger j = rightMinorIncrement; j <= rightYMax; j += rightMinorIncrement) {
    NSUInteger mod = j % rightMajorIncrement;
    if (mod == 0) {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSNumberFormatter localizedStringFromNumber:@(13 - j) numberStyle:NSNumberFormatterNoStyle] textStyle:rightY.labelTextStyle];
        NSDecimal rightLocation = CPTDecimalFromInteger(j);
        label.tickLocation = rightLocation;
        label.offset = 2;
        if (label) {
            [rightYLabels addObject:label];
        }
        [rightYMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:rightLocation]];
    } else {
        [rightYMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
    }
}
rightY.axisLabels = rightYLabels;

当我将 rightYLabels 添加到 axisLabels 时,它在上面的行上崩溃了。

编辑:

崩溃信息:

    Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Application Specific Information:
abort() called

Filtered syslog:
None found

Last Exception Backtrace:
(0x182f67d38 0x18247c528 0x182f67c0c 0x1838f6c24 0x1026561d4 0x102656634 0x1025b7918 0x1025b9934 0x102576d30 0x102574510 0x1025744b4 0x102571a48 0x102570cc8 0x18c377bfc 0x18c3777d4 0x18c4fce28 0x18c41ee04 0x18c41ea34 0x18c41e95c 0x18c375000 0x186f450b4 0x186f49194 0x186eb7f24 0x186ede340 0x186edf180 0x182f0f8b8 0x182f0d270 0x182f0d82c 0x182e2e2d8 0x184cbff84 0x18c3db880 0x102519218 0x18295256c)

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x0000000182a81348 0x182a60000 + 136008
1   libsystem_pthread.dylib         0x0000000182b95354 0x182b92000 + 13140
2   libsystem_c.dylib               0x00000001829f0fd8 0x18298e000 + 405464
3   libc++abi.dylib                 0x0000000182454068 0x182452000 + 8296
4   libc++abi.dylib                 0x0000000182454210 0x182452000 + 8720
5   libobjc.A.dylib                 0x000000018247c810 0x182474000 + 34832
6   libc++abi.dylib                 0x000000018246c54c 0x182452000 + 107852
7   libc++abi.dylib                 0x000000018246c158 0x182452000 + 106840
8   libobjc.A.dylib                 0x000000018247c6e8 0x182474000 + 34536
9   CoreFoundation                  0x0000000182e2e344 0x182e26000 + 33604
10  GraphicsServices                0x0000000184cbff84 0x184cb5000 + 44932
11  UIKit                           0x000000018c3db880 0x18c368000 + 473216
12  Counting                        0x0000000102519218 0x102514000 + 21016
13  libdyld.dylib                   0x000000018295256c 0x182951000 + 5484

Thread 1:
0   libsystem_kernel.dylib          0x0000000182a81dbc 0x182a60000 + 138684
1   libsystem_pthread.dylib         0x0000000182b92fb0 0x182b92000 + 4016
2   libsystem_pthread.dylib         0x0000000182b92c30 0x182b92000 + 3120

Thread 2:
0   libsystem_kernel.dylib          0x0000000182a81dbc 0x182a60000 + 138684
1   libsystem_pthread.dylib         0x0000000182b92fb0 0x182b92000 + 4016
2   libsystem_pthread.dylib         0x0000000182b92c30 0x182b92000 + 3120

Thread 3:
0   libsystem_kernel.dylib          0x0000000182a81dbc 0x182a60000 + 138684
1   libsystem_pthread.dylib         0x0000000182b93144 0x182b92000 + 4420
2   libsystem_pthread.dylib         0x0000000182b92c30 0x182b92000 + 3120

Thread 4 name:  com.apple.uikit.eventfetch-thread
Thread 4:
0   libsystem_kernel.dylib          0x0000000182a60bc4 0x182a60000 + 3012
1   libsystem_kernel.dylib          0x0000000182a60a3c 0x182a60000 + 2620
2   CoreFoundation                  0x0000000182f0fce4 0x182e26000 + 957668
3   CoreFoundation                  0x0000000182f0d8b0 0x182e26000 + 948400
4   CoreFoundation                  0x0000000182e2e2d8 0x182e26000 + 33496
5   Foundation                      0x00000001838566e4 0x18384a000 + 50916
6   Foundation                      0x0000000183875afc 0x18384a000 + 178940
7   UIKit                           0x000000018cf2702c 0x18c368000 + 12316716
8   Foundation                      0x0000000183957860 0x18384a000 + 1103968
9   libsystem_pthread.dylib         0x0000000182b9432c 0x182b92000 + 9004
10  libsystem_pthread.dylib         0x0000000182b941f8 0x182b92000 + 8696
11  libsystem_pthread.dylib         0x0000000182b92c38 0x182b92000 + 3128

Thread 5 name:  AVAudioSession Notify Thread
Thread 5:
0   libsystem_kernel.dylib          0x0000000182a60bc4 0x182a60000 + 3012
1   libsystem_kernel.dylib          0x0000000182a60a3c 0x182a60000 + 2620
2   CoreFoundation                  0x0000000182f0fce4 0x182e26000 + 957668
3   CoreFoundation                  0x0000000182f0d8b0 0x182e26000 + 948400
4   CoreFoundation                  0x0000000182e2e2d8 0x182e26000 + 33496
5   AVFAudio                        0x00000001886f7774 0x188673000 + 542580
6   AVFAudio                        0x0000000188722018 0x188673000 + 716824
7   libsystem_pthread.dylib         0x0000000182b9432c 0x182b92000 + 9004
8   libsystem_pthread.dylib         0x0000000182b941f8 0x182b92000 + 8696
9   libsystem_pthread.dylib         0x0000000182b92c38 0x182b92000 + 3128

Thread 6 name:  com.apple.coreaudio.AQClient
Thread 6:
0   libsystem_kernel.dylib          0x0000000182a60bc4 0x182a60000 + 3012
1   libsystem_kernel.dylib          0x0000000182a60a3c 0x182a60000 + 2620
2   CoreFoundation                  0x0000000182f0fce4 0x182e26000 + 957668
3   CoreFoundation                  0x0000000182f0d8b0 0x182e26000 + 948400
4   CoreFoundation                  0x0000000182e2e2d8 0x182e26000 + 33496
5   AudioToolbox                    0x0000000186aea99c 0x1868df000 + 2144668
6   AudioToolbox                    0x0000000186d22630 0x1868df000 + 4470320
7   libsystem_pthread.dylib         0x0000000182b9432c 0x182b92000 + 9004
8   libsystem_pthread.dylib         0x0000000182b941f8 0x182b92000 + 8696
9   libsystem_pthread.dylib         0x0000000182b92c38 0x182b92000 + 3128

Thread 7 name:  com.apple.NSURLConnectionLoader
Thread 7:
0   libsystem_kernel.dylib          0x0000000182a60bc4 0x182a60000 + 3012
1   libsystem_kernel.dylib          0x0000000182a60a3c 0x182a60000 + 2620
2   CoreFoundation                  0x0000000182f0fce4 0x182e26000 + 957668
3   CoreFoundation                  0x0000000182f0d8b0 0x182e26000 + 948400
4   CoreFoundation                  0x0000000182e2e2d8 0x182e26000 + 33496
5   CFNetwork                       0x0000000183597b40 0x1834e9000 + 715584
6   Foundation                      0x0000000183957860 0x18384a000 + 1103968
7   libsystem_pthread.dylib         0x0000000182b9432c 0x182b92000 + 9004
8   libsystem_pthread.dylib         0x0000000182b941f8 0x182b92000 + 8696
9   libsystem_pthread.dylib         0x0000000182b92c38 0x182b92000 + 3128

Thread 8:
0   libsystem_kernel.dylib          0x0000000182a81dbc 0x182a60000 + 138684
1   libsystem_pthread.dylib         0x0000000182b92fb0 0x182b92000 + 4016
2   libsystem_pthread.dylib         0x0000000182b92c30 0x182b92000 + 3120

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x00000001c04e10b7
    x4: 0x0000000182471afd   x5: 0x000000016d8eb4b0   x6: 0x000000000000006e   x7: 0xffffffffffffffec
    x8: 0x0000000008000000   x9: 0x0000000004000000  x10: 0x0000000182b9963c  x11: 0x0000000000000003
   x12: 0xffffffffffffffff  x13: 0x0000000000000001  x14: 0x0000000182a07557  x15: 0x0000000000000010
   x16: 0x0000000000000148  x17: 0x00000000ffffffff  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x00000001b3804b80  x21: 0x000000016d8eb4b0  x22: 0x0000000000000303  x23: 0x00000001b3804c60
   x24: 0x00000001c401f6a0  x25: 0x0000000000000000  x26: 0x0000000000000001  x27: 0x0000000000000000
   x28: 0x000000016d8ebb28   fp: 0x000000016d8eb410   lr: 0x0000000182b95354
    sp: 0x000000016d8eb3e0   pc: 0x0000000182a81348 cpsr: 0x00000000

编辑:

 *** Assertion failure in -[CPTXYAxis orthogonalCoordinateViewLowerBound:upperBound:], /Users/ric/Documents/Project/Pods/CorePlot/framework/Source/CPTXYAxis.m:128
2017-11-21 09:35:42.790995-0500 Counting[808:591102] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The orthogonalRange was nil in orthogonalCoordinateViewLowerBound:upperBound:'
*** First throw call stack:
(0x182f67d38 0x18247c528 0x182f67c0c 0x1838f6c24 0x1028d21d4 0x1028d2634 0x102833918 0x102835934 0x1027f2d30 0x1027f0510 0x1027f04b4 0x1027eda48 0x1027eccc8 0x18c377bfc 0x18c3777d4 0x18c4fce28 0x18c41ee04 0x18c41ea34 0x18c41e95c 0x18c375000 0x186f450b4 0x186f49194 0x186eb7f24 0x186ede340 0x186edf180 0x182f0f8b8 0x182f0d270 0x182f0d82c 0x182e2e2d8 0x184cbff84 0x18c3db880 0x102795218 0x18295256c)
libc++abi.dylib: terminating with uncaught exception of type NSException

缺少的是在分配 axisLabels 之前分配 plotSpace。

所以将这行代码移到标签分配上方解决了这个问题。 rightY.plotSpace = rightAxisPlotSpace;

这是上面的完整代码。

CPTXYAxis *rightY = [[CPTXYAxis alloc] init];
// Configure the right y-axis
rightY.labelingPolicy = CPTAxisLabelingPolicyNone;
rightY.title = NSLocalizedStringFromTable(@"Time (Sec)", @"Scores", @"Time in seconds label for scores");
rightY.titleTextStyle = axisTitleStyle;
rightY.titleOffset = 23.0f;
rightY.axisLineStyle = axisLineStyle;
rightY.tickDirection = CPTSignPositive;
rightY.labelingPolicy = CPTAxisLabelingPolicyNone;
rightY.labelOffset = 4.0f;
rightY.majorTickLineStyle = axisLineStyle;
rightY.majorTickLength = -5.0f;
rightY.minorTickLength = -2.5f;
rightY.axisConstraints =  [CPTConstraints constraintWithUpperOffset:0];
rightY.plotSpace = rightAxisPlotSpace;

NSInteger rightMajorIncrement = 1;
NSInteger rightMinorIncrement = 1;


CGFloat rightYMax = 12; // should determine dynamically based on max price
NSMutableSet *rightYLabels = [NSMutableSet set];
NSMutableSet *rightYMajorLocations = [NSMutableSet set];
NSMutableSet *rightYMinorLocations = [NSMutableSet set];
for (NSInteger j = rightMinorIncrement; j <= rightYMax; j += rightMinorIncrement) {
    NSUInteger mod = j % rightMajorIncrement;
    if (mod == 0) {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSNumberFormatter localizedStringFromNumber:@(13 - j) numberStyle:NSNumberFormatterNoStyle] textStyle:rightY.labelTextStyle];
        NSDecimal rightLocation = CPTDecimalFromInteger(j);
        label.tickLocation = rightLocation;
        label.offset = 2;
        if (label) {
            [rightYLabels addObject:label];
        }
        [rightYMajorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:rightLocation]];
    } else {
        [rightYMinorLocations addObject:[NSDecimalNumber decimalNumberWithDecimal:CPTDecimalFromInteger(j)]];
    }
}
rightY.axisLabels = rightYLabels;