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. Please help me find out where i am going wrong.
Problems I am facing:
- Not able to get the axis with hours distributed evenly (hourly basis, 0900, 1000,....)
- When i am storing it into the plotData array , and when the graph is drawn, all the x values are 0. i mean, all the values are plotted on the y axis only. The x values are not stored.
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 *plotSpac开发者_运维技巧e = (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. I had made my x axis use custom labels ( as suggested by 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. 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.
-(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. see core-plot's bar chart sample. It has custom labels. that way you can show hour,date or anything else you want.
精彩评论