2012-04-03 73 views
2

在散點圖上,我的x座標數據只是表示自時間以秒計的時間的長數字,而y座標數據如下所示:0.675088,0.670629,0.6669599。我想在y軸上顯示的主要ticks是這樣的:0.64,0.65,0.66,0.67,但是圖表上目前顯示的是:0.5,0.7,1.0,1.2。核心圖 - 在散點圖上繪製非常小的數字

首先,我不知道爲什麼我會像上面提到的那樣得到不規則的滴答間隔(0.5到0.7 = 0.2,0.7到1.0 = 0.3?),但我猜測是因爲我正在試驗標籤政策並將其設置爲CPTAxisLabelingPolicyEqualDivisions - 有人可以解釋所有這些標籤政策的含義嗎?

真正的問題是,考慮到我繪製的數字非常小,我應該在y軸上的mainIntervalLength中使用什麼樣的值?目前我有0.01,但是當我調整這個數值的時候,它的數量級爲-n,它實際上對我的圖表沒有任何影響。

這是我的代碼基於Plot_Gallery_iOS DatePlot.m片段

-(void)renderInLayer:(CPTGraphHostingView *)layerHostingView withTheme:(CPTTheme *)theme 
{ 
CGRect bounds = layerHostingView.bounds; 

NSDate *refDate = [NSDate dateWithTimeIntervalSince1970:0]; 
NSTimeInterval oneDay = 24 * 60 * 60; 

CPTGraph *graph = [[[CPTXYGraph alloc] initWithFrame:bounds] autorelease]; 
[self addGraph:graph toHostingView:layerHostingView]; 
[self applyTheme:theme toGraph:graph withDefault:[CPTTheme themeNamed:kCPTPlainWhiteTheme]]; 

[self setTitleDefaultsForGraph:graph withBounds:bounds]; 
[self setPaddingDefaultsForGraph:graph withBounds:bounds]; 

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *)graph.defaultPlotSpace; 

NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; 
NSTimeInterval threeMonthsago = now - (90*oneDay); 

plotSpace.xRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(threeMonthsago) length:CPTDecimalFromFloat(now - threeMonthsago)]; 
plotSpace.yRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.6f) length:CPTDecimalFromFloat(0.7f)]; 

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
dateFormatter.dateStyle = kCFDateFormatterShortStyle; 
CPTTimeFormatter *timeFormatter = [[[CPTTimeFormatter alloc] initWithDateFormatter:dateFormatter] autorelease]; 
timeFormatter.referenceDate = refDate; 

// Axes 
CPTXYAxisSet *axisSet = (CPTXYAxisSet *)graph.axisSet; 
CPTXYAxis *x   = axisSet.xAxis; 
x.majorIntervalLength   = CPTDecimalFromFloat(oneDay*30); 
x.orthogonalCoordinateDecimal = CPTDecimalFromFloat(0.6f); 
x.minorTicksPerInterval  = 7; 
x.labelFormatter   = timeFormatter; 
x.labelRotation    = M_PI/4; 
x.preferredNumberOfMajorTicks = 5; 

CPTXYAxis *y = axisSet.yAxis; 
y.majorIntervalLength   = CPTDecimalFromDouble(0.001); 
y.labelingPolicy = CPTAxisLabelingPolicyEqualDivisions; 
y.preferredNumberOfMajorTicks = 5; 
y.minorTicksPerInterval  = 10; 
y.orthogonalCoordinateDecimal = CPTDecimalFromFloat(now); 
// y.visibleRange = [CPTPlotRange plotRangeWithLocation:CPTDecimalFromFloat(0.6f) length:CPTDecimalFromFloat(0.1f)]; 
CPTScatterPlot *dataSourceLinePlot = [[[CPTScatterPlot alloc] init] autorelease]; 
dataSourceLinePlot.identifier = @"Date Plot"; 

// [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]]; 

CPTMutableLineStyle *lineStyle = [[dataSourceLinePlot.dataLineStyle mutableCopy] autorelease]; 
lineStyle.lineWidth    = .5f; 
lineStyle.lineColor    = [CPTColor greenColor]; 
dataSourceLinePlot.dataLineStyle = lineStyle; 

// Auto scale the plot space to fit the plot data 
// Extend the ranges by 30% for neatness 
[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:dataSourceLinePlot, nil]]; 
CPTMutablePlotRange *xRange = [[plotSpace.xRange mutableCopy] autorelease]; 
CPTMutablePlotRange *yRange = [[plotSpace.yRange mutableCopy] autorelease]; 
[xRange expandRangeByFactor:CPTDecimalFromDouble(1.3)]; 
[yRange expandRangeByFactor:CPTDecimalFromDouble(1.3)]; 
plotSpace.xRange = xRange; 
plotSpace.yRange = yRange; 

dataSourceLinePlot.dataSource = self; 
[graph addPlot:dataSourceLinePlot]; 
} 

UPDATE:

y.labelingPolicy = CPTAxisLabelingPolicyLocationsProvided; 

NSSet *majorTickLoc = [NSSet setWithObjects:[NSDecimalNumber numberWithFloat:0.64f], [NSDecimalNumber numberWithFloat:0.65f], [NSDecimalNumber numberWithFloat:0.66f], [NSDecimalNumber numberWithFloat:0.67f],[NSDecimalNumber numberWithFloat:0.68f],nil]; 
y.majorTickLocations = majorTickLoc; 

通過這樣設置自定義Ÿ主要蜱問題解決了但這個數字似乎仍然是四捨五入的,即只有一個小數位0.6,0.7,0.7等,結果證明這只是標籤。標籤上設置數字格式化的工作原理:

NSNumberFormatter *yFormatter = [[NSNumberFormatter alloc] init]; 
[yFormatter setMinimumFractionDigits:4]; 
[yFormatter setMaximumFractionDigits:4]; 
[yFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
y.labelFormatter = yFormatter; 

回答

0

試試這個下面的代碼:

y.majorIntervalLength = CPTDecimalFromString(@"0.1") 
+0

我不確定這是否回答我的問題?我希望下面的數字作爲y主要蜱:0.64,0.65,0.66,0.67,但我不知道如何實現這一點。 – user1187378 2012-04-03 05:43:46

+0

現在我更新了answer.try這段代碼...! – Dinesh 2012-04-03 06:39:32

0

設置labelFormatter爲y軸。

NSNumberFormatter *newFormatter = [[[NSNumberFormatter alloc] init] autorelease]; 
newFormatter.minimumIntegerDigits = 1; 
newFormatter.maximumFractionDigits = 2; 
newFormatter.minimumFractionDigits = 2; 
y.labelFormatter = newFormatter;