如何使用Core plot在X轴上绘制NSDate

jee*_*ngs 5 iphone graph objective-c nsdate core-plot

我已经尝试了核心情节中的所有例子,esp dateplot示例,但我仍然坚持沿着x轴实现小时数小时.请帮我找出错误的地方.

我面临的问题:

  • 无法获得轴均匀分布的轴(每小时,0900,1000,......)
  • 当我将它存储到plotData数组中时,并且当绘制图形时,所有x值都是0.我的意思是,所有值仅绘制在y轴上.不存储x值.

代码如下:

NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:@"HH:mm a"];
 NSTimeInterval oneDay = 24 * 60 * 60;
 NSTimeInterval xLow = 0.0f;

//This part is used to set up the plot space.
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction=YES;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(xLow)
                                                 length:CPDecimalFromFloat(oneDay)];


    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x=axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromFloat(1*60*60);
x.minorTicksPerInterval = 0;
x.axisLineStyle = lineStyle;
x.majorTickLength = 7.0f;
//x.visibleRange=xAxisRange;

CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc]
                                   initWithDateFormatter:dateFormatter1] autorelease];
 Chirp *retValue=[someTemp objectAtIndex:0];
NSDate *tempDate=retValue.startingAt;
timeFormatter.referenceDate = tempDate;
x.labelFormatter = timeFormatter;
x.orthogonalCoordinateDecimal=CPDecimalFromString(@"0");


  NSMutableArray *newData = [NSMutableArray array];
NSUInteger i;
for ( i = 0; i < [someTemp count]; i++ ) 
{
    Chirp *retValue=[[Chirp alloc]init];
    retValue=[someTemp objectAtIndex:i];
    NSDate *tempDate=retValue.startingAt;
    NSString *dateFromString=[dateFormatter1 stringFromDate:tempDate];
    NSLog(@"%@", dateFromString);
    id x=dateFromString;
    id y=[NSDecimalNumber numberWithFloat:[retValue.chipCount floatValue]];
    [newData addObject:
     [NSDictionary dictionaryWithObjectsAndKeys:
      x, [NSNumber numberWithInt:CPScatterPlotFieldX], 
      y, [NSNumber numberWithInt:CPScatterPlotFieldY], 
      nil]];
}
plotData = newData;


-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
return plotData.count;
}

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}
Run Code Online (Sandbox Code Playgroud)

Rah*_*yas 2

您需要为 x 轴创建自定义标签。请参阅 core-plot 的条形图示例。它有自定义标签。这样你就可以显示时间、日期或任何你想要的东西。