散点图 y 轴不应延伸到 x 轴或 yMin 值以下

Scatter plot y-Axis should not extend below the x-axis or yMin value

我有以下代码来绘制散点图,但我无法让 y 轴在最小值处开始标记。 y 轴一直延伸到 minorIncrement 浮点数,但是当我尝试设置 y 轴的最小值时,它就消失了。

-(void)configureAxes {

// Calculate the increment values for the y-axis
DataSource *sharedManager = [DataSource sharedManager];
float ymax = -MAXFLOAT;
float ymin = MAXFLOAT;
for (NSNumber *num in sharedManager.finalFIELDreading) {
    float y = num.floatValue;
    if (y < ymin) ymin = y;
    if (y > ymax) ymax = y;
}
int MinFieldReading = roundf(ymin);
int MaxFieldReading = roundf(ymax);
int diffFieldReading = MaxFieldReading-MinFieldReading;
int Increment = diffFieldReading/[sharedManager.finalFIELDreading count];
int incrementValue = 10 * floor( Increment / 10 + 0.5 );


// 1 - Create styles
CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle];
axisTitleStyle.color = [CPTColor whiteColor];
axisTitleStyle.fontName = @"Helvetica-Bold";
axisTitleStyle.fontSize = 12.0f;
CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle];
axisLineStyle.lineWidth = 2.0f;
axisLineStyle.lineColor = [CPTColor whiteColor];
CPTMutableTextStyle *axisTextStyle = [[CPTMutableTextStyle alloc] init];
axisTextStyle.color = [CPTColor whiteColor];
axisTextStyle.fontName = @"Helvetica-Bold";
axisTextStyle.fontSize = 11.0f;
CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle];
tickLineStyle.lineColor = [CPTColor whiteColor];
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
NSArray *yMinNumbers = [sharedManager.finalFIELDreading sortedArrayUsingSelector:@selector(compare:)];// should determine dynamically based on max reading
CGFloat yMin = [yMinNumbers[0] floatValue];

// Set Axis to start just below the min y value
axisSet.xAxis.orthogonalPosition = @(yMin); //this was yMin-40
CPTAxis *x = axisSet.xAxis;
x.title = @"Station (m)";
    x.titleTextStyle = axisTitleStyle;
    x.titleOffset = 15.0f;
    x.axisLineStyle = axisLineStyle;
    x.majorGridLineStyle = gridLineStyle;
    x.labelingPolicy = CPTAxisLabelingPolicyNone;
    x.labelTextStyle = axisTextStyle;
    x.majorTickLineStyle = axisLineStyle;
    x.majorTickLength = 4.0f;
    x.tickDirection = CPTSignNegative;


CGFloat stationCount = [sharedManager.finalSTNreading count];


    NSMutableSet *xLabels = [NSMutableSet setWithCapacity:stationCount];
    NSMutableSet *xLocations = [NSMutableSet setWithCapacity:stationCount];
    NSInteger i = 0;

    for (NSString *station in [[DataSource sharedManager] finalSTNreading]) {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:station  textStyle:x.labelTextStyle];
        CGFloat location = i++;
        label.tickLocation = @(location);
        label.offset = x.majorTickLength;
        if (label) {
            [xLabels addObject:label];
            [xLocations addObject:[NSNumber numberWithFloat:location]];
        }
    }

x.axisLabels = xLabels;
x.majorTickLocations = xLocations;


 // 4 - Configure y-axis

CPTAxis *y = axisSet.yAxis;
y.title = @"Field Reading (nTesla)";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = 37.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = -7.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 4.0f;
// y.minorTickLength = 2.0f;
y.tickDirection = CPTSignNegative;
NSInteger majorIncrement = incrementValue;
NSArray *yMaxNumbers = [[[DataSource sharedManager] finalFIELDreading] sortedArrayUsingSelector:@selector(compare:)];// should determine dynamically based on max reading
CGFloat yMax = [[yMaxNumbers lastObject] floatValue];
NSMutableSet *yLabels = [NSMutableSet set];
NSMutableSet *yMajorLocations = [NSMutableSet set];
for (NSInteger j = yMin; j <= yMax; j += majorIncrement) {

        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[NSString stringWithFormat:@"%li", (long)j] textStyle:y.labelTextStyle];
        NSNumber *location = @(j);
        label.tickLocation = location;
        label.offset = -y.majorTickLength - y.labelOffset;
        if (label) {
            [yLabels addObject:label];
        }
        [yMajorLocations addObject:location];

}
y.axisLabels = yLabels;    
y.majorTickLocations = yMajorLocations;
}

  1. 我在任何地方都没有看到你配置情节 space。这就是控制图表上可见数据范围的原因。如果希望坐标轴停在绘图区域的边缘之前,请设置可见范围。

  2. 创建 y 标签的循环可能应该从 yMin 开始,而不是 minorIncrement。我怀疑您正在创建很多永远不会出现在屏幕上的标签。这需要时间来设置(影响性能)并使用额外的内存。