iOS Core Plot CPTAxisLabel与CPTPlot对齐 [英] iOS Core Plot CPTAxisLabel alignment to CPTPlot

查看:101
本文介绍了iOS Core Plot CPTAxisLabel与CPTPlot对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面这张图片。我有附件。我的问题是 CPTAxisLabel 标签。它们被置于刻度线下,但它们不会被置于 CPTPlot 之下。

Please look at this image below. I have attached file. My problem with CPTAxisLabel labels. They are placed under the tick, but they are not placed under the CPTPlot as I want.

我该怎么办?在此标签中添加左侧偏移量?我需要将我的标签放在 CPTPlot 对象的中间位置。

How can I add left offset to this labels? I need that my labels will be placed middle the CPTPlot object.

(更新:)

-(void)configureGraph
{

    NSInteger max = 0;
    for (NSNumber *number in self.values) {
        NSInteger num = [number integerValue];
        if (num > max) {
            max = num;
        }
    }

    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.bounds];

    graph.plotAreaFrame.masksToBorder = NO;

    self.hostedGraph = graph;

    graph.paddingBottom = 80.0f;
    graph.paddingLeft  = 0.0f;
    graph.paddingTop    = 50.0f;
    graph.paddingRight  = 0.0f;

    CGFloat xMin = -0.5f;
    CGFloat xMax = [self.titles count];
    if (xMax < 7.0) {
        xMax = 7.0;
    }
    CGFloat yMin = 0.0f;
    CGFloat yMax = max + 25;

    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace;
    plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xMin) length:CPTDecimalFromFloat(xMax)];
    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(yMin) length:CPTDecimalFromFloat(yMax)];

    //graph.backgroundColor = [[UIColor grayColor] CGColor];
    //graph.plotAreaFrame.fill = [CPTFill fillWithColor:[CPTColor redColor]];

}

-(void)configurePlots
{    
    CPTBarPlot *plot = [[CPTBarPlot alloc] init];
    //plot.barOffset = CPTDecimalFromFloat(0.30);
    plot.fill = [CPTFill fillWithColor:[CPTColor colorWithComponentRed:190.0f/255.0f green:203.0f/255.0f blue:103.0f/255.0f alpha:1.0]];
    //plot.barWidth = CPTDecimalFromDouble(0.75);
    plot.lineStyle = nil;
    plot.barCornerRadius = 1.0;
    plot.dataSource = self;
    plot.delegate = self;

    CPTGraph *graph = self.hostedGraph;
    [graph addPlot:plot toPlotSpace:graph.defaultPlotSpace];

}

#pragma mark - CPTPlotDataSource methods
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
    return [self.titles count];
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index {
    if ((fieldEnum == CPTBarPlotFieldBarTip) && (index < [self.titles count])) {
            return [self.values objectAtIndex:index];
    }
    return [NSDecimalNumber numberWithUnsignedInteger:index];
}

-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx
{

    plot.labelOffset = 0;

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

    NSString *valueString = [NSString stringWithFormat:@"%@", [self.values objectAtIndex:idx]];

    return [[CPTTextLayer alloc] initWithText:valueString style:style];
}

- (void)configureAxes
{

    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostedGraph.axisSet;
    axisSet.xAxis.hidden = YES;
    axisSet.yAxis.hidden = YES;

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

    NSMutableArray *labels = [[NSMutableArray alloc] initWithCapacity:5];
    int idx =0;
    for (NSString *string in self.titles)
    {
        CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:string textStyle:style];
        label.rotation = M_PI/2;
        label.tickLocation = CPTDecimalFromInt(idx);
        label.offset = 10.0f;
        [labels addObject:label];
        idx+=1;
    }

    axisSet.xAxis.axisLabels = [NSSet setWithArray:labels];
}


推荐答案

设置 barOffset 为零(0)并使条形位置与刻度位置相同或调整数据源返回的条形位置以计算 barOffset

Either set the barOffset to zero (0) and make the bar locations the same as the tick locations or adjust the bar locations returned by the datasource to account for the barOffset.

这篇关于iOS Core Plot CPTAxisLabel与CPTPlot对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