核心情节:仍然不了解如何具有自定义标签和刻度 [英] core-plot: still do not understand how to have custom label AND ticks

查看:88
本文介绍了核心情节:仍然不了解如何具有自定义标签和刻度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间以来,我一直遇到这个问题,我找不到任何解决方法.我读了几个论坛,但找不到有效的解决方案.我有以下代码:

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

我看到x上的标签,但完全没有刻度.

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

推荐答案

为结束这个问题,我得到了其中一位核心人物的回答:

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

关于, 卢克

这篇关于核心情节:仍然不了解如何具有自定义标签和刻度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