【问题标题】:Core Plot : X Axis Labels don't appear the first time the graph is drawn核心情节:第一次绘制图表时不会出现 X 轴标签
【发布时间】:2013-08-22 07:59:11
【问题描述】:

我正在尝试使用 Core Plot 在 iPhone 应用程序中绘制一些图表。这是来自应用程序的一张这样的图表:

如您所见,X 轴标签仅显示 1,2,3... 标签,而我已配置相应的日期以显示在其中。该图每隔几分钟重新绘制一次。重新绘制时,它会正确显示 x 轴标签(即日期值),如下所示:

而第一次,它只显示 1,2,3....

如何设置 X 轴标签?

我在“numberOfRecordsForPlot”中执行此操作,而不是在为图形设置所有其他参数(如颜色、填充等)时执行此操作。这是因为我只有在为图形设置了颜色等之后才获得绘图值,所以在 numberOfRecordsForPlot() 中,我计算了两个轴的轴范围(取决于每隔几分钟出现的值)并计算自定义刻度位置和 x 轴标签并将其设置在那里。这是代码 sn-p :

-(NSUInteger)numberOfRecordsForPlot:(CPTPlot *)plot {
...
...

NSArray *customTickLocations = [NSArray arrayWithObjects :
                                [NSDecimalNumber numberWithInt:0],
                                [NSDecimalNumber numberWithInt:1],
                                [NSDecimalNumber numberWithInt:2],
                                [NSDecimalNumber numberWithInt:3],
                                [NSDecimalNumber numberWithInt:4],
                                nil];
// assume that we are plotting 5 points at a time
NSMutableArray *xAxisLabels = [[NSMutableArray alloc] init];
for (int i=0; i<self.arrayKeys.count ; i++) {

    NSString *myData = [dict objectForKey:[self.arrayKeys objectAtIndex:i]];
    if ((myData == nil) || (myData == NULL)) {
        NSLog(@"WARN : Invalid data");
        continue;
    }
    NSDate *date = (NSDate *) [self.arrayKeys objectAtIndex:i];
    [xAxisLabels addObject:date];
}
NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:xAxisLabels.count];
CPTMutableTextStyle *textStyle = [[CPTMutableTextStyle alloc] init];
textStyle.fontSize = 10.0;
textStyle.color = [CPTColor darkGrayColor];
for (int i=0 ; i<dictHealth.count ; i++) {
    NSNumber *tickLocation = [customTickLocations objectAtIndex:i];
    NSDateComponents *dateComponents = [self getComponentFromDate:
                                        (NSDate *)[xAxisLabels objectAtIndex:labelLocation++]];
    CPTAxisLabel *newLabel = [[CPTAxisLabel alloc]
                              initWithText:[NSString stringWithFormat:@"%d-%d-%d\n%d:%d:%d",
                                            dateComponents.year, dateComponents.month, dateComponents.day,
                                            dateComponents.hour, dateComponents.minute, dateComponents.second]
                              textStyle:textStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    newLabel.offset = 7.5;
    newLabel.rotation = M_PI/3;
    [customLabels addObject:newLabel];
}
axisSet.xAxis.axisLabels =  [NSSet setWithArray:customLabels];

...
...
}

有人可以帮忙吗?

【问题讨论】:

  • 您使用的是什么版本的 Core Plot?一些旧版本有这样的问题,但它已在 1.3 及更高版本中修复。
  • @EricSkroch,我只使用 1.3 版。当我开始开发这个应用程序时,我最初使用的是早期版本。但是现在,上个月,我升级到了最新的。问题仍然存在。你能指导一下如何解决它吗?
  • 我看错了你的问题。请参阅下面的详细答案。

标签: iphone ios core-plot


【解决方案1】:

不要在数据源中进行大量计算或图形配置。 -numberOfRecordsForPlot: 可以在数据加载周期内多次调用。在查询数据源以获取数据时,它应该已经知道有多少数据可用。计算数据计数应该像读取实例变量或检索集合的count 一样简单。

设置轴通常是视图控制器的功能。如果您需要在收到新数据时调整绘图空间范围并创建新的轴标签,请在控制器中进行。

绘图数据独立于其显示方式。如果图表将在您获得任何数据之前绘制,请在创建图表时为绘图范围和轴标签提供一些合理的默认值。让-numberOfRecordsForPlot: 返回零 (0),直到您有要绘制的数据。当您收到要显示的数据时,让控制器更新绘图范围和坐标轴,并使用 -reloadData-insertDataAtIndex:numberOfRecords: 告诉绘图它有要显示的新数据。

【讨论】:

  • 让我试试这个然后回来。谢谢,@EricSkroch
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多