aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/foundation/e/privacycentralapp/common
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/foundation/e/privacycentralapp/common')
-rw-r--r--app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt171
1 files changed, 130 insertions, 41 deletions
diff --git a/app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt b/app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt
index d7a9dd0..5622806 100644
--- a/app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt
+++ b/app/src/main/java/foundation/e/privacycentralapp/common/GraphHolder.kt
@@ -29,15 +29,19 @@ import androidx.core.content.ContextCompat
import androidx.core.text.toSpannable
import androidx.core.view.isVisible
import com.github.mikephil.charting.charts.BarChart
+import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.MarkerView
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
+import com.github.mikephil.charting.components.YAxis.AxisDependency
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.data.Entry
+import com.github.mikephil.charting.formatter.ValueFormatter
import com.github.mikephil.charting.highlight.Highlight
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
+import com.github.mikephil.charting.renderer.XAxisRenderer
import com.github.mikephil.charting.utils.MPPointF
import foundation.e.privacycentralapp.R
import foundation.e.privacycentralapp.common.extensions.dpToPxF
@@ -50,60 +54,145 @@ class GraphHolder(val barChart: BarChart, val context: Context, val isMarkerAbov
}
var labels = emptyList<String>()
+ var graduations: List<String?>? = null
+
private var isHighlighted = false
init {
- barChart.apply {
- description = null
- setTouchEnabled(true)
- setScaleEnabled(false)
+ barChart.description = null
+ barChart.setTouchEnabled(true)
+ barChart.setScaleEnabled(false)
+
+ barChart.setDrawGridBackground(false)
+ barChart.setDrawBorders(false)
+ barChart.axisLeft.isEnabled = false
+ barChart.axisRight.isEnabled = false
+
+ barChart.legend.isEnabled = false
+
+ if (isMarkerAbove) prepareXAxisDashboardDay() else prepareXAxisMarkersBelow()
+
+ val periodMarker = PeriodMarkerView(context, isMarkerAbove)
+ periodMarker.chartView = barChart
+ barChart.marker = periodMarker
+
+ barChart.setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
+ override fun onValueSelected(e: Entry?, h: Highlight?) {
+ h?.let {
+ val index = it.x.toInt()
+ if (index >= 0 &&
+ index < labels.size &&
+ index < this@GraphHolder.data.size
+ ) {
+ val period = labels[index]
+ val (blocked, leaked) = this@GraphHolder.data[index]
+ periodMarker.setLabel(period, blocked, leaked)
+ }
+ }
+ isHighlighted = true
+ }
- setDrawGridBackground(false)
- setDrawBorders(false)
- axisLeft.isEnabled = false
- axisRight.isEnabled = false
+ override fun onNothingSelected() {
+ isHighlighted = false
+ }
+ })
+ }
- legend.isEnabled = false
+ private fun prepareXAxisDashboardDay() {
+ barChart.extraTopOffset = 44f
- if (isMarkerAbove) {
- extraTopOffset = 44f
- } else {
- extraBottomOffset = 44f
- }
+ barChart.offsetTopAndBottom(0)
+
+ barChart.setXAxisRenderer(object : XAxisRenderer(barChart.viewPortHandler, barChart.xAxis, barChart.getTransformer(AxisDependency.LEFT)) {
+ override fun renderAxisLine(c: Canvas) {
+ mAxisLinePaint.color = mXAxis.axisLineColor
+ mAxisLinePaint.strokeWidth = mXAxis.axisLineWidth
+ mAxisLinePaint.pathEffect = mXAxis.axisLineDashPathEffect
+
+ // Top line
+ c.drawLine(
+ mViewPortHandler.contentLeft(),
+ mViewPortHandler.contentTop(), mViewPortHandler.contentRight(),
+ mViewPortHandler.contentTop(), mAxisLinePaint
+ )
- offsetTopAndBottom(0)
- xAxis.apply {
- isEnabled = true
- position = XAxis.XAxisPosition.BOTH_SIDED
- setDrawGridLines(false)
- setDrawLabels(false)
- setDrawValueAboveBar(false)
+ // Bottom line
+ c.drawLine(
+ mViewPortHandler.contentLeft(),
+ mViewPortHandler.contentBottom() - 7.dpToPxF(context),
+ mViewPortHandler.contentRight(),
+ mViewPortHandler.contentBottom() - 7.dpToPxF(context),
+ mAxisLinePaint
+ )
}
- val periodMarker = PeriodMarkerView(context, isMarkerAbove)
- periodMarker.chartView = this
- marker = periodMarker
-
- setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
- override fun onValueSelected(e: Entry?, h: Highlight?) {
- h?.let {
- val index = it.x.toInt()
- if (index >= 0 &&
- index < labels.size &&
- index < this@GraphHolder.data.size
- ) {
- val period = labels[index]
- val (blocked, leaked) = this@GraphHolder.data[index]
- periodMarker.setLabel(period, blocked, leaked)
- }
+ override fun renderGridLines(c: Canvas) {
+ if (!mXAxis.isDrawGridLinesEnabled || !mXAxis.isEnabled) return
+ val clipRestoreCount = c.save()
+ c.clipRect(gridClippingRect)
+ if (mRenderGridLinesBuffer.size != mAxis.mEntryCount * 2) {
+ mRenderGridLinesBuffer = FloatArray(mXAxis.mEntryCount * 2)
+ }
+ val positions = mRenderGridLinesBuffer
+ run {
+ var i = 0
+ while (i < positions.size) {
+ positions[i] = mXAxis.mEntries[i / 2]
+ positions[i + 1] = mXAxis.mEntries[i / 2]
+ i += 2
}
- isHighlighted = true
}
- override fun onNothingSelected() {
- isHighlighted = false
+ mTrans.pointValuesToPixel(positions)
+ setupGridPaint()
+ val gridLinePath = mRenderGridLinesPath
+ gridLinePath.reset()
+ var i = 0
+ while (i < positions.size) {
+ val bottomY = if (graduations?.getOrNull(i / 2) != null) 0 else 3
+ val x = positions[i]
+ gridLinePath.moveTo(x, mViewPortHandler.contentBottom() - 7.dpToPxF(context))
+ gridLinePath.lineTo(x, mViewPortHandler.contentBottom() - bottomY.dpToPxF(context))
+
+ c.drawPath(gridLinePath, mGridPaint)
+
+ gridLinePath.reset()
+
+ i += 2
+ }
+ c.restoreToCount(clipRestoreCount)
+ }
+ })
+
+ barChart.setDrawValueAboveBar(false)
+ barChart.xAxis.apply {
+ isEnabled = true
+ position = XAxis.XAxisPosition.BOTTOM
+
+ setDrawGridLines(true)
+ setDrawLabels(true)
+ setCenterAxisLabels(false)
+ setLabelCount(25, true)
+
+ valueFormatter = object : ValueFormatter() {
+ override fun getAxisLabel(value: Float, axis: AxisBase?): String {
+ return graduations?.getOrNull(value.toInt() + 1) ?: ""
}
- })
+ }
+ }
+ }
+
+ private fun prepareXAxisMarkersBelow() {
+ barChart.extraBottomOffset = 44f
+
+ barChart.offsetTopAndBottom(0)
+ barChart.setDrawValueAboveBar(false)
+
+ barChart.xAxis.apply {
+ isEnabled = true
+ position = XAxis.XAxisPosition.BOTH_SIDED
+ setDrawGridLines(false)
+ setDrawLabels(false)
}
}