Reputation: 337
Following different tutorials, I've successfully created a scatter plot using Core-Plot. Now, setting dates on X-Axis I'm obtaining totally wrong dates. The source of my graph is an array containing a dictionary. In every record of the dictionary the first key of the dictionary is a unix timestamp and the second key, the value to draw. It looks like this:
(
{
0 = 1334152500;
1 = 0;
},
{
0 = 1334152800;
1 = 0;
},
{
0 = 1334153100;
1 = 0;
},
AFAIK Core-Plot Needs a start date and a step. In this case start date is 1334152500 (Wed, 11 Apr 2012 13:55:00 GMT) and step is 300 seconds. Every 300 seconds a new value is present. Now the code I use to draw the X-axis:
// this string contains the first UNIX Timestamp registered
NSString *tstamp_start = [NSString stringWithFormat:@"%@", [[[self.graphData objectForKey:@"1"] objectAtIndex:0] objectForKey:@"0"]];
NSDate *refDate = [NSDate dateWithTimeIntervalSince1970:[tstamp_start doubleValue]];
// definition of the graph skipped...
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
// tInterval is previous set to 300 - float.
// here I should set the Interval between every value on graph...
axisSet.xAxis.majorIntervalLength = CPTDecimalFromFloat(tInterval);
axisSet.xAxis.axisLineStyle = axisLineStyle;
axisSet.xAxis.majorTickLineStyle = axisLineStyle;
axisSet.xAxis.minorTickLineStyle = axisLineStyle;
axisSet.xAxis.labelTextStyle = textStyle;
axisSet.xAxis.labelOffset = 3.0f;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mma"];
CPTTimeFormatter *timeFormatter = [[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter];
timeFormatter.referenceDate = refDate;
axisSet.xAxis.labelFormatter = timeFormatter;
axisSet.xAxis.labelingPolicy = CPTAxisLabelingPolicyAutomatic;
axisSet.xAxis.orthogonalCoordinateDecimal = CPTDecimalFromFloat(yAxisMin);
(...)
The line is correctly shown, but on X Axis I'm reading values/labels like: '22/07/2054 06:35AM'. Year 2054? Without considering the timezone (local +2, unix +0) and CPTAxisLabelingPolicyAutomatic, at least I should read a date between 13:55 and now on current day. What I'm missing?
Thank's a lot!
Simon
Upvotes: 3
Views: 1036
Reputation: 27381
The referenceDate
is the offset applied to each data point when formatting. Therefore, the value of the first data point is being doubled. You either need to adjust the reference date or change the data values to start from 0.
NSDate *refDate = [NSDate dateWithTimeIntervalSince1970:0.0];
Upvotes: 5