【问题标题】:core-plot: still do not understand how to have custom label AND ticks核心情节:仍然不明白如何拥有自定义标签和刻度
【发布时间】:2010-07-08 21:08:01
【问题描述】:

我有这个问题很久了,我找不到任何解决这个问题的方法。我阅读了几个论坛,但无法找到可行的解决方案。我有以下代码:

// Adjust graph using above data                
CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-10) length:CPDecimalFromFloat(xmax + 17)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-450) length:CPDecimalFromFloat(3750)];

// Setup axis
CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;
CPLineStyle *lineStyle = [CPLineStyle lineStyle];
lineStyle.lineColor = [CPColor whiteColor];
lineStyle.lineWidth = 1.0f;
CPTextStyle *cyanStyle = [CPTextStyle textStyle];
cyanStyle.color = [CPColor cyanColor];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:0];

// x axis with custom labels
axisSet.xAxis.labelingPolicy = CPAxisLabelingPolicyNone;
axisSet.xAxis.majorTickLineStyle = lineStyle;
axisSet.xAxis.axisLineStyle = lineStyle;
axisSet.xAxis.majorTickLength = 3.0f;
axisSet.xAxis.labelOffset = 3.0f;

axisSet.xAxis.visibleRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromInteger(0) length:CPDecimalFromInteger(xmax)];
axisSet.xAxis.labelExclusionRanges = [NSArray arrayWithObjects:
                                      [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(-10) 
                                                                  length:CPDecimalFromFloat(10)], 
                                      nil];

NSArray *customTickLocations = [NSArray arrayWithObjects:[NSDecimalNumber numberWithInt:0],
                                [NSDecimalNumber numberWithInt:6],
                                [NSDecimalNumber numberWithInt:12],
                                [NSDecimalNumber numberWithInt:18],
                                [NSDecimalNumber numberWithInt:24],
                                [NSDecimalNumber numberWithInt:30],
                                [NSDecimalNumber numberWithInt:36],
                                [NSDecimalNumber numberWithInt:41],                                             
                                [NSDecimalNumber numberWithInt:47],                                             
                                nil];

    // This return the labels (11h, 14h, ..., 22h, ..)
NSArray *xAxisLabels = [GPHelpers getHoursList:fromStr to:toStr];

NSUInteger labelLocation = 0;
NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
for (NSNumber *tickLocation in customTickLocations) {
    CPAxisLabel *newLabel = [[CPAxisLabel alloc] initWithText: [xAxisLabels objectAtIndex:labelLocation++] textStyle:axisSet.xAxis.labelTextStyle];
    newLabel.tickLocation = [tickLocation decimalValue];
    //newLabel.majorTickLocations = [tickLocation decimalValue];
    newLabel.offset = axisSet.xAxis.labelOffset + axisSet.xAxis.majorTickLength;                
    [customLabels addObject:newLabel];
    [newLabel release];
}
axisSet.xAxis.axisLabels =  [NSSet setWithArray:customLabels];

我在 x 上看到了标签,但根本没有刻度。

【问题讨论】:

  • 使用上面的代码,我看到了刻度,但没有看到标签。你有什么建议吗? (@Cesare 问)

标签: iphone label core-plot


【解决方案1】:

为了结束这个问题,我得到了一位核心情节专家的回答:

只需要设置tick位置,加上这个语句:

axisSet.xAxis.majorTickLocations = [NSSet setWithArray:customTickLocations]; 

问候, 卢克

【讨论】:

  • 我必须添加这一行,我得到同样的错误,我添加了 5 个标签我只能在屏幕上看到 2 个标签,请帮助我摆脱这个问题,我有已经花了很多时间。
猜你喜欢
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 2014-04-10
  • 1970-01-01
  • 1970-01-01
  • 2012-12-06
  • 2019-04-11
相关资源
最近更新 更多