ios - coreplot 中的 x 轴标签未显示

标签 ios ios5 ios6 core-plot bar-chart

我已经附上了我正在使用的整个代码,但没有生成 x 轴标签。我不知道哪里出了问题。我是核心情节中的菜鸟。请帮助我。提前致谢新年快乐。

#import "BarPlotViewController.h"

@interface BarPlotViewController ()



@end



@implementation BarPlotViewController


#define BAR_POSITION @"POSITION"
#define BAR_HEIGHT @"HEIGHT"
#define COLOR @"COLOR"
#define CATEGORY @"CATEGORY"

#define AXIS_START 0
#define AXIS_END 50

//
@synthesize data;
@synthesize graph;
@synthesize hostingView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.data = [NSMutableArray array];

        int bar_heights[] = {20,30,10,40};
        UIColor *colors[] = {
            [UIColor redColor],
            [UIColor blueColor],
            [UIColor orangeColor],
            [UIColor purpleColor]};
        NSString *categories[] = {@"Plain Milk", @"Milk + Caramel", @"White", @"Dark"};

        for (int i = 0; i < 4 ; i++){
            double position = i*10; //Bars will be 10 pts away from each other
            double height = bar_heights[i];

            NSDictionary *bar = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithDouble:position],BAR_POSITION,
                                 [NSNumber numberWithDouble:height],BAR_HEIGHT,
                                 colors[i],COLOR,
                                 categories[i],CATEGORY,
                                 nil];
            [self.data addObject:bar];

        }
        [self generateBarPlot];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)generateBarPlot
{
    //Create host view
    self.hostingView = [[CPTGraphHostingView alloc]
                        initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.hostingView];

    //Create graph and set it as host view's graph
    self.graph = [[CPTXYGraph alloc] initWithFrame:self.hostingView.bounds];
    [self.hostingView setHostedGraph:self.graph];

    //set graph padding and theme
    self.graph.plotAreaFrame.paddingTop = 20.0f;
    self.graph.plotAreaFrame.paddingRight = 20.0f;
    self.graph.plotAreaFrame.paddingBottom = 70.0f;
    self.graph.plotAreaFrame.paddingLeft = 70.0f;
    [self.graph applyTheme:[CPTTheme themeNamed:kCPTDarkGradientTheme]];

    //set axes ranges
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)self.graph.defaultPlotSpace;
  //  plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(AXIS_START)                                            length:CPTDecimalFromFloat((AXIS_END - AXIS_START)+5)];


 //   plotSpace.xRange                = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(-1)                                                                   length:CPTDecimalFromInt(8)];


        CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;

    //X labels
    int labelLocations = 0;
    NSMutableArray *customXLabels = [NSMutableArray array];
   NSArray *dates = [NSArray arrayWithObjects:@"2012-05-01", @"2012-05-02", @"2012-05-03",
             @"2012-05-04", @"2012-05-05", @"2012-05-06", @"2012-05-07", nil];
    for (NSString *day in dates) {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:day textStyle:axisSet.xAxis.labelTextStyle];
        newLabel.tickLocation   = [[NSNumber numberWithInt:labelLocations] decimalValue];
        newLabel.offset         = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
     //   newLabel.rotation       = M_PI / 4;
        [customXLabels addObject:newLabel];
        labelLocations++;

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

    plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:
                        CPTDecimalFromFloat(AXIS_START)
                                                    length:CPTDecimalFromFloat((AXIS_END - AXIS_START)+5)];


    //set axes' title, labels and their text styles
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.fontName = @"Helvetica";
    textStyle.fontSize = 14;
    textStyle.color = [CPTColor whiteColor];
    axisSet.xAxis.title = @"Years";
    axisSet.yAxis.title = @"Expenses";
    axisSet.xAxis.titleTextStyle = textStyle;
    axisSet.yAxis.titleTextStyle = textStyle;
    axisSet.xAxis.titleOffset = 30.0f;
    axisSet.yAxis.titleOffset = 40.0f;
    axisSet.xAxis.labelTextStyle = textStyle;
    axisSet.xAxis.labelOffset = 3.0f;
    axisSet.yAxis.labelTextStyle = textStyle;
    axisSet.yAxis.labelOffset = 3.0f;


    //set axes' line styles and interval ticks
    CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
    lineStyle.lineColor = [CPTColor whiteColor];
    lineStyle.lineWidth = 3.0f;
    axisSet.xAxis.axisLineStyle = lineStyle;
    axisSet.yAxis.axisLineStyle = lineStyle;
    axisSet.xAxis.majorTickLineStyle = lineStyle;
    axisSet.yAxis.majorTickLineStyle = lineStyle;
    axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(5.0f);
    axisSet.yAxis.majorIntervalLength = CPTDecimalFromFloat(5.0f);
    axisSet.xAxis.majorTickLength = 7.0f;
    axisSet.yAxis.majorTickLength = 7.0f;
    axisSet.xAxis.minorTickLineStyle = lineStyle;
    axisSet.yAxis.minorTickLineStyle = lineStyle;
    axisSet.xAxis.minorTicksPerInterval = 1;
    axisSet.yAxis.minorTicksPerInterval = 1;
    axisSet.xAxis.minorTickLength = 5.0f;
    axisSet.yAxis.minorTickLength = 5.0f;

    // Create bar plot and add it to the graph
    CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
    plot.dataSource = self;
    plot.delegate = self;
    plot.barWidth = [[NSDecimalNumber decimalNumberWithString:@"5.0"]
                     decimalValue];
    plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                      decimalValue];
    plot.barCornerRadius = 5.0;
    // Remove bar outlines
    CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
    borderLineStyle.lineColor = [CPTColor clearColor];
    plot.lineStyle = borderLineStyle;
    // Identifiers are handy if you want multiple plots in one graph
    plot.identifier = @"chocoplot";
    [self.graph addPlot:plot];

    }

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot
{
    if ( [plot.identifier isEqual:@"chocoplot"] )
        return [self.data count];

    return 0;
}

