Skip to content

Commit 4845e1b

Browse files
committed
Fix issue regarding corrupted translation (issue PhilJay#1551)
1 parent f2771a6 commit 4845e1b

3 files changed

Lines changed: 80 additions & 34 deletions

File tree

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/LineChartActivity2.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ protected void onCreate(Bundle savedInstanceState) {
5959

6060
mChart = (LineChart) findViewById(R.id.chart1);
6161
mChart.setOnChartValueSelectedListener(this);
62-
62+
6363
// no description text
6464
mChart.setDescription("");
6565
mChart.setNoDataTextDescription("You need to provide data for the chart.");
6666

6767
// enable touch gestures
6868
mChart.setTouchEnabled(true);
69-
69+
7070
mChart.setDragDecelerationFrictionCoef(0.9f);
7171

7272
// enable scaling and dragging
@@ -114,7 +114,7 @@ protected void onCreate(Bundle savedInstanceState) {
114114
leftAxis.setAxisMaxValue(200f);
115115
leftAxis.setAxisMinValue(0f);
116116
leftAxis.setDrawGridLines(true);
117-
117+
118118
YAxis rightAxis = mChart.getAxisRight();
119119
rightAxis.setTypeface(tf);
120120
rightAxis.setTextColor(Color.RED);
@@ -148,7 +148,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
148148
break;
149149
}
150150
case R.id.actionToggleHighlight: {
151-
if(mChart.getData() != null) {
151+
if (mChart.getData() != null) {
152152
mChart.getData().setHighlightEnabled(!mChart.getData().isHighlightEnabled());
153153
mChart.invalidate();
154154
}
@@ -266,8 +266,8 @@ private void setData(int count, float range) {
266266
for (int i = 0; i < count; i++) {
267267
float mult = range / 2f;
268268
float val = (float) (Math.random() * mult) + 50;// + (float)
269-
// ((mult *
270-
// 0.1) / 10);
269+
// ((mult *
270+
// 0.1) / 10);
271271
yVals1.add(new Entry(val, i));
272272
}
273273

@@ -292,8 +292,8 @@ private void setData(int count, float range) {
292292
for (int i = 0; i < count; i++) {
293293
float mult = range;
294294
float val = (float) (Math.random() * mult) + 450;// + (float)
295-
// ((mult *
296-
// 0.1) / 10);
295+
// ((mult *
296+
// 0.1) / 10);
297297
yVals2.add(new Entry(val, i));
298298
}
299299

‎MPChartLib/src/com/github/mikephil/charting/listener/BarLineChartTouchListener.java‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,19 @@ public class BarLineChartTouchListener extends ChartTouchListener<BarLineChartBa
6868
*/
6969
private float mDragTriggerDist;
7070

71+
/**
72+
* the minimum distance between the pointers that will trigger a zoom gesture
73+
*/
74+
private float mMinScalePointerDistance;
75+
7176
public BarLineChartTouchListener(BarLineChartBase<? extends BarLineScatterCandleBubbleData<? extends IBarLineScatterCandleBubbleDataSet<? extends Entry>>> chart, Matrix touchMatrix) {
7277
super(chart);
7378
this.mMatrix = touchMatrix;
7479

7580
// this equals to about 9 pixels on a 5.5" FHD screen
7681
this.mDragTriggerDist = Utils.convertDpToPixel(3f);
82+
83+
this.mMinScalePointerDistance = Utils.convertDpToPixel(3.5f);
7784
}
7885

7986
@SuppressLint("ClickableViewAccessibility")
@@ -162,7 +169,6 @@ public boolean onTouch(View v, MotionEvent event) {
162169
&& Math.abs(distance(event.getX(), mTouchStartPoint.x, event.getY(),
163170
mTouchStartPoint.y)) > mDragTriggerDist) {
164171

165-
166172
if (mChart.hasNoDragOffset()) {
167173

168174
if (!mChart.isFullyZoomedOut() && mChart.isDragEnabled()) {
@@ -241,8 +247,7 @@ public boolean onTouch(View v, MotionEvent event) {
241247
break;
242248
}
243249

244-
// Perform the transformation, update the chart
245-
// if (needsRefresh())
250+
// perform the transformation, update the chart
246251
mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, true);
247252

248253
return true; // indicate event was handled
@@ -311,36 +316,40 @@ private void performDrag(MotionEvent event) {
311316
*/
312317
private void performZoom(MotionEvent event) {
313318

314-
if (event.getPointerCount() >= 2) {
319+
if (event.getPointerCount() >= 2) { // two finger zoom
315320

316321
OnChartGestureListener l = mChart.getOnChartGestureListener();
317322

318-
// get the distance between the pointers of the touch
319-
// event
323+
// get the distance between the pointers of the touch event
320324
float totalDist = spacing(event);
321325

322-
if (totalDist > 10f) {
326+
if (totalDist > mMinScalePointerDistance) {
323327

324328
// get the translation
325329
PointF t = getTrans(mTouchPointCenter.x, mTouchPointCenter.y);
330+
ViewPortHandler h = mChart.getViewPortHandler();
326331

327-
// take actions depending on the activated touch
328-
// mode
332+
// take actions depending on the activated touch mode
329333
if (mTouchMode == PINCH_ZOOM) {
330334

331335
mLastGesture = ChartGesture.PINCH_ZOOM;
332336

333337
float scale = totalDist / mSavedDist; // total scale
334338

335339
boolean isZoomingOut = (scale < 1);
340+
336341
boolean canZoomMoreX = isZoomingOut ?
337-
mChart.getViewPortHandler().canZoomOutMoreX() :
338-
mChart.getViewPortHandler().canZoomInMoreX();
342+
h.canZoomOutMoreX() :
343+
h.canZoomInMoreX();
344+
345+
boolean canZoomMoreY = isZoomingOut ?
346+
h.canZoomOutMoreY() :
347+
h.canZoomInMoreY();
339348

340349
float scaleX = (mChart.isScaleXEnabled()) ? scale : 1f;
341350
float scaleY = (mChart.isScaleYEnabled()) ? scale : 1f;
342351

343-
if (mChart.isScaleYEnabled() || canZoomMoreX) {
352+
if (canZoomMoreY || canZoomMoreX) {
344353

345354
mMatrix.set(mSavedMatrix);
346355
mMatrix.postScale(scaleX, scaleY, t.x, t.y);
@@ -358,8 +367,8 @@ private void performZoom(MotionEvent event) {
358367

359368
boolean isZoomingOut = (scaleX < 1);
360369
boolean canZoomMoreX = isZoomingOut ?
361-
mChart.getViewPortHandler().canZoomOutMoreX() :
362-
mChart.getViewPortHandler().canZoomInMoreX();
370+
h.canZoomOutMoreX() :
371+
h.canZoomInMoreX();
363372

364373
if (canZoomMoreX) {
365374

@@ -377,13 +386,19 @@ private void performZoom(MotionEvent event) {
377386
float yDist = getYDist(event);
378387
float scaleY = yDist / mSavedYDist; // y-axis scale
379388

380-
mMatrix.set(mSavedMatrix);
389+
boolean isZoomingOut = (scaleY < 1);
390+
boolean canZoomMoreY = isZoomingOut ?
391+
h.canZoomOutMoreY() :
392+
h.canZoomInMoreY();
393+
394+
if (canZoomMoreY) {
381395

382-
// y-axis comes from top to bottom, revert y
383-
mMatrix.postScale(1f, scaleY, t.x, t.y);
396+
mMatrix.set(mSavedMatrix);
397+
mMatrix.postScale(1f, scaleY, t.x, t.y);
384398

385-
if (l != null)
386-
l.onChartScale(event, 1f, scaleY);
399+
if (l != null)
400+
l.onChartScale(event, 1f, scaleY);
401+
}
387402
}
388403
}
389404
}
@@ -400,7 +415,7 @@ private void performHighlightDrag(MotionEvent e) {
400415

401416
if (h != null && !h.equalTo(mLastHighlighted)) {
402417
mLastHighlighted = h;
403-
mChart.highlightTouch(h);
418+
mChart.highlightValue(h, true);
404419
}
405420
}
406421

‎MPChartLib/src/com/github/mikephil/charting/utils/ViewPortHandler.java‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public void centerViewPort(final float[] transformedPts, final View view) {
352352
}
353353

354354
/**
355-
* buffer for storing matrix values
355+
* buffer for storing the 9 matrix values of a 3x3 matrix
356356
*/
357357
protected final float[] matrixBuffer = new float[9];
358358

@@ -391,7 +391,7 @@ public void limitTransAndScale(Matrix matrix, RectF content) {
391391
float curTransY = matrixBuffer[Matrix.MTRANS_Y];
392392
float curScaleY = matrixBuffer[Matrix.MSCALE_Y];
393393

394-
// min scale-x is 1f, max is the max float
394+
// min scale-x is 1f
395395
mScaleX = Math.min(Math.max(mMinScaleX, curScaleX), mMaxScaleX);
396396

397397
// min scale-y is 1f
@@ -406,12 +406,10 @@ public void limitTransAndScale(Matrix matrix, RectF content) {
406406
}
407407

408408
float maxTransX = -width * (mScaleX - 1f);
409-
float newTransX = Math.min(Math.max(curTransX, maxTransX - mTransOffsetX), mTransOffsetX);
410-
mTransX = newTransX;
409+
mTransX = Math.min(Math.max(curTransX, maxTransX - mTransOffsetX), mTransOffsetX);
411410

412411
float maxTransY = height * (mScaleY - 1f);
413-
float newTransY = Math.max(Math.min(curTransY, maxTransY + mTransOffsetY), -mTransOffsetY);
414-
mTransY = newTransY;
412+
mTransY = Math.max(Math.min(curTransY, maxTransY + mTransOffsetY), -mTransOffsetY);
415413

416414
matrixBuffer[Matrix.MTRANS_X] = mTransX;
417415
matrixBuffer[Matrix.MSCALE_X] = mScaleX;
@@ -562,6 +560,22 @@ public float getScaleY() {
562560
return mScaleY;
563561
}
564562

563+
public float getMinScaleX() {
564+
return mMinScaleX;
565+
}
566+
567+
public float getMaxScaleX() {
568+
return mMaxScaleX;
569+
}
570+
571+
public float getMinScaleY() {
572+
return mMinScaleY;
573+
}
574+
575+
public float getMaxScaleY() {
576+
return mMaxScaleY;
577+
}
578+
565579
/**
566580
* Returns the translation (drag / pan) distance on the x-axis
567581
*
@@ -665,4 +679,21 @@ public boolean canZoomInMoreX() {
665679
return (mScaleX < mMaxScaleX);
666680
}
667681

682+
/**
683+
* Returns true if the chart is not yet fully zoomed out on the y-axis
684+
*
685+
* @return
686+
*/
687+
public boolean canZoomOutMoreY() {
688+
return (mScaleY > mMinScaleY);
689+
}
690+
691+
/**
692+
* Returns true if the chart is not yet fully zoomed in on the y-axis
693+
*
694+
* @return
695+
*/
696+
public boolean canZoomInMoreY() {
697+
return (mScaleY < mMaxScaleY);
698+
}
668699
}

0 commit comments

Comments
 (0)