Search code examples
ios6xamarin.iosplotcore-plotpinchzoom

Implementing true Pinch and Zoom in the OxyPlot 2D library with MonoTouch


For plotting graphs I used the coreplot library for a while in my MonoTouch based iPhone app, but with iOS 6.0 the already annoying binding problems become so many that I decided to drop it for a library natively written in C#.

Searching around I found the excellent OxyPlot 2D library, and more specifically the MonoTouch port made by dvkwong.

The library works fine and has tons of useful features, but its output is just a rendered bitmap UIImage.

This means that I need to add myself the pinch and zoom features to the library.

The current implementation, based on the dvkwong preliminary example, uses the UIScrollView to zoom and unzoom the resulting bitmap image added to a simple subview.

This is not a good solution because when zooming the aliasing of the bitmap is made visible , and if the resolution is increased the text fonts becomes unreadable because are not optimized for the current zoom resolution.

I need to render the image each time at the correct resolution, without using UIScrollView but just overriding the DrawRect() call in a custom UIView.

But how to reproduce the the pinch-zoom gesture of Apple UIScrollView and draw the correct subrect of the OxyPlot plot model?

I tried to implement this method suggested here:

position the pinched view between the two fingers

But it doesn't work because I need to know the sub rect to draw, not applying a transformation matrix. Also there is no "draw sub rect" method in the OxyPlot library, so I need to set a cliprect in the image context and drawing a bigger image first and then clipping it. This is clearly too slow, because at some zoom levels the image can become huge (and I need the user to be able to zoom indefinitely on any part of the graph).

Any help is appreciated. Thanks in advance.


Solution

  • I solved the problem myself. I've created another MonoTouch port of the OxyPlot 2D library, this time supporting both Pan & Pinch-Zoom gestures. I've also added iPad support. Now we have a native C# plot library for MonoTouch.

    You can download it under the MIT Licence here:

    https://github.com/Emasoft/OxyPlot.2DGraphLib.MonoTouch