Is it possible to create a date plot with irregular date intervals

92 views
Skip to first unread message

Sam Kochukalikkal

unread,
Mar 27, 2012, 9:16:19 PM3/27/12
to coreplot...@googlegroups.com
Hi,

Sorry the long post.

I'm trying to plot a graph that has irregular date intervals. Is it possible to plot this date using the format used in the dateplot example. I tried it but the x-axis date labels are incorrect. I believe this is because the dates are not in regular intervals. Below is my code

- (void)createChart
{
// 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.
    int nLastIndex = [[self.resultDetailsList reports] count] - 1;
    HistoricalReportDetail *reportDetail = (HistoricalReportDetail *)[[self.resultDetailsList reports] objectAtIndex:nLastIndex];
NSDate *startDate  = [reportDetail collectionDate];
    reportDetail = (HistoricalReportDetail *)[[self.resultDetailsList reports] objectAtIndex:0];
    NSDate *endDate = [reportDetail collectionDate];
    
// Create graph from theme
_graph = [(CPTXYGraph *)[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTTheme *theme = [CPTTheme themeNamed:kCPTDarkGradientTheme];
[_graph applyTheme:theme];
    
    
    _graph.paddingLeft = 10.0;
    _graph.paddingTop = 10.0;
    _graph.paddingRight = 10.0;
    _graph.paddingBottom = 10.0;
    
    CPTGraphHostingView *hostingView = (CPTGraphHostingView *)[self ChartView];
    hostingView.collapsesLayers = NO;
hostingView.hostedGraph = _graph;
    hostingView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
// Setup scatter plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)_graph.defaultPlotSpace;
NSTimeInterval xLow  = [startDate timeIntervalSinceReferenceDate];
    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:xLow];
    NSLog (@"Date Before = %@, Date After = %@", startDate, date);
    
    NSTimeInterval xHigh = [endDate timeIntervalSinceReferenceDate];
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(xLow - (xHigh - xLow)/10.0f) length:CPTDecimalFromFloat(xHigh - xLow + (xHigh - xLow)/10.0f)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(-2.0) length:CPTDecimalFromFloat(4)];
    
// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)_graph.axisSet;
CPTXYAxis *x  = axisSet.xAxis;
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0.0");
x.minorTicksPerInterval  = 0;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = startDate;
x.labelFormatter = timeFormatter;
    x.title = @"Date";
    x.labelRotation = M_PI/4;
    x.titleOffset = 55.5f;
    x.majorIntervalLength = CPTDecimalFromFloat((xHigh - xLow)/10.0f);
    x.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
    
CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength  = CPTDecimalFromString(@"0.5");
y.minorTicksPerInterval  = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(xLow);
    y.labelingPolicy = CPTAxisLabelingPolicyNone;
    
// Define some custom labels for the data elements
NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:-1], [NSDecimalNumber numberWithInt:0], [NSDecimalNumber numberWithInt:1],nil];
NSArray *yAxisLabels = [NSArray arrayWithObjects:@"Low", @"Mean", @"High", nil];
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[yAxisLabels count]];
for ( NSNumber *tickLocation in customTickLocations ) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[yAxisLabels objectAtIndex:labelLocation++] textStyle:y.labelTextStyle];
newLabel.tickLocation = [tickLocation decimalValue];
newLabel.offset  = y.labelOffset + y.majorTickLength;
newLabel.rotation  = M_PI / 2;
[customLabels addObject:newLabel];
}
y.axisLabels = [NSSet setWithArray:customLabels];

    CPTFill *bandFill = [CPTFill fillWithColor:[[CPTColor greenColor] colorWithAlphaComponent:0.3]];
    CPTFill *bandFill2 = [CPTFill fillWithColor:[[CPTColor redColor] colorWithAlphaComponent:0.3]];
    [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-1.0) length:CPTDecimalFromDouble(2.0)] fill:bandFill]];
    [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(-2.0) length:CPTDecimalFromDouble(1.0)] fill:bandFill2]];
    [y addBackgroundLimitBand:[CPTLimitBand limitBandWithRange:[CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(1.0) length:CPTDecimalFromDouble(1.0)] fill:bandFill2]];
    
    // Create a plot that uses the data source method
CPTScatterPlot *boundLinePlot  = [[CPTScatterPlot alloc] init];
CPTMutableLineStyle *lineStyle = [CPTMutableLineStyle lineStyle];
lineStyle.miterLimit = 1.0f;
lineStyle.lineWidth = 3.0f;
lineStyle.lineColor = [CPTColor blueColor];
boundLinePlot.dataLineStyle = lineStyle;
boundLinePlot.identifier = @"Blue Plot";
boundLinePlot.dataSource = self;
[_graph addPlot:boundLinePlot];
    
// Add plot symbols
CPTMutableLineStyle *symbolLineStyle = [CPTMutableLineStyle lineStyle];
symbolLineStyle.lineColor = [CPTColor blackColor];
CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
plotSymbol.fill = [CPTFill fillWithColor:[CPTColor blueColor]];
plotSymbol.lineStyle = symbolLineStyle;
plotSymbol.size = CGSizeMake(10.0, 10.0);
boundLinePlot.plotSymbol = plotSymbol;
    
// Add some data
NSMutableArray *newData = [NSMutableArray array];
NSUInteger i;
    NSArray *reports = self.resultDetailsList.reports;
    
for ( i = 0; i < [reports count]; i++ ) {
        HistoricalReportDetail *hr = (HistoricalReportDetail *) [reports objectAtIndex:i];
id y = [NSDecimalNumber numberWithFloat:[[hr testNormalizedResultValue] floatValue]];
        NSTimeInterval x = [[hr collectionDate] timeIntervalSinceReferenceDate];
        NSLog(@"Date before = %@, Date after = %@", [hr collectionDate], [NSDate dateWithTimeIntervalSinceReferenceDate:x]);
[newData addObject:
[NSDictionary dictionaryWithObjectsAndKeys:
 [NSDecimalNumber numberWithFloat:x], [NSNumber numberWithInt:CPTScatterPlotFieldX],
 y, [NSNumber numberWithInt:CPTScatterPlotFieldY],
 nil]];
}
    self.dataForPlot = newData;
}

Data:

X: 2011-08-21 Y: 0.99
X: 2011-08-22 Y: 0.99
X: 2011-08-22 Y: -2
X: 2011-08-23 Y: 0.09
X: 2011-08-23 Y: -2
X: 2011-08-24 Y: 0.99
X: 2011-08-25 Y: 0.09
X: 2011-08-29 Y: 0.09

As you can see the plot matches the data but the x-axis labels are wrong. Any help would be much appreciated.

thx
Sam

iOS Simulator Screen shot 2012-03-27 6.07.25 PM.png

Sam Kochukalikkal

unread,
Mar 29, 2012, 1:00:27 AM3/29/12
to coreplot...@googlegroups.com
Got it to work. I just removed reference date assignment to the CPTTimeFormatter. Thanks for a wonderful and seamless charting tool.

thx
sam

Tik Sathaphorn

unread,
Jan 6, 2017, 10:12:51 PM1/6/17
to coreplot-discuss
Hi Same, 
I have the same problem too but i get idea from your code .
Can i have your code on 

- (double)doubleForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx {}

Thanks
Reply all
Reply to author
Forward
0 new messages