Skip to content

Commit d623dbd

Browse files
authored
Merge pull request #38 from Pfuster12/feat@touch-overlay-display
feat(overlay): add always display flag to touch overlay
2 parents ae2c1b1 + 161b404 commit d623dbd

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ livechart.setDataset(dataset)
288288

289289
This allows us to show the current point the user is dragging along.
290290

291+
Since v1.3.5, the touch overlay can be displayed at all times by applying the `drawTouchOverlayAlways()` flag.
292+
293+
```kotlin
294+
val textView = findViewById(R.id.text_view)
295+
296+
livechart.setDataset(dataset)
297+
.drawTouchOverlayAlways()
298+
.drawDataset()
299+
```
300+
301+
291302
### Disabling the touch overlay
292303

293304
If you don't want the touch overlay it can be disabled easily:

app/src/main/java/com/yabu/livechartdemoapp/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MainActivity : AppCompatActivity() {
4040

4141
livechartSimple.setDataset(dataset)
4242
.setLiveChartStyle(chartStyle)
43+
.drawTouchOverlayAlways()
4344
.drawSmoothPath()
4445
.setOnTouchCallbackListener(object : LiveChart.OnTouchCallback {
4546
@SuppressLint("SetTextI18n")

app/src/main/java/com/yabu/livechartdemoapp/SampleData.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ object SampleData {
235235

236236
val data = Gson().fromJson(json, TestDataResponse::class.java)
237237
return Dataset(data.mapIndexed { index, it ->
238-
DataPoint(index.toFloat() + 16, it.close.toFloat())
238+
DataPoint(index.toFloat(), it.close.toFloat())
239239
}.toMutableList())
240240
}
241241

livechart/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ android {
1313
defaultConfig {
1414
minSdkVersion 21
1515
targetSdkVersion 29
16-
versionCode 2
17-
versionName "1.3.4"
16+
versionCode 3
17+
versionName "1.3.5"
1818

1919
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
2020
consumerProguardFiles "consumer-rules.pro"

livechart/src/main/java/com/yabu/livechart/view/LiveChart.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ class LiveChart(context: Context, attrs: AttributeSet? = null) : FrameLayout(con
238238
return this
239239
}
240240

241+
/**
242+
* Show Overlay by default, not just on touch.
243+
*/
244+
@PublicApi
245+
fun drawTouchOverlayAlways(): LiveChart {
246+
disableTouchOverlay = false
247+
overlay.alwaysDisplay()
248+
249+
return this
250+
}
251+
241252
/**
242253
* Draw on chart and bind overlay to dataset.
243254
*/

livechart/src/main/java/com/yabu/livechart/view/LiveChartTouchOverlay.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import android.graphics.Path
77
import android.graphics.PathMeasure
88
import android.util.AttributeSet
99
import android.util.DisplayMetrics
10+
import android.util.Log
1011
import android.view.LayoutInflater
1112
import android.view.MotionEvent
1213
import android.view.View
@@ -20,8 +21,10 @@ import com.yabu.livechart.utils.EPointF
2021
import com.yabu.livechart.utils.PolyBezierPathUtil
2122
import com.yabu.livechart.utils.PublicApi
2223
import com.yabu.livechart.view.LiveChart.OnTouchCallback
24+
import java.lang.Exception
2325
import kotlin.math.max
2426
import kotlin.math.min
27+
import kotlin.math.round
2528
import kotlin.math.roundToInt
2629

2730
/**
@@ -86,6 +89,11 @@ class LiveChartTouchOverlay(context: Context, attrs: AttributeSet?)
8689
*/
8790
private var oldRoundedPos = 0
8891

92+
/**
93+
* Always display flag.
94+
*/
95+
private var alwaysDisplay = false
96+
8997
/**
9098
* Y Bounds display flag.
9199
*/
@@ -146,6 +154,12 @@ class LiveChartTouchOverlay(context: Context, attrs: AttributeSet?)
146154
return this
147155
}
148156

157+
@PublicApi
158+
fun alwaysDisplay() {
159+
overlay.alpha = 1f
160+
alwaysDisplay = true
161+
}
162+
149163
/**
150164
* Draw straight path flag.
151165
*/
@@ -234,6 +248,15 @@ class LiveChartTouchOverlay(context: Context, attrs: AttributeSet?)
234248

235249
extractCoordinatesFromPath()
236250

251+
try {
252+
// Set to default in the middle of the path
253+
val coordinates = pathCoordinates[round(pathCoordinates.size.div(2).toFloat()).toInt()]
254+
overlay.x = coordinates[0] - (chartStyle.overlayCircleDiameter/2)
255+
overlayPoint.y = coordinates[1] - (chartStyle.overlayCircleDiameter/2)
256+
} catch (e: Exception) {
257+
Log.e("Overlay", e.message.toString())
258+
}
259+
237260
invalidate()
238261
}
239262
}
@@ -391,7 +414,9 @@ class LiveChartTouchOverlay(context: Context, attrs: AttributeSet?)
391414
MotionEvent.ACTION_OUTSIDE -> {
392415
touchListener?.onTouchFinished()
393416

394-
overlay.alpha = 0f
417+
if (!alwaysDisplay) {
418+
overlay.alpha = 0f
419+
}
395420
true
396421
}
397422

0 commit comments

Comments
 (0)