如何在CorePlot中动态setPlotSpace?

时间:2013-12-05 15:37:56

标签: ios core-plot

我正在尝试动态设置我的图表的范围。所有图的scaleToFitPlots有时都没有给出预期的结果,因此我试图在那之后检查图。我比较了x和y轴的(范围长度)/(边界的大小(以像素为单位))。通过这种方式,我试图猜测绘图的哪个方面应该扩展以符合另一个方面。问题是为什么扩展一个轴也扩展另一个轴?我在某处错了吗?

-(void)setPlotSpaceForHostView:(CPTGraphHostingView*)hostView{

CPTXYPlotSpace *plotSpace = (CPTXYPlotSpace *) hostView.hostedGraph.defaultPlotSpace;
[plotSpace scaleToFitPlots:hostView.hostedGraph.allPlots];

CPTMutablePlotRange *xRange = [plotSpace.xRange mutableCopy];
CPTMutablePlotRange *yRange = [plotSpace.yRange mutableCopy];

CGRect b = hostView.hostedGraph.bounds;
float xRangeLength = [[NSDecimalNumber decimalNumberWithDecimal:xRange.length] doubleValue];
float yRangeLength = [[NSDecimalNumber decimalNumberWithDecimal:yRange.length] doubleValue];

float xAxisBound = b.size.width;
float yAxisBound = b.size.height;

if (xAxisBound == 0 || yAxisBound == 0)
    return;

float xRatio = xRangeLength/xAxisBound;
float yRatio = yRangeLength/yAxisBound;

if (xRatio == 0 || yRatio == 0)
    return;

NSLog(@"%f %f %f %f %f %f",xRangeLength,yRangeLength,xAxisBound,yAxisBound, xRangeLength/xAxisBound,yRangeLength/yAxisBound);

if (xRatio > yRatio){

    //xRatio is bigger so scale is:
    float expandScale = xRatio/yRatio;
    NSLog(@"e1:%f",expandScale);

    [yRange expandRangeByFactor:CPTDecimalFromCGFloat(expandScale)];
    plotSpace.yRange = yRange;

}else if (yRatio > xRatio){

    //yRatio is bigger so scale is:
    float expandScale = yRatio/xRatio;
    NSLog(@"e2:%f",expandScale);

    [xRange expandRangeByFactor:CPTDecimalFromCGFloat(expandScale)];
    plotSpace.xRange = xRange;

}else{

    //if equal we dont do anything!
    return;
}
}

1 个答案:

答案 0 :(得分:0)

-scaleToFitPlots:独立调整两个绘图范围以适合绘图数据。