Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 46:Fix issue with chart line if all y values are the same #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MainActivity : AppCompatActivity() {
.setLiveChartStyle(chartStyle)
.drawTouchOverlayAlways()
.drawSmoothPath()
.setInitialTouchOverlayPosition(dataset.points[3].x)
.setInitialTouchOverlayPosition(dataset.points[3])
.setOnTouchCallbackListener(object : LiveChart.OnTouchCallback {
@SuppressLint("SetTextI18n")
override fun onTouchCallback(point: DataPoint) {
Expand Down
31 changes: 26 additions & 5 deletions livechart/src/main/java/com/yabu/livechart/model/Dataset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@ package com.yabu.livechart.model

data class Dataset(val points: MutableList<DataPoint>) {

private var upperBound= 0.0f
private var lowerBound= 0.0f

init {
val sortedPoint = points.sortedBy { point -> point.y }
upperBound = sortedPoint.lastOrNull()?.y ?: 0f
lowerBound = sortedPoint.firstOrNull { point -> point.x >= 0 }?.y ?: 0f
// If the upper and lower bound are the same the chart won't draw anything because
// `upperBound` and `lowerBound` are used or division and that can cause problems
if(hasData() && lowerBound==upperBound){
upperBound = increaseBound(upperBound)
}
}

fun hasData(): Boolean {
val sortedPoint = points.filter { point -> point.x > 0 }
return sortedPoint.firstOrNull() != null

}

fun upperBound(): Float {
val sortedPoint = points.sortedBy { point -> point.y }
return sortedPoint.lastOrNull()?.y ?: 0f
return upperBound
}

fun lowerBound(): Float {
val sortedPoint = points.sortedBy { point -> point.y }
.filter { point -> point.x >= 0 }
return sortedPoint.firstOrNull()?.y ?: 0f
return lowerBound
}

private fun increaseBound(bound:Float): Float{
var step = bound
var increase = 0.1f
while (step < 1.0f) {
step *= 10.0f
increase *= 0.1f
}
return bound + increase
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ open class LiveChartView(context: Context, attrs: AttributeSet?) : View(context,

// Assign the lower bound of this chart,
lowerBound = if (secondDataset.hasData()) {
min(dataset.lowerBound(), secondDataset.lowerBound())
min(dataset.lowerBound(), secondDataset.lowerBound())
} else {
dataset.lowerBound()
}
Expand Down