0

尽管花费了无数个小时检查代码行,并且在创建了一个功能齐全的散点图之后,我还是无法让条形图显示在我的条形图上。

我正在使用 Core Plot,我确信我已经导入了 core plot 库并正确设置了 core plot 环境,因为散点图完全正常工作。

有人可以帮我拔掉头发,也许可以告诉我哪里出错了?我有一种感觉,因为我的 #define BAR_POSITION @"POSITION" 和 #define BAR_HEIGHT @"HEIGHT" 没有正确设置/调用。

下面是我用于头文件和主文件的代码。

任何和所有的帮助表示赞赏。

头文件:

#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"

@interface BarPlotViewController : UIViewController 
<CPTBarPlotDataSource, CPTBarPlotDelegate> {

}

@property (nonatomic, retain) NSMutableArray *data;
@property (nonatomic, retain) CPTGraphHostingView *hostingView;
@property (nonatomic, retain) CPTXYGraph *graph;

- (void) generateBarPlot;

@end

主文件:

#import "BarPlotViewController.h"

@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};
    NSLog(@"After heights initialised.");


    int bar_positions[] = {10,20,30,40};
    NSLog(@"After position initialised.");


    UIColor *colors[] = {
        [UIColor redColor],
        [UIColor blueColor],
        [UIColor orangeColor],
        [UIColor purpleColor]};
    NSLog(@"After colors initialised.");


    NSString *categories[] = {@"A", @"B", @"C", @"D"};
    NSLog(@"After categories initialised.");


    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];
        //double position = bar_positions[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];

        NSLog(@"Data entered into bar dictionary.");

        //NSString *positionStringValue = [NSString stringWithFormat:@"%.02f", position];
        //NSString *log = [@"Position " stringByAppendingFormat: positionStringValue];
        //NSLog( log );

        //NSString *colorsStringValue = [NSString stringWithFormat:@"%.02f", position];
        //NSString *log2 = [@"Colors " stringByAppendingFormat: colors[i]];
        //NSLog( log2 );

    }
    [self generateBarPlot];
    NSLog(@"After generate bar plot.");
}
return self;
}



- (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.graph = [[CPTXYGraph alloc] initWithHostingView:_graphHostingView];

[self.hostingView setHostedGraph:self.graph];
//self.scatterPlot = [[TUTSimpleScatterPlot alloc] initWithHostingView:_graphHostingView andData:data];
//[self.scatterPlot initialisePlot];



//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]];


// Gets rid of decimal on years
NSNumberFormatter *labelFormatter = [[NSNumberFormatter alloc] init];
labelFormatter.maximumFractionDigits = 0;


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



CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
//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 = @"A";
axisSet.yAxis.title = @"B";
axisSet.xAxis.titleTextStyle = textStyle;
axisSet.yAxis.titleTextStyle = textStyle;
axisSet.xAxis.titleOffset = 30.0f;
axisSet.yAxis.titleOffset = 40.0f;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.yAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
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 = 10.0f;
axisSet.yAxis.majorTickLength = 10.0f;
axisSet.xAxis.minorTickLineStyle = lineStyle;
axisSet.yAxis.minorTickLineStyle = lineStyle;
axisSet.xAxis.minorTicksPerInterval = 1;
axisSet.yAxis.minorTicksPerInterval = 1;
axisSet.xAxis.minorTickLength = .0f;
axisSet.yAxis.minorTickLength = .0f;
axisSet.xAxis.labelFormatter = labelFormatter;
axisSet.yAxis.labelFormatter = labelFormatter;



// Create bar plot and add it to the graph
CPTBarPlot *plot = [[CPTBarPlot alloc] init] ;
plot.dataSource = self;
plot.delegate = self;
plot.barWidth = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                 decimalValue];
plot.barOffset = [[NSDecimalNumber decimalNumberWithString:@"10.0"]
                  decimalValue];
plot.barCornerRadius = 5.0;


// Remove bar outlines
CPTMutableLineStyle *borderLineStyle = [CPTMutableLineStyle lineStyle];
borderLineStyle.lineColor = [CPTColor greenColor];
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];
        NSLog(@"bar position before");
        return [NSNumber numberWithDouble:30];
        NSLog(@"bar position after");
    }
    else if(fieldEnum ==CPTBarPlotFieldBarTip) {
        //return [bar valueForKey:BAR_HEIGHT];
        NSLog(@"bar height before");
        return [NSNumber numberWithDouble:20];
        NSLog(@"bar height before");
    }
}
NSLog(@"numberForPlot return before");
return [NSNumber numberWithFloat:0];
NSLog(@"numberForPlot return after");
}


-(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"]]];
    label.textStyle =textStyle;

    return label;
}

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

}



-(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
4

2 回答 2

0

你确定你的 xrange 设置正确吗?我还没有调试过代码,但它很容易被忽略。

于 2012-07-23T14:36:47.343 回答
0

You need to give the #define AXIS_START -xx so that you can show up

于 2012-07-24T06:51:16.540 回答