在CorePlot中缩放轴

时间:2011-02-18 17:28:22

标签: objective-c xcode ios core-plot

我有一个应用程序,我使用CorePlot绘制图形。我已经使用捏合手势实现了图形本身的缩放,但我仍然无法使轴附近的标签(包含1,2等数字)正确缩放,因此不是1,主要间隔变为5或0.5(或任何其他数字)取决于捏合手势。

-(void) viewDidAppear
{   
UIPinchGestureRecognizer* rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
// set options for this recognizer.
[self.view addGestureRecognizer:rec];

...
xMajorInterval = 1;
yMajorInterval = 1;

axisSet = (CPXYAxisSet *)graph.axisSet;

axisSet.xAxis.majorIntervalLength = CPDecimalFromFloat(xMajorInterval);
axisSet.xAxis.minorTicksPerInterval = 4;
axisSet.xAxis.minorTickLength = 4.0f;
axisSet.xAxis.majorTickLength = 8.0f;
axisSet.xAxis.labelOffset = 1.0f;

axisSet.yAxis.majorIntervalLength = CPDecimalFromFloat(yMajorInterval);
axisSet.yAxis.minorTicksPerInterval = 4;
axisSet.yAxis.minorTickLength = 4.0f;
axisSet.yAxis.majorTickLength = 8.0f;
axisSet.yAxis.labelOffset = 1.0f;




...


}

- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer {

//Here I tried to change xMajorInterval and yMajorInterval and redraw axes


}

以下是我如何缩放图

- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer {

if (PlotSpaceX > -0.1) {

if ([gestureRecognizer scale] > 1) {

    PlotSpaceY = PlotSpaceY - 0.1;
    PlotSpaceX = PlotSpaceX - 0.1;

}
}

if (PlotSpaceY >= -0.1) {
if ([gestureRecognizer scale] < 1){

    PlotSpaceY = PlotSpaceY + 0.1;
    PlotSpaceX = PlotSpaceX + 0.1;
}
}

plotSpace.xRange = [CPPlotRange plotRangeWithLocation:plotSpace.xRange.location length:CPDecimalFromFloat(PlotSpaceX * 2.0)];
plotSpace.yRange = [CPPlotRange plotRangeWithLocation:plotSpace.yRange.location length:CPDecimalFromFloat(PlotSpaceY * 2.0)];
majorInterval = majorInterval*PlotSpaceX/4;

intMajorInterval = majorInterval;
NSLog(@"%i", intMajorInterval);
axisSet.xAxis.majorIntervalLength = CPDecimalFromInt(intMajorInterval);
axisSet.yAxis.majorIntervalLength = CPDecimalFromInt(intMajorInterval);


}

在scalePiece方法中,我尝试更改xMajorInterval和yMajorInterval以及重绘轴,但不幸的是,这种方法在捏合手势期间经常被调用,标签只显示huuuge数字。

请帮帮我吗?

1 个答案:

答案 0 :(得分:3)

似乎这个问题的答案取决于你如何缩放图表本身。

你基本上想要缩放majorIntervalLength,就像缩放绘图空间的范围一样。也就是说,如果将范围扩展2倍,那么您还希望将majorIntervalLength的值更改为其当前值的两倍。如果扩展1.1,则将majorIntervalLength的当前值乘以1.1,然后将majorIntervalLength设置为该新值。

如果您发布图表缩放代码,我可以通过一些代码提供更详细的答案。

[UPDATE] 看了你的代码。我建议进行以下更改

更新PlotSpaceX之前

执行此操作:

intervalScale = (PlotSpaceX + 0.1 / PlotSpaceX)

然后像这样更新majorInterval

majorInterval = majorInterval*intervalScale

这应该以与x坐标完全相同的方式缩放轴间距。