【问题标题】:How to plot NSDate along X axis using Core plot如何使用核心图沿 X 轴绘制 NSDate
【发布时间】:2011-04-13 02:51:31
【问题描述】:

我已经尝试过核心图中的所有示例,尤其是 dateplot 示例,但我仍然坚持沿 x 轴以小时数来实现该图。请帮我找出我哪里出错了。

我面临的问题:

  • 无法获得小时均匀分布的轴(按小时计算,0900、1000、....)
  • 当我将它存储到 plotData 数组中并且绘制图形时,所有 x 值都是 0。我的意思是,所有值都仅绘制在 y 轴上。不存储 x 值。

代码如下:

NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:@"HH:mm a"];
 NSTimeInterval oneDay = 24 * 60 * 60;
 NSTimeInterval xLow = 0.0f;

//This part is used to set up the plot space.
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction=YES;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(xLow)
                                                 length:CPDecimalFromFloat(oneDay)];


    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPXYAxis *x=axisSet.xAxis;
x.majorIntervalLength = CPDecimalFromFloat(1*60*60);
x.minorTicksPerInterval = 0;
x.axisLineStyle = lineStyle;
x.majorTickLength = 7.0f;
//x.visibleRange=xAxisRange;

CPTimeFormatter *timeFormatter = [[[CPTimeFormatter alloc]
                                   initWithDateFormatter:dateFormatter1] autorelease];
 Chirp *retValue=[someTemp objectAtIndex:0];
NSDate *tempDate=retValue.startingAt;
timeFormatter.referenceDate = tempDate;
x.labelFormatter = timeFormatter;
x.orthogonalCoordinateDecimal=CPDecimalFromString(@"0");


  NSMutableArray *newData = [NSMutableArray array];
NSUInteger i;
for ( i = 0; i < [someTemp count]; i++ ) 
{
    Chirp *retValue=[[Chirp alloc]init];
    retValue=[someTemp objectAtIndex:i];
    NSDate *tempDate=retValue.startingAt;
    NSString *dateFromString=[dateFormatter1 stringFromDate:tempDate];
    NSLog(@"%@", dateFromString);
    id x=dateFromString;
    id y=[NSDecimalNumber numberWithFloat:[retValue.chipCount floatValue]];
    [newData addObject:
     [NSDictionary dictionaryWithObjectsAndKeys:
      x, [NSNumber numberWithInt:CPScatterPlotFieldX], 
      y, [NSNumber numberWithInt:CPScatterPlotFieldY], 
      nil]];
}
plotData = newData;


