开发者

Zooming axes in CorePlot

开发者 https://www.devze.com 2023-02-12 10:44 出处:网络
I have an app where I use CorePlot to plot a graph. I have implemented the zooming of the graph itself with pinch gestures, but I still can\'t make the labels near the axis (which contain numbers like

I have an app where I use CorePlot to plot a graph. I have implemented the zooming of the graph itself with pinch gestures, but I still can't make the labels near the axis (which contain numbers like 1, 2 etc.) zoom properly, so instead of 1 the major interval changes to 5 or 0.5 (or any other number) depending on the pinch gesture.

-(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


}

Here's how I zoom the plot

开发者_如何转开发
- (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);


}

In the scalePiece method I tried to change xMajorInterval and yMajorInterval and redraw axes but, unfortunately, this method is called so often during the pinch gesture that the labels just display huuuge numbers.

Can you help me, please?


It seems like the answer to this question depends on how you're doing the zooming of the graph itself.

You'll basically want to scale the majorIntervalLength in the same way you're scaling the ranges for your plot space. That is, if you expand the range by a factor of 2 then you want to also change the value of majorIntervalLength to twice it's current value. If you expand by 1.1, you'll multiply the current value of majorIntervalLength by 1.1 and then set majorIntervalLength to that new value.

If you post your code for the graph scaling I can provide a more detailed answer with some code.

[UPDATE] After looking at your code. I would recommend the following changes

before you update PlotSpaceX do this:

intervalScale = (PlotSpaceX + 0.1 / PlotSpaceX)

then update majorInterval like this

majorInterval = majorInterval*intervalScale

This should scale your axis interval in exactly the same way as your x coordinate.

0

精彩评论

暂无评论...
验证码 换一张
取 消