繁体   English   中英

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

[英]How to keep the x-axis visible when I display only a interval on the y-axis using Core-Plot

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

}

如果您始终希望轴在同一位置相交,请设置orthogonalCoordinateDecimal

x.orthogonalCoordinateDecimal = CPTDecimalFromUnsignedInteger(0);

如果要使轴保持在屏幕上的同一位置,则无论其他轴范围是多少,请使用约束:

x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0.0];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM