【问题标题】:How to keep the x-axis visible when I display only a interval on the y-axis using Core-Plot当我使用 Core-Plot 在 y 轴上仅显示一个间隔时如何保持 x 轴可见
【发布时间】:2012-12-20 15:02:53
【问题描述】:

当我在 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;

}

【问题讨论】:

    标签: ios plot core-plot axis-labels


    【解决方案1】:

    如果您总是希望轴在同一位置交叉,请设置 orthogonalCoordinateDecimal:

    x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);
    

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

    x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];
    

    【讨论】:

    • 您的回答正是我所需要的。非常感谢大师!但现在我有另一个问题:我还必须向上移动 y 轴的标签,因为 y 轴的第一个值现在位于 x 轴下方。
    • 我希望 y 轴标签从 x 轴的开头开始,而不是在下面。
    • 我解决了这个问题。也许它会帮助某人。为了查看从 x 轴开始的所有 y 轴标签,我使用了 plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax - yMin)]。 yMin 是最小值,yMax 是最大值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多