-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
    if ( [plot.identifier isEqual:@"chocoplot"] )
    {
        NSDictionary *bar = [self.data objectAtIndex:index];

        if(fieldEnum == CPTBarPlotFieldBarLocation)
            return [bar valueForKey:BAR_POSITION];
        else if(fieldEnum ==CPTBarPlotFieldBarTip)
            return [bar valueForKey:BAR_HEIGHT];
    }
    return [NSNumber numberWithFloat:0];
}


-(CPTLayer *)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)index
{
    if ( [plot.identifier isEqual: @"chocoplot"] )
    {
        CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
        textStyle.fontName = @"Helvetica";
        textStyle.fontSize = 14;
        textStyle.color = [CPTColor whiteColor];

        NSDictionary *bar = [self.data objectAtIndex:index];
//        CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [bar valueForKey:@"CATEGORY"]]];

        CPTTextLayer *label = [[CPTTextLayer alloc] initWithText:[NSString stringWithFormat:@"%@", [bar valueForKey:@"HEIGHT"]]];

        label.textStyle =textStyle;

        return label;
    }

    CPTTextLayer *defaultLabel = [[CPTTextLayer alloc] initWithText:@"Label"];
    return defaultLabel;

}

-(void)barPlot:(CPTBarPlot *)plot barWasSelectedAtRecordIndex:(NSUInteger)index
{
    NSLog(@"barWasSelectedAtRecordIndex %d", index);
}

-(CPTFill *)barFillForBarPlot:(CPTBarPlot *)barPlot
                  recordIndex:(NSUInteger)index
{
    if ( [barPlot.identifier isEqual:@"chocoplot"] )
    {
        NSDictionary *bar = [self.data objectAtIndex:index];
        CPTGradient *gradient = [CPTGradient gradientWithBeginningColor:[CPTColor whiteColor]
                                                            endingColor:[bar valueForKey:@"COLOR"]
                                                      beginningPosition:0.0 endingPosition:0.3 ];
        [gradient setGradientType:CPTGradientTypeAxial];
        [gradient setAngle:320.0];

        CPTFill *fill = [CPTFill fillWithGradient:gradient];

        return fill;

    }
    return [CPTFill fillWithColor:[CPTColor colorWithComponentRed:1.0 green:1.0 blue:1.0 alpha:1.0]];

}

@end

绘制 x 轴标签的主要代码: 这是 x 轴标签的代码:

 CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;

    //X labels
    int labelLocations = 0;
    NSMutableArray *customXLabels = [NSMutableArray array];
   NSArray *dates = [NSArray arrayWithObjects:@"2012-05-01", @"2012-05-02", @"2012-05-03",
             @"2012-05-04", @"2012-05-05", @"2012-05-06", @"2012-05-07", nil];
    for (NSString *day in dates) {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:day textStyle:axisSet.xAxis.labelTextStyle];
        newLabel.tickLocation   = [[NSNumber numberWithInt:labelLocations] decimalValue];
        newLabel.offset         = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;
     //   newLabel.rotation       = M_PI / 4;
        [customXLabels addObject:newLabel];
        labelLocations++;

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

最佳答案

你试过吗this ?您必须正确处理填充。

graph.plotAreaFrame.paddingBottom=40;

关于ios - coreplot 中的 x 轴标签未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14110913/

相关文章:

iphone - 将资源预加载到 NSURLCache 中

uiview - 从 parentViewController 中删除后,childViewController 未被释放

iphone - 是否可以使用以前的 SDK (iOS5) 构建适用于 iPhone 5 屏幕尺寸的应用程序?

ios - 在 tableView.deselectRowAtIndexPath 中使用 indexPath 与 indexPath.row

ios - UIAlertView 上的 EXC_BAD_ACCESS

ios - 为什么 AVAssetWriter 会膨胀视频文件?

ios - CGAffineTransformMakeRotation 不调整 iOS 6 中的 View 大小

IOS 设置 UIViewController.view.frame = CGCGRect(x : -200. .) 不能点击按钮

ios - 为什么double类型的主窗口是可选的?

ios - iPad Mini 上的 UIImage 渲染不清晰