i am facing an issue with a bar chart where the bars are not filling with the assigned colors properly. Instead, they appear as empty. I have tried various methods to set custom colors for the bars, but none seem to work as expected.
private fun showEnergy() {
val list = listOf(
Triple("23-5-2024", 2010, 4.91),
Triple("22-5-2024", 960, 13.62),
Triple("21-5-2024", 5642, 2.0),
Triple("20-5-2024", 1464, 4.85)
)
constraintLayoutStatisticsData.removeAllViews()
constraintLayoutStatisticsData.addView(energyLayout)
barChart = constraintLayoutStatisticsData.findViewById(R.id.barChart)
val maxValue=10000f
setupBarChart(maxValue)
val currentDate = Date()
// Create a list of dates for the last 30 days
val dates = ArrayList<Date>()
val calendar = Calendar.getInstance()
for (i in 0 until 6) {
calendar.time = currentDate
calendar.add(Calendar.DAY_OF_MONTH, -i)
dates.add(calendar.time)
}
dates.reverse()
// Create a list of values, adding 0 for missing dates
val firstBarEntries = ArrayList<BarEntry>()
val secondBarEntries = ArrayList<BarEntry>()
val barWidth = 0.26f
val groupSpace = 0.32f
val barSpace = 0.08f
val dateFormat = SimpleDateFormat("d.M.yy", Locale.getDefault())
for (date in dates) {
val formattedDate = dateFormat.format(date)
val value = list.find { it.first == formattedDate }?.second ?: 0
firstBarEntries.add(BarEntry(date.time.toFloat()- barWidth / 2, value.toFloat()))
val value2 = 4000f
firstBarEntries.add(BarEntry(date.time.toFloat()- barWidth / 2, value2.toFloat()))
}
val firstDataSet = BarDataSet(firstBarEntries, "")
firstDataSet.color=ContextCompat.getColor(constraintLayoutStatisticsData.context, R.color.grey)
val secondDataSet = BarDataSet(secondBarEntries, "")
val customColors = mutableListOf<Int>()
for (i in list.indices) {
val ratio = list[i].second / 4000f
val color = when {
ratio >= 0.85 && ratio <= 1.15 -> ContextCompat.getColor(constraintLayoutStatisticsData.context, R.color.green)
ratio < 0.85 -> ContextCompat.getColor(constraintLayoutStatisticsData.context, R.color.orange)
else -> ContextCompat.getColor(constraintLayoutStatisticsData.context, R.color.red)
}
customColors.add(color)
}
firstDataSet.setDrawValues(false)
secondDataSet.setDrawValues(false)
secondDataSet.setColors(customColors)
val dataSet = listOf(firstDataSet, secondDataSet)
// Create a BarData object and set data sets
val barData = BarData(dataSet)
barData.barWidth = barWidth
val xAxis = barChart.xAxis
xAxis.axisMinimum = -0.5f
xAxis.axisMaximum = list.size - 0.5f
xAxis.valueFormatter = object : ValueFormatter() {
private val dateFormat = SimpleDateFormat("dd.MM", Locale.getDefault())
override fun getFormattedValue(value: Float): String {
val index = value.toInt()
return if (index >= 0 && index < dates.size) {
dateFormat.format(dates[index])
} else {
""
}
}
}
barChart.axisLeft.valueFormatter = object : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
// Replace commas with spaces
val formattedValue = value.toInt().toString().replace(',', ' ')
// Return the formatted value as a string
return formattedValue
}
}
// Set the chart data
barChart.data = barData
// Update chart
barChart.invalidate()
}
private fun setupBarChart(maxValue: Float) {
barChart.setDrawBarShadow(false)
barChart.description.isEnabled = false
barChart.setMaxVisibleValueCount(60)
barChart.setPinchZoom(false)
barChart.setDrawGridBackground(false)
val xAxis = barChart.xAxis
xAxis.setDrawGridLines(false)
xAxis.position = XAxis.XAxisPosition.BOTTOM
xAxis.textColor = ContextCompat.getColor(constraintLayoutStatisticsData.context, R.color.text)
xAxis.granularity = 1f
val yAxisLeft = barChart.axisLeft
yAxisLeft.setDrawAxisLine(false)
yAxisLeft.textColor = ContextCompat.getColor(constraintLayoutStatisticsData.context, R.color.text)
val minValue = 0f
val labelCount = 5
val interval = Math.ceil((maxValue - minValue) / (labelCount - 1).toDouble()).toFloat()
yAxisLeft.axisMinimum = minValue
yAxisLeft.axisMaximum = maxValue
yAxisLeft.setGranularity(interval)
yAxisLeft.setLabelCount(labelCount, true)
yAxisLeft.gridLineWidth = 2f
barChart.axisRight.isEnabled = false
barChart.legend.isEnabled = false
}
I have also tried using ARGB values and gradients, but the result remains the same. The bars simply refuse to display the assigned colors.
Has anyone encountered a similar issue with bar charts? What could be causing this problem? Are there any specific configurations or settings that I might be missing?
Any help or suggestions would be greatly appreciated. Thank you in advance!
it needs to be like this:bars with colors
but it is like this:
a
Liam Harpe is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.