2015-08-26 74 views
0

相關視圖控制器爲H文件如何修正Coreplot餅圖問題64位版本VS 32位版本

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


@interface AttendenceViewController : UIViewController  <UITabBarDelegate,UITableViewDataSource,CPTLegendDelegate,CPTPieChartDataSource,CPTPieChartDelegate> 

@property (strong,nonatomic) StudentModel *studentA; 
@property (strong,nonatomic) NSDictionary *studentAttendanceDetails; 
@property (weak, nonatomic) IBOutlet UIView *graphContainer; 

用於相關視圖控制器

#import "AttendenceViewController.h" 

@interface AttendenceViewController() 
@property (nonatomic,strong) CPTGraphHostingView *hostView; 
@property (nonatomic,strong) CPTTheme *selectedTheme; 

-(void)initPlot; 
-(void)configureHost; 
-(void)configureGraph; 
-(void)configureChart; 
-(void)configureLegend; 
@end 

@implementation AttendenceViewController 

- (void)viewDidLoad { 
[super viewDidLoad]; 
// Do any additional setup after loading the view. 
// float attendanceTotal = [self.studentA.percentageAttendance floatValue]; 
// float authorisedAbsences = [self.studentA.authorisedAbsences floatValue]; 
// float unathorisedAbsences = [self.studentA.unautherisedAbsences floatValue]; 

self.studentAttendanceDetails = [NSDictionary dictionaryWithObjectsAndKeys:self.studentA.percentageAttendance,@"totalAttendance",self.studentA.authorisedAbsences,@"authorisedAbsences",self.studentA.unautherisedAbsences,@"unauthorisedAbsences", nil]; 
NSLog(@"%@",self.studentAttendanceDetails); 
} 

-(void)viewDidAppear:(BOOL)animated 
{ 
[super viewDidAppear:animated]; 
[self initPlot]; 

} 

#pragma mark Core Plot Initialisation Methods 
-(void)initPlot 
{ 
[self configureHost]; 
[self configureGraph]; 
[self configureChart]; 
[self configureLegend]; 

} 

-(void)configureHost; 
{ 
CGRect parentRect = self.graphContainer.bounds; 
self.hostView = [(CPTGraphHostingView*)[CPTGraphHostingView alloc]initWithFrame:parentRect]; 
self.hostView.allowPinchScaling=NO; 
[self.graphContainer addSubview:self.hostView]; 
} 
-(void)configureGraph; 
{ 
//create and initialise the graph 
CPTGraph *graph = [[CPTXYGraph alloc]initWithFrame:self.hostView.bounds]; 
self.hostView.hostedGraph=graph; 
graph.paddingLeft=0.0f; 
graph.paddingTop=0.0f; 
graph.paddingRight=0.0f; 
graph.paddingBottom=0.0f; 
graph.axisSet=nil; 
//set up the text styles 
CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle]; 
textStyle.color= [CPTColor grayColor]; 
textStyle.fontName [email protected]"Helvetica-Bold"; 
textStyle.fontSize=16.0f; 
//configure the title 
NSString *title = @"Student Attendance"; 
graph.title=title; 
graph.titleTextStyle=textStyle; 
graph.titlePlotAreaFrameAnchor= CPTRectAnchorTop; 
graph.titleDisplacement= CGPointMake(0.0f, -12.0f); 
self.selectedTheme = [CPTTheme themeNamed:kCPTPlainWhiteTheme]; 
[graph applyTheme:self.selectedTheme]; 

} 
-(void)configureChart; 
{ 
//get reference to graph 
CPTGraph *graph = self.hostView.hostedGraph; 
//create chart 
CPTPieChart *pieChart = [[CPTPieChart alloc]init]; 
pieChart.delegate=self; 
pieChart.dataSource=self; 
pieChart.pieRadius=(self.hostView.bounds.size.height*0.9)/3; 
pieChart.identifier= graph.title; 
pieChart.startAngle = M_PI_4; 
pieChart.sliceDirection=CPTPieDirectionClockwise; 
//gradient 
CPTGradient *overlayGradient = [[CPTGradient alloc]init]; 
overlayGradient.gradientType=CPTGradientTypeRadial; 
overlayGradient=[overlayGradient addColorStop:[[CPTColor blackColor]colorWithAlphaComponent:0.0] atPosition:0.9]; 
overlayGradient=[overlayGradient addColorStop:[[CPTColor blackColor]colorWithAlphaComponent:0.4] atPosition:1.0]; 
pieChart.overlayFill = [CPTFill fillWithGradient:overlayGradient]; 
[graph addPlot:pieChart]; 
} 
-(void)configureLegend; 
{ 

} 


#pragma mark Core Plot Datasource Methods 

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot 
{ 
return [self.studentAttendanceDetails count]; 
} 

-(NSNumber*)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx 
{ 
if (CPTPieChartFieldSliceWidth == fieldEnum) 
{ 
    NSArray *values = [self.studentAttendanceDetails allValues]; 
    return [values objectAtIndex:idx]; 

} 
return [NSDecimalNumber zero]; 
} 

-(CPTLayer*)dataLabelForPlot:(CPTPlot *)plot recordIndex:(NSUInteger)idx 
{ 
static CPTMutableTextStyle *labelText = nil; 
if (!labelText) { 
    labelText= [[CPTMutableTextStyle alloc] init]; 
    labelText.color = [CPTColor grayColor]; 
} 

NSString *labelValue = nil; 

switch (idx) { 

    case 0:{ 
     NSString *unauthorised= [self.studentAttendanceDetails objectForKey:@"unauthorisedAbsences"]; 
     NSLog(@"%@",unauthorised); 
     if ([unauthorised isEqualToString:@"0.00"]) { 
      labelText=nil; 
     } 
     labelValue = [NSString stringWithFormat:@"%@",[self.studentAttendanceDetails objectForKey:@"unauthorisedAbsences"]]; 
    } 
     break; 

    case 1: 
     labelValue = [NSString stringWithFormat:@"%@",[self.studentAttendanceDetails objectForKey:@"totalAttendance"]]; 
        break; 
     case 2: 
     labelValue = [NSString stringWithFormat:@"%@",[self.studentAttendanceDetails objectForKey:@"authorisedAbsences"]]; 

     break; 
      } 
return [[CPTTextLayer alloc] initWithText:labelValue style:labelText]; 


} 

-(NSString*)legendTitleForPieChart:(CPTPieChart *)pieChart recordIndex:(NSUInteger)idx 
{ 
return @"Student Attendance"; 
} 

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

圖像的M檔結果當模擬器被設置爲iPhone 5和運行enter image description here(即32位)

結果的圖像時模擬器被設置爲的iPhone 5S或更高(即64位)

enter image description here

的問題是如何我調整代碼在用於視圖控制器以確保輸出的執行文件相同的32位和64位運行?

回答

1

詞典沒有定義的順序。 allValues陣列在每個平臺上可能都不相同。對於這個應用程序,根本不需要在字典中存儲值。只需根據索引從數據源中返回authorisedAbsencesunautherisedAbsences即可。

+0

我會看看這個謝謝,但這並不能解釋爲什麼餅圖繪製的顏色已經改變了,或者它會改變嗎? – jon

+0

是的,因爲兩個平臺之間的實現差異可能會使'allValues'數組的順序不同。無論如何,這不是你應該依賴的。 –

相關問題