-(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
{
return plotData.count;
}

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
{
NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
return num;
}

【问题讨论】:

    标签: iphone objective-c graph nsdate core-plot


    【解决方案1】:

    没关系...!我找到了qn的解决方案。我让我的 x 轴使用自定义标签(如 Rahul Vyas 所建议)。这解决了我面临的一个问题。对于另一个问题,我尝试使用 NSDateformatter 和字符串从 NSDate 获取字符串。我在下面发布代码。希望它对某人有所帮助。 在 x 轴上带有时间的自定义标签的代码,在条形图示例中已经存在。

    -(void)configureXAxisForGraph:(CPXYGraph*)graphForAxis
    {
     CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
     CPXYAxis *x=axisSet.xAxis;
     x.majorIntervalLength = [[NSDecimalNumber decimalNumberWithString:@"1"] decimalValue];
     CPPlotRange *xAxisRange=[CPPlotRange plotRangeWithLocation:CPDecimalFromString(@"0.0") length:CPDecimalFromString(@"24.0")];
     x.minorTicksPerInterval = 4;
     x.majorTickLineStyle = lineStyle;
     x.minorTickLineStyle = lineStyle;
     x.axisLineStyle = lineStyle;
     x.minorTickLength = 5.0f;
     x.majorTickLength = 7.0f;
     x.visibleRange=xAxisRange;
     x.orthogonalCoordinateDecimal=CPDecimalFromString(@"0");
     x.title=@"Hours";
     x.titleOffset=47.0f;
     x.labelRotation=M_PI/4;
     x.labelingPolicy=CPAxisLabelingPolicyNone;
     NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:3], [NSDecimalNumber numberWithInt:6], [NSDecimalNumber numberWithInt:9], [NSDecimalNumber numberWithInt:12], 
                                    [NSDecimalNumber numberWithInt:15], [NSDecimalNumber numberWithInt:18], [NSDecimalNumber numberWithInt:21], [NSDecimalNumber numberWithInt:24],nil];
     NSArray *xAxisLabels = [NSArray arrayWithObjects:@"3 AM", @"6 AM", @"9 AM", @"12PM", @"3 PM",@"6 PM",@"9 PM", @"12 AM", nil];
     NSUInteger labelLocation = 0;
     NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
     for (NSNumber *tickLocation in customTickLocations) 
     {
        CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:x.labelTextStyle];
        newLabel.tickLocation = [tickLocation decimalValue];
        newLabel.offset = x.labelOffset + x.majorTickLength;
        newLabel.rotation = M_PI/4;
        [customLabels addObject:newLabel];
        [newLabel release];
    }
     x.axisLabels =  [NSSet setWithArray:customLabels];
    }
    

    这是我用来根据数字和时间(小时)绘制值的代码。

    self.dataToBePlotted = [NSMutableArray array];
    NSUInteger i;
    for ( i = 0; i < [dummyDataArray count]; i++ ) 
    {
        Chirp *retValue=[[Chirp alloc]init];
        retValue=[dummyDataArray objectAtIndex:i];
        NSDate *tempDate=retValue.startingAt;
        NSString *dateFromString=[dateFormatter1 stringFromDate:tempDate];
        NSLog(@"%@", dateFromString);
        id x=dateFromString;
        id y=[NSDecimalNumber numberWithFloat:[retValue.chipCount floatValue]];
        [self.dataToBePlotted addObject:
         [NSDictionary dictionaryWithObjectsAndKeys:
          x, [NSNumber numberWithInt:CPScatterPlotFieldX], 
          y, [NSNumber numberWithInt:CPScatterPlotFieldY], 
          nil]];
    }
    self.plotData = self.dataToBePlotted;
    
    
     -(NSUInteger)numberOfRecordsForPlot:(CPPlot *)plot
     {  
       return [plotData count];
     } 
    
    -(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index 
    {
      NSDecimalNumber *num = [[plotData objectAtIndex:index] objectForKey:[NSNumber numberWithInt:fieldEnum]];
      return num;
    }
    

    【讨论】:

    • 非常感谢您的代码,我有疑问:您只工作一小时而不是一分钟,对吗?是否可以在 xaxis 或日期和时间上同时使用小时和分钟来绘制折线图
    • 我想实现折线图 y 值包含数字,对于 x 轴我仍然不确定,但可能会遵循 1. time with hour, 2.time with hour+minute 3. only date, still我不确定核心情节支持什么......你能告诉我核心情节将支持哪一个吗
    • @Pooja:这通常取决于您要从绘制图形的数组中存储什么值。在我的例子中,我存储了时间戳值,我可以将它与日期和时间一起降低到毫秒。在使用自定义标签进行展示时,我只需要展示我想要展示的内容。
    • 你能把这个成功的例子分享给我吗??因为我有同样的问题!请!!!!
    【解决方案2】:

    您需要为 x 轴创建自定义标签。请参阅 core-plot 的条形图示例。它有自定义标签。这样你就可以显示小时、日期或任何你想要的东西。

    【讨论】:

    • 我会尝试使用自定义标签。但我也面临存储 X 值的问题。我在图中的所有值都绘制在 y 轴上,这意味着该数组将所有 X 值存储为零。你能帮我找出我哪里出错了。
    • @jeevangs 您在上面发布的代码缺少 y 轴的绘图范围。尝试调试您的代码并将您的代码与示例应用程序进行比较。您只需将示例代码复制并粘贴到您的应用程序中,并根据您的要求修改代码。希望这会有所帮助
    • 我没有故意贴出 y 轴的代码,因为它工作正常,不想放在这里。
    • x 轴上的自定义标签工作正常。我试图找出我自己的逻辑,将 x 轴上的时间存储为十进制值并将其存储在数组中。非常感谢..!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多