[英]How to plot NSDate along X axis using Core plot
I have tried going through all the examples in core plot, esp dateplot example, but still i am stuck in implementing the plot with hours along the x- axis. 我已经尝试过核心绘图中的所有示例,特别是dateplot示例,但是我仍然停留在沿x轴使用小时的绘图中。 Please help me find out where i am going wrong.
请帮助我找出我要去哪里。
Problems I am facing: 我面临的问题:
The code is as follows: 代码如下:
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;
}
Never mind...! 没关系...! I found the solution to the qn.
我找到了解决qn的方法。 I had made my x axis use custom labels ( as suggested by Rahul Vyas) .
我已经使我的x轴使用自定义标签(如Rahul Vyas所建议)。 This solved by one of the problem i was facing.
这由我面临的问题之一解决。 And for the other problem, I tried to get the string from NSDate using NSDateformatter and from the string.
对于另一个问题,我尝试使用NSDateformatter从NSDate和字符串中获取字符串。 I am posting the code below.
我在下面发布代码。 Hope it helps some one.
希望它能帮助一些人。 The code for having custom labels with time on x axis, which is already there in bar chart example.
条形图示例中已经存在用于在x轴上带有时间的自定义标签的代码。
-(void)configureXAxisForGraph:(CPXYGraph*)graphForAxis
{
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x=axisSet.xAxis;
x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];
CPPlotRange *xAxisRange=[CPPlotRange plotRangeWithLocation:CPDecimalFromString(@"0.0") length:CPDecimalFromString(@"24.0")];
x.minorTicksPerInterval = 4;
x.majorTickLineStyle = lineStyle;
x.minorTickLineStyle = lineStyle;
x.axisLineStyle = lineStyle;
x.minorTickLength = 5.0f;
x.majorTickLength = 7.0f;
x.visibleRange=xAxisRange;
x.orthogonalCoordinateDecimal=CPDecimalFromString(@"0");
x.title=@"Hours";
x.titleOffset=47.0f;
x.labelRotation=M_PI/4;
x.labelingPolicy=CPAxisLabelingPolicyNone;
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:3], [NSDecimalNumber numberWithInt:6], [NSDecimalNumber numberWithInt:9], [NSDecimalNumber numberWithInt:12],
[NSDecimalNumber numberWithInt:15], [NSDecimalNumber numberWithInt:18], [NSDecimalNumber numberWithInt:21], [NSDecimalNumber numberWithInt:24],nil];
NSArray *xAxisLabels = [NSArray arrayWithObjects:@"3 AM", @"6 AM", @"9 AM", @"12PM", @"3 PM",@"6 PM",@"9 PM", @"12 AM", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations)
{
CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset = x.labelOffset + x.majorTickLength;
newLabel.rotation = M_PI/4;
[customLabels addObject:newLabel];
[newLabel release];
}
x.axisLabels = [NSSet setWithArray:customLabels];
}
Here is the code that worked for me to plot values against number and time(hour). 这是对我有用的代码,用于针对数字和时间(小时)绘制值。
self.dataToBePlotted = [NSMutableArray array];
NSUInteger i;
for ( i = 0; i < [dummyDataArray count]; i++ )
{
Chirp *retValue=[[Chirp alloc]init];
retValue=[dummyDataArray objectAtIndex:i];
NSDate *tempDate=retValue.startingAt;
NSString *dateFromString=[dateFormatter1 stringFromDate:tempDate];
NSLog(@"%@", dateFromString);
id x=dateFromString;
id y=[NSDecimalNumber numberWithFloat:[retValue.chipCount floatValue]];
[self.dataToBePlotted addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
x, [NSNumber numberWithInt:CPScatterPlotFieldX],
y, [NSNumber numberWithInt:CPScatterPlotFieldY],
nil]];
}
self.plotData = self.dataToBePlotted;
-(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;
}
you need to create custom labels for the x-axis. 您需要为x轴创建自定义标签。 see core-plot's bar chart sample.
请参阅核心图的条形图示例。 It has custom labels.
它具有自定义标签。 that way you can show hour,date or anything else you want.
这样,您可以显示小时,日期或您想要的其他任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.