当我使用Core-Plot仅显示y轴上的间隔时,如何保持x轴可见

时间:2012-12-20 15:02:54

标签: ios plot core-plot axis-labels

当我在y轴上只显示一个间隔时,我想在屏幕上看到x轴。当间隔从0显示时,x轴是可见的,所以我认为我应该使用yRange向上移动x轴,但我不知道如何做到这一点。

这是我设置xRange和yRange的部分。对于y轴,我搜索我想要显示的最小值和最大值。

    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)];
plotSpace.xRange = xRange;
//CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];
//[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)];
//plotSpace.yRange = yRange;

cpd = [CPDStockPriceStore sharedInstance];
NSMutableArray *arr = cpd.prices;
minY = 10000;
maxY = 0;
for (unsigned i = 1; i < [arr count]; i++){
    if([[arr objectAtIndex:i] intValue] < minY) minY = [[arr objectAtIndex:i]intValue];
    if([[arr objectAtIndex:i] intValue] > maxY) maxY = [[arr objectAtIndex:i]intValue];
}

CGFloat yMin = 100*((minY-200)/100);
CGFloat yMax = 100*((maxY+200)/100);  // should determine dynamically based on max price

plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)];

这是我配置轴的部分:

-(void)configureAxes {

// 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
CPTAxis *x = axisSet.xAxis;
x.title = @"Ziua Lunii";
x.titleTextStyle = axisTitleStyle;
x.titleOffset = 15.0f;
x.axisLineStyle = axisLineStyle;
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.labelTextStyle = axisTextStyle;
x.majorTickLineStyle = axisLineStyle;
x.majorTickLength = 4.0f;
x.tickDirection = CPTSignNegative;
CGFloat dateCount = [[[CPDStockPriceStore sharedInstance] datesInMonth] count];
NSMutableSet *xLabels = [NSMutableSet setWithCapacity:dateCount];
NSMutableSet *xLocations = [NSMutableSet setWithCapacity:dateCount];
NSInteger i = 0;

for (NSString *date in [[CPDStockPriceStore sharedInstance] datesInMonth]) {
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:date textStyle:x.labelTextStyle];
    label.rotation = M_PI/4;
    CGFloat location = i++;
    label.tickLocation = CPTDecimalFromCGFloat(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 = @"Pret";
y.titleTextStyle = axisTitleStyle;
y.titleOffset = -50.0f;
y.axisLineStyle = axisLineStyle;
y.majorGridLineStyle = gridLineStyle;
y.labelingPolicy = CPTAxisLabelingPolicyNone;
y.labelTextStyle = axisTextStyle;
y.labelOffset = 23.0f;
y.majorTickLineStyle = axisLineStyle;
y.majorTickLength = 4.0f;
y.minorTickLength = 2.0f;
y.tickDirection = CPTSignPositive;

// Here is the problem. I think I have to use minY here, instead of 50 or 100 as values, in order to see the x-axis on the screen, but I don't exactly know how. 

NSInteger majorIncrement = 100;
NSInteger minorIncrement = 50;
CGFloat yMax = maxY;  // 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:@"%i", j] textStyle:y.labelTextStyle];
        NSDecimal location = CPTDecimalFromInteger(j);
        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;

}

1 个答案:

答案 0 :(得分:6)

如果您始终希望轴在同一个地方交叉,请设置orthogonalCoordinateDecimal

x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);

如果您希望轴保持在屏幕上的相同位置,无论其他轴范围是什么,请使用约束:

x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];