Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<T : Chart<*>?>(
@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()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
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)
*
* @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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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)
}

This file was deleted.

Loading
Loading