ios - 如何仅在核心图的轴外放置间隔线

标签 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];

最佳答案

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

y.tickDirection = CPTSignPositive;

您还可以选择 CPTSignNegative,具体取决于您希望相对于轴放置刻度的方向。

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

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

关于ios - 如何仅在核心图的轴外放置间隔线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21272650/

相关文章:

ios - 核心图温度梯度线

ios - Core Plot - 条形图自动初始缩放

ios - 快速解析Json数据

Swift:通过 Bridging-Header 文件导入时找不到 CorePlot-CocoaTouch.h 文件

ios - UITableView(在 UIScrollView 内部)didSelectRowAtIndexPath : not being called on first tap

ios - 安装新pod后依赖分析错误

ios - 核心图和显示 y 标签

ios - 核心剧情: axis alternatingBandFills with majorTickLocations inproper display

ios - 我如何通过 Foundation 获取星期几?

iphone - XCode 引用类型 - 相对于项目