/
BarChartViewHolder.kt
345 lines (325 loc) · 12.6 KB
/
BarChartViewHolder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package org.wordpress.android.ui.stats.refresh.lists.sections.viewholders
import android.content.Context
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import com.github.mikephil.charting.charts.BarChart
import com.github.mikephil.charting.components.Description
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.highlight.Highlight
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet
import com.github.mikephil.charting.listener.OnChartValueSelectedListener
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.wordpress.android.R
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.BarChartItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.BarChartItem.Bar
import org.wordpress.android.ui.stats.refresh.utils.BarChartAccessibilityHelper
import org.wordpress.android.ui.stats.refresh.utils.BarChartAccessibilityHelper.BarChartAccessibilityEvent
import org.wordpress.android.ui.stats.refresh.utils.LargeValueFormatter
import org.wordpress.android.util.DisplayUtils
import android.R as AndroidR
private const val MIN_COLUMN_COUNT = 5
private const val MIN_VALUE = 4f
private typealias BarCount = Int
class BarChartViewHolder(parent: ViewGroup) : BlockListItemViewHolder(
parent,
R.layout.stats_block_bar_chart_item
) {
private val coroutineScope = CoroutineScope(Dispatchers.Main)
private val chart = itemView.findViewById<BarChart>(R.id.chart)
private val labelStart = itemView.findViewById<TextView>(R.id.label_start)
private val labelEnd = itemView.findViewById<TextView>(R.id.label_end)
private lateinit var accessibilityHelper: BarChartAccessibilityHelper
fun bind(item: BarChartItem) {
chart.setNoDataText("")
coroutineScope.launch {
delay(50)
val barCount = chart.draw(item, labelStart, labelEnd)
chart.post {
val accessibilityEvent = object : BarChartAccessibilityEvent {
override fun onHighlight(
entry: BarEntry,
index: Int
) {
chart.highlightColumn(index, item.overlappingEntries != null)
val value = entry.data as? String
value?.let {
item.onBarSelected?.invoke(it)
}
}
}
val cutContentDescriptions = takeEntriesWithinGraphWidth(barCount, item.entryContentDescriptions)
accessibilityHelper = BarChartAccessibilityHelper(
chart,
contentDescriptions = cutContentDescriptions,
accessibilityEvent = accessibilityEvent
)
ViewCompat.setAccessibilityDelegate(chart, accessibilityHelper)
}
}
}
@SuppressWarnings("CyclomaticComplexMethod")
private fun BarChart.draw(
item: BarChartItem,
labelStart: TextView,
labelEnd: TextView
): BarCount {
resetChart()
val graphWidth = DisplayUtils.pxToDp(context, width)
val columnNumber = (graphWidth / 24) - 1
val count = if (columnNumber > MIN_COLUMN_COUNT) columnNumber else MIN_COLUMN_COUNT
val cutEntries = takeEntriesWithinGraphWidth(count, item.entries)
val mappedEntries = cutEntries.mapIndexed { index, pair -> toBarEntry(pair, index) }
val maxYValue = cutEntries.maxByOrNull { it.value }!!.value
val hasData = item.entries.isNotEmpty() && item.entries.any { it.value > 0 }
val dataSet = if (hasData) {
buildDataSet(context, mappedEntries)
} else {
buildEmptyDataSet(context, cutEntries.size)
}
item.onBarChartDrawn?.invoke(dataSet.entryCount)
val dataSets = mutableListOf<IBarDataSet>()
dataSets.add(dataSet)
val hasOverlappingEntries = hasData && item.overlappingEntries != null
if (hasData && item.overlappingEntries != null) {
val overlappingCut = takeEntriesWithinGraphWidth(count, item.overlappingEntries)
val mappedOverlappingEntries = overlappingCut.mapIndexed { index, pair -> toBarEntry(pair, index) }
val overlappingDataSet = buildOverlappingDataSet(context, mappedOverlappingEntries)
dataSets.add(overlappingDataSet)
}
if (hasData && item.onBarSelected != null) {
getHighlightDataSet(context, mappedEntries)?.let { dataSets.add(it) }
}
data = BarData(dataSets)
val greyColor = ContextCompat.getColor(
context,
R.color.neutral_30
)
val lightGreyColor = ContextCompat.getColor(
context,
R.color.stats_bar_chart_gridline
)
axisLeft.apply {
valueFormatter = LargeValueFormatter()
setDrawGridLines(true)
setDrawTopYLabelEntry(true)
setDrawZeroLine(false)
setDrawAxisLine(false)
granularity = 1f
axisMinimum = 0f
axisMaximum = if (maxYValue < MIN_VALUE) {
MIN_VALUE
} else {
roundUp(maxYValue.toFloat())
}
setLabelCount(5, true)
textColor = greyColor
gridColor = lightGreyColor
textSize = 10f
gridLineWidth = 1f
}
extraLeftOffset = 8f
axisRight.apply {
setDrawGridLines(false)
setDrawZeroLine(false)
setDrawLabels(false)
setDrawAxisLine(false)
}
xAxis.apply {
granularity = 1f
setDrawAxisLine(false)
setDrawGridLines(false)
setDrawLabels(false)
}
labelStart.text = cutEntries.first().label
labelEnd.text = cutEntries.last().label
setPinchZoom(false)
setScaleEnabled(false)
legend.isEnabled = false
setDrawBorders(false)
val isClickable = item.onBarSelected != null
if (isClickable) {
setOnChartValueSelectedListener(object : OnChartValueSelectedListener {
override fun onNothingSelected() {
item.selectedItem
highlightColumn(cutEntries.indexOfFirst { it.id == item.selectedItem }, hasOverlappingEntries)
item.onBarSelected?.invoke(item.selectedItem)
}
override fun onValueSelected(e: Entry, h: Highlight) {
val value = (e as? BarEntry)?.data as? String
highlightColumn(e.x.toInt(), hasOverlappingEntries)
item.onBarSelected?.invoke(value)
}
})
} else {
setOnChartValueSelectedListener(null)
}
isHighlightFullBarEnabled = isClickable
isHighlightPerDragEnabled = false
isHighlightPerTapEnabled = isClickable
val description = Description()
description.text = ""
this.description = description
if (item.selectedItem != null) {
val index = cutEntries.indexOfFirst { it.id == item.selectedItem }
if (index >= 0) {
highlightColumn(index, hasOverlappingEntries)
} else {
highlightValue(null, false)
}
}
invalidate()
return cutEntries.size
}
private fun BarChart.highlightColumn(index: Int, hasOverlappingColumns: Boolean) {
if (hasOverlappingColumns) {
val high = Highlight(index.toFloat(), 0, 0)
val high2 = Highlight(index.toFloat(), 1, 1)
val high3 = Highlight(index.toFloat(), 2, 2)
high.dataIndex = index
high2.dataIndex = index
high3.dataIndex = index
highlightValues(arrayOf(high3, high, high2))
} else {
val high = Highlight(index.toFloat(), 0, 0)
val high2 = Highlight(index.toFloat(), 1, 1)
high.dataIndex = index
high2.dataIndex = index
highlightValues(arrayOf(high2, high))
}
}
private fun buildEmptyDataSet(context: Context, count: Int): BarDataSet {
val emptyValues = (0 until count).map { index -> BarEntry(index.toFloat(), 1f, "empty") }
val dataSet = BarDataSet(emptyValues, "Empty")
dataSet.setGradientColor(
ContextCompat.getColor(
context,
R.color.primary_5
), ContextCompat.getColor(
context,
AndroidR.color.transparent
)
)
dataSet.formLineWidth = 0f
dataSet.setDrawValues(false)
dataSet.isHighlightEnabled = false
dataSet.highLightAlpha = 255
return dataSet
}
private fun buildDataSet(context: Context, cut: List<BarEntry>): BarDataSet {
val dataSet = BarDataSet(cut, "Data")
dataSet.color = ContextCompat.getColor(context, R.color.stats_bar_chart_top)
dataSet.setGradientColor(
ContextCompat.getColor(
context,
R.color.stats_bar_chart_top
), ContextCompat.getColor(
context,
R.color.stats_bar_chart_top
)
)
dataSet.formLineWidth = 0f
dataSet.setDrawValues(false)
dataSet.isHighlightEnabled = true
dataSet.highLightColor = ContextCompat.getColor(
context,
R.color.stats_bar_chart_accent_top
)
dataSet.highLightAlpha = 255
return dataSet
}
private fun buildOverlappingDataSet(context: Context, cut: List<BarEntry>): BarDataSet {
val dataSet = BarDataSet(cut, "Overlapping data")
dataSet.color = ContextCompat.getColor(context, R.color.primary_60)
dataSet.setGradientColor(
ContextCompat.getColor(
context,
R.color.stats_bar_chart_bottom
), ContextCompat.getColor(
context,
R.color.stats_bar_chart_bottom
)
)
dataSet.formLineWidth = 0f
dataSet.setDrawValues(false)
dataSet.isHighlightEnabled = true
dataSet.highLightColor = ContextCompat.getColor(
context,
R.color.stats_bar_chart_accent_bottom
)
dataSet.highLightAlpha = 255
return dataSet
}
private fun getHighlightDataSet(context: Context, cut: List<BarEntry>): BarDataSet? {
val maxEntry = cut.maxByOrNull { it.y } ?: return null
val highlightedDataSet = cut.map {
BarEntry(it.x, maxEntry.y, it.data)
}
val dataSet = BarDataSet(highlightedDataSet, "Highlight")
dataSet.color = ContextCompat.getColor(context, AndroidR.color.transparent)
dataSet.setGradientColor(
ContextCompat.getColor(
context,
AndroidR.color.transparent
), ContextCompat.getColor(
context,
AndroidR.color.transparent
)
)
dataSet.formLineWidth = 0f
dataSet.isHighlightEnabled = true
dataSet.highLightColor = ContextCompat.getColor(
context,
R.color.stats_bar_chart_accent_top
)
dataSet.setDrawValues(false)
dataSet.highLightAlpha = 51
return dataSet
}
private fun <T> takeEntriesWithinGraphWidth(
count: Int,
entries: List<T>
): List<T> {
return if (count < entries.size) entries.subList(
entries.size - count,
entries.size
) else {
entries
}
}
private fun BarChart.resetChart() {
fitScreen()
data?.clearValues()
xAxis.valueFormatter = null
notifyDataSetChanged()
clear()
invalidate()
}
private fun toBarEntry(bar: Bar, index: Int): BarEntry {
return BarEntry(
index.toFloat(),
bar.value.toFloat(),
bar.id
)
}
private fun roundUp(input: Float): Float {
return if (input > 100) {
roundUp(input / 10) * 10
} else {
for (i in 1..25) {
val limit = 4 * i
if (input < limit) {
return limit.toFloat()
}
}
return 100F
}
}
}