1
我正在試驗iOS的Core Plot庫。我設法用我的信息顯示圖表,但是我無法根據需要個性化可視化。
我正在嘗試使用簡單的散點圖來顯示用戶在兩個日期之間每天走過的距離。我已經設法創建了繪圖數據,但是我很難個性化情節,也許有人使用這個庫可以幫助我;)
我的問題是:
- 如果我只顯示積極的x軸和y軸兩側,我無法看到標籤。
- 例如,如果兩個日期之間有31天,則x軸從0到30.如何更改標籤以顯示日期,例如startDate + x。 這是我迄今所做的:核心 - 劇情:個性化劇情
@interface DistanceChart : CPGraphHostingView <CPPlotDataSource> {
CPXYGraph *graph;
NSMutableArray *dataForPlot;
RouteManager *routeManager;
NSDate *startDate;
NSDate *endDate;
}
@property (nonatomic, retain) NSMutableArray *dataForPlot;
@property (nonatomic, retain) RouteManager *routeManager;
@property (nonatomic, retain) NSDate *startDate;
@property (nonatomic, retain) NSDate *endDate;
- (void)initPlot;
@end
@implementation DistanceChart
@synthesize dataForPlot;
@synthesize routeManager;
@synthesize startDate;
@synthesize endDate;
- (void)initPlot {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
self.startDate = [dateFormatter dateFromString:@"01-05-2011"];
self.endDate = [dateFormatter dateFromString:@"31-05-2011"];
//get the data
self.dataForPlot = [routeManager getTotalDistanceByDay:routeManager.routes from:startDate to:endDate];
NSLog(@"Data: %@", dataForPlot);
graph = [[CPXYGraph alloc] initWithFrame:CGRectZero];
self.hostedGraph = graph;
graph.paddingLeft = 5;
graph.paddingTop = 5;
graph.paddingRight = 5;
graph.paddingBottom = 5;
// Setup plot space
int days = [endDate timeIntervalSinceDate:startDate]/60/60/24 + 1;
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) length:CPDecimalFromFloat(days)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(0) length:CPDecimalFromFloat(15)]; //TODO: set maximum Y according to max distance
// Axes
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromString(@"1");
x.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.minorTicksPerInterval = 1;
CPXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPDecimalFromString(@"1");
y.minorTicksPerInterval = 1;
y.orthogonalCoordinateDecimal = CPDecimalFromString(@"0");
x.labelRotation = M_PI/8;
// Create a green plot area
CPScatterPlot *dataSourceLinePlot = [[[CPScatterPlot alloc] init] autorelease];
dataSourceLinePlot.identifier = @"Green Plot";
CPMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease];
lineStyle.lineWidth = 2.f;
lineStyle.lineColor = [CPColor greenColor];
dataSourceLinePlot.dataLineStyle = lineStyle;
dataSourceLinePlot.dataSource = self;
// Put an area gradient under the plot above
CPColor *areaColor = [CPColor colorWithComponentRed:0.3 green:1.0 blue:0.3 alpha:0.8];
CPGradient *areaGradient = [CPGradient gradientWithBeginningColor:areaColor endingColor:[CPColor clearColor]];
areaGradient.angle = -90.0f;
CPFill *areaGradientFill = [CPFill fillWithGradient:areaGradient];
dataSourceLinePlot.areaFill = areaGradientFill;
dataSourceLinePlot.areaBaseValue = CPDecimalFromString(@"1.75");
// Animate in the new plot, as an example
dataSourceLinePlot.opacity = 0.0f;
dataSourceLinePlot.cachePrecision = CPPlotCachePrecisionDecimal;
[graph addPlot:dataSourceLinePlot];
CABasicAnimation *fadeInAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeInAnimation.duration = 1.0f;
fadeInAnimation.removedOnCompletion = NO;
fadeInAnimation.fillMode = kCAFillModeForwards;
fadeInAnimation.toValue = [NSNumber numberWithFloat:1.0];
[dataSourceLinePlot addAnimation:fadeInAnimation forKey:@"animateOpacity"];
}
#pragma mark -
#pragma mark Plot Data Source Methods
- (NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
return [dataForPlot count];
}
- (NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index
{
NSDecimalNumber *num = [[dataForPlot objectAtIndex:index] valueForKey:(fieldEnum == CPScatterPlotFieldX ? @"x" : @"y")];
return num;
}
- (CPLayer *)dataLabelForPlot:(CPPlot *)plot recordIndex:(NSUInteger)index
{
return nil;
}
- (void)dealloc {
[dataForPlot release];
[routeManager release];
[startDate release];
[endDate release];
[super dealloc];
}
@end
感謝您的幫助;)有很多例子,這幫助我很多。 – ffleandro 2011-05-18 21:35:48
@Eric Skroch我真的對你印象深刻!!1 – 2012-03-04 10:26:44