2012-12-14 43 views
2

調用scaleToFitPlots後,我的X軸縮放比例正確,但是我的Y軸太高。附上兩張截圖,第1張圖片:原始圖片,第2張圖片:我滾動後。Coreplot scaletofitplotissue

enter image description here enter image description here

我希望圖形的綠色框內是完全可見(從Y = 0到Y = MAX)。 在iPhone,iPad上,我得到了這個錯誤的Y軸縮放。

由於某種原因,它認爲plotArea比實際上更高?的我的圖表配置碼

段:

-(void)configureGraph { 
    CPTGraph *graph = [[CPTXYGraph alloc] initWithFrame:self.hostView.bounds]; 
    graph.plotAreaFrame.borderLineStyle = nil; 
    self.hostView.hostedGraph = graph; 

    //Controls padding between frame & draw 
    [graph.plotAreaFrame setPaddingTop:20.0f]; 
    [graph.plotAreaFrame setPaddingRight:20.0f]; 
    [graph.plotAreaFrame setPaddingBottom:20.0f]; 
    [graph.plotAreaFrame setPaddingLeft:30.0f]; 
    //Controls padding between frame and graph (graph is wrapper) 
    graph.paddingLeft = 0.0; 
    graph.paddingTop = 0.0; 
    graph.paddingRight = 0.0; 
    graph.paddingBottom = 0.0; 

    // 5 - Enable user interactions for plot space 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
    plotSpace.allowsUserInteraction = YES; 

} 

-(void)scaleToFitPlot 
{ 
    CPTGraph *graph = [self getGraph]; 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 
    [plotSpace scaleToFitPlots:[NSArray arrayWithObjects:self.meterPlot, nil]]; 
} 

-(void)configurePlots { 
    // 1 - Get graph and plot space 
    CPTGraph *graph = self.hostView.hostedGraph; 
    CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) graph.defaultPlotSpace; 

    // 2 - Create the three plots 
    meterPlot = [[CPTScatterPlot alloc] init]; 

    meterPlot.dataSource = self; 
    meterPlot.identifier = MYMSymbolHourly; 
    // Set plot delegate, to know when symbols have been touched 
    // We will display an annotation when a symbol is touched, but this can be another graph! 
    meterPlot.delegate       = self; 
    meterPlot.plotSymbolMarginForHitDetection = 5.0f; 

    //CPTColor *meterColor = [CPTColor greenColor]; 
    CPTColor *meterColor = [self orangeClr]; 

    [graph addPlot:meterPlot toPlotSpace:plotSpace]; 
    // 3 - Set up plot space 
    printf("\nset initialscale"); 
    //[plotSpace scaleToFitPlots:[NSArray arrayWithObjects:meterPlot, nil]]; 
    [self scaleToFitPlot]; 
    CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy]; 
    //[xRange expandRangeByFactor:CPTDecimalFromCGFloat(1.1f)]; 
    plotSpace.xRange = xRange; 
    CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy]; 
    //[yRange expandRangeByFactor:CPTDecimalFromCGFloat(1.2f)]; 
    plotSpace.yRange = yRange; 
    // 4 - Create styles and symbols 
    CPTMutableLineStyle *meterLineStyle = [meterPlot.dataLineStyle mutableCopy]; 
    meterLineStyle.lineWidth = 2.5; 
    meterLineStyle.lineColor = meterColor; 
    meterPlot.dataLineStyle = meterLineStyle; 
    CPTMutableLineStyle *meterSymbolLineStyle = [CPTMutableLineStyle lineStyle]; 
    meterSymbolLineStyle.lineColor = meterColor; 
    CPTPlotSymbol *meterSymbol = [CPTPlotSymbol ellipsePlotSymbol]; 
    meterSymbol.fill = [CPTFill fillWithColor:meterColor]; 
    meterSymbol.lineStyle = meterSymbolLineStyle; 
    meterSymbol.size = CGSizeMake(6.0f, 6.0f); 
    meterPlot.plotSymbol = meterSymbol; 
} 

-(void)configureAxes { 
    // 1 - Create styles 
    CPTMutableTextStyle *axisTitleStyle = [CPTMutableTextStyle textStyle]; 
    axisTitleStyle.color = [self grey222Clr]; 
    axisTitleStyle.fontName = @"Helvetica-Bold"; 
    axisTitleStyle.fontSize = 12.0f; 
    CPTMutableLineStyle *axisLineStyle = [CPTMutableLineStyle lineStyle]; 
    axisLineStyle.lineWidth = 2.0f; 
    axisLineStyle.lineColor = [self axisGreyClr]; 
    CPTMutableTextStyle *xAxisTextStyle = [[CPTMutableTextStyle alloc] init]; 
    xAxisTextStyle.color = [self grey222Clr]; // This is causing that line bug on y axis 
    xAxisTextStyle.fontName = @"Helvetica-Bold"; 
    xAxisTextStyle.fontSize = 10.0f; 


    CPTMutableLineStyle *tickLineStyle = [CPTMutableLineStyle lineStyle]; 
    tickLineStyle.lineColor = [self axisGreyClr]; 
    tickLineStyle.lineWidth = 2.0f; 
    CPTMutableLineStyle *gridLineStyle = [CPTMutableLineStyle lineStyle]; 

    gridLineStyle.lineColor = [self axisGreyClr]; 
    gridLineStyle.lineWidth = 2.0f; 

    gridLineStyle.dashPattern = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.8f], nil]; 
    gridLineStyle.patternPhase=0.0f; 

    // 2 - Get axis set 
    CPTXYAxisSet *axisSet = (CPTXYAxisSet *) self.hostView.hostedGraph.axisSet; 
    // 3 - Configure x-axis 
    CPTAxis *x = axisSet.xAxis; 
    x.title = [self getXAxisTitleForChart]; 
    x.titleTextStyle = axisTitleStyle; 
    x.titleOffset = 15.0f; 
    x.axisLineStyle = axisLineStyle; 
    x.labelingPolicy = CPTAxisLabelingPolicyNone; 
    x.labelTextStyle = xAxisTextStyle; 
    x.majorTickLineStyle = axisLineStyle; 
    x.majorTickLength = 4.0f; 
    x.tickDirection = CPTSignNegative; 
    x.majorGridLineStyle = gridLineStyle; 

    [self setXAxisLabels]; 

    CPTMutableTextStyle *yAxisTextStyle = [[CPTMutableTextStyle alloc] init]; 
    yAxisTextStyle.color = [self orangeClr]; 
    yAxisTextStyle.fontName = @"Helvetica-Bold"; 
    yAxisTextStyle.fontSize = 11.0f; 

    // 4 - Configure y-axis 
    CPTAxis *y = axisSet.yAxis; 
    y.title = @"Usage kWh"; 
    y.titleTextStyle = axisTitleStyle; 
    y.titleOffset = -46.0f; 
    y.axisLineStyle = axisLineStyle; 
    y.majorGridLineStyle = gridLineStyle; 
    y.labelingPolicy = CPTAxisLabelingPolicyNone; 
    y.labelTextStyle = yAxisTextStyle; 

    y.labelOffset = 30.0f; 
    y.majorTickLineStyle = axisLineStyle; 
    y.majorTickLength = 4.0f; 
    y.minorTickLength = 2.0f; 
    y.tickDirection = CPTSignPositive; 

    [self setYAxisLabels]; 
} 
+0

這是不是縮放以適應視圖中的所有當前繪圖?你想要達到什麼樣的行爲? –

+0

只有一個情節,是的,如果我把自己拖到情節空間,情節就在那裏,但我希望那個情節完全被包含在綠框的大小之內。 – Baconbeastnz

+0

這是由yRange引起的問題嗎? – iDev

回答

3

第一個圖像顯示的-scaleToFitPlots:正確的結果。它計算完全包含x和y的繪圖數據的繪圖範圍。如果你已經知道你想要的y範圍(例如,Y = 0到Y = MAX),那麼直接設置即可。如果您只想縮放x軸,請致電-scaleToFitPlots:,然後再設置yRange

+0

運行scaleToFitPlots後,如何獲得plotSpace的新y max/min? – whyoz

+0

Look在'yRange'。繪圖範圍有屬性給你的位置,長度,最小值,最大值和範圍的中點。 –