2012-01-04 96 views
1

我想了解如何使用CorePlot。以前,我曾經使用谷歌圖表來繪製我的圖表。在CorePlot中固定軸 - iOS

我想把我的圖表的軸設置到左下方,並從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; 

會有人能告訴我,我應該怎麼辦?

回答

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

,改變它的plotRangeWithLocation爲0的yRange。

此外,您需要更改此

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

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

和Y的orthogonalCoordinateDecimal這樣做。

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;