2012-12-29 464 views
0

這個核心劇情創建於:http://codejunkster.wordpress.com/2011/11/23/core-plot-2-bar-plot/#comment-85。我已經創建了2個應用程序使用相同的代碼,一個用於iPhone,另一個用於iPad。iPhone應用程序完美工作,但ipad應用程序顯示x座標和y座標以及標題,而不是列我不知道如何修復它。請幫助我。這將是一個很大的幫助。使用Xcode 4.5.2 ios6。感謝提前。這個核心劇情代碼是在iphone上工作,但不是在ipad上

BarPlotViewController.h

#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 

BarPlotViewController.m

#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)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.yRange = [CPTPlotRange plotRangeWithLocation: 
         CPTDecimalFromFloat(AXIS_START) 
                length:CPTDecimalFromFloat((AXIS_END - AXIS_START)+5)]; 

    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 = @"CHOCOLATE"; 
    axisSet.yAxis.title = @"AWESOMENESS"; 
    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; 
} 


-(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:@"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]]; 

} 



- (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. 
} 

@end 

AppDelegate.h

#import <UIKit/UIKit.h> 
#import "BarPlotViewController.h" 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (strong, nonatomic) BarPlotViewController *barPlotViewController; 

@end 

AppDelegate.m

#import "AppDelegate.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize barPlotViewController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 

    barPlotViewController = [[BarPlotViewController alloc] init]; 
    [self.window setRootViewController:barPlotViewController]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 
@end 

在iPad模擬器的圖片:enter image description here

這是它的外觀上iPhone相同的代碼:enter image description here

回答

1

你的數據源需要實現以下方法之一:

-(NSArray *)numbersForPlot:(CPTPlot *)plot 
        field:(NSUInteger)fieldEnum 
      recordIndexRange:(NSRange)indexRange; 

-(NSNumber *)numberForPlot:(CPTPlot *)plot 
        field:(NSUInteger)fieldEnum 
       recordIndex:(NSUInteger)idx; 

-(double *)doublesForPlot:(CPTPlot *)plot 
        field:(NSUInteger)fieldEnum 
     recordIndexRange:(NSRange)indexRange; 

-(double)doubleForPlot:(CPTPlot *)plot 
       field:(NSUInteger)fieldEnum 
      recordIndex:(NSUInteger)idx; 

-(CPTNumericData *)dataForPlot:(CPTPlot *)plot 
         field:(NSUInteger)fieldEnum 
       recordIndexRange:(NSRange)indexRange; 

-(CPTNumericData *)dataForPlot:(CPTPlot *)plot 
       recordIndexRange:(NSRange)indexRange; 
+0

感謝您的回覆。我應該在下面的代碼中替換什麼,或者上面的代碼中的最終代碼如何。請修改上面的代碼,我仍然感到困惑。感謝提前Eric.And爲什麼它在iphone中工作? –

+0

我不知道你的應用如何在沒有這些方法的任何平臺上工作。查看示例代碼的任何Core Plot示例應用程序。 '-numberForPlot:field:recordIndex:'在這些例子中最常見。正確的選擇取決於您的數據如何存儲在模型中。 –

+0

感謝您回覆。我已經更新了上面我的問題的圖片,請查看它並告訴我問題出在哪裏?我已經嘗試了從最後1周開始的所有事情,但是我堅持不了,因爲沒有好的定製核心教程,這是一種迫切的要求。再次感謝Eric。 –

0

我已經改變所有的設置,除了目標 - >設備 - > iPad,然後它開始工作。這是有史以來最糟糕的錯誤和可怕的時間浪費。

相關問題