diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt index 260cd749d9..8045a8b6cf 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt @@ -29,11 +29,6 @@ class BarLineChartTouchListener( dragTriggerDistance: Float ) : ChartTouchListener?>?>?>(chart) { - /** - * returns the matrix object the listener holds - * - * @return - */ /** * the original touch-matrix from the chart */ @@ -59,16 +54,16 @@ class BarLineChartTouchListener( private var savedYDist = 1f private var savedDist = 1f - private var mClosestDataSetToTouch: IDataSet<*>? = null + private var closestDataSetToTouch: IDataSet<*>? = null /** * used for tracking velocity of dragging */ - private var mVelocityTracker: VelocityTracker? = null + private var velocityTracker: VelocityTracker? = null - private var mDecelerationLastTime: Long = 0 - private val mDecelerationCurrentPoint: MPPointF = MPPointF.getInstance(0f, 0f) - private val mDecelerationVelocity: MPPointF = MPPointF.getInstance(0f, 0f) + private var decelerationLastTime: Long = 0 + private val decelerationCurrentPoint: MPPointF = MPPointF.getInstance(0f, 0f) + private val decelerationVelocity: MPPointF = MPPointF.getInstance(0f, 0f) /** * the distance of movement that will be counted as a drag @@ -102,23 +97,23 @@ class BarLineChartTouchListener( @SuppressLint("ClickableViewAccessibility") override fun onTouch(v: View, event: MotionEvent): Boolean { - if (mVelocityTracker == null) { - mVelocityTracker = VelocityTracker.obtain() + if (velocityTracker == null) { + velocityTracker = VelocityTracker.obtain() } - mVelocityTracker!!.addMovement(event) + velocityTracker!!.addMovement(event) if (event.actionMasked == MotionEvent.ACTION_CANCEL) { - if (mVelocityTracker != null) { - mVelocityTracker!!.recycle() - mVelocityTracker = null + if (velocityTracker != null) { + velocityTracker!!.recycle() + velocityTracker = null } } - if (mTouchMode == NONE || mChart!!.isFlingEnabled) { - mGestureDetector.onTouchEvent(event) + if (touchMode == NONE || chart!!.isFlingEnabled) { + gestureDetector?.onTouchEvent(event) } - if (!mChart!!.isDragEnabled && (!mChart!!.isScaleXEnabled && !mChart!!.isScaleYEnabled)) return true + if (!chart!!.isDragEnabled && (!chart!!.isScaleXEnabled && !chart!!.isScaleYEnabled)) return true // Handle touch events here... when (event.action and MotionEvent.ACTION_MASK) { @@ -131,7 +126,7 @@ class BarLineChartTouchListener( } MotionEvent.ACTION_POINTER_DOWN -> if (event.pointerCount >= 2) { - mChart!!.disableScroll() + chart!!.disableScroll() saveTouchStart(event) @@ -145,11 +140,11 @@ class BarLineChartTouchListener( savedDist = spacing(event) if (savedDist > 10f) { - mTouchMode = if (mChart!!.isPinchZoomEnabled) { + touchMode = if (chart!!.isPinchZoomEnabled) { PINCH_ZOOM } else { - if (mChart!!.isScaleXEnabled != mChart!!.isScaleYEnabled) { - if (mChart!!.isScaleXEnabled) X_ZOOM else Y_ZOOM + if (chart!!.isScaleXEnabled != chart!!.isScaleYEnabled) { + if (chart!!.isScaleXEnabled) X_ZOOM else Y_ZOOM } else { if (savedXDist > savedYDist) X_ZOOM else Y_ZOOM } @@ -160,18 +155,18 @@ class BarLineChartTouchListener( midPoint(touchPointCenter, event) } - MotionEvent.ACTION_MOVE -> if (mTouchMode == DRAG) { - mChart!!.disableScroll() + MotionEvent.ACTION_MOVE -> if (touchMode == DRAG) { + chart!!.disableScroll() - val x = if (mChart!!.isDragXEnabled) event.x - touchStartPoint.x else 0f - val y = if (mChart!!.isDragYEnabled) event.y - touchStartPoint.y else 0f + val x = if (chart!!.isDragXEnabled) event.x - touchStartPoint.x else 0f + val y = if (chart!!.isDragYEnabled) event.y - touchStartPoint.y else 0f performDrag(event, x, y) - } else if (mTouchMode == X_ZOOM || mTouchMode == Y_ZOOM || mTouchMode == PINCH_ZOOM) { - mChart!!.disableScroll() + } else if (touchMode == X_ZOOM || touchMode == Y_ZOOM || touchMode == PINCH_ZOOM) { + chart!!.disableScroll() - if (mChart!!.isScaleXEnabled || mChart!!.isScaleYEnabled) performZoom(event) - } else if (mTouchMode == NONE + if (chart!!.isScaleXEnabled || chart!!.isScaleYEnabled) performZoom(event) + } else if (touchMode == NONE && abs( distance( event.x, touchStartPoint.x, event.y, @@ -179,89 +174,90 @@ class BarLineChartTouchListener( ).toDouble() ) > dragTriggerDist ) { - if (mChart!!.isDragEnabled) { - val shouldPan = !mChart!!.isFullyZoomedOut || - !mChart!!.hasNoDragOffset() + if (chart!!.isDragEnabled) { + val shouldPan = !chart!!.isFullyZoomedOut || + !chart!!.hasNoDragOffset() if (shouldPan) { val distanceX = abs((event.x - touchStartPoint.x).toDouble()).toFloat() val distanceY = abs((event.y - touchStartPoint.y).toDouble()).toFloat() // Disable dragging in a direction that's disallowed - if ((mChart!!.isDragXEnabled || distanceY >= distanceX) && - (mChart!!.isDragYEnabled || distanceY <= distanceX) + if ((chart!!.isDragXEnabled || distanceY >= distanceX) && + (chart!!.isDragYEnabled || distanceY <= distanceX) ) { - mLastGesture = ChartGesture.DRAG - mTouchMode = DRAG + lastGesture = ChartGesture.DRAG + touchMode = DRAG } } else { - if (mChart!!.isHighlightPerDragEnabled) { - mLastGesture = ChartGesture.DRAG + if (chart!!.isHighlightPerDragEnabled) { + lastGesture = ChartGesture.DRAG - if (mChart!!.isHighlightPerDragEnabled) performHighlightDrag(event) + if (chart!!.isHighlightPerDragEnabled) performHighlightDrag(event) } } } } MotionEvent.ACTION_UP -> { - val velocityTracker = mVelocityTracker - val pointerId = event.getPointerId(0) - velocityTracker!!.computeCurrentVelocity(1000, Utils.maximumFlingVelocity.toFloat()) - val velocityY = velocityTracker.getYVelocity(pointerId) - val velocityX = velocityTracker.getXVelocity(pointerId) + velocityTracker?.let { + val pointerId = event.getPointerId(0) + it.computeCurrentVelocity(1000, Utils.maximumFlingVelocity.toFloat()) + val velocityY = it.getYVelocity(pointerId) + val velocityX = it.getXVelocity(pointerId) - if (abs(velocityX.toDouble()) > Utils.minimumFlingVelocity || - abs(velocityY.toDouble()) > Utils.minimumFlingVelocity - ) { - if (mTouchMode == DRAG && mChart!!.isDragDecelerationEnabled) { - stopDeceleration() + if (abs(velocityX.toDouble()) > Utils.minimumFlingVelocity || + abs(velocityY.toDouble()) > Utils.minimumFlingVelocity + ) { + if (touchMode == DRAG && chart!!.isDragDecelerationEnabled) { + stopDeceleration() - mDecelerationLastTime = AnimationUtils.currentAnimationTimeMillis() + decelerationLastTime = AnimationUtils.currentAnimationTimeMillis() - mDecelerationCurrentPoint.x = event.x - mDecelerationCurrentPoint.y = event.y + decelerationCurrentPoint.x = event.x + decelerationCurrentPoint.y = event.y - mDecelerationVelocity.x = velocityX - mDecelerationVelocity.y = velocityY + decelerationVelocity.x = velocityX + decelerationVelocity.y = velocityY - // This causes computeScroll to fire, recommended for this by Google - Utils.postInvalidateOnAnimation(mChart!!) + // This causes computeScroll to fire, recommended for this by Google + Utils.postInvalidateOnAnimation(chart!!) + } } - } - if (mTouchMode == X_ZOOM || mTouchMode == Y_ZOOM || mTouchMode == PINCH_ZOOM || mTouchMode == POST_ZOOM) { - // Range might have changed, which means that Y-axis labels - // could have changed in size, affecting Y-axis size. - // So we need to recalculate offsets. + if (touchMode == X_ZOOM || touchMode == Y_ZOOM || touchMode == PINCH_ZOOM || touchMode == POST_ZOOM) { + // Range might have changed, which means that Y-axis labels + // could have changed in size, affecting Y-axis size. + // So we need to recalculate offsets. - mChart!!.calculateOffsets() - mChart!!.postInvalidate() - } + chart!!.calculateOffsets() + chart!!.postInvalidate() + } - mTouchMode = NONE - mChart!!.enableScroll() + touchMode = NONE + chart!!.enableScroll() - mVelocityTracker?.recycle() - mVelocityTracker = null + it.recycle() + velocityTracker = null - endAction(event) + endAction(event) + } } MotionEvent.ACTION_POINTER_UP -> { - mVelocityTracker?.let { Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, it) } + velocityTracker?.let { Utils.velocityTrackerPointerUpCleanUpIfNecessary(event, it) } - mTouchMode = POST_ZOOM + touchMode = POST_ZOOM } MotionEvent.ACTION_CANCEL -> { - mTouchMode = NONE + touchMode = NONE endAction(event) } } // perform the transformation, update the chart - matrix = mChart!!.viewPortHandler.refresh(matrix, mChart!!, true) + matrix = chart!!.viewPortHandler.refresh(matrix, chart!!, true) return true // indicate event was handled } @@ -277,7 +273,7 @@ class BarLineChartTouchListener( touchStartPoint.x = event.x touchStartPoint.y = event.y - mClosestDataSetToTouch = mChart!!.getDataSetByTouchPoint(event.x, event.y) + closestDataSetToTouch = chart!!.getDataSetByTouchPoint(event.x, event.y) } /** @@ -288,17 +284,17 @@ class BarLineChartTouchListener( private fun performDrag(event: MotionEvent, distanceX: Float, distanceY: Float) { var distanceXLocal = distanceX var distanceYLocal = distanceY - mLastGesture = ChartGesture.DRAG + lastGesture = ChartGesture.DRAG matrix.set(savedMatrix) - val l = mChart!!.onChartGestureListener + val l = chart!!.onChartGestureListener // check if axis is inverted if (inverted()) { // if there is an inverted horizontalbarchart - if (mChart is HorizontalBarChart) { + if (chart is HorizontalBarChart) { distanceXLocal = -distanceXLocal } else { distanceYLocal = -distanceYLocal @@ -318,7 +314,7 @@ class BarLineChartTouchListener( private fun performZoom(event: MotionEvent) { if (event.pointerCount >= 2) { // two finger zoom - val l = mChart!!.onChartGestureListener + val l = chart!!.onChartGestureListener // get the distance between the pointers of the touch event val totalDist = spacing(event) @@ -327,11 +323,11 @@ class BarLineChartTouchListener( // get the translation val t = getTrans(touchPointCenter.x, touchPointCenter.y) - val h = mChart!!.viewPortHandler + val h = chart!!.viewPortHandler // take actions depending on the activated touch mode - if (mTouchMode == PINCH_ZOOM) { - mLastGesture = ChartGesture.PINCH_ZOOM + if (touchMode == PINCH_ZOOM) { + lastGesture = ChartGesture.PINCH_ZOOM val scale = totalDist / savedDist // total scale @@ -341,8 +337,8 @@ class BarLineChartTouchListener( val canZoomMoreY = if (isZoomingOut) h.canZoomOutMoreY() else h.canZoomInMoreY() - val scaleX = if (mChart!!.isScaleXEnabled) scale else 1f - val scaleY = if (mChart!!.isScaleYEnabled) scale else 1f + val scaleX = if (chart!!.isScaleXEnabled) scale else 1f + val scaleY = if (chart!!.isScaleYEnabled) scale else 1f if (canZoomMoreY || canZoomMoreX) { matrix.set(savedMatrix) @@ -350,8 +346,8 @@ class BarLineChartTouchListener( l?.onChartScale(event, scaleX, scaleY) } - } else if (mTouchMode == X_ZOOM && mChart!!.isScaleXEnabled) { - mLastGesture = ChartGesture.X_ZOOM + } else if (touchMode == X_ZOOM && chart!!.isScaleXEnabled) { + lastGesture = ChartGesture.X_ZOOM val xDist = getXDist(event) val scaleX = xDist / savedXDist // x-axis scale @@ -365,8 +361,8 @@ class BarLineChartTouchListener( l?.onChartScale(event, scaleX, 1f) } - } else if (mTouchMode == Y_ZOOM && mChart!!.isScaleYEnabled) { - mLastGesture = ChartGesture.Y_ZOOM + } else if (touchMode == Y_ZOOM && chart!!.isScaleYEnabled) { + lastGesture = ChartGesture.Y_ZOOM val yDist = getYDist(event) val scaleY = yDist / savedYDist // y-axis scale @@ -394,7 +390,7 @@ class BarLineChartTouchListener( * @return */ private fun getLimitedScaleX(scaleX: Float, t: MPPointF): Float { - val h = mChart!!.viewPortHandler + val h = chart!!.viewPortHandler tempMatrix.set(savedMatrix) tempMatrix.postScale(scaleX, 1f, t.x, t.y) @@ -421,7 +417,7 @@ class BarLineChartTouchListener( * @return */ private fun getLimitedScaleY(scaleY: Float, t: MPPointF): Float { - val h = mChart!!.viewPortHandler + val h = chart!!.viewPortHandler tempMatrix.set(savedMatrix) tempMatrix.postScale(1f, scaleY, t.x, t.y) @@ -444,14 +440,14 @@ class BarLineChartTouchListener( /** * Highlights upon dragging, generates callbacks for the selection-listener. * - * @param e + * @param motionEvent */ - private fun performHighlightDrag(e: MotionEvent) { - val h = mChart!!.getHighlightByTouchPoint(e.x, e.y) + private fun performHighlightDrag(motionEvent: MotionEvent) { + val highlight = chart!!.getHighlightByTouchPoint(motionEvent.x, motionEvent.y) - if (h != null && !h.equalTo(mLastHighlighted)) { - mLastHighlighted = h - mChart!!.highlightValue(h, true) + if (highlight != null && !highlight.equalTo(mLastHighlighted)) { + mLastHighlighted = highlight + chart!!.highlightValue(highlight, true) } } @@ -466,7 +462,7 @@ class BarLineChartTouchListener( * @return */ fun getTrans(x: Float, y: Float): MPPointF { - val vph = mChart!!.viewPortHandler + val vph = chart!!.viewPortHandler val xTrans = x - vph.offsetLeft() @@ -474,7 +470,7 @@ class BarLineChartTouchListener( val yTrans: Float = if (inverted()) { -(y - vph.offsetTop()) } else { - -(mChart!!.measuredHeight - y - vph.offsetBottom()) + -(chart!!.measuredHeight - y - vph.offsetBottom()) } return MPPointF.getInstance(xTrans, yTrans) @@ -486,15 +482,10 @@ class BarLineChartTouchListener( * @return */ private fun inverted(): Boolean { - return (mClosestDataSetToTouch == null && mChart!!.isAnyAxisInverted) || (mClosestDataSetToTouch != null - && mChart!!.isInverted(mClosestDataSetToTouch!!.axisDependency)) + return (closestDataSetToTouch == null && chart!!.isAnyAxisInverted) || (closestDataSetToTouch != null + && chart!!.isInverted(closestDataSetToTouch!!.axisDependency)) } - /** - * ################ ################ ################ ################ - */ - /** GETTERS AND GESTURE RECOGNITION BELOW */ - /** * Sets the minimum distance that will be interpreted as a "drag" by the chart in dp. * Default: 3dp @@ -506,27 +497,27 @@ class BarLineChartTouchListener( } override fun onDoubleTap(e: MotionEvent): Boolean { - mLastGesture = ChartGesture.DOUBLE_TAP + lastGesture = ChartGesture.DOUBLE_TAP - val l = mChart!!.onChartGestureListener + val onChartGestureListener = chart!!.onChartGestureListener - l?.onChartDoubleTapped(e) + onChartGestureListener?.onChartDoubleTapped(e) // check if double-tap zooming is enabled - if (mChart!!.isDoubleTapToZoomEnabled && mChart!!.data!!.entryCount > 0) { + if (chart!!.isDoubleTapToZoomEnabled && chart!!.data!!.entryCount > 0) { val trans = getTrans(e.x, e.y) - val scaleX = if (mChart!!.isScaleXEnabled) 1.4f else 1f - val scaleY = if (mChart!!.isScaleYEnabled) 1.4f else 1f + val scaleX = if (chart!!.isScaleXEnabled) 1.4f else 1f + val scaleY = if (chart!!.isScaleYEnabled) 1.4f else 1f - mChart!!.zoom(scaleX, scaleY, trans.x, trans.y) + chart!!.zoom(scaleX, scaleY, trans.x, trans.y) - if (mChart!!.isLogEnabled) Log.i( + if (chart!!.isLogEnabled) Log.i( "BarlineChartTouch", ("Double-Tap, Zooming In, x: " + trans.x + ", y: " + trans.y) ) - l?.onChartScale(e, scaleX, scaleY) + onChartGestureListener?.onChartScale(e, scaleX, scaleY) MPPointF.recycleInstance(trans) } @@ -535,84 +526,84 @@ class BarLineChartTouchListener( } override fun onLongPress(e: MotionEvent) { - mLastGesture = ChartGesture.LONG_PRESS + lastGesture = ChartGesture.LONG_PRESS - val l = mChart!!.onChartGestureListener + val l = chart!!.onChartGestureListener l?.onChartLongPressed(e) } override fun onSingleTapUp(e: MotionEvent): Boolean { - mLastGesture = ChartGesture.SINGLE_TAP + lastGesture = ChartGesture.SINGLE_TAP - val l = mChart!!.onChartGestureListener + val l = chart!!.onChartGestureListener l?.onChartSingleTapped(e) - if (!mChart!!.isHighlightPerTapEnabled) { + if (!chart!!.isHighlightPerTapEnabled) { return false } - val h = mChart!!.getHighlightByTouchPoint(e.x, e.y) + val h = chart!!.getHighlightByTouchPoint(e.x, e.y) performHighlight(h, e) return super.onSingleTapUp(e) } override fun onFling(e1: MotionEvent?, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean { - mLastGesture = ChartGesture.FLING + lastGesture = ChartGesture.FLING - val l = mChart!!.onChartGestureListener + val chartGestureListener = chart!!.onChartGestureListener - l?.onChartFling(e1, e2, velocityX, velocityY) + chartGestureListener?.onChartFling(e1, e2, velocityX, velocityY) return super.onFling(e1, e2, velocityX, velocityY) } fun stopDeceleration() { - mDecelerationVelocity.x = 0f - mDecelerationVelocity.y = 0f + decelerationVelocity.x = 0f + decelerationVelocity.y = 0f } fun computeScroll() { - if (mDecelerationVelocity.x == 0f && mDecelerationVelocity.y == 0f) return // There's no deceleration in progress + if (decelerationVelocity.x == 0f && decelerationVelocity.y == 0f) return // There's no deceleration in progress val currentTime = AnimationUtils.currentAnimationTimeMillis() - mDecelerationVelocity.x *= mChart!!.dragDecelerationFrictionCoef - mDecelerationVelocity.y *= mChart!!.dragDecelerationFrictionCoef + decelerationVelocity.x *= chart!!.dragDecelerationFrictionCoef + decelerationVelocity.y *= chart!!.dragDecelerationFrictionCoef - val timeInterval = (currentTime - mDecelerationLastTime).toFloat() / 1000f + val timeInterval = (currentTime - decelerationLastTime).toFloat() / 1000f - val distanceX = mDecelerationVelocity.x * timeInterval - val distanceY = mDecelerationVelocity.y * timeInterval + val distanceX = decelerationVelocity.x * timeInterval + val distanceY = decelerationVelocity.y * timeInterval - mDecelerationCurrentPoint.x += distanceX - mDecelerationCurrentPoint.y += distanceY + decelerationCurrentPoint.x += distanceX + decelerationCurrentPoint.y += distanceY val event = MotionEvent.obtain( - currentTime, currentTime, MotionEvent.ACTION_MOVE, mDecelerationCurrentPoint.x, - mDecelerationCurrentPoint.y, 0 + currentTime, currentTime, MotionEvent.ACTION_MOVE, decelerationCurrentPoint.x, + decelerationCurrentPoint.y, 0 ) - val dragDistanceX = if (mChart!!.isDragXEnabled) mDecelerationCurrentPoint.x - touchStartPoint.x else 0f - val dragDistanceY = if (mChart!!.isDragYEnabled) mDecelerationCurrentPoint.y - touchStartPoint.y else 0f + val dragDistanceX = if (chart!!.isDragXEnabled) decelerationCurrentPoint.x - touchStartPoint.x else 0f + val dragDistanceY = if (chart!!.isDragYEnabled) decelerationCurrentPoint.y - touchStartPoint.y else 0f performDrag(event, dragDistanceX, dragDistanceY) event.recycle() - matrix = mChart!!.viewPortHandler.refresh(matrix, mChart!!, false) + matrix = chart!!.viewPortHandler.refresh(matrix, chart!!, false) - mDecelerationLastTime = currentTime + decelerationLastTime = currentTime - if (abs(mDecelerationVelocity.x.toDouble()) >= 0.01 || abs(mDecelerationVelocity.y.toDouble()) >= 0.01) Utils.postInvalidateOnAnimation(mChart) // This causes computeScroll to fire, recommended for this by Google + if (abs(decelerationVelocity.x.toDouble()) >= 0.01 || abs(decelerationVelocity.y.toDouble()) >= 0.01) Utils.postInvalidateOnAnimation(chart) // This causes computeScroll to fire, recommended for this by Google else { // Range might have changed, which means that Y-axis labels // could have changed in size, affecting Y-axis size. // So we need to recalculate offsets. - mChart!!.calculateOffsets() - mChart!!.postInvalidate() + chart!!.calculateOffsets() + chart!!.postInvalidate() stopDeceleration() } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.java deleted file mode 100644 index 75c8e864b4..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.java +++ /dev/null @@ -1,143 +0,0 @@ -package com.github.mikephil.charting.listener; - -import android.view.GestureDetector; -import android.view.MotionEvent; -import android.view.View; - -import com.github.mikephil.charting.charts.Chart; -import com.github.mikephil.charting.highlight.Highlight; - -/** - * Created by philipp on 12/06/15. - */ -public abstract class ChartTouchListener> extends GestureDetector.SimpleOnGestureListener implements View.OnTouchListener { - - public enum ChartGesture { - NONE, DRAG, X_ZOOM, Y_ZOOM, PINCH_ZOOM, ROTATE, SINGLE_TAP, DOUBLE_TAP, LONG_PRESS, FLING - } - - /** - * the last touch gesture that has been performed - **/ - protected ChartGesture mLastGesture = ChartGesture.NONE; - - // states - protected static final int NONE = 0; - protected static final int DRAG = 1; - protected static final int X_ZOOM = 2; - protected static final int Y_ZOOM = 3; - protected static final int PINCH_ZOOM = 4; - protected static final int POST_ZOOM = 5; - protected static final int ROTATE = 6; - - /** - * integer field that holds the current touch-state - */ - protected int mTouchMode = NONE; - - /** - * the last highlighted object (via touch) - */ - protected Highlight mLastHighlighted; - - /** - * the gesturedetector used for detecting taps and longpresses, ... - */ - protected GestureDetector mGestureDetector; - - /** - * the chart the listener represents - */ - protected T mChart; - - public ChartTouchListener(T chart) { - this.mChart = chart; - - mGestureDetector = new GestureDetector(chart.getContext(), this); - } - - /** - * Calls the OnChartGestureListener to do the start callback - * - * @param me - */ - public void startAction(MotionEvent me) { - - OnChartGestureListener l = mChart.getOnChartGestureListener(); - - if (l != null) - l.onChartGestureStart(me, mLastGesture); - } - - /** - * Calls the OnChartGestureListener to do the end callback - * - * @param me - */ - public void endAction(MotionEvent me) { - - OnChartGestureListener l = mChart.getOnChartGestureListener(); - - if (l != null) - l.onChartGestureEnd(me, mLastGesture); - } - - /** - * Sets the last value that was highlighted via touch. - * - * @param high - */ - public void setLastHighlighted(Highlight high) { - mLastHighlighted = high; - } - - /** - * returns the touch mode the listener is currently in - * - * @return - */ - public int getTouchMode() { - return mTouchMode; - } - - /** - * Returns the last gesture that has been performed on the chart. - * - * @return - */ - public ChartGesture getLastGesture() { - return mLastGesture; - } - - - /** - * Perform a highlight operation. - * - * @param e - */ - protected void performHighlight(Highlight h, MotionEvent e) { - - if (h == null || h.equalTo(mLastHighlighted)) { - mChart.highlightValue(null, true); - mLastHighlighted = null; - } else { - mChart.highlightValue(h, true); - mLastHighlighted = h; - } - } - - /** - * returns the distance between two points - * - * @param eventX - * @param startX - * @param eventY - * @param startY - * @return - */ - protected static float distance(float eventX, float startX, float eventY, float startY) { - float dx = eventX - startX; - float dy = eventY - startY; - return (float) Math.sqrt(dx * dx + dy * dy); - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt new file mode 100644 index 0000000000..203d73c8aa --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.kt @@ -0,0 +1,122 @@ +package com.github.mikephil.charting.listener + +import android.view.GestureDetector +import android.view.GestureDetector.SimpleOnGestureListener +import android.view.MotionEvent +import android.view.View.OnTouchListener +import com.github.mikephil.charting.charts.Chart +import com.github.mikephil.charting.highlight.Highlight +import kotlin.math.sqrt + +abstract class ChartTouchListener?>( + @JvmField protected var chart: T?) : SimpleOnGestureListener(), OnTouchListener { + enum class ChartGesture { + NONE, DRAG, X_ZOOM, Y_ZOOM, PINCH_ZOOM, ROTATE, SINGLE_TAP, DOUBLE_TAP, LONG_PRESS, FLING + } + + /** + * Returns the last gesture that has been performed on the chart. + * + * @return + */ + /** + * the last touch gesture that has been performed + */ + var lastGesture: ChartGesture = ChartGesture.NONE + protected set + + /** + * returns the touch mode the listener is currently in + * + * @return + */ + /** + * integer field that holds the current touch-state + */ + var touchMode: Int = NONE + protected set + + /** + * the last highlighted object (via touch) + */ + protected var mLastHighlighted: Highlight? = null + + /** + * the gesturedetector used for detecting taps and longpresses, ... + */ + @JvmField + protected var gestureDetector: GestureDetector? = GestureDetector(chart!!.getContext(), this) + + /** + * Calls the OnChartGestureListener to do the start callback + * + * @param me + */ + fun startAction(me: MotionEvent) { + val l = chart!!.getOnChartGestureListener() + + if (l != null) l.onChartGestureStart(me, this.lastGesture) + } + + /** + * Calls the OnChartGestureListener to do the end callback + * + * @param me + */ + fun endAction(me: MotionEvent) { + val l = chart!!.getOnChartGestureListener() + + if (l != null) l.onChartGestureEnd(me, this.lastGesture) + } + + /** + * Sets the last value that was highlighted via touch. + * + * @param high + */ + fun setLastHighlighted(high: Highlight?) { + mLastHighlighted = high + } + + /** + * Perform a highlight operation. + * + * @param motionEvent + */ + protected fun performHighlight(highlight: Highlight?, motionEvent: MotionEvent?) { + if (highlight == null || highlight.equalTo(mLastHighlighted)) { + chart!!.highlightValue(null, true) + mLastHighlighted = null + } else { + chart!!.highlightValue(highlight, true) + mLastHighlighted = highlight + } + } + + companion object { + // states + protected const val NONE: Int = 0 + protected const val DRAG: Int = 1 + protected const val X_ZOOM: Int = 2 + protected const val Y_ZOOM: Int = 3 + protected const val PINCH_ZOOM: Int = 4 + protected const val POST_ZOOM: Int = 5 + protected const val ROTATE: Int = 6 + + /** + * returns the distance between two points + * + * @param eventX + * @param startX + * @param eventY + * @param startY + * @return + */ + @JvmStatic + protected fun distance(eventX: Float, startX: Float, eventY: Float, startY: Float): Float { + val dx = eventX - startX + val dy = eventY - startY + return sqrt((dx * dx + dy * dy).toDouble()).toFloat() + } + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt similarity index 64% rename from MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.java rename to MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt index da0c5ed180..9b25339be4 100644 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.java +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt @@ -1,21 +1,19 @@ -package com.github.mikephil.charting.listener; +package com.github.mikephil.charting.listener -import android.view.MotionEvent; +import android.view.MotionEvent +import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture /** * Listener for callbacks when doing gestures on the chart. - * - * @author Philipp Jahoda */ -public interface OnChartGestureListener { - +interface OnChartGestureListener { /** * Callbacks when a touch-gesture has started on the chart (ACTION_DOWN) * * @param me * @param lastPerformedGesture */ - void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture); + fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture?) /** * Callbacks when a touch-gesture has ended on the chart (ACTION_UP, ACTION_CANCEL) @@ -23,28 +21,28 @@ public interface OnChartGestureListener { * @param me * @param lastPerformedGesture */ - void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture); + fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartGesture?) /** * Callbacks when the chart is longpressed. * * @param me */ - void onChartLongPressed(MotionEvent me); + fun onChartLongPressed(me: MotionEvent) /** * Callbacks when the chart is double-tapped. * * @param me */ - void onChartDoubleTapped(MotionEvent me); + fun onChartDoubleTapped(me: MotionEvent) /** * Callbacks when the chart is single-tapped. * * @param me */ - void onChartSingleTapped(MotionEvent me); + fun onChartSingleTapped(me: MotionEvent) /** * Callbacks then a fling gesture is made on the chart. @@ -54,7 +52,7 @@ public interface OnChartGestureListener { * @param velocityX * @param velocityY */ - void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY); + fun onChartFling(me1: MotionEvent?, me2: MotionEvent, velocityX: Float, velocityY: Float) /** * Callbacks when the chart is scaled / zoomed via pinch zoom / double-tap gesture. @@ -63,7 +61,7 @@ public interface OnChartGestureListener { * @param scaleX scalefactor on the x-axis * @param scaleY scalefactor on the y-axis */ - void onChartScale(MotionEvent me, float scaleX, float scaleY); + fun onChartScale(me: MotionEvent, scaleX: Float, scaleY: Float) /** * Callbacks when the chart is moved / translated via drag gesture. @@ -72,5 +70,5 @@ public interface OnChartGestureListener { * @param dX translation distance on the x-axis * @param dY translation distance on the y-axis */ - void onChartTranslate(MotionEvent me, float dX, float dY); + fun onChartTranslate(me: MotionEvent, dX: Float, dY: Float) } diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.java deleted file mode 100644 index 7f50232b7e..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.github.mikephil.charting.listener; - -import com.github.mikephil.charting.data.Entry; -import com.github.mikephil.charting.highlight.Highlight; - -/** - * Listener for callbacks when selecting values inside the chart by - * touch-gesture. - * - * @author Philipp Jahoda - */ -public interface OnChartValueSelectedListener { - - /** - * Called when a value has been selected inside the chart. - * - * @param e The selected Entry - * @param h The corresponding highlight object that contains information - * about the highlighted position such as dataSetIndex, ... - */ - void onValueSelected(Entry e, Highlight h); - - /** - * Called when nothing has been selected or an "un-select" has been made. - */ - void onNothingSelected(); -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt new file mode 100644 index 0000000000..48ef465ca8 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.kt @@ -0,0 +1,24 @@ +package com.github.mikephil.charting.listener + +import com.github.mikephil.charting.data.Entry +import com.github.mikephil.charting.highlight.Highlight + +/** + * Listener for callbacks when selecting values inside the chart by + * touch-gesture. + */ +interface OnChartValueSelectedListener { + /** + * Called when a value has been selected inside the chart. + * + * @param entry The selected Entry + * @param highlight The corresponding highlight object that contains information + * about the highlighted position such as dataSetIndex, ... + */ + fun onValueSelected(entry: Entry, highlight: Highlight) + + /** + * Called when nothing has been selected or an "un-select" has been made. + */ + fun onNothingSelected() +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.java deleted file mode 100644 index ca3a67f677..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.mikephil.charting.listener; - -import android.view.GestureDetector.SimpleOnGestureListener; -import android.view.MotionEvent; -import android.view.View; -import android.view.View.OnTouchListener; - -public class OnDrawLineChartTouchListener extends SimpleOnGestureListener implements OnTouchListener { - - @Override - public boolean onTouch(View v, MotionEvent event) { - return false; - } - -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt new file mode 100644 index 0000000000..1b1b99e1c6 --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawLineChartTouchListener.kt @@ -0,0 +1,12 @@ +package com.github.mikephil.charting.listener + +import android.view.GestureDetector.SimpleOnGestureListener +import android.view.MotionEvent +import android.view.View +import android.view.View.OnTouchListener + +class OnDrawLineChartTouchListener : SimpleOnGestureListener(), OnTouchListener { + override fun onTouch(v: View?, event: MotionEvent?): Boolean { + return false + } +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.java deleted file mode 100644 index 5890350bcd..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.github.mikephil.charting.listener; - -import com.github.mikephil.charting.data.DataSet; -import com.github.mikephil.charting.data.Entry; - -/** - * Listener for callbacks when drawing on the chart. - * - * @author Philipp - * - */ -public interface OnDrawListener { - - /** - * Called whenever an entry is added with the finger. Note this is also called for entries that are generated by the - * library, when the touch gesture is too fast and skips points. - * - * @param entry - * the last drawn entry - */ - void onEntryAdded(Entry entry); - - /** - * Called whenever an entry is moved by the user after beeing highlighted - * - * @param entry - */ - void onEntryMoved(Entry entry); - - /** - * Called when drawing finger is lifted and the draw is finished. - * - * @param dataSet - * the last drawn DataSet - */ - void onDrawFinished(DataSet dataSet); - -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt new file mode 100644 index 0000000000..e2db326c9f --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnDrawListener.kt @@ -0,0 +1,33 @@ +package com.github.mikephil.charting.listener + +import com.github.mikephil.charting.data.DataSet +import com.github.mikephil.charting.data.Entry + +/** + * Listener for callbacks when drawing on the chart. + */ +interface OnDrawListener { + /** + * Called whenever an entry is added with the finger. Note this is also called for entries that are generated by the + * library, when the touch gesture is too fast and skips points. + * + * @param entry + * the last drawn entry + */ + fun onEntryAdded(entry: Entry) + + /** + * Called whenever an entry is moved by the user after beeing highlighted + * + * @param entry + */ + fun onEntryMoved(entry: Entry) + + /** + * Called when drawing finger is lifted and the draw is finished. + * + * @param dataSet + * the last drawn DataSet + */ + fun onDrawFinished(dataSet: DataSet<*>) +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java deleted file mode 100644 index d3527f924a..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.java +++ /dev/null @@ -1,288 +0,0 @@ - -package com.github.mikephil.charting.listener; - -import android.annotation.SuppressLint; -import android.graphics.PointF; -import android.view.MotionEvent; -import android.view.View; -import android.view.animation.AnimationUtils; - -import com.github.mikephil.charting.charts.PieRadarChartBase; -import com.github.mikephil.charting.highlight.Highlight; -import com.github.mikephil.charting.utils.MPPointF; -import com.github.mikephil.charting.utils.Utils; - -import java.util.ArrayList; - -/** - * Touchlistener for the PieChart. - * - * @author Philipp Jahoda - */ -public class PieRadarChartTouchListener extends ChartTouchListener> { - - private MPPointF mTouchStartPoint = MPPointF.getInstance(0,0); - - /** - * the angle where the dragging started - */ - private float mStartAngle = 0f; - - private ArrayList _velocitySamples = new ArrayList(); - - private long mDecelerationLastTime = 0; - private float mDecelerationAngularVelocity = 0.f; - - public PieRadarChartTouchListener(PieRadarChartBase chart) { - super(chart); - } - - @SuppressLint("ClickableViewAccessibility") - @Override - public boolean onTouch(View v, MotionEvent event) { - - if (mGestureDetector.onTouchEvent(event)) - return true; - - // if rotation by touch is enabled - // TODO: Also check if the pie itself is being touched, rather than the entire chart area - if (mChart.isRotationEnabled()) { - - float x = event.getX(); - float y = event.getY(); - - switch (event.getAction()) { - - case MotionEvent.ACTION_DOWN: - - startAction(event); - - stopDeceleration(); - - resetVelocity(); - - if (mChart.isDragDecelerationEnabled()) - sampleVelocity(x, y); - - setGestureStartAngle(x, y); - mTouchStartPoint.x = x; - mTouchStartPoint.y = y; - - break; - case MotionEvent.ACTION_MOVE: - - if (mChart.isDragDecelerationEnabled()) - sampleVelocity(x, y); - - if (mTouchMode == NONE - && distance(x, mTouchStartPoint.x, y, mTouchStartPoint.y) - > Utils.convertDpToPixel(8f)) { - mLastGesture = ChartGesture.ROTATE; - mTouchMode = ROTATE; - mChart.disableScroll(); - } else if (mTouchMode == ROTATE) { - updateGestureRotation(x, y); - mChart.invalidate(); - } - - endAction(event); - - break; - case MotionEvent.ACTION_UP: - - if (mChart.isDragDecelerationEnabled()) { - - stopDeceleration(); - - sampleVelocity(x, y); - - mDecelerationAngularVelocity = calculateVelocity(); - - if (mDecelerationAngularVelocity != 0.f) { - mDecelerationLastTime = AnimationUtils.currentAnimationTimeMillis(); - - Utils.postInvalidateOnAnimation(mChart); // This causes computeScroll to fire, recommended for this by Google - } - } - - mChart.enableScroll(); - mTouchMode = NONE; - - endAction(event); - - break; - } - } - - return true; - } - - @Override - public void onLongPress(MotionEvent me) { - - mLastGesture = ChartGesture.LONG_PRESS; - - OnChartGestureListener l = mChart.getOnChartGestureListener(); - - if (l != null) { - l.onChartLongPressed(me); - } - } - - @Override - public boolean onSingleTapConfirmed(MotionEvent e) { - return true; - } - - @Override - public boolean onSingleTapUp(MotionEvent e) { - - mLastGesture = ChartGesture.SINGLE_TAP; - - OnChartGestureListener l = mChart.getOnChartGestureListener(); - - if (l != null) { - l.onChartSingleTapped(e); - } - - if(!mChart.isHighlightPerTapEnabled()) { - return false; - } - - Highlight high = mChart.getHighlightByTouchPoint(e.getX(), e.getY()); - performHighlight(high, e); - - return true; - } - - private void resetVelocity() { - _velocitySamples.clear(); - } - - private void sampleVelocity(float touchLocationX, float touchLocationY) { - - long currentTime = AnimationUtils.currentAnimationTimeMillis(); - - _velocitySamples.add(new AngularVelocitySample(currentTime, mChart.getAngleForPoint(touchLocationX, touchLocationY))); - - // Remove samples older than our sample time - 1 seconds - for (int i = 0, count = _velocitySamples.size(); i < count - 2; i++) { - if (currentTime - _velocitySamples.get(i).time > 1000) { - _velocitySamples.remove(0); - i--; - count--; - } else { - break; - } - } - } - - private float calculateVelocity() { - - if (_velocitySamples.isEmpty()) - return 0.f; - - AngularVelocitySample firstSample = _velocitySamples.get(0); - AngularVelocitySample lastSample = _velocitySamples.get(_velocitySamples.size() - 1); - - // Look for a sample that's closest to the latest sample, but not the same, so we can deduce the direction - AngularVelocitySample beforeLastSample = firstSample; - for (int i = _velocitySamples.size() - 1; i >= 0; i--) { - beforeLastSample = _velocitySamples.get(i); - if (beforeLastSample.angle != lastSample.angle) { - break; - } - } - - // Calculate the sampling time - float timeDelta = (lastSample.time - firstSample.time) / 1000.f; - if (timeDelta == 0.f) { - timeDelta = 0.1f; - } - - // Calculate clockwise/ccw by choosing two values that should be closest to each other, - // so if the angles are two far from each other we know they are inverted "for sure" - boolean clockwise = lastSample.angle >= beforeLastSample.angle; - if (Math.abs(lastSample.angle - beforeLastSample.angle) > 270.0) { - clockwise = !clockwise; - } - - // Now if the "gesture" is over a too big of an angle - then we know the angles are inverted, and we need to move them closer to each other from both sides of the 360.0 wrapping point - if (lastSample.angle - firstSample.angle > 180.0) { - firstSample.angle += 360.0; - } else if (firstSample.angle - lastSample.angle > 180.0) { - lastSample.angle += 360.0; - } - - // The velocity - float velocity = Math.abs((lastSample.angle - firstSample.angle) / timeDelta); - - // Direction? - if (!clockwise) { - velocity = -velocity; - } - - return velocity; - } - - /** - * sets the starting angle of the rotation, this is only used by the touch - * listener, x and y is the touch position - * - * @param x - * @param y - */ - public void setGestureStartAngle(float x, float y) { - mStartAngle = mChart.getAngleForPoint(x, y) - mChart.getRawRotationAngle(); - } - - /** - * updates the view rotation depending on the given touch position, also - * takes the starting angle into consideration - * - * @param x - * @param y - */ - public void updateGestureRotation(float x, float y) { - mChart.setRotationAngle(mChart.getAngleForPoint(x, y) - mStartAngle); - } - - /** - * Sets the deceleration-angular-velocity to 0f - */ - public void stopDeceleration() { - mDecelerationAngularVelocity = 0.f; - } - - public void computeScroll() { - - if (mDecelerationAngularVelocity == 0.f) - return; // There's no deceleration in progress - - final long currentTime = AnimationUtils.currentAnimationTimeMillis(); - - mDecelerationAngularVelocity *= mChart.getDragDecelerationFrictionCoef(); - - final float timeInterval = (float) (currentTime - mDecelerationLastTime) / 1000.f; - - mChart.setRotationAngle(mChart.getRotationAngle() + mDecelerationAngularVelocity * timeInterval); - - mDecelerationLastTime = currentTime; - - if (Math.abs(mDecelerationAngularVelocity) >= 0.001) - Utils.postInvalidateOnAnimation(mChart); // This causes computeScroll to fire, recommended for this by Google - else - stopDeceleration(); - } - - private class AngularVelocitySample { - - public long time; - public float angle; - - public AngularVelocitySample(long time, float angle) { - this.time = time; - this.angle = angle; - } - } -} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt new file mode 100644 index 0000000000..99ae558d6c --- /dev/null +++ b/MPChartLib/src/main/java/com/github/mikephil/charting/listener/PieRadarChartTouchListener.kt @@ -0,0 +1,251 @@ +package com.github.mikephil.charting.listener + +import android.annotation.SuppressLint +import android.view.MotionEvent +import android.view.View +import android.view.animation.AnimationUtils +import com.github.mikephil.charting.charts.PieRadarChartBase +import com.github.mikephil.charting.utils.MPPointF +import com.github.mikephil.charting.utils.Utils +import kotlin.math.abs + +class PieRadarChartTouchListener(chart: PieRadarChartBase<*>?) : ChartTouchListener?>(chart) { + private val touchStartPoint: MPPointF = MPPointF.getInstance(0f, 0f) + + /** + * the angle where the dragging started + */ + private var startAngle = 0f + + private val velocitySamples: ArrayList = ArrayList() + + private var decelerationLastTime: Long = 0 + private var decelerationAngularVelocity = 0f + + @SuppressLint("ClickableViewAccessibility") + override fun onTouch(v: View?, event: MotionEvent): Boolean { + if (gestureDetector!!.onTouchEvent(event)) { + return true + } + + // if rotation by touch is enabled + // TODO: Also check if the pie itself is being touched, rather than the entire chart area + if (chart!!.isRotationEnabled) { + val x = event.x + val y = event.y + + when (event.action) { + MotionEvent.ACTION_DOWN -> { + startAction(event) + + stopDeceleration() + + resetVelocity() + + if (chart!!.isDragDecelerationEnabled) { + sampleVelocity(x, y) + } + + setGestureStartAngle(x, y) + touchStartPoint.x = x + touchStartPoint.y = y + } + + MotionEvent.ACTION_MOVE -> { + if (chart!!.isDragDecelerationEnabled) { + sampleVelocity(x, y) + } + + if (touchMode == NONE + && (distance(x, touchStartPoint.x, y, touchStartPoint.y) + > Utils.convertDpToPixel(8f)) + ) { + lastGesture = ChartGesture.ROTATE + touchMode = ROTATE + chart!!.disableScroll() + } else if (touchMode == ROTATE) { + updateGestureRotation(x, y) + chart!!.invalidate() + } + + endAction(event) + } + + MotionEvent.ACTION_UP -> { + if (chart!!.isDragDecelerationEnabled) { + stopDeceleration() + + sampleVelocity(x, y) + + decelerationAngularVelocity = calculateVelocity() + + if (decelerationAngularVelocity != 0f) { + decelerationLastTime = AnimationUtils.currentAnimationTimeMillis() + + Utils.postInvalidateOnAnimation(chart) // This causes computeScroll to fire, recommended for this by Google + } + } + + chart!!.enableScroll() + touchMode = NONE + + endAction(event) + } + } + } + + return true + } + + override fun onLongPress(me: MotionEvent) { + lastGesture = ChartGesture.LONG_PRESS + + val onChartGestureListener = chart!!.onChartGestureListener + + onChartGestureListener?.onChartLongPressed(me) + } + + override fun onSingleTapConfirmed(motionEvent: MotionEvent) = true + + override fun onSingleTapUp(e: MotionEvent): Boolean { + lastGesture = ChartGesture.SINGLE_TAP + + val onChartGestureListener = chart!!.onChartGestureListener + + onChartGestureListener?.onChartSingleTapped(e) + + if (!chart!!.isHighlightPerTapEnabled) { + return false + } + + val high = chart!!.getHighlightByTouchPoint(e.x, e.y) + performHighlight(high, e) + + return true + } + + private fun resetVelocity() { + velocitySamples.clear() + } + + private fun sampleVelocity(touchLocationX: Float, touchLocationY: Float) { + val currentTime = AnimationUtils.currentAnimationTimeMillis() + + velocitySamples.add(AngularVelocitySample(currentTime, chart!!.getAngleForPoint(touchLocationX, touchLocationY))) + + // Remove samples older than our sample time - 1 seconds + var i = 0 + var count = velocitySamples.size + while (i < count - 2) { + if (currentTime - velocitySamples[i].time > 1000) { + velocitySamples.removeAt(0) + i-- + count-- + } else { + break + } + i++ + } + } + + private fun calculateVelocity(): Float { + if (velocitySamples.isEmpty()) { + return 0f + } + + val firstSample = velocitySamples[0] + val lastSample = velocitySamples[velocitySamples.size - 1] + + // Look for a sample that's closest to the latest sample, but not the same, so we can deduce the direction + var beforeLastSample = firstSample + for (i in velocitySamples.indices.reversed()) { + beforeLastSample = velocitySamples[i] + if (beforeLastSample.angle != lastSample.angle) { + break + } + } + + // Calculate the sampling time + var timeDelta = (lastSample.time - firstSample.time) / 1000f + if (timeDelta == 0f) { + timeDelta = 0.1f + } + + // Calculate clockwise/ccw by choosing two values that should be closest to each other, + // so if the angles are two far from each other we know they are inverted "for sure" + var clockwise = lastSample.angle >= beforeLastSample.angle + if (abs(lastSample.angle - beforeLastSample.angle) > 270.0) { + clockwise = !clockwise + } + + // Now if the "gesture" is over a too big of an angle - then we know the angles are inverted, and we need to move them closer to each other from both sides of the 360.0 wrapping point + if (lastSample.angle - firstSample.angle > 180.0) { + firstSample.angle += 360.0.toFloat() + } else if (firstSample.angle - lastSample.angle > 180.0) { + lastSample.angle += 360.0.toFloat() + } + + // The velocity + var velocity = abs((lastSample.angle - firstSample.angle) / timeDelta) + + // Direction? + if (!clockwise) { + velocity = -velocity + } + + return velocity + } + + /** + * sets the starting angle of the rotation, this is only used by the touch + * listener, x and y is the touch position + * + * @param x + * @param y + */ + fun setGestureStartAngle(x: Float, y: Float) { + startAngle = chart!!.getAngleForPoint(x, y) - chart!!.rawRotationAngle + } + + /** + * updates the view rotation depending on the given touch position, also + * takes the starting angle into consideration + * + * @param x + * @param y + */ + fun updateGestureRotation(x: Float, y: Float) { + chart!!.setRotationAngle(chart!!.getAngleForPoint(x, y) - startAngle) + } + + /** + * Sets the deceleration-angular-velocity to 0f + */ + fun stopDeceleration() { + decelerationAngularVelocity = 0f + } + + fun computeScroll() { + if (decelerationAngularVelocity == 0f) { + return // There's no deceleration in progress + } + + val currentTime = AnimationUtils.currentAnimationTimeMillis() + + decelerationAngularVelocity *= chart!!.dragDecelerationFrictionCoef + + val timeInterval = (currentTime - decelerationLastTime).toFloat() / 1000f + + chart!!.setRotationAngle(chart!!.rotationAngle + decelerationAngularVelocity * timeInterval) + + decelerationLastTime = currentTime + + if (abs(decelerationAngularVelocity) >= 0.001) { + Utils.postInvalidateOnAnimation(chart) // This causes computeScroll to fire, recommended for this by Google + } else { + stopDeceleration() + } + } + + private class AngularVelocitySample(var time: Long, var angle: Float) +} diff --git a/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.java b/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.java deleted file mode 100644 index c4457248d9..0000000000 --- a/MPChartLib/src/main/java/com/github/mikephil/charting/matrix/Vector3.java +++ /dev/null @@ -1,134 +0,0 @@ - -package com.github.mikephil.charting.matrix; - -/** - * Simple 3D vector class. Handles basic vector math for 3D vectors. - */ -public final class Vector3 { - public float x; - public float y; - public float z; - - public static final Vector3 ZERO = new Vector3(0, 0, 0); - public static final Vector3 UNIT_X = new Vector3(1, 0, 0); - public static final Vector3 UNIT_Y = new Vector3(0, 1, 0); - public static final Vector3 UNIT_Z = new Vector3(0, 0, 1); - - public Vector3() { - } - - public Vector3(float[] array) { - set(array[0], array[1], array[2]); - } - - public Vector3(float xValue, float yValue, float zValue) { - set(xValue, yValue, zValue); - } - - public Vector3(Vector3 other) { - set(other); - } - - public final void add(Vector3 other) { - x += other.x; - y += other.y; - z += other.z; - } - - public final void add(float otherX, float otherY, float otherZ) { - x += otherX; - y += otherY; - z += otherZ; - } - - public final void subtract(Vector3 other) { - x -= other.x; - y -= other.y; - z -= other.z; - } - - public final void subtractMultiple(Vector3 other, float multiplicator) { - x -= other.x * multiplicator; - y -= other.y * multiplicator; - z -= other.z * multiplicator; - } - - public final void multiply(float magnitude) { - x *= magnitude; - y *= magnitude; - z *= magnitude; - } - - public final void multiply(Vector3 other) { - x *= other.x; - y *= other.y; - z *= other.z; - } - - public final void divide(float magnitude) { - if (magnitude != 0.0f) { - x /= magnitude; - y /= magnitude; - z /= magnitude; - } - } - - public final void set(Vector3 other) { - x = other.x; - y = other.y; - z = other.z; - } - - public final void set(float xValue, float yValue, float zValue) { - x = xValue; - y = yValue; - z = zValue; - } - - public final float dot(Vector3 other) { - return (x * other.x) + (y * other.y) + (z * other.z); - } - - public final Vector3 cross(Vector3 other) { - return new Vector3(y * other.z - z * other.y, - z * other.x - x * other.z, - x * other.y - y * other.x); - } - - public final float length() { - return (float) Math.sqrt(length2()); - } - - public final float length2() { - return (x * x) + (y * y) + (z * z); - } - - public final float distance2(Vector3 other) { - float dx = x - other.x; - float dy = y - other.y; - float dz = z - other.z; - return (dx * dx) + (dy * dy) + (dz * dz); - } - - public final float normalize() { - final float magnitude = length(); - - // TODO: I'm choosing safety over speed here. - if (magnitude != 0.0f) { - x /= magnitude; - y /= magnitude; - z /= magnitude; - } - - return magnitude; - } - - public final void zero() { - set(0.0f, 0.0f, 0.0f); - } - - public final boolean pointsInSameDirection(Vector3 other) { - return this.dot(other) > 0; - } - -} diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt index 3a58c02f90..76ee396c54 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt @@ -293,12 +293,10 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect private val onValueSelectedRectF = RectF() - override fun onValueSelected(e: Entry?, h: Highlight?) { - if (e == null) return - + override fun onValueSelected(entry: Entry, highlight: Highlight) { val bounds = onValueSelectedRectF - chart!!.getBarBounds(e as BarEntry, bounds) - val position = chart!!.getPosition(e, AxisDependency.LEFT) + chart!!.getBarBounds(entry as BarEntry, bounds) + val position = chart!!.getPosition(entry, AxisDependency.LEFT) Log.i("bounds", bounds.toString()) Log.i("position", position.toString()) diff --git a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt index 243b413f44..8277c453f6 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt @@ -256,8 +256,8 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar override fun onStopTrackingTouch(seekBar: SeekBar?) {} - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i("Activity", "Selected: " + e.toString() + ", dataSet: " + h.dataSetIndex) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("Activity", "Selected: " + entry.toString() + ", dataSet: " + highlight.dataSetIndex) } override fun onNothingSelected() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt index 1c4435c0b7..d31b5d25da 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt @@ -226,12 +226,8 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel saveToGallery(chart, "BubbleChartActivity") } - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i( - "VAL SELECTED", - ("Value: " + e.y + ", xIndex: " + e.x - + ", DataSet index: " + h.dataSetIndex) - ) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("VAL SELECTED", "Value: " + entry.y + ", xIndex: " + entry.x + ", DataSet index: " + highlight.dataSetIndex) } override fun onNothingSelected() {} diff --git a/app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt index c3bb6d55ba..6a3ec74154 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt @@ -126,8 +126,8 @@ class DrawChartActivity : DemoBase(), OnChartValueSelectedListener, OnDrawListen saveToGallery(binding.chart1, "DrawChartActivity") } - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i("VAL SELECTED", ("Value: " + e.y + ", xIndex: " + e.x + ", DataSet index: " + h.dataSetIndex)) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("VAL SELECTED", ("Value: " + entry.y + ", xIndex: " + entry.x + ", DataSet index: " + highlight.dataSetIndex)) } override fun onNothingSelected() = Unit diff --git a/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt index 9708d03a90..f31a86155c 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt @@ -159,8 +159,8 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener { return set } - override fun onValueSelected(e: Entry, h: Highlight?) { - Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show() + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Toast.makeText(this, entry.toString(), Toast.LENGTH_SHORT).show() } override fun onNothingSelected() {} diff --git a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt index 383e8548a2..33bd1bf845 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt @@ -251,14 +251,12 @@ class HorizontalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartV private val mOnValueSelectedRectF = RectF() - override fun onValueSelected(e: Entry?, h: Highlight) { - if (e == null) return - + override fun onValueSelected(entry: Entry, highlight: Highlight) { val bounds = mOnValueSelectedRectF - chart!!.getBarBounds(e as BarEntry, bounds) + chart!!.getBarBounds(entry as BarEntry, bounds) val position = chart!!.getPosition( - e, chart!!.data!!.getDataSetByIndex(h.dataSetIndex) + entry, chart!!.data!!.getDataSetByIndex(highlight.dataSetIndex) .getAxisDependency() ) diff --git a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt index 4ee1f04fd2..8335ea1657 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/HorizontalBarNegativeChartActivity.kt @@ -249,14 +249,12 @@ class HorizontalBarNegativeChartActivity : DemoBase(), OnSeekBarChangeListener, private val mOnValueSelectedRectF = RectF() - override fun onValueSelected(e: Entry?, h: Highlight) { - if (e == null) return - + override fun onValueSelected(entry: Entry, highlight: Highlight) { val bounds = mOnValueSelectedRectF - chart!!.getBarBounds(e as BarEntry, bounds) + chart!!.getBarBounds(entry as BarEntry, bounds) val position = chart!!.getPosition( - e, chart!!.data!!.getDataSetByIndex(h.dataSetIndex) + entry, chart!!.data!!.getDataSetByIndex(highlight.dataSetIndex) .getAxisDependency() ) diff --git a/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt index 3167f8bcff..bfa7154700 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/InvertedLineChartActivity.kt @@ -11,6 +11,7 @@ import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.components.Legend.LegendForm import com.github.mikephil.charting.data.Entry @@ -23,7 +24,6 @@ import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.custom.MyMarkerView import info.appdev.chartexample.notimportant.DemoBase import java.util.Collections -import androidx.core.net.toUri class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { private var chart: LineChart? = null @@ -139,7 +139,8 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa when (item.itemId) { R.id.viewGithub -> { val i = Intent(Intent.ACTION_VIEW) - i.data = "https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt".toUri() + i.data = + "https://github.com/AppDevNext/AndroidChart/blob/master/app/src/main/java/info/appdev/chartexample/InvertedLineChartActivity.kt".toUri() startActivity(i) } @@ -232,12 +233,8 @@ class InvertedLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa saveToGallery(chart, "InvertedLineChartActivity") } - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i( - "VAL SELECTED", - ("Value: " + e.y + ", xIndex: " + e.x - + ", DataSet index: " + h.dataSetIndex) - ) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("VAL SELECTED", "Value: " + entry.y + ", xIndex: " + entry.x + ", DataSet index: " + highlight.dataSetIndex) } override fun onNothingSelected() {} diff --git a/app/src/main/kotlin/info/appdev/chartexample/LineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/LineChartActivity.kt index 326d13d901..392eb24bd1 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/LineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/LineChartActivity.kt @@ -11,6 +11,7 @@ import android.view.MenuItem import android.widget.SeekBar import android.widget.SeekBar.OnSeekBarChangeListener import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing import com.github.mikephil.charting.components.Legend.LegendForm import com.github.mikephil.charting.components.LimitLine @@ -24,7 +25,6 @@ import info.appdev.chartexample.DataTools.Companion.setData import info.appdev.chartexample.custom.MyMarkerView import info.appdev.chartexample.databinding.ActivityLinechartBinding import info.appdev.chartexample.notimportant.DemoBase -import androidx.core.net.toUri /** * Example of a heavily customized [LineChart] with limit lines, custom line shapes, etc. @@ -263,8 +263,8 @@ class LineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelec override fun onStartTrackingTouch(seekBar: SeekBar) {} override fun onStopTrackingTouch(seekBar: SeekBar) {} - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i("Entry selected", e.toString()) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("Entry selected", entry.toString()) Log.i("LOW HIGH", "low: " + binding.chart1.lowestVisibleX + ", high: " + binding.chart1.highestVisibleX) Log.i( "MIN MAX", diff --git a/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt index 4992396146..03fbac9ade 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/LineChartDualAxisActivity.kt @@ -358,11 +358,11 @@ class LineChartDualAxisActivity : DemoBase(), OnSeekBarChangeListener, OnChartVa saveToGallery(chart, "LineChartActivity2") } - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i("Entry selected", e.toString()) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("Entry selected", entry.toString()) chart!!.centerViewToAnimated( - e.x, e.y, chart!!.data!!.getDataSetByIndex(h.dataSetIndex) + entry.x, entry.y, chart!!.data!!.getDataSetByIndex(highlight.dataSetIndex) .getAxisDependency(), 500 ) //chart.zoomAndCenterAnimated(2.5f, 2.5f, e.getX(), e.getY(), chart.getData().getDataSetByIndex(dataSetIndex) diff --git a/app/src/main/kotlin/info/appdev/chartexample/MultiLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/MultiLineChartActivity.kt index 33030c5771..bd248cbc1c 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/MultiLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/MultiLineChartActivity.kt @@ -270,39 +270,39 @@ class MultiLineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartGestu Log.i("Gesture", "START, x: " + me.x + ", y: " + me.y) } - override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + override fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartGesture?) { Log.i("Gesture", "END, lastGesture: $lastPerformedGesture") // un-highlight values after the gesture is finished and no single-tap if (lastPerformedGesture != ChartGesture.SINGLE_TAP) chart!!.highlightValues(null) // or highlightTouch(null) for callback to onNothingSelected(...) } - override fun onChartLongPressed(me: MotionEvent?) { + override fun onChartLongPressed(me: MotionEvent) { Log.i("LongPress", "Chart long pressed.") } - override fun onChartDoubleTapped(me: MotionEvent?) { + override fun onChartDoubleTapped(me: MotionEvent) { Log.i("DoubleTap", "Chart double-tapped.") } - override fun onChartSingleTapped(me: MotionEvent?) { + override fun onChartSingleTapped(me: MotionEvent) { Log.i("SingleTap", "Chart single-tapped.") } - override fun onChartFling(me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float) { + override fun onChartFling(me1: MotionEvent?, me2: MotionEvent, velocityX: Float, velocityY: Float) { Log.i("Fling", "Chart fling. VelocityX: $velocityX, VelocityY: $velocityY") } - override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) { + override fun onChartScale(me: MotionEvent, scaleX: Float, scaleY: Float) { Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY") } - override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) { + override fun onChartTranslate(me: MotionEvent, dX: Float, dY: Float) { Log.i("Translate / Move", "dX: $dX, dY: $dY") } - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i("VAL SELECTED", "Value: " + e.y + ", xIndex: " + e.x + ", DataSet index: " + h.dataSetIndex) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("VAL SELECTED", "Value: " + entry.y + ", xIndex: " + entry.x + ", DataSet index: " + highlight.dataSetIndex) } override fun onNothingSelected() = Unit diff --git a/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt index 59b38c8392..7918f0c119 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PieChartActivity.kt @@ -18,6 +18,7 @@ import android.widget.SeekBar.OnSeekBarChangeListener import android.widget.TextView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat +import androidx.core.net.toUri import com.github.mikephil.charting.animation.Easing import com.github.mikephil.charting.charts.PieChart import com.github.mikephil.charting.components.Legend @@ -32,7 +33,6 @@ import com.github.mikephil.charting.utils.ColorTemplate import com.github.mikephil.charting.utils.MPPointF import info.appdev.chartexample.DataTools.Companion.getValues import info.appdev.chartexample.notimportant.DemoBase -import androidx.core.net.toUri class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelectedListener { private var chart: PieChart? = null @@ -278,13 +278,8 @@ class PieChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect return s } - override fun onValueSelected(e: Entry?, h: Highlight) { - if (e == null) return - Log.i( - "VAL SELECTED", - ("Value: " + e.y + ", index: " + h.x - + ", DataSet index: " + h.dataSetIndex) - ) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("VAL SELECTED", "Value: " + entry.y + ", index: " + highlight.x + ", DataSet index: " + highlight.dataSetIndex) } override fun onNothingSelected() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt index ebfc3014a3..0f5ab00fa4 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PieChartRoundedActivity.kt @@ -283,13 +283,8 @@ class PieChartRoundedActivity : DemoBase(), OnSeekBarChangeListener, OnChartValu return s } - override fun onValueSelected(e: Entry?, h: Highlight) { - if (e == null) return - Log.i( - "VAL SELECTED", - ("Value: " + e.y + ", index: " + h.x - + ", DataSet index: " + h.dataSetIndex) - ) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("VAL SELECTED", "Value: " + entry.y + ", index: " + highlight.x + ", DataSet index: " + highlight.dataSetIndex) } override fun onNothingSelected() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt index 92f20a7ce6..a30249d4cd 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/PiePolylineChartActivity.kt @@ -266,13 +266,8 @@ class PiePolylineChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartVal return s } - override fun onValueSelected(e: Entry?, h: Highlight) { - if (e == null) return - Log.i( - "VAL SELECTED", - ("Value: " + e.y + ", xIndex: " + e.x - + ", DataSet index: " + h.dataSetIndex) - ) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("VAL SELECTED", "Value: " + entry.y + ", xIndex: " + entry.x + ", DataSet index: " + highlight.dataSetIndex) } override fun onNothingSelected() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt index 9abbd25744..085126dc95 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/RealtimeLineChartActivity.kt @@ -200,8 +200,8 @@ class RealtimeLineChartActivity : DemoBase(), OnChartValueSelectedListener { saveToGallery(chart, "RealtimeLineChartActivity") } - override fun onValueSelected(e: Entry, h: Highlight?) { - Log.i("Entry selected", e.toString()) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("Entry selected", entry.toString()) } override fun onNothingSelected() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt index e32ae3c17c..59f1da0c0f 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/ScatterChartActivity.kt @@ -208,12 +208,8 @@ class ScatterChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSe saveToGallery(chart, "ScatterChartActivity") } - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i( - "VAL SELECTED", - ("Value: " + e.y + ", xIndex: " + e.x - + ", DataSet index: " + h.dataSetIndex) - ) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("VAL SELECTED", "Value: " + entry.y + ", xIndex: " + entry.x + ", DataSet index: " + highlight.dataSetIndex) } override fun onNothingSelected() {} diff --git a/app/src/main/kotlin/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt index 8cfd15718b..36c4d69d3e 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/SpecificPositionsLineChartActivity.kt @@ -290,11 +290,11 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, mChart!!.data = data } - override fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture) { + override fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture?) { Log.i("Gesture", "START, x: " + me.x + ", y: " + me.y) } - override fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartGesture) { + override fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartGesture?) { Log.i("Gesture", "END, lastGesture: $lastPerformedGesture") // un-highlight values after the gesture is finished and no single-tap @@ -315,7 +315,7 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, Log.i("SingleTap", "Chart single-tapped.") } - override fun onChartFling(me1: MotionEvent, me2: MotionEvent, velocityX: Float, velocityY: Float) { + override fun onChartFling(me1: MotionEvent?, me2: MotionEvent, velocityX: Float, velocityY: Float) { Log.i("Fling", "Chart flinged. VeloX: $velocityX, VeloY: $velocityY") } @@ -327,8 +327,8 @@ class SpecificPositionsLineChartActivity : DemoBase(), OnSeekBarChangeListener, Log.i("Translate / Move", "dX: $dX, dY: $dY") } - override fun onValueSelected(e: Entry, h: Highlight) { - Log.i("Entry selected", e.toString()) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + Log.i("Entry selected", entry.toString()) Log.i("LOWHIGH", "low: ${mChart!!.lowestVisibleX}, high: ${mChart!!.highestVisibleX}") Log.i("MIN MAX", "xmin: ${mChart!!.xChartMin}, xmax: ${mChart!!.xChartMax}, ymin: ${mChart!!.yChartMin}, ymax: ${mChart!!.yChartMax}") } diff --git a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt index f1b048f9aa..669a323f32 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivity.kt @@ -240,11 +240,13 @@ class StackedBarActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSele override fun onStopTrackingTouch(seekBar: SeekBar?) {} - override fun onValueSelected(e: Entry?, h: Highlight) { - val entry = e as BarEntry + override fun onValueSelected(entry: Entry, highlight: Highlight) { + val barEntry = entry as BarEntry - if (entry.yVals != null) Log.i("VAL SELECTED", "Value: " + entry.yVals!![h.stackIndex]) - else Log.i("VAL SELECTED", "Value: " + entry.y) + if (barEntry.yVals != null) + Log.i("VAL SELECTED", "Value: " + barEntry.yVals!![highlight.stackIndex]) + else + Log.i("VAL SELECTED", "Value: " + barEntry.y) } override fun onNothingSelected() {} diff --git a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt index 3d2d9335f1..87bf63f18f 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/StackedBarActivityNegative.kt @@ -208,12 +208,9 @@ class StackedBarActivityNegative : DemoBase(), OnChartValueSelectedListener { saveToGallery(chart, "StackedBarActivityNegative") } - override fun onValueSelected(e: Entry?, h: Highlight) { - val entry = e as BarEntry - Log.i( - "VAL SELECTED", - "Value: " + abs(entry.yVals!![h.stackIndex]) - ) + override fun onValueSelected(entry: Entry, highlight: Highlight) { + val barEntry = entry as BarEntry + Log.i("VAL SELECTED", "Value: " + abs(barEntry.yVals!![highlight.stackIndex])) } override fun onNothingSelected() { diff --git a/app/src/main/kotlin/info/appdev/chartexample/fragments/BarChartFrag.kt b/app/src/main/kotlin/info/appdev/chartexample/fragments/BarChartFrag.kt index 3a3097baf9..9ac90fbffc 100644 --- a/app/src/main/kotlin/info/appdev/chartexample/fragments/BarChartFrag.kt +++ b/app/src/main/kotlin/info/appdev/chartexample/fragments/BarChartFrag.kt @@ -56,36 +56,36 @@ class BarChartFrag : SimpleFragment(), OnChartGestureListener { return v } - override fun onChartGestureStart(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + override fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture?) { Log.i("Gesture", "START") } - override fun onChartGestureEnd(me: MotionEvent?, lastPerformedGesture: ChartGesture?) { + override fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartGesture?) { Log.i("Gesture", "END") chart!!.highlightValues(null) } - override fun onChartLongPressed(me: MotionEvent?) { + override fun onChartLongPressed(me: MotionEvent) { Log.i("LongPress", "Chart long pressed.") } - override fun onChartDoubleTapped(me: MotionEvent?) { + override fun onChartDoubleTapped(me: MotionEvent) { Log.i("DoubleTap", "Chart double-tapped.") } - override fun onChartSingleTapped(me: MotionEvent?) { + override fun onChartSingleTapped(me: MotionEvent) { Log.i("SingleTap", "Chart single-tapped.") } - override fun onChartFling(me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float) { + override fun onChartFling(me1: MotionEvent?, me2: MotionEvent, velocityX: Float, velocityY: Float) { Log.i("Fling", "Chart fling. VelocityX: $velocityX, VelocityY: $velocityY") } - override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) { + override fun onChartScale(me: MotionEvent, scaleX: Float, scaleY: Float) { Log.i("Scale / Zoom", "ScaleX: $scaleX, ScaleY: $scaleY") } - override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) { + override fun onChartTranslate(me: MotionEvent, dX: Float, dY: Float) { Log.i("Translate / Move", "dX: $dX, dY: $dY") }