使用核心绘图的iPhone,在X轴上绘制date

我有一个包含NSDateNSNumber值的字典数组。 我想在X轴上绘制date。 为了绘图,我需要提供xRanges来绘制一些小数值。 我不明白我怎么能提供NSDate值xRange(低和长)。

在这个方法中应该有什么:

 -(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 

我的意思是如何将我的date值作为NSNumber返回? 我想我应该在那边用一些时间间隔,但是应该是什么样的确切转换? 任何人都可以解释一下在xAxis上绘制date的具体要求是什么?

我正在绘制我的情节一半的看法。

在这里,你可以使用iOS上的DatePlot。 不要忘记检查它是正确的。

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // If you make sure your dates are calculated at noon, you shouldn't have to // worry about daylight savings. If you use midnight, you will have to adjust // for daylight savings time. NSDate *refDate = [NSDate dateWithTimeIntervalSinceReferenceDate:31556926 * 10]; NSTimeInterval oneDay = 24 * 60 * 60; // Invert graph view to compensate for iOS coordinates CGAffineTransform verticalFlip = CGAffineTransformMakeScale(1,-1); self.view.transform = verticalFlip; // allocate the graph within the current view bounds graph = [[CPTXYGraph alloc] initWithFrame: self.view.bounds]; // assign theme to graph CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme]; [graph applyTheme:theme]; // Setting the graph as our hosting layer CPTGraphHostingView *hostingView = [[CPTGraphHostingView alloc] initWithFrame:self.view.bounds]; [self.view addSubview:hostingView]; hostingView.hostedGraph = graph; graph.paddingLeft = 20.0; graph.paddingTop = 20.0; graph.paddingRight = 20.0; graph.paddingBottom = 150.0; // setup a plot space for the plot to live in CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; NSTimeInterval xLow = 0.0f; // sets the range of x values plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow) length:CPTDecimalFromFloat(oneDay*5.0f)]; // sets the range of y values plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(5)]; // plotting style is set to line plots CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle]; lineStyle.lineColor = [CPTColor blackColor]; lineStyle.lineWidth = 2.0f; // X-axis parameters setting CPTXYAxisSet *axisSet = (id)graph.axisSet; axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(oneDay); axisSet.xAxis.minorTicksPerInterval = 0; axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"1"); //added for date, adjust x line axisSet.xAxis.majorTickLineStyle = lineStyle; axisSet.xAxis.minorTickLineStyle = lineStyle; axisSet.xAxis.axisLineStyle = lineStyle; axisSet.xAxis.minorTickLength = 5.0f; axisSet.xAxis.majorTickLength = 7.0f; axisSet.xAxis.labelOffset = 3.0f; // added for date NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.dateStyle = kCFDateFormatterShortStyle; CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease]; timeFormatter.referenceDate = refDate; axisSet.xAxis.labelFormatter = timeFormatter; // Y-axis parameters setting axisSet.yAxis.majorIntervalLength = CPTDecimalFromString(@"0.5"); axisSet.yAxis.minorTicksPerInterval = 2; axisSet.yAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(oneDay); // added for date, adjusts y line axisSet.yAxis.majorTickLineStyle = lineStyle; axisSet.yAxis.minorTickLineStyle = lineStyle; axisSet.yAxis.axisLineStyle = lineStyle; axisSet.yAxis.minorTickLength = 5.0f; axisSet.yAxis.majorTickLength = 7.0f; axisSet.yAxis.labelOffset = 3.0f; // This actually performs the plotting CPTScatterPlot *xSquaredPlot = [[[CPTScatterPlot alloc] init] autorelease]; CPTMutableLineStyle *dataLineStyle = [CPTMutableLineStyle lineStyle]; //xSquaredPlot.identifier = @"X Squared Plot"; xSquaredPlot.identifier = @"Date Plot"; dataLineStyle.lineWidth = 1.0f; dataLineStyle.lineColor = [CPTColor redColor]; xSquaredPlot.dataLineStyle = dataLineStyle; xSquaredPlot.dataSource = self; CPTPlotSymbol *greenCirclePlotSymbol = [CPTPlotSymbol ellipsePlotSymbol]; greenCirclePlotSymbol.fill = [CPTFill fillWithColor:[CPTColor greenColor]]; greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0); xSquaredPlot.plotSymbol = greenCirclePlotSymbol; // add plot to graph [graph addPlot:xSquaredPlot]; // Add some data NSMutableArray *newData = [NSMutableArray array]; NSUInteger i; for ( i = 0; i < 5; i++ ) { NSTimeInterval x = oneDay*i; id y = [NSDecimalNumber numberWithFloat:1.2*rand()/(float)RAND_MAX + 1.2]; [newData addObject: [NSDictionary dictionaryWithObjectsAndKeys: [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX], y, [NSNumber numberWithInt:CPTScatterPlotFieldY], nil]]; NSLog(@"%@",newData); } plotData = [newData retain]; } #pragma mark - Plot Data Source Methods -(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot { return plotData.count; } -(NSNumber *)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index { NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]]; return num; } 

看一下示例文件夹中的DatePlot程序。 它显示如何将轴标签格式化为date。