我刚刚开始使用Core Plot,并且测试中有一个CPTGraphHostingView
嵌入在一个简单的自定义视图控制器中,绘制来自Core Data fetchRequest的值(它是一个应用程序图表,每天摄取的卡路里摄入量)。 / p>
代码主要是从教程here中粘贴的。
问题是当将视图控制器推入视图(它嵌入在导航控制器中)时,UI会冻结大约两秒钟。这是在设备上运行时(iPhone 4S)。
“仪器”中的分析显示主要线程被[CPTAxis layoutSublayers]
和[CPTLayer drawInContext]
阻止。
滞后不是由于数据集过大:它目前只包含两个点。该图表非常简单,如下所示:
完成视图控制器的实现:
接口:
//
// ICCaloriesGraphController.h
// iCalories
//
// Created by David Fearon on 22/08/2013.
// Copyright (c) 2013 David Fearon. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#import "ICDatabaseBrain.h"
#import "Day.h"
#import "CalorieEntry.h"
@interface ICCaloriesGraphController : UIViewController<CPTPlotDataSource, NSFetchedResultsControllerDelegate>
@property (weak, nonatomic) IBOutlet CPTGraphHostingView *graphHostingView;
@property (nonatomic, retain)ICDatabaseBrain* sharedDatabaseBrain;
@property (nonatomic, retain)NSFetchedResultsController* resultsController;
@end
实现:
//
// ICCaloriesGraphController.m
// iCalories
//
// Created by David Fearon on 22/08/2013.
// Copyright (c) 2013 David Fearon. All rights reserved.
//
#import "ICCaloriesGraphController.h"
@interface ICCaloriesGraphController ()
@property int numberOfRecordsForPlot;
@property float maxDailyCaloriesInDataset;
@property NSArray* days;
@end
@implementation ICCaloriesGraphController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.sharedDatabaseBrain = [ICDatabaseBrain sharedDatabaseBrain];
self.resultsController = self.sharedDatabaseBrain.fetchDaysResultsController;
self.days = [self.resultsController fetchedObjects];
self.numberOfRecordsForPlot = [self.days count];
[self updateMaxDailyCaloriesInDataset];
// Create a CPTGraph object and add to hostView
CPTGraph* graph = [[CPTXYGraph alloc] initWithFrame:self.graphHostingView.bounds];
self.graphHostingView.hostedGraph = graph;
// Get the (default) plotspace from the graph so we can set its x/y ranges
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) self.graphHostingView.hostedGraph.defaultPlotSpace;
// Note that these CPTPlotRange are defined by START and LENGTH (not START and END) !!
[plotSpace setYRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( self.maxDailyCaloriesInDataset )]];
[plotSpace setXRange: [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat( 0 ) length:CPTDecimalFromFloat( self.numberOfRecordsForPlot )]];
// Create the plot (we do not define actual x/y values yet, these will be supplied by the datasource...)
CPTScatterPlot* plot = [[CPTScatterPlot alloc] initWithFrame:CGRectZero];
// Let's keep it simple and let this class act as datasource (therefore we implement <CPTPlotDataSource>)
plot.dataSource = self;
// Finally, add the created plot to the default plot space of the CPTGraph object we created before
[self.graphHostingView.hostedGraph addPlot:plot toPlotSpace:self.graphHostingView.hostedGraph.defaultPlotSpace];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plotnumberOfRecords {
return self.numberOfRecordsForPlot;
}
-(void)updateMaxDailyCaloriesInDataset{
if (self.numberOfRecordsForPlot == 0){
self.maxDailyCaloriesInDataset = 0;
return;
}
int max = 0;
for (Day* day in self.days) {
//count the calories in the day:
int calorieSum = 0;
for (CalorieEntry* calorieEntry in [day.calorieEntries allObjects]) {
calorieSum += calorieEntry.calories.intValue;
}
if(calorieSum > max) max = calorieSum;
}
self.maxDailyCaloriesInDataset = max;
}
-(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
if (self.numberOfRecordsForPlot == 0){
return 0;
}
if(fieldEnum == CPTScatterPlotFieldY){
Day* day = [self.days objectAtIndex:index];
//count the calories in the day:
int calorieSum = 0;
for (CalorieEntry* calorieEntry in [day.calorieEntries allObjects]) {
calorieSum += calorieEntry.calories.intValue;
}
return [NSNumber numberWithInt:calorieSum];
//TODO: Set range for y axis
}
if(fieldEnum == CPTScatterPlotFieldX){
return [NSNumber numberWithInt: index];
}else{
NSLog(@"fieldEnum was neither CPTScatterPlotFieldY or CPTScatterPlotFieldX in numberForPlot:");
return 0;
}
}
@end
滞后绝对与核心数据代码无关;一切正常。谷歌搜索只显示巨大的数据集的问题,而不是只有两点的数据集。显然我正在使用Core Plot代码做一些根本性的错误,但实际上看不到什么。
有人能发现问题吗?
答案 0 :(得分:1)
maxDailyCaloriesInDataset
大吗?默认轴标签策略将刻度线和标签沿轴分开一个单位。如果yRange
很大,则会产生大量重叠标签,这可能会非常慢。根据应用程序的要求,增加majorIntervalLength
以使其不会生成这么多标签,或更改标签政策。 CPTAxisLabelingPolicyAutomatic
和CPTAxisLabelingPolicyEqualDivisions
将是不错的选择。