0

我在视图控制器中显示了 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];
4

2 回答 2

1

有几种可能的解决方案。哪一个最简单取决于数据的存储方式以及用户如何与绘图进行交互。

  1. 你可以打破这条线,这样它就不会像那样环绕。在您当天最后一个点之后和第二天第一个点之前的索引处插入一个“虚拟”数据点。返回nilNAN为该索引处的数据值换行。

  2. 将 x 轴设置为从一天开始到第二天结束的时间刻度。要么使用 Core Plot 内置的格式化程序之一,要么制作自定义轴标签来显示时间。

于 2013-11-08T00:05:37.543 回答
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;

顶部没有我的自定义标签,底部没有自定义标签的情况:

在此处输入图像描述

于 2013-11-10T04:55:49.900 回答