Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset the zoom level in MPAndroidChart?

I am using MPAndroidChart, how I can reset the zoom? For example:

1. User make zoomming in the chart
2. User click one button to reset the zoom (back to the default zoom)

like image 231
rafaelasguerra Avatar asked Sep 18 '15 11:09

rafaelasguerra


2 Answers

You can reset the zoom by calling chart.fitScreen(). This will reset the chart viewport to it's original state (fully zoomed out).

like image 132
Philipp Jahoda Avatar answered Oct 18 '22 16:10

Philipp Jahoda


I think that the flagged answer is not complete, in case the whole chart is not shown in the viewport (e.g. max 5 samples in viewport over the total of 20 dataset samples). Checking deeply the documentation I've found a working solution for me:

  • zoom(float scaleX, float scaleY, float x, float y): Zooms in or out by the given scale factor. x and y are the coordinates (in pixels) of the zoom center. Remember that a scale of 1f = no zoom.
  • zoom(float scaleX, float scaleY, float xValue, float yValue, AxisDependency axis): Zooms in or out by the given scale factor. xValue and yValue are the actual data values (not pixels) of the zoom center. Remember that a scale of 1f = no zoom.
  • zoomAndCenterAnimated(float scaleX, float scaleY, float xValue, float yValue, AxisDependency axis, long duration): Zooms by the specified scale factor and centers the viewport to the specified values on the specified axis in an animated way (v2.2.3 or above).

Calling one of the methods above will provide the trick (e.g. zoomAndCenterAnimated(1f, 1f, 0, 0, AxisDependency.LEFT, 500L)).

like image 28
matmacci Avatar answered Oct 18 '22 16:10

matmacci