3

请看下面这张图片。我已附上文件。我的CPTAxisLabel标签问题。它们被放置在勾号下,但它们没有CPTPlot按我的意愿放置在下面。

如何向此标签添加左偏移量?我需要将我的标签放置在CPTPlot对象中间。

在此处输入图像描述

(更新:)

-(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];
}
4

2 回答 2

1

将 设置barOffset为零 (0) 并使条形位置与刻度位置相同,或者调整数据源返回的条形位置以考虑barOffset.

于 2013-03-29T00:32:11.950 回答
0

CPTAxis 类有一个属性 labelOffset,根据文档,它是“刻度线和标签之间的偏移距离。”

有很多方法可以从 CPTAxis 类自定义标签,您应该检查: https ://core-plot.googlecode.com/hg-history/efa73ee50d8edfd0e1d4294cf4bd2c07d9f8af77/documentation/html/iOS/interface_c_p_t_axis.html

于 2013-03-28T13:36:22.443 回答