2010-01-13 85 views
5

編輯:我認爲我的問題更好措詞如下:我怎麼能有一個不從零開始的Y軸?看起來x軸始終位於y = 0處,但我希望x軸在y軸上處於某個正數。爲什麼我的X軸不會在iPhone上顯示核心圖?

下面是與更規則數據的圖...我只希望x軸被設置在最小的y值的曲線圖(約77),而不是在0

alt text

這是我用來創建圖形的函數。

這是一大堆代碼,我不太確定哪一部分可能不顯示x軸。

x軸不顯示與我的數據有關嗎?

- (void) showGraph:(SavedDetailScreen*)dataSource { 

    // create the graph and add it to the view 
    CPXYGraph *graph = [[CPXYGraph alloc] initWithFrame: CGRectZero]; 
    graph.plotArea.masksToBorder = NO; 
    CPLayerHostingView *graphView = [[CPLayerHostingView alloc] initWithFrame:CGRectMake(0,0, 280, 240)]; 
    [self addSubview:graphView]; 
    graphView.hostedLayer = graph; 
    graph.paddingLeft = 50.0; 
    graph.paddingTop = 20.0; 
    graph.paddingRight = 10.0; 
    graph.paddingBottom = 40.0;    

    // set up the ranges for the graph axis 
    float minElevation = dataSource.track.tbStats.minAlt; 
    float maxElevation = dataSource.track.tbStats.maxAlt-dataSource.track.tbStats.minAlt; 
    float minDistance = 0.0f; 
    float maxDistance = dataSource.track.tbStats.totalDistance; 
    CPXYPlotSpace *plotSpace = (CPXYPlotSpace *)graph.defaultPlotSpace; 
    plotSpace.xRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(minDistance) 
              length:CPDecimalFromFloat(maxDistance)]; 
    plotSpace.yRange = [CPPlotRange plotRangeWithLocation:CPDecimalFromFloat(minElevation) 
               length:CPDecimalFromFloat(maxElevation)]; 

    // style the graph with white text and lines 
    CPTextStyle *whiteText = [CPTextStyle textStyle]; 
    whiteText.color = [CPColor whiteColor];    
    CPLineStyle *whiteStyle = [CPLineStyle lineStyle]; 
    whiteStyle.lineColor = [CPColor whiteColor]; 
    whiteStyle.lineWidth = 2.0f; 

    // set up the axis 
    CPXYAxisSet *axisSet = (CPXYAxisSet *)graph.axisSet;  
    CPXYAxis *x = axisSet.xAxis; 
    CPXYAxis *y = axisSet.yAxis;    
    x.majorIntervalLength = CPDecimalFromFloat(maxDistance/10.0f); 
    x.minorTicksPerInterval = 0; 
    x.majorTickLineStyle = whiteStyle; 
    x.minorTickLineStyle = whiteStyle; 
    x.axisLineStyle = whiteStyle; 
    x.minorTickLength = 5.0f; 
    x.majorTickLength = 10.0f; 
    x.labelOffset = 3.0f; 
    x.labelTextStyle = whiteText; 
    y.majorIntervalLength = CPDecimalFromFloat(maxElevation/5.0f); 
    y.minorTicksPerInterval = 0; 
    y.majorTickLineStyle = whiteStyle; 
    y.minorTickLineStyle = whiteStyle; 
    y.axisLineStyle = whiteStyle; 
    y.minorTickLength = 5.0f; 
    y.majorTickLength = 10.0f; 
    y.labelOffset = 3.0f; 
    y.labelTextStyle = whiteText; 

    CPScatterPlot *plot = [[[CPScatterPlot alloc] initWithFrame:graph.bounds] autorelease]; 
    plot.dataLineStyle.lineWidth = 2.0f; 
    plot.dataLineStyle.lineColor = [CPColor blueColor]; 
    plot.dataSource = dataSource; 
    [graph addPlot:plot]; 
    CPPlotSymbol *greenCirclePlotSymbol = [CPPlotSymbol ellipsePlotSymbol]; 
    greenCirclePlotSymbol.fill = [CPFill fillWithColor:[CPColor greenColor]]; 
    greenCirclePlotSymbol.size = CGSizeMake(2.0, 2.0); 
    plot.plotSymbol = greenCirclePlotSymbol;     

} 

alt text

回答

5

您可以使用軸的constantCoordinateValue屬性在正交軸上設置軸的位置。只需在x軸上將該屬性設置爲y的正值,並且該軸應向上移動。

1

該框架確實需要一個更好的方式來配置自身。我沒有看到任何問題跳出來。也許只是試着簡化方法,使調試變得易於管理。檢查您傳遞的數據以確保它符合您的期望。更好的是,創建一些你知道會產生你期望的陰謀的虛擬數據。

+0

的數據是所有罰款。圖形顯示,y軸顯示,y軸標籤顯示,圖顯示...只是x軸不顯示,也沒有標籤。數據唯一的問題是y值有時可能低於最小y值。這會搞砸了嗎? – 2010-01-13 06:00:37

+0

對不起,不太熟悉這個框架。但是,你有這個方法呢:plotRangeWithLocation:CPDecimalFromFloat(minDistance)length:CPDecimalFromFloat(maxDistance)]; 這是正確的長度值嗎?例如。同時你有maxAlt是你的數據源min和max之間的差異,但是對於距離你有它在0和數據源max之間。 – 2010-01-13 06:40:37

+0

此外,作爲一般觀察,它看起來像整個圖表可能在那裏,但由於一些低y值切斷x軸。你可以創建一個更簡單的虛擬數據集(也許在所有的y都相同的情況下產生一條水平線)來查看x軸是否出現了嗎? – 2010-01-13 06:41:41

6

我昨天/今天有同樣的問題,解決方案似乎是使用軸上的orthogonalCoordinateDecimal屬性。例如:

graph.axisSet.xAxis.orthogonalCoordinateDecimal = CPDecimalFromFloat(minHeight); 
+0

minHeight是什麼? – 2014-07-21 07:01:46

相關問題