mrd
mrd

Reputation: 4699

CorePlot: x-Axis with dates, only the first date label is showing?

I have a Core-Plot Scatter Plot with dates on the x-Axis:

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

[dateComponents setMonth:10];
[dateComponents setDay:29];
[dateComponents setYear:2009];
[dateComponents setHour:12];
[dateComponents setMinute:0];
[dateComponents setSecond:0];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *refDate = [gregorian dateFromComponents:dateComponents];
NSTimeInterval oneDay = 24 * 60 * 60;
CPTXYAxis *x          = axisSet.xAxis;
x.majorIntervalLength         = CPTDecimalFromFloat(oneDay);
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");
x.minorTicksPerInterval       = 0;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateStyle = kCFDateFormatterShortStyle;
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = refDate;
x.labelFormatter            = timeFormatter;
x.labelRotation             = M_PI / 4;

But only the first date is displayed on the x-Axis:

enter image description here

The test data for the graphs is created with this code:

-(NSMutableArray *)testData{
    NSMutableArray *testArray = [NSMutableArray arrayWithCapacity:100];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.0f], @"x", [NSNumber numberWithFloat:19.0f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.5f], @"x", [NSNumber numberWithFloat:20.0f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:1.0f], @"x", [NSNumber numberWithFloat:18.5f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:1.5f], @"x", [NSNumber numberWithFloat:19.5f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:2.0f], @"x", [NSNumber numberWithFloat:21.5f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:2.5f], @"x", [NSNumber numberWithFloat:20.5f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:3.0f], @"x", [NSNumber numberWithFloat:20.0f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:3.5f], @"x", [NSNumber numberWithFloat:20.5f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:4.0f], @"x", [NSNumber numberWithFloat:24.0f], @"y", nil]];
    [testArray addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:4.5f], @"x", [NSNumber numberWithFloat:23.5f], @"y", nil]];
    return testArray;
}

Either soemthing is wrong with my x-Axis or the data supplied is not correct?

Upvotes: 0

Views: 319

Answers (1)

Eric Skroch
Eric Skroch

Reputation: 27381

Your x-values are 0.5 units apart while the major tick marks are oneDay (24 * 60 * 60 = 86,400) units apart. If the x-values are supposed to be dates, multiply the current values by oneDay and expand the xRange of the plot space to cover the larger range of values.

Upvotes: 1

Related Questions