Skip to content

Commit aeb13df

Browse files
authored
Merge pull request #450 from AppDevNext/KotlinOnChartGestureListener
Kotlin on chart gesture listener
2 parents ce5b385 + 344b7e6 commit aeb13df

33 files changed

+658
-905
lines changed

MPChartLib/src/main/java/com/github/mikephil/charting/listener/BarLineChartTouchListener.kt

Lines changed: 134 additions & 143 deletions
Large diffs are not rendered by default.

MPChartLib/src/main/java/com/github/mikephil/charting/listener/ChartTouchListener.java

Lines changed: 0 additions & 143 deletions
This file was deleted.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.github.mikephil.charting.listener
2+
3+
import android.view.GestureDetector
4+
import android.view.GestureDetector.SimpleOnGestureListener
5+
import android.view.MotionEvent
6+
import android.view.View.OnTouchListener
7+
import com.github.mikephil.charting.charts.Chart
8+
import com.github.mikephil.charting.highlight.Highlight
9+
import kotlin.math.sqrt
10+
11+
abstract class ChartTouchListener<T : Chart<*>?>(
12+
@JvmField protected var chart: T?) : SimpleOnGestureListener(), OnTouchListener {
13+
enum class ChartGesture {
14+
NONE, DRAG, X_ZOOM, Y_ZOOM, PINCH_ZOOM, ROTATE, SINGLE_TAP, DOUBLE_TAP, LONG_PRESS, FLING
15+
}
16+
17+
/**
18+
* Returns the last gesture that has been performed on the chart.
19+
*
20+
* @return
21+
*/
22+
/**
23+
* the last touch gesture that has been performed
24+
*/
25+
var lastGesture: ChartGesture = ChartGesture.NONE
26+
protected set
27+
28+
/**
29+
* returns the touch mode the listener is currently in
30+
*
31+
* @return
32+
*/
33+
/**
34+
* integer field that holds the current touch-state
35+
*/
36+
var touchMode: Int = NONE
37+
protected set
38+
39+
/**
40+
* the last highlighted object (via touch)
41+
*/
42+
protected var mLastHighlighted: Highlight? = null
43+
44+
/**
45+
* the gesturedetector used for detecting taps and longpresses, ...
46+
*/
47+
@JvmField
48+
protected var gestureDetector: GestureDetector? = GestureDetector(chart!!.getContext(), this)
49+
50+
/**
51+
* Calls the OnChartGestureListener to do the start callback
52+
*
53+
* @param me
54+
*/
55+
fun startAction(me: MotionEvent) {
56+
val l = chart!!.getOnChartGestureListener()
57+
58+
if (l != null) l.onChartGestureStart(me, this.lastGesture)
59+
}
60+
61+
/**
62+
* Calls the OnChartGestureListener to do the end callback
63+
*
64+
* @param me
65+
*/
66+
fun endAction(me: MotionEvent) {
67+
val l = chart!!.getOnChartGestureListener()
68+
69+
if (l != null) l.onChartGestureEnd(me, this.lastGesture)
70+
}
71+
72+
/**
73+
* Sets the last value that was highlighted via touch.
74+
*
75+
* @param high
76+
*/
77+
fun setLastHighlighted(high: Highlight?) {
78+
mLastHighlighted = high
79+
}
80+
81+
/**
82+
* Perform a highlight operation.
83+
*
84+
* @param motionEvent
85+
*/
86+
protected fun performHighlight(highlight: Highlight?, motionEvent: MotionEvent?) {
87+
if (highlight == null || highlight.equalTo(mLastHighlighted)) {
88+
chart!!.highlightValue(null, true)
89+
mLastHighlighted = null
90+
} else {
91+
chart!!.highlightValue(highlight, true)
92+
mLastHighlighted = highlight
93+
}
94+
}
95+
96+
companion object {
97+
// states
98+
protected const val NONE: Int = 0
99+
protected const val DRAG: Int = 1
100+
protected const val X_ZOOM: Int = 2
101+
protected const val Y_ZOOM: Int = 3
102+
protected const val PINCH_ZOOM: Int = 4
103+
protected const val POST_ZOOM: Int = 5
104+
protected const val ROTATE: Int = 6
105+
106+
/**
107+
* returns the distance between two points
108+
*
109+
* @param eventX
110+
* @param startX
111+
* @param eventY
112+
* @param startY
113+
* @return
114+
*/
115+
@JvmStatic
116+
protected fun distance(eventX: Float, startX: Float, eventY: Float, startY: Float): Float {
117+
val dx = eventX - startX
118+
val dy = eventY - startY
119+
return sqrt((dx * dx + dy * dy).toDouble()).toFloat()
120+
}
121+
}
122+
}

MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.java renamed to MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartGestureListener.kt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,48 @@
1-
package com.github.mikephil.charting.listener;
1+
package com.github.mikephil.charting.listener
22

3-
import android.view.MotionEvent;
3+
import android.view.MotionEvent
4+
import com.github.mikephil.charting.listener.ChartTouchListener.ChartGesture
45

56
/**
67
* Listener for callbacks when doing gestures on the chart.
7-
*
8-
* @author Philipp Jahoda
98
*/
10-
public interface OnChartGestureListener {
11-
9+
interface OnChartGestureListener {
1210
/**
1311
* Callbacks when a touch-gesture has started on the chart (ACTION_DOWN)
1412
*
1513
* @param me
1614
* @param lastPerformedGesture
1715
*/
18-
void onChartGestureStart(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);
16+
fun onChartGestureStart(me: MotionEvent, lastPerformedGesture: ChartGesture?)
1917

2018
/**
2119
* Callbacks when a touch-gesture has ended on the chart (ACTION_UP, ACTION_CANCEL)
2220
*
2321
* @param me
2422
* @param lastPerformedGesture
2523
*/
26-
void onChartGestureEnd(MotionEvent me, ChartTouchListener.ChartGesture lastPerformedGesture);
24+
fun onChartGestureEnd(me: MotionEvent, lastPerformedGesture: ChartGesture?)
2725

2826
/**
2927
* Callbacks when the chart is longpressed.
3028
*
3129
* @param me
3230
*/
33-
void onChartLongPressed(MotionEvent me);
31+
fun onChartLongPressed(me: MotionEvent)
3432

3533
/**
3634
* Callbacks when the chart is double-tapped.
3735
*
3836
* @param me
3937
*/
40-
void onChartDoubleTapped(MotionEvent me);
38+
fun onChartDoubleTapped(me: MotionEvent)
4139

4240
/**
4341
* Callbacks when the chart is single-tapped.
4442
*
4543
* @param me
4644
*/
47-
void onChartSingleTapped(MotionEvent me);
45+
fun onChartSingleTapped(me: MotionEvent)
4846

4947
/**
5048
* Callbacks then a fling gesture is made on the chart.
@@ -54,7 +52,7 @@ public interface OnChartGestureListener {
5452
* @param velocityX
5553
* @param velocityY
5654
*/
57-
void onChartFling(MotionEvent me1, MotionEvent me2, float velocityX, float velocityY);
55+
fun onChartFling(me1: MotionEvent?, me2: MotionEvent, velocityX: Float, velocityY: Float)
5856

5957
/**
6058
* Callbacks when the chart is scaled / zoomed via pinch zoom / double-tap gesture.
@@ -63,7 +61,7 @@ public interface OnChartGestureListener {
6361
* @param scaleX scalefactor on the x-axis
6462
* @param scaleY scalefactor on the y-axis
6563
*/
66-
void onChartScale(MotionEvent me, float scaleX, float scaleY);
64+
fun onChartScale(me: MotionEvent, scaleX: Float, scaleY: Float)
6765

6866
/**
6967
* Callbacks when the chart is moved / translated via drag gesture.
@@ -72,5 +70,5 @@ public interface OnChartGestureListener {
7270
* @param dX translation distance on the x-axis
7371
* @param dY translation distance on the y-axis
7472
*/
75-
void onChartTranslate(MotionEvent me, float dX, float dY);
73+
fun onChartTranslate(me: MotionEvent, dX: Float, dY: Float)
7674
}

MPChartLib/src/main/java/com/github/mikephil/charting/listener/OnChartValueSelectedListener.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)