修复CorePlot中的轴 - iOS

时间:2012-01-04 22:37:08

标签: ios core-plot

我试图了解如何使用CorePlot。以前,我曾经使用谷歌图表绘制我的图表。

我正在尝试将图表的轴设置为左下角,并从x和y轴开始从底部开始0。但是,我无法理解代码是如何工作的。

这是代码 -

// Setup plot space
CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace;
plotSpace.allowsUserInteraction = YES;
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(2.0)];
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(3.0)];

// Axes
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet;
CPTXYAxis *x = axisSet.xAxis;
x.majorIntervalLength = CPTDecimalFromString(@"0.5");
x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");
x.minorTicksPerInterval = 2;
NSArray *exclusionRanges = [NSArray arrayWithObjects:
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)], 
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(2.99) length:CPTDecimalFromFloat(0.02)],
    nil];
x.labelExclusionRanges = exclusionRanges;

CPTXYAxis *y = axisSet.yAxis;
y.majorIntervalLength = CPTDecimalFromString(@"0.5");
y.minorTicksPerInterval = 5;
y.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");
exclusionRanges = [NSArray arrayWithObjects:
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.99) length:CPTDecimalFromFloat(0.02)], 
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.99) length:CPTDecimalFromFloat(0.02)],
    [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(3.99) length:CPTDecimalFromFloat(0.02)],
    nil];
y.labelExclusionRanges = exclusionRanges;

有人能告诉我应该做什么吗?

2 个答案:

答案 0 :(得分:3)

要在(0,0)处启动图形,您需要更改x和y范围以及交叉点。您可以通过修改

来更改此设置
plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(1.0) length:CPTDecimalFromFloat(2.0)];

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.0) length:CPTDecimalFromFloat(2.0)];

并且还将yRange的plotRangeWithLocation更改为0.

此外,您需要更改此

x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"2");

x.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");

并对y的orthogonalCoordinateDecimal执行相同的操作。

答案 1 :(得分:1)

NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
[numberFormatter setGeneratesDecimalNumbers:NO];

axisSet = (CPTXYAxisSet *)graph.axisSet;

x_axis = axisSet.xAxis;
x_axis.majorIntervalLength = CPTDecimalFromString(@"1");
x_axis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
x_axis.minorTicksPerInterval = 0;
x_axis.labelFormatter = numberFormatter;

 axisSet.xAxis.visibleRange =[CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(100)]; // you can set your number instead of 100
 axisSet.xAxis.gridLinesRange = axisSet.xAxis.visibleRange;

y_axis = axisSet.yAxis;
y_axis.majorIntervalLength = CPTDecimalFromString(@"1");
y_axis.orthogonalCoordinateDecimal = CPTDecimalFromString(@"0");
y_axis.minorTicksPerInterval = 1;
y_axis.labelFormatter = numberFormatter;

axisSet.yAxis.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromInt(0) length:CPTDecimalFromInt(100)]; // you can set your number instead of 100
axisSet.yAxis.gridLinesRange = axisSet.yAxis.visibleRange;