Skip to content

Commit fc7f3d1

Browse files
committed
Kotlin OnChartGestureListener
1 parent 223cda6 commit fc7f3d1

24 files changed

+108
-146
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,9 @@ class BarLineChartTouchListener(
562562
override fun onFling(e1: MotionEvent?, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
563563
mLastGesture = ChartGesture.FLING
564564

565-
val l = mChart!!.onChartGestureListener
565+
val chartGestureListener = mChart!!.onChartGestureListener
566566

567-
l?.onChartFling(e1, e2, velocityX, velocityY)
567+
chartGestureListener?.onChartFling(e1, e2, velocityX, velocityY)
568568

569569
return super.onFling(e1, e2, velocityX, velocityY)
570570
}

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.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.github.mikephil.charting.listener
2+
3+
import com.github.mikephil.charting.data.Entry
4+
import com.github.mikephil.charting.highlight.Highlight
5+
6+
/**
7+
* Listener for callbacks when selecting values inside the chart by
8+
* touch-gesture.
9+
*/
10+
interface OnChartValueSelectedListener {
11+
/**
12+
* Called when a value has been selected inside the chart.
13+
*
14+
* @param entry The selected Entry
15+
* @param highlight The corresponding highlight object that contains information
16+
* about the highlighted position such as dataSetIndex, ...
17+
*/
18+
fun onValueSelected(entry: Entry, highlight: Highlight)
19+
20+
/**
21+
* Called when nothing has been selected or an "un-select" has been made.
22+
*/
23+
fun onNothingSelected()
24+
}

app/src/main/kotlin/info/appdev/chartexample/BarChartActivity.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,10 @@ class BarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSelect
293293

294294
private val onValueSelectedRectF = RectF()
295295

296-
override fun onValueSelected(e: Entry?, h: Highlight?) {
297-
if (e == null) return
298-
296+
override fun onValueSelected(entry: Entry, highlight: Highlight) {
299297
val bounds = onValueSelectedRectF
300-
chart!!.getBarBounds(e as BarEntry, bounds)
301-
val position = chart!!.getPosition(e, AxisDependency.LEFT)
298+
chart!!.getBarBounds(entry as BarEntry, bounds)
299+
val position = chart!!.getPosition(entry, AxisDependency.LEFT)
302300

303301
Log.i("bounds", bounds.toString())
304302
Log.i("position", position.toString())

app/src/main/kotlin/info/appdev/chartexample/BarChartActivityMultiDataset.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ class BarChartActivityMultiDataset : DemoBase(), OnSeekBarChangeListener, OnChar
256256

257257
override fun onStopTrackingTouch(seekBar: SeekBar?) {}
258258

259-
override fun onValueSelected(e: Entry, h: Highlight) {
260-
Log.i("Activity", "Selected: " + e.toString() + ", dataSet: " + h.dataSetIndex)
259+
override fun onValueSelected(entry: Entry, highlight: Highlight) {
260+
Log.i("Activity", "Selected: " + entry.toString() + ", dataSet: " + highlight.dataSetIndex)
261261
}
262262

263263
override fun onNothingSelected() {

app/src/main/kotlin/info/appdev/chartexample/BubbleChartActivity.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,8 @@ class BubbleChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartValueSel
226226
saveToGallery(chart, "BubbleChartActivity")
227227
}
228228

229-
override fun onValueSelected(e: Entry, h: Highlight) {
230-
Log.i(
231-
"VAL SELECTED",
232-
("Value: " + e.y + ", xIndex: " + e.x
233-
+ ", DataSet index: " + h.dataSetIndex)
234-
)
229+
override fun onValueSelected(entry: Entry, highlight: Highlight) {
230+
Log.i("VAL SELECTED", "Value: " + entry.y + ", xIndex: " + entry.x + ", DataSet index: " + highlight.dataSetIndex)
235231
}
236232

237233
override fun onNothingSelected() {}

app/src/main/kotlin/info/appdev/chartexample/DrawChartActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ class DrawChartActivity : DemoBase(), OnChartValueSelectedListener, OnDrawListen
126126
saveToGallery(binding.chart1, "DrawChartActivity")
127127
}
128128

129-
override fun onValueSelected(e: Entry, h: Highlight) {
130-
Log.i("VAL SELECTED", ("Value: " + e.y + ", xIndex: " + e.x + ", DataSet index: " + h.dataSetIndex))
129+
override fun onValueSelected(entry: Entry, highlight: Highlight) {
130+
Log.i("VAL SELECTED", ("Value: " + entry.y + ", xIndex: " + entry.x + ", DataSet index: " + highlight.dataSetIndex))
131131
}
132132

133133
override fun onNothingSelected() = Unit

app/src/main/kotlin/info/appdev/chartexample/DynamicalAddingActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ class DynamicalAddingActivity : DemoBase(), OnChartValueSelectedListener {
159159
return set
160160
}
161161

162-
override fun onValueSelected(e: Entry, h: Highlight?) {
163-
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show()
162+
override fun onValueSelected(entry: Entry, highlight: Highlight) {
163+
Toast.makeText(this, entry.toString(), Toast.LENGTH_SHORT).show()
164164
}
165165

166166
override fun onNothingSelected() {}

app/src/main/kotlin/info/appdev/chartexample/HorizontalBarChartActivity.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,12 @@ class HorizontalBarChartActivity : DemoBase(), OnSeekBarChangeListener, OnChartV
251251

252252
private val mOnValueSelectedRectF = RectF()
253253

254-
override fun onValueSelected(e: Entry?, h: Highlight) {
255-
if (e == null) return
256-
254+
override fun onValueSelected(entry: Entry, highlight: Highlight) {
257255
val bounds = mOnValueSelectedRectF
258-
chart!!.getBarBounds(e as BarEntry, bounds)
256+
chart!!.getBarBounds(entry as BarEntry, bounds)
259257

260258
val position = chart!!.getPosition(
261-
e, chart!!.data!!.getDataSetByIndex(h.dataSetIndex)
259+
entry, chart!!.data!!.getDataSetByIndex(highlight.dataSetIndex)
262260
.getAxisDependency()
263261
)
264262

0 commit comments

Comments
 (0)