Skip to content

Commit

Permalink
Make sure to only store at most CHART_MAX_POINTS points for each chart
Browse files Browse the repository at this point in the history
  • Loading branch information
MGaetan89 committed Aug 12, 2024
1 parent f516750 commit 138ef77
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,20 @@ fun LineChart(
Chart(
data = data,
modifier = modifier,
stretchChartToPointsCount = stretchChartToPointsCount,
scaleItemsCount = scaleItemsCount,
scaleTextFormatter = scaleTextFormatter,
scaleTextStyle = scaleTextStyle,
scaleTextHorizontalPadding = scaleTextHorizontalPadding,
scaleLineColor = scaleLineColor,
drawChart = { points, maxValue, bounds ->
drawChart = { maxValue, bounds ->
drawLineChart(
points = points,
points = data,
bounds = bounds,
maxValue = maxValue,
lineColor = lineColor,
lineWidth = lineWidth,
lineCornerRadius = lineCornerRadius,
maxPoints = stretchChartToPointsCount ?: points.size,
maxPoints = stretchChartToPointsCount ?: data.size,
)
},
)
Expand Down Expand Up @@ -132,20 +131,19 @@ fun BarChart(
Chart(
data = data,
modifier = modifier,
stretchChartToPointsCount = stretchChartToPointsCount,
scaleItemsCount = scaleItemsCount,
scaleTextFormatter = scaleTextFormatter,
scaleTextStyle = scaleTextStyle,
scaleTextHorizontalPadding = scaleTextHorizontalPadding,
scaleLineColor = scaleLineColor,
drawChart = { points, maxValue, bounds ->
drawChart = { maxValue, bounds ->
drawBarChart(
points = points,
points = data,
bounds = bounds,
maxValue = maxValue,
barColor = barColor,
barSpacing = barSpacing,
maxPoints = stretchChartToPointsCount ?: points.size,
maxPoints = stretchChartToPointsCount ?: data.size,
)
},
)
Expand All @@ -155,22 +153,16 @@ fun BarChart(
private fun Chart(
data: List<Float>,
modifier: Modifier,
stretchChartToPointsCount: Int?,
scaleItemsCount: Int,
scaleTextFormatter: NumberFormat,
scaleTextStyle: TextStyle,
scaleTextHorizontalPadding: Dp,
scaleLineColor: Color,
drawChart: DrawScope.(points: List<Float>, maxValue: Int, bounds: Rect) -> Unit,
drawChart: DrawScope.(maxValue: Int, bounds: Rect) -> Unit,
) {
val trimmedData = if (stretchChartToPointsCount != null) data.takeLast(stretchChartToPointsCount) else data
if (trimmedData.isEmpty()) {
return
}

val textMeasurer = rememberTextMeasurer()

val maxValue = trimmedData.max()
val maxValue = data.max()
val numberOfDigitsInMaxValue = log10(abs(maxValue.toDouble())).toInt()
val increment = 10.0.pow(numberOfDigitsInMaxValue).toInt()

Expand All @@ -189,7 +181,7 @@ private fun Chart(
),
)

drawChart(trimmedData, nextMaxMultipleOfScales, chartBounds)
drawChart(nextMaxMultipleOfScales, chartBounds)

drawScale(
textMeasurer = textMeasurer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ class StatsForNerdsViewModel(application: Application) : AndroidViewModel(applic
}

_indicatedBitRates.update {
BitRates(
data = it.data + (value.indicatedBitrate / TO_MEGA),
)
val newData = (it.data + (value.indicatedBitrate / TO_MEGA))
.takeLast(CHART_MAX_POINTS)

BitRates(data = newData)
}

_information.update {
Expand All @@ -103,9 +104,10 @@ class StatsForNerdsViewModel(application: Application) : AndroidViewModel(applic
}

_observedBitRates.update {
BitRates(
data = it.data + (value.bandwidth / TO_MEGA),
)
val newData = (it.data + (value.bandwidth / TO_MEGA))
.takeLast(CHART_MAX_POINTS)

BitRates(data = newData)
}

_stalls.update {
Expand All @@ -115,9 +117,11 @@ class StatsForNerdsViewModel(application: Application) : AndroidViewModel(applic
} else {
stallCount - it.data.sum()
}
val newData = (it.data + stall.coerceAtLeast(0f))
.takeLast(CHART_MAX_POINTS)

Stalls(
data = it.data + stall.coerceAtLeast(0f),
data = newData,
total = value.stallCount.toFloat().toFormattedBytes(includeUnit = false)
)
}
Expand All @@ -139,9 +143,11 @@ class StatsForNerdsViewModel(application: Application) : AndroidViewModel(applic
} else {
totalBytesLoaded / TO_MEGA - it.data.sum()
}
val newData = (it.data + volume.coerceAtLeast(0f))
.takeLast(CHART_MAX_POINTS)

DataVolumes(
data = it.data + volume.coerceAtLeast(0f),
data = newData,
total = totalBytesLoaded.toFormattedBytes(includeUnit = true),
)
}
Expand Down

0 comments on commit 138ef77

Please sign in to comment.