2016-06-19 83 views
0

我有以下代码绘制散点图,但我无法让y轴开始标记为最小值。 y轴一直延伸到minorIncrement float,但是当我尝试设置y轴的最小值时,它就会消失。散点图y轴不应该低于x轴或yMin值

-(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; 
} 

enter image description here

graph after changes to code, just a different data set

回答

1
  1. 我没有看到任何地方,您配置的情节空间。这就是控制图上可见数据范围的原因。如果要使轴在绘图区边缘之前停止,请设置可见范围。

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

+0

情节空间完美地工作,但是当我将y的开始更改为yMin而不是'minorIncrement'时,它根本不会在y轴上显示标签。将尝试并再次弄清楚。 – Wilbe

+0

编辑代码以排除模%,并取消了小增量 – Wilbe

+0

这样做是否改变了标签?最小和最大标签应该是什么? –