Skip to content

Commit e2cbafa

Browse files
committed
Code cleanup & new zoom method
1 parent 1a39ac9 commit e2cbafa

4 files changed

Lines changed: 88 additions & 60 deletions

File tree

‎MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java‎

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ public void computeScroll() {
590590
*/
591591

592592
protected Matrix mZoomInMatrixBuffer = new Matrix();
593+
593594
/**
594595
* Zooms in by 1.4f, into the charts center. center.
595596
*/
@@ -610,6 +611,7 @@ public void zoomIn() {
610611
}
611612

612613
protected Matrix mZoomOutMatrixBuffer = new Matrix();
614+
613615
/**
614616
* Zooms out by 0.7f, from the charts center. center.
615617
*/
@@ -630,6 +632,7 @@ public void zoomOut() {
630632
}
631633

632634
protected Matrix mZoomMatrixBuffer = new Matrix();
635+
633636
/**
634637
* Zooms in or out by the given scale factor. x and y are the coordinates
635638
* (in pixels) of the zoom center.
@@ -641,7 +644,7 @@ public void zoomOut() {
641644
*/
642645
public void zoom(float scaleX, float scaleY, float x, float y) {
643646
Matrix save = mZoomMatrixBuffer;
644-
mViewPortHandler.zoom(scaleX, scaleY, x, y, save);
647+
mViewPortHandler.zoom(scaleX, scaleY, x, -y, save);
645648
mViewPortHandler.refresh(save, this, false);
646649

647650
// Range might have changed, which means that Y-axis labels
@@ -653,20 +656,35 @@ public void zoom(float scaleX, float scaleY, float x, float y) {
653656

654657
/**
655658
* Zooms in or out by the given scale factor.
656-
* x and y are the values (NOT PIXELS) which to zoom to or from (the values of the zoom center).
659+
* x and y are the values (NOT PIXELS) of the zoom center..
657660
*
658661
* @param scaleX
659662
* @param scaleY
660663
* @param xValue
661664
* @param yValue
662665
* @param axis the axis relative to which the zoom should take place
663666
*/
664-
public void zoomAndCenter(float scaleX, float scaleY, float xValue, float yValue, AxisDependency axis) {
667+
public void zoom(float scaleX, float scaleY, float xValue, float yValue, AxisDependency axis) {
665668

666669
Runnable job = ZoomJob.getInstance(mViewPortHandler, scaleX, scaleY, xValue, yValue, getTransformer(axis), axis, this);
667670
addViewportJob(job);
668671
}
669672

673+
/**
674+
* Zooms to the center of the chart with the given scale factor.
675+
*
676+
* @param scaleX
677+
* @param scaleY
678+
*/
679+
public void zoomCenter(float scaleX, float scaleY) {
680+
681+
MPPointF center = getCenterOffsets();
682+
683+
Matrix save = mZoomMatrixBuffer;
684+
mViewPortHandler.zoom(scaleX, scaleY, center.x, -center.y, save);
685+
mViewPortHandler.refresh(save, this, false);
686+
}
687+
670688
/**
671689
* Zooms by the specified scale factor to the specified values on the specified axis.
672690
*
@@ -686,7 +704,7 @@ public void zoomAndCenterAnimated(float scaleX, float scaleY, float xValue, floa
686704
MPPointD origin = getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentTop(), axis);
687705

688706
Runnable job = AnimatedZoomJob.getInstance(mViewPortHandler, this, getTransformer(axis), getAxis(axis), mXAxis
689-
.mAxisRange, scaleX, scaleY, mViewPortHandler.getScaleX(), mViewPortHandler.getScaleY(),
707+
.mAxisRange, scaleX, scaleY, mViewPortHandler.getScaleX(), mViewPortHandler.getScaleY(),
690708
xValue, yValue, (float) origin.x, (float) origin.y, duration);
691709
addViewportJob(job);
692710

@@ -698,6 +716,7 @@ public void zoomAndCenterAnimated(float scaleX, float scaleY, float xValue, floa
698716
}
699717

700718
protected Matrix mFitScreenMatrixBuffer = new Matrix();
719+
701720
/**
702721
* Resets all zooming and dragging and makes the chart fit exactly it's
703722
* bounds.
@@ -771,7 +790,7 @@ public void setVisibleXRange(float minXRange, float maxXRange) {
771790
* @param axis the axis for which this limit should apply
772791
*/
773792
public void setVisibleYRangeMaximum(float maxYRange, AxisDependency axis) {
774-
float yScale = getDeltaY(axis) / maxYRange;
793+
float yScale = getAxisRange(axis) / maxYRange;
775794
mViewPortHandler.setMinimumScaleY(yScale);
776795
}
777796

@@ -782,7 +801,7 @@ public void setVisibleYRangeMaximum(float maxYRange, AxisDependency axis) {
782801
* @param axis the axis for which this limit should apply
783802
*/
784803
public void setVisibleYRangeMinimum(float minYRange, AxisDependency axis) {
785-
float yScale = getDeltaY(axis) / minYRange;
804+
float yScale = getAxisRange(axis) / minYRange;
786805
mViewPortHandler.setMaximumScaleY(yScale);
787806
}
788807

@@ -794,8 +813,8 @@ public void setVisibleYRangeMinimum(float minYRange, AxisDependency axis) {
794813
* @param axis
795814
*/
796815
public void setVisibleYRange(float minYRange, float maxYRange, AxisDependency axis) {
797-
float minScale = getDeltaY(axis) / minYRange;
798-
float maxScale = getDeltaY(axis) / maxYRange;
816+
float minScale = getAxisRange(axis) / minYRange;
817+
float maxScale = getAxisRange(axis) / maxYRange;
799818
mViewPortHandler.setMinMaxScaleY(minScale, maxScale);
800819
}
801820

@@ -825,7 +844,7 @@ public void moveViewToX(float xValue) {
825844
*/
826845
public void moveViewTo(float xValue, float yValue, AxisDependency axis) {
827846

828-
float yInView = getDeltaY(axis) / mViewPortHandler.getScaleY();
847+
float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY();
829848

830849
Runnable job = MoveViewJob.getInstance(mViewPortHandler, xValue, yValue + yInView / 2f,
831850
getTransformer(axis), this);
@@ -850,7 +869,7 @@ public void moveViewToAnimated(float xValue, float yValue, AxisDependency axis,
850869

851870
MPPointD bounds = getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentTop(), axis);
852871

853-
float yInView = getDeltaY(axis) / mViewPortHandler.getScaleY();
872+
float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY();
854873

855874
Runnable job = AnimatedMoveViewJob.getInstance(mViewPortHandler, xValue, yValue + yInView / 2f,
856875
getTransformer(axis), this, (float) bounds.x, (float) bounds.y, duration);
@@ -872,7 +891,7 @@ public void moveViewToAnimated(float xValue, float yValue, AxisDependency axis,
872891
*/
873892
public void centerViewToY(float yValue, AxisDependency axis) {
874893

875-
float valsInView = getDeltaY(axis) / mViewPortHandler.getScaleY();
894+
float valsInView = getAxisRange(axis) / mViewPortHandler.getScaleY();
876895

877896
Runnable job = MoveViewJob.getInstance(mViewPortHandler, 0f, yValue + valsInView / 2f,
878897
getTransformer(axis), this);
@@ -891,7 +910,7 @@ public void centerViewToY(float yValue, AxisDependency axis) {
891910
*/
892911
public void centerViewTo(float xValue, float yValue, AxisDependency axis) {
893912

894-
float yInView = getDeltaY(axis) / mViewPortHandler.getScaleY();
913+
float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY();
895914
float xInView = getXAxis().mAxisRange / mViewPortHandler.getScaleX();
896915

897916
Runnable job = MoveViewJob.getInstance(mViewPortHandler,
@@ -917,7 +936,7 @@ public void centerViewToAnimated(float xValue, float yValue, AxisDependency axis
917936

918937
MPPointD bounds = getValuesByTouchPoint(mViewPortHandler.contentLeft(), mViewPortHandler.contentTop(), axis);
919938

920-
float yInView = getDeltaY(axis) / mViewPortHandler.getScaleY();
939+
float yInView = getAxisRange(axis) / mViewPortHandler.getScaleY();
921940
float xInView = getXAxis().mAxisRange / mViewPortHandler.getScaleX();
922941

923942
Runnable job = AnimatedMoveViewJob.getInstance(mViewPortHandler,
@@ -980,12 +999,12 @@ public void resetViewPortOffsets() {
980999
/** CODE BELOW IS GETTERS AND SETTERS */
9811000

9821001
/**
983-
* Returns the delta-y value (y-value range) of the specified axis.
1002+
* Returns the range of the specified axis.
9841003
*
9851004
* @param axis
9861005
* @return
9871006
*/
988-
public float getDeltaY(AxisDependency axis) {
1007+
protected float getAxisRange(AxisDependency axis) {
9891008
if (axis == AxisDependency.LEFT)
9901009
return mAxisLeft.mAxisRange;
9911010
else
@@ -1011,6 +1030,7 @@ public OnDrawListener getDrawListener() {
10111030
}
10121031

10131032
protected float[] mGetPositionBuffer = new float[2];
1033+
10141034
/**
10151035
* Returns a recyclable MPPointF instance.
10161036
* Returns the position (in pixels) the provided Entry has inside the chart
@@ -1213,12 +1233,12 @@ public void setKeepPositionOnRotation(boolean keepPositionOnRotation) {
12131233
* @return
12141234
*/
12151235
public MPPointD getValuesByTouchPoint(float x, float y, AxisDependency axis) {
1216-
MPPointD result = MPPointD.getInstance(0,0);
1217-
getValuesByTouchPoint(x,y,axis,result);
1236+
MPPointD result = MPPointD.getInstance(0, 0);
1237+
getValuesByTouchPoint(x, y, axis, result);
12181238
return result;
12191239
}
12201240

1221-
public void getValuesByTouchPoint(float x, float y, AxisDependency axis, MPPointD outputPoint){
1241+
public void getValuesByTouchPoint(float x, float y, AxisDependency axis, MPPointD outputPoint) {
12221242
getTransformer(axis).getValuesByTouchPoint(x, y, outputPoint);
12231243
}
12241244

@@ -1235,7 +1255,8 @@ public MPPointD getPixelsForValues(float x, float y, AxisDependency axis) {
12351255
return getTransformer(axis).getPixelsForValues(x, y);
12361256
}
12371257

1238-
MPPointD pointForGetYValueByTouchPoint = MPPointD.getInstance(0,0);
1258+
MPPointD pointForGetYValueByTouchPoint = MPPointD.getInstance(0, 0);
1259+
12391260
/**
12401261
* Returns y value at the given touch position (must not necessarily be
12411262
* a value contained in one of the datasets)
@@ -1280,8 +1301,10 @@ public IBarLineScatterCandleBubbleDataSet getDataSetByTouchPoint(float x, float
12801301
return null;
12811302
}
12821303

1283-
/** buffer for storing lowest visible x point */
1284-
protected MPPointD posForGetLowestVisibleX = MPPointD.getInstance(0,0);
1304+
/**
1305+
* buffer for storing lowest visible x point
1306+
*/
1307+
protected MPPointD posForGetLowestVisibleX = MPPointD.getInstance(0, 0);
12851308

12861309
/**
12871310
* Returns the lowest x-index (value on the x-axis) that is still visible on
@@ -1297,8 +1320,10 @@ public float getLowestVisibleX() {
12971320
return result;
12981321
}
12991322

1300-
/** buffer for storing highest visible x point */
1301-
protected MPPointD posForGetHighestVisibleX = MPPointD.getInstance(0,0);
1323+
/**
1324+
* buffer for storing highest visible x point
1325+
*/
1326+
protected MPPointD posForGetHighestVisibleX = MPPointD.getInstance(0, 0);
13021327

13031328
/**
13041329
* Returns the highest x-index (value on the x-axis) that is still visible
@@ -1547,16 +1572,17 @@ public Paint getPaint(int which) {
15471572
}
15481573

15491574
protected float[] mOnSizeChangedBuffer = new float[2];
1575+
15501576
@Override
15511577
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
15521578

15531579
// Saving current position of chart.
1554-
float[] pts = mOnSizeChangedBuffer;
1555-
pts[0] = pts[1] = 0;
1580+
mOnSizeChangedBuffer[0] = mOnSizeChangedBuffer[1] = 0;
1581+
15561582
if (mKeepPositionOnRotation) {
1557-
pts[0] = mViewPortHandler.contentLeft();
1558-
pts[1] = mViewPortHandler.contentTop();
1559-
getTransformer(AxisDependency.LEFT).pixelsToValue(pts);
1583+
mOnSizeChangedBuffer[0] = mViewPortHandler.contentLeft();
1584+
mOnSizeChangedBuffer[1] = mViewPortHandler.contentTop();
1585+
getTransformer(AxisDependency.LEFT).pixelsToValue(mOnSizeChangedBuffer);
15601586
}
15611587

15621588
//Superclass transforms chart.
@@ -1565,8 +1591,8 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
15651591
if (mKeepPositionOnRotation) {
15661592

15671593
//Restoring old position of chart.
1568-
getTransformer(AxisDependency.LEFT).pointValuesToPixel(pts);
1569-
mViewPortHandler.centerViewPort(pts, this);
1594+
getTransformer(AxisDependency.LEFT).pointValuesToPixel(mOnSizeChangedBuffer);
1595+
mViewPortHandler.centerViewPort(mOnSizeChangedBuffer, this);
15701596
} else {
15711597
mViewPortHandler.refresh(mViewPortHandler.getMatrixTouch(), this, true);
15721598
}

‎MPChartLib/src/main/java/com/github/mikephil/charting/charts/HorizontalBarChart.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,20 @@ public void setVisibleXRange(float minXRange, float maxXRange) {
248248

249249
@Override
250250
public void setVisibleYRangeMaximum(float maxYRange, AxisDependency axis) {
251-
float yScale = getDeltaY(axis) / maxYRange;
251+
float yScale = getAxisRange(axis) / maxYRange;
252252
mViewPortHandler.setMinimumScaleX(yScale);
253253
}
254254

255255
@Override
256256
public void setVisibleYRangeMinimum(float minYRange, AxisDependency axis) {
257-
float yScale = getDeltaY(axis) / minYRange;
257+
float yScale = getAxisRange(axis) / minYRange;
258258
mViewPortHandler.setMaximumScaleX(yScale);
259259
}
260260

261261
@Override
262262
public void setVisibleYRange(float minYRange, float maxYRange, AxisDependency axis) {
263-
float minScale = getDeltaY(axis) / minYRange;
264-
float maxScale = getDeltaY(axis) / maxYRange;
263+
float minScale = getAxisRange(axis) / minYRange;
264+
float maxScale = getAxisRange(axis) / maxYRange;
265265
mViewPortHandler.setMinMaxScaleX(minScale, maxScale);
266266
}
267267
}

‎MPChartLib/src/main/java/com/github/mikephil/charting/jobs/ZoomJob.java‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public class ZoomJob extends ViewPortJob {
1818
private static ObjectPool<ZoomJob> pool;
1919

2020
static {
21-
pool = ObjectPool.create(1, new ZoomJob(null,0,0,0,0,null,null,null));
21+
pool = ObjectPool.create(1, new ZoomJob(null, 0, 0, 0, 0, null, null, null));
2222
pool.setReplenishPercentage(0.5f);
2323
}
2424

25-
public static ZoomJob getInstance(ViewPortHandler viewPortHandler, float scaleX, float scaleY, float xValue, float yValue, Transformer trans, YAxis.AxisDependency axis, View v) {
25+
public static ZoomJob getInstance(ViewPortHandler viewPortHandler, float scaleX, float scaleY, float xValue, float yValue,
26+
Transformer trans, YAxis.AxisDependency axis, View v) {
2627
ZoomJob result = pool.get();
2728
result.xValue = xValue;
2829
result.yValue = yValue;
@@ -35,7 +36,7 @@ public static ZoomJob getInstance(ViewPortHandler viewPortHandler, float scaleX,
3536
return result;
3637
}
3738

38-
public static void recycleInstance(ZoomJob instance){
39+
public static void recycleInstance(ZoomJob instance) {
3940
pool.recycle(instance);
4041
}
4142

@@ -44,7 +45,8 @@ public static void recycleInstance(ZoomJob instance){
4445

4546
protected YAxis.AxisDependency axisDependency;
4647

47-
public ZoomJob(ViewPortHandler viewPortHandler, float scaleX, float scaleY, float xValue, float yValue, Transformer trans, YAxis.AxisDependency axis, View v) {
48+
public ZoomJob(ViewPortHandler viewPortHandler, float scaleX, float scaleY, float xValue, float yValue, Transformer trans,
49+
YAxis.AxisDependency axis, View v) {
4850
super(viewPortHandler, xValue, yValue, trans, v);
4951

5052
this.scaleX = scaleX;
@@ -53,18 +55,19 @@ public ZoomJob(ViewPortHandler viewPortHandler, float scaleX, float scaleY, floa
5355
}
5456

5557
protected Matrix mRunMatrixBuffer = new Matrix();
58+
5659
@Override
5760
public void run() {
5861

5962
Matrix save = mRunMatrixBuffer;
6063
mViewPortHandler.zoom(scaleX, scaleY, save);
6164
mViewPortHandler.refresh(save, view, false);
6265

63-
float valsInView = ((BarLineChartBase) view).getDeltaY(axisDependency) / mViewPortHandler.getScaleY();
64-
float xsInView = ((BarLineChartBase) view).getXAxis().mAxisRange / mViewPortHandler.getScaleX();
66+
float yValsInView = ((BarLineChartBase) view).getAxis(axisDependency).mAxisRange / mViewPortHandler.getScaleY();
67+
float xValsInView = ((BarLineChartBase) view).getXAxis().mAxisRange / mViewPortHandler.getScaleX();
6568

66-
pts[0] = xValue - xsInView / 2f;
67-
pts[1] = yValue + valsInView / 2f;
69+
pts[0] = xValue - xValsInView / 2f;
70+
pts[1] = yValue + yValsInView / 2f;
6871

6972
mTrans.pointValuesToPixel(pts);
7073

@@ -79,6 +82,6 @@ public void run() {
7982

8083
@Override
8184
protected ObjectPool.Poolable instantiate() {
82-
return new ZoomJob(null,0,0,0,0,null,null,null);
85+
return new ZoomJob(null, 0, 0, 0, 0, null, null, null);
8386
}
8487
}

0 commit comments

Comments
 (0)