我在iOS应用程序中使用Core Plot并希望格式化轴的步骤。我得到的只是0.5的步骤,我不知道还有什么可以改变。我的刻度位置应为5,10,15和20。
以下是绘制轴的代码:
-(void) addAxis{
// Create grid line styles
CPTMutableLineStyle *majorGridLineStyle = [CPTMutableLineStyle lineStyle];
majorGridLineStyle.lineWidth = 1.0f;
majorGridLineStyle.lineColor = [[CPTColor whiteColor] colorWithAlphaComponent:0.75];
NSSet *majorTickLocations = [NSSet setWithObjects:[NSDecimalNumber zero],
[NSDecimalNumber numberWithUnsignedInteger:5],
[NSDecimalNumber numberWithUnsignedInteger:10],
[NSDecimalNumber numberWithUnsignedInteger:15],
[NSDecimalNumber numberWithUnsignedInteger:20],
nil];
// Create axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)self.graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
{
x.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;
x.tickDirection = CPTSignNone;
x.majorTickLocations = majorTickLocations;
x.labelOffset = 6.0;
x.labelRotation = M_PI/2;
x.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(19.0f)];
x.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(30.0f)];
x.title = @"Jahre";
x.titleOffset = 35.0f;
x.titleLocation = CPTDecimalFromFloat(x.visibleRange.lengthDouble / 2.0);
x.plotSpace = self.barPlotSpace;
}
CPTXYAxis *y = axisSet.yAxis;
{
y.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided;
y.majorTickLocations = majorTickLocations;
y.minorTicksPerInterval = 0;
y.preferredNumberOfMajorTicks = 8;
y.majorGridLineStyle = majorGridLineStyle;
y.axisLineStyle = nil;
y.majorTickLineStyle = nil;
y.minorTickLineStyle = nil;
y.labelOffset = 10.0;
y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(30.0f)];
y.gridLinesRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0f) length:CPTDecimalFromFloat(18.5f)];
y.title = @"Sparbetrag";
y.titleOffset = 40.0f;
y.titleLocation = CPTDecimalFromFloat(y.visibleRange.lengthDouble / 2.0);
y.plotSpace = barPlotSpace;
}
// Set axes
self.graph.axisSet.axes = [NSArray arrayWithObjects:x, y, nil];
}
答案 0 :(得分:3)
找到解决方案:应用主题会改变绘图的绘画行为。如果我不申请主题 - 一切都很好。
因为这不是很直观。我希望主题只能改变颜色,或者某些方面可以被覆盖。
答案 1 :(得分:0)
该代码应该可以正常工作 - 最终会在0,5,10,15和20处显示刻度线和标签。visibleRange
和gridLinesRange
的设置可能不正确 - 将它们保留为默认值nil,使轴和网格线延伸绘图区域的整个宽度和高度。
如果要更改标签的数字格式,请将labelFormatter
设置为新的NSNumberFormatter
对象,以配置所需的数字格式。