2012-07-23 103 views
0

儘管花了無數小時檢查代碼行,並且在創建完整功能的散點圖後,我無法在條形圖上顯示條形圖。核心繪圖條形圖條不會顯示

我正在使用核心繪圖,我相信我已經導入了核心繪圖庫並在散點圖完全正常工作時正確設置了核心繪圖環境。

請問有人能救我拔出我的頭髮,也許告訴我我要去哪裏?我有一種感覺,因爲我的#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 
+0

您是否嘗試過用把一些價值,而不是BAR_POSITION和BAR_HEIGHT? – parilogic 2012-07-23 11:58:37

+0

贊#define BAR_POSITION 10.0 – parilogic 2012-07-23 11:59:30

+0

你好,感謝你的回覆!我試過了,只要應用程序試圖在模擬器中加載,我就會得到THREAD 1 EXC_BAD_ACCESS。 – 2012-07-23 13:09:26

回答

0

你確定你的x範圍設置正確。我沒有調試代碼,但很容易忽略。

+0

我想通了! if(fieldEnum == CPTBarPlotFieldBarLocation)不正確。只要我將它改爲if(fieldEnum == 2),它就進入了if語句,並且所有東西都落到了原位。感謝您的幫助! – 2012-07-24 10:50:08

0

你需要給的#define AXIS_START -xx,這樣就可以顯示出來

+0

我想通了! if(fieldEnum == CPTBarPlotFieldBarLocation)不正確。只要我將它改爲if(fieldEnum == 2),它就進入了if語句,並且所有東西都落到了原位。感謝您的幫助! – 2012-07-24 10:49:11