相关视图控制器的h文件
#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;
相关视图控制器的m文件
#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 =@"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.
}
将模拟器设置为iPhone 5并运行(即32位)时的结果图像
模拟器设置为iPhone 5s或更高版本(即64位)时的结果图像
问题是我如何调整视图控制器的实现文件中的代码,以确保32位和64位运行的输出相同?
答案 0 :(得分:1)
字典没有定义的顺序。每个平台上的allValues
数组可能不同。对于此应用程序,您根本不需要将值存储在字典中。只需根据索引从数据源返回authorisedAbsences
或unautherisedAbsences
。