我有2个图表,我在视图控制器中显示。 y轴是记录的时间值,x是数据,风平,阵风,平均值。 我的问题是我的阵列中的数据收集时间大约是8到10个小时。所以有时第一个值是晚上19:00,最后一个是早上05:00。现在,如果我尝试将其放入散点图中,数据将遍布整个地方,而不是按顺序。如果我只是使用数组索引号,它会很好地绘制,但数据并不总是按时间顺序排列,有时一小时就可以到达没有数据的地方。当我将y轴更改为记录的时间值时,我将从HH:mm:ss转换为19.0和5.0,并且在之前的示例中,如图所示,图表错误,如下所示。我已经在y轴上添加了自定义标签等,但需要一种方法使其成为线性?
编辑:
我希望x轴看起来如下所示,除了当数据有前一天和当前日期时轴数如下运行??:
19,20,21,22,23,24,1,2,3,4,5,6
目前我有一个客户标签循环来做到这一点,但数据仍然包裹?
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat([xFirstEntry integerValue]) length:CPTDecimalFromFloat(10.5)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat([yMaxValue integerValue]+12)];
plotSpace.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble([xFirstEntry integerValue]) length:CPTDecimalFromDouble(10.5)];
plotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromDouble(0) length:CPTDecimalFromDouble([yMaxValue doubleValue]*1.23)];
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];
x.axisConstraints = [CPTConstraints constraintWithLowerOffset:0];
x.labelingPolicy = CPTAxisLabelingPolicyNone;
xFirstEntry = [[[changedArray objectAtIndex:0]objectForKey:@"date"] substringFromIndex:11];
xFirstEntry = [xFirstEntry substringToIndex:[xFirstEntry length] - 6];
xLastEntry = [[[changedArray objectAtIndex:changedArray.count-1]objectForKey:@"date"] substringFromIndex:11];
xLastEntry = [xLastEntry substringToIndex:[xLastEntry length] - 6];
NSLog(@"%@ %@", xFirstEntry, xLastEntry);
NSMutableArray *xCustomTickLocations = [[NSMutableArray alloc]init];
NSMutableArray *xCustomAxisLabels = [[NSMutableArray alloc]init];
NSInteger newValue = [xFirstEntry integerValue];
if ([xFirstEntry integerValue] > [xLastEntry integerValue]) {
NSLog(@"gretaer than");
while (newValue <= 24) {
[xCustomTickLocations addObject:[NSDecimalNumber numberWithInt:newValue]];
[xCustomAxisLabels addObject:[NSString stringWithFormat:@"%d",newValue]];
newValue++;
}
newValue = 1;
[xCustomTickLocations addObject:[NSDecimalNumber numberWithInt:newValue]];
[xCustomAxisLabels addObject:[NSString stringWithFormat:@"%d",newValue]];
while (newValue <= [xLastEntry integerValue]+1) {
[xCustomTickLocations addObject:[NSDecimalNumber numberWithInt:newValue]];
[xCustomAxisLabels addObject:[NSString stringWithFormat:@"%d",newValue]];
newValue++;
}
}
else {
NSLog(@"less than");
while (newValue <= [xLastEntry integerValue]+1) {
[xCustomTickLocations addObject:[NSDecimalNumber numberWithInt:newValue]];
[xCustomAxisLabels addObject:[NSString stringWithFormat:@"%d",newValue]];
newValue++;
}
}
NSUInteger XlabelLocation = 0;
CPTMutableTextStyle *xLabelStyle = [CPTMutableTextStyle textStyle];
xLabelStyle.color = [[CPTColor whiteColor] colorWithAlphaComponent:1];
xLabelStyle.fontName = @"Helvetica-Bold";
xLabelStyle.fontSize = 9.0f;
NSMutableArray *XcustomLabels = [NSMutableArray arrayWithCapacity:[xCustomAxisLabels count]];
for (NSNumber *XtickLocation in xCustomTickLocations) {
CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText: [xCustomAxisLabels objectAtIndex:XlabelLocation++] textStyle:xLabelStyle];
newLabel.tickLocation = [XtickLocation decimalValue];
newLabel.offset = 2;
newLabel.rotation=M_PI_2;
[XcustomLabels addObject:newLabel];
}
x.axisLabels = [NSSet setWithArray:XcustomLabels];
答案 0 :(得分:1)
有几种可能的解决方案。哪一个最简单取决于数据的存储方式以及用户如何与图表进行交互。
你可以打破这条线,这样它就不会缠绕。在索引处插入“虚拟”数据点,该索引在当天的最后一个点之后以及在第二天的第一个点之前。对此索引处的数据值返回nil
或NAN
以打破该行。
将x轴设置为从一天开始到下一天结束的时间刻度。使用Core Plot中内置的格式化程序之一或制作自定义轴标签来显示时间。
答案 1 :(得分:0)
以下是我用于解决方案的代码:
定义图表:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
[dateFormatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
NSDate *firstDate = [dateFormatter dateFromString:[[changedArray objectAtIndex:0]objectForKey:@"date"]];
NSDate *lastDate = [dateFormatter dateFromString:[[changedArray objectAtIndex:changedArray.count-1]objectForKey:@"date"]];
NSNumber *difference = [NSDecimalNumber numberWithDouble:[lastDate timeIntervalSinceDate:firstDate]];
CPTXYPlotSpace *plotSpace2 = (CPTXYPlotSpace *) graph2.defaultPlotSpace;
plotSpace2.allowsUserInteraction = YES;
plotSpace2.delegate = self;
plotSpace2.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromInteger([difference integerValue]*1.03)];
plotSpace2.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(9)];
plotSpace2.globalXRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromInteger([difference integerValue]*1.03)];
plotSpace2.globalYRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0) length:CPTDecimalFromFloat(10)];
创建标签和刻度位置:
xFirstEntry = [[[changedArray objectAtIndex:0]objectForKey:@"date"] substringFromIndex:11];
xFirstEntry = [xFirstEntry substringToIndex:[xFirstEntry length] - 6];
xLastEntry = [[[changedArray objectAtIndex:changedArray.count-1]objectForKey:@"date"] substringFromIndex:11];
xLastEntry = [xLastEntry substringToIndex:[xLastEntry length] - 6];
NSMutableArray *x2MajorLocations = [[NSMutableArray alloc]init];
NSMutableArray *x2MinorLocations = [[NSMutableArray alloc]init];
NSMutableArray *x2Labels = [[NSMutableArray alloc]init];
int newValue = [xFirstEntry integerValue];
NSNumber *x2MajorDataPoint = [NSNumber numberWithInt:(newValue)];
NSNumber *x2MinorDataPoint = [NSNumber numberWithInt:(newValue)];
while (newValue <= [xLastEntry integerValue]+1) {
x2MinorDataPoint = @([x2MajorDataPoint integerValue] + 1800);
NSNumber *x2LabelValue = [NSNumber numberWithInt:newValue];
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:[x2LabelValue stringValue] textStyle:xLabelStyle];
label.tickLocation = CPTDecimalFromInt([x2MajorDataPoint intValue]);
label.offset = 4;
[x2Labels addObject:label];
[x2MajorLocations addObject:x2MajorDataPoint];
[x2MinorLocations addObject:x2MinorDataPoint];
newValue++;
x2MajorDataPoint = @([x2MajorDataPoint integerValue] + 3600);
}
x2.labelingPolicy = CPTAxisLabelingPolicyNone;
x2.axisLabels = [NSSet setWithArray:x2Labels];
x2.majorTickLocations = [NSSet setWithArray:x2MajorLocations];
x2.minorTickLocations = [NSSet setWithArray:x2MinorLocations];
我的numberForPlot:链接后的样子:
case CPTScatterPlotFieldX:
if (index < valueCount) {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyy-MM-dd HH:mm:ss"];
NSDate *firstDate = [formatter dateFromString:[[changedArray objectAtIndex:0]objectForKey:@"date"]];
NSDate *newDate = [formatter dateFromString:[[changedArray objectAtIndex:index]objectForKey:@"date"]];
return [NSDecimalNumber numberWithDouble:[newDate timeIntervalSinceDate:firstDate]];
}
break;
如果没有顶部的自定义标签,并且自定义在底部,它看起来是什么样子: