如何仅在核心图上将间隔线放在轴外

时间:2014-01-22 01:46:23

标签: ios core-plot

我目前正在显示图表,其中左右两侧的y轴相同。在我的代码的某处,我告诉x和左y轴的轴间隔保持在图表空间/图形区域之外。

我有一个大脑放屁,任何状态都可以显而易见吗?

我的代码:

if ((int)[[UIScreen mainScreen] bounds].size.height == 568) {
    self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 190, 320, 265)];
}
else {
    self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:CGRectMake(0, 190, 320, 178)];
}
self.hostView.allowPinchScaling = NO;
[self.view addSubview:self.hostView];

graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds];
self.hostView.hostedGraph = graph;
CPTGradient *gradient = [CPTGradient gradientWithBeginningColor:[CPTColor colorWithCGColor:[UIColorFromRGB(0x2468d8)CGColor]]
                                                    endingColor:[CPTColor colorWithCGColor:[UIColorFromRGB(0xa4bbe0)CGColor]]];
gradient.angle     = 90.0;
graph.plotAreaFrame.fill = [CPTFill fillWithGradient:gradient];
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor colorWithCGColor:[UIColorFromRGB(0x717a72)CGColor]];
borderLineStyle.lineWidth = 4.0;
graph.plotAreaFrame.borderLineStyle = borderLineStyle;
graph.plotAreaFrame.cornerRadius        = 10.0;
[graph.plotAreaFrame setPaddingLeft:21.0f];
[graph.plotAreaFrame setPaddingRight:21.0f];
[graph.plotAreaFrame setPaddingBottom:18.0f];
graph.paddingLeft   = 3.0;
graph.paddingTop    = 3.0;
graph.paddingRight  = 3.0;
graph.paddingBottom = 3.0;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
NSDate *firstDate = [dateFormatter dateFromString:[[changedArray objectAtIndex:0]objectForKey:@"date"]];
NSDate *lastDate = [dateFormatter dateFromString:[[changedArray objectAtIndex:changedArray.count-1]objectForKey:@"date"]];
NSNumber *difference = [NSDecimalNumber numberWithDouble:[lastDate timeIntervalSinceDate:firstDate]];

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = NO;
plotSpace.delegate = self;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat([difference integerValue]*1.05)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat([yMaxValue integerValue]+5)];
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromFloat([difference integerValue]*1.05)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble([yMaxValue doubleValue]*1.23)];

CPTMutableLineStyle *graphLineStyle = [CPTMutableLineStyle lineStyle];
graphLineStyle.lineCap   = kCGLineCapRound;
graphLineStyle.lineColor = [CPTColor whiteColor];
graphLineStyle.lineWidth = 1.0;

CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.axisLineStyle = graphLineStyle;
x.axisLineStyle = graphLineStyle;
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
x.minorTickLength = 3.0f;
x.majorTickLength = 5.0f;

CPTMutableTextStyle *labelStyle = [CPTMutableTextStyle textStyle];
labelStyle.color = [[CPTColor whiteColor] colorWithAlphaComponent:1];
labelStyle.fontName = @"Helvetica-Bold";
labelStyle.fontSize = 9.0f;

xFirstEntry = [[[changedArray objectAtIndex:0]objectForKey:@"date"] substringFromIndex:11];
xFirstEntry = [xFirstEntry substringToIndex:[xFirstEntry length] - 6];
xLastEntry = [[[changedArray objectAtIndex:changedArray.count-1]objectForKey:@"date"] substringFromIndex:11];
xLastEntry = [xLastEntry substringToIndex:[xLastEntry length] - 6];

NSMutableArray *xMajorLocations = [[NSMutableArray alloc]init];
NSMutableArray *xMinorLocations = [[NSMutableArray alloc]init];
NSMutableArray *xLabels = [[NSMutableArray alloc]init];
int newValue = [xFirstEntry integerValue];
NSNumber *xMajorDataPoint = [NSNumber numberWithInt:(newValue)];
NSNumber *xMinorDataPoint = [NSNumber numberWithInt:(newValue)];
if ([xFirstEntry integerValue] > [xLastEntry integerValue]) {
    while (newValue <= 24) {
        xMinorDataPoint = @([xMajorDataPoint integerValue] + 1800);
        NSNumber *x2LabelValue = [NSNumber numberWithInt:newValue];
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[x2LabelValue stringValue] textStyle:labelStyle];
        label.tickLocation = CPTDecimalFromInt([xMajorDataPoint intValue]);
        label.offset = 5;
        [xLabels addObject:label];
        [xMajorLocations addObject:xMajorDataPoint];
        [xMinorLocations addObject:xMinorDataPoint];
        newValue++;
        xMajorDataPoint = @([xMajorDataPoint integerValue] + 3600);
    }
    newValue = 1;
    xMinorDataPoint = @([xMajorDataPoint integerValue] + 1800);
    NSNumber *x2LabelValue = [NSNumber numberWithInt:newValue];
    CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[x2LabelValue stringValue] textStyle:labelStyle];
    label.tickLocation = CPTDecimalFromInt([xMajorDataPoint intValue]);
    label.offset = 5;
    [xLabels addObject:label];
    [xMajorLocations addObject:xMajorDataPoint];
    [xMinorLocations addObject:xMinorDataPoint];
    newValue++;
    xMajorDataPoint = @([xMajorDataPoint integerValue] + 3600);
    while (newValue <= [xLastEntry integerValue]+1) {
        xMinorDataPoint = @([xMajorDataPoint integerValue] + 1800);
        NSNumber *x2LabelValue = [NSNumber numberWithInt:newValue];
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[x2LabelValue stringValue] textStyle:labelStyle];
        label.tickLocation = CPTDecimalFromInt([xMajorDataPoint intValue]);
        label.offset = 5;
        [xLabels addObject:label];
        [xMajorLocations addObject:xMajorDataPoint];
        [xMinorLocations addObject:xMinorDataPoint];
        newValue++;
        xMajorDataPoint = @([xMajorDataPoint integerValue] + 3600);
    }
}
else {
    while (newValue <= [xLastEntry integerValue]+1) {
        xMinorDataPoint = @([xMajorDataPoint integerValue] + 1800);
        NSNumber *xLabelValue = [NSNumber numberWithInt:newValue];
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[xLabelValue stringValue] textStyle:labelStyle];
        label.tickLocation = CPTDecimalFromInt([xMajorDataPoint intValue]);
        label.offset = 5;
        [xLabels addObject:label];
        [xMajorLocations addObject:xMajorDataPoint];
        [xMinorLocations addObject:xMinorDataPoint];
        newValue++;
        xMajorDataPoint = @([xMajorDataPoint integerValue] + 3600);
    }
}
x.labelingPolicy = CPTAxisLabelingPolicyNone;
x.axisLabels = [NSSet setWithArray:xLabels];
x.majorTickLocations = [NSSet setWithArray:xMajorLocations];
x.minorTickLocations = [NSSet setWithArray:xMinorLocations];
x.majorTickLineStyle = graphLineStyle;
x.minorTickLineStyle = graphLineStyle;

CPTXYAxis *y = axisSet.yAxis;
y.axisLineStyle = graphLineStyle;
y.majorIntervalLength = CPTDecimalFromDouble(10);
y.minorTicksPerInterval = 9;
y.minorTickLength = 3;
y.majorTickLength = 5;
y.labelOffset = 0;
y.orthogonalCoordinateDecimal = CPTDecimalFromDouble(0);
y.labelTextStyle = labelStyle;
y.majorTickLineStyle = graphLineStyle;
y.minorTickLineStyle = graphLineStyle;
NSNumberFormatter *Yformatter = [[NSNumberFormatter alloc] init];
[Yformatter setGeneratesDecimalNumbers:NO];
[Yformatter setNumberStyle:NSNumberFormatterDecimalStyle];
y.labelFormatter = Yformatter;
y.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];

CPTXYAxis *y2 = [(CPTXYAxis *)[CPTXYAxis alloc] initWithFrame:CGRectZero];
y2.coordinate = CPTCoordinateY;
y2.plotSpace = plotSpace;
y2.axisLineStyle = graphLineStyle;
y2.majorIntervalLength = CPTDecimalFromDouble(10);
y2.minorTicksPerInterval = 9;
y2.minorTickLength = 3;
y2.majorTickLength = 5;
y2.labelOffset = -20;
y2.labelTextStyle = labelStyle;
y2.majorTickLineStyle = graphLineStyle;
y2.minorTickLineStyle = graphLineStyle;
y2.labelFormatter = Yformatter;
y2.axisConstraints = [CPTConstraints constraintWithUpperOffset:0];

graph.axisSet.axes = @[x, y, y2];

1 个答案:

答案 0 :(得分:1)

您可以按如下方式设置刻度线的方向:

y.tickDirection = CPTSignPositive;

您也可以选择CPTSignNegative,具体取决于您想要相对于轴放置刻度线的方向。

在您的代码中,我认为您所看到的是由于

CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[x2LabelValue stringValue] textStyle:labelStyle];
label.tickLocation = CPTDecimalFromInt([xMajorDataPoint intValue]);
label.offset = 5;