core-plot:仍然不明白如何拥有自定义标签和刻度

发布于 2024-09-09 01:49:33 字数 3079 浏览 1 评论 0原文

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

// 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 上根本没有勾号。

I have this problem since a long time and I cannot find anything to solve this. I read several forum but was not able to find a working solution. I have the following code:

// 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];

I see the labels but no ticks at all on x.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒芜了季节 2024-09-16 01:49:33

为了结束这个问题,我从一位核心情节人员那里得到了答案:

只需设置刻度位置,添加以下语句:

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

问候,
吕克

Just to close this question, I had an answer from one of the core-plot guys:

Just need to set the tick locations, adding this statement:

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

Regards,
Luc

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文