From 4a9ddc4f7ea9f79f92a2cbd420ea195f0bcb5c0c Mon Sep 17 00:00:00 2001 From: Yauheni Padshyvalau Date: Sun, 16 Jan 2022 23:41:51 +0300 Subject: [PATCH] Allow highlight per drag when drag is disabled on a scaled chart --- .../charting/charts/BarLineChartBase.java | 7 +++ .../listener/BarLineChartTouchListener.java | 52 +++++++------------ 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java index 0926dff244..d06db1d34b 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/charts/BarLineChartBase.java @@ -1159,6 +1159,13 @@ public void setScaleEnabled(boolean enabled) { this.mScaleYEnabled = enabled; } + /** + * Returns true if scale X or scale Y is enabled, false otherwise. + * + * @return + */ + public boolean isScaleEnabled() { return mScaleXEnabled || mScaleYEnabled; } + public void setScaleXEnabled(boolean enabled) { mScaleXEnabled = enabled; } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.java index 5685d32fa0..b4fe38a808 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.java @@ -112,9 +112,6 @@ public boolean onTouch(View v, MotionEvent event) { mGestureDetector.onTouchEvent(event); } - if (!mChart.isDragEnabled() && (!mChart.isScaleXEnabled() && !mChart.isScaleYEnabled())) - return true; - // Handle touch events here... switch (event.getAction() & MotionEvent.ACTION_MASK) { @@ -130,7 +127,7 @@ public boolean onTouch(View v, MotionEvent event) { case MotionEvent.ACTION_POINTER_DOWN: - if (event.getPointerCount() >= 2) { + if (mChart.isScaleEnabled() && event.getPointerCount() >= 2) { mChart.disableScroll(); @@ -178,43 +175,30 @@ public boolean onTouch(View v, MotionEvent event) { mChart.disableScroll(); - if (mChart.isScaleXEnabled() || mChart.isScaleYEnabled()) - performZoom(event); + performZoom(event); } else if (mTouchMode == NONE && Math.abs(distance(event.getX(), mTouchStartPoint.x, event.getY(), mTouchStartPoint.y)) > mDragTriggerDist) { - if (mChart.isDragEnabled()) { - - boolean shouldPan = !mChart.isFullyZoomedOut() || - !mChart.hasNoDragOffset(); - - if (shouldPan) { - - float distanceX = Math.abs(event.getX() - mTouchStartPoint.x); - float distanceY = Math.abs(event.getY() - mTouchStartPoint.y); - - // Disable dragging in a direction that's disallowed - if ((mChart.isDragXEnabled() || distanceY >= distanceX) && - (mChart.isDragYEnabled() || distanceY <= distanceX)) { - - mLastGesture = ChartGesture.DRAG; - mTouchMode = DRAG; - } + if (mChart.isDragEnabled() && (!mChart.isFullyZoomedOut() || !mChart.hasNoDragOffset())) { - } else { + float distanceX = Math.abs(event.getX() - mTouchStartPoint.x); + float distanceY = Math.abs(event.getY() - mTouchStartPoint.y); - if (mChart.isHighlightPerDragEnabled()) { - mLastGesture = ChartGesture.DRAG; + // Disable dragging in a direction that's disallowed + if ((mChart.isDragXEnabled() || distanceY >= distanceX) && + (mChart.isDragYEnabled() || distanceY <= distanceX)) { - if (mChart.isHighlightPerDragEnabled()) - performHighlightDrag(event); - } + mLastGesture = ChartGesture.DRAG; + mTouchMode = DRAG; } - } + } else if (mChart.isHighlightPerDragEnabled()) { + mLastGesture = ChartGesture.DRAG; + performHighlightDrag(event); + } } break; @@ -246,7 +230,7 @@ public boolean onTouch(View v, MotionEvent event) { } } - if (mTouchMode == X_ZOOM || + if (mChart.isScaleEnabled() && mTouchMode == X_ZOOM || mTouchMode == Y_ZOOM || mTouchMode == PINCH_ZOOM || mTouchMode == POST_ZOOM) { @@ -282,9 +266,11 @@ public boolean onTouch(View v, MotionEvent event) { break; } - // perform the transformation, update the chart - mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, true); + if (mChart.isDragEnabled() || mChart.isScaleEnabled()) { + // perform the transformation, update the chart + mMatrix = mChart.getViewPortHandler().refresh(mMatrix, mChart, true); + } return true; // indicate event was handled }