First of all english isn't my first language so please excuse errors. I'm using Android Studio, some years ago i wrote a simple app to store some data and generate different graphs (not in real time), it was in java and using MPAndroidChart. Now i wanted to update the app and i decided to convert to kotlin (it wasn't so difficult) and i wanted to update the graph library too, so after trying some others i used AndroidChart (a fork of MPAndroidChart), but now grouping of datasets doesn't work (single dataset, and stacked datasets are ok).
This is the reduced code to an only one function:
private fun testbargroup(): BarData {
var titolo = "dataset 1"
var titolo2 = "dataset 2"
val set1: BarDataSet
var set2: BarDataSet
val serie1: ArrayList<BarEntry> = arrayListOf<BarEntry>()
var serie2: ArrayList<BarEntry> = arrayListOf<BarEntry>()
for (i in 0..12) {
serie1.add(BarEntry(i.toFloat(), ((i*0.8)+1).toFloat()))
serie2.add(BarEntry(i.toFloat(), ((i*1.2)+0.9).toFloat()))
}
set1 = BarDataSet(serie1, titolo) //perchè serve anche per il mensile
set1.setDrawValues(true)
set1.color = getColor(R.color.colorSet1)
set1.setValueTextColor(R.color.colorSet1)
set2 = BarDataSet(serie2, titolo2)
set2.setDrawValues(true)
set2.color = getColor(R.color.colorSet2)
set2.setValueTextColor(R.color.colorSet2)
val dataSets: ArrayList<IBarDataSet> = ArrayList()
dataSets.add(set1)
dataSets.add(set2!!)
var bdata = BarData(dataSets)
mChart.setData(bdata)
mChart.barData.setValueFormatter(CustDefValueFormatter())
val xAxis = mChart.xAxis
xAxis.textColor = getColor(R.color.colorDark1)
mChart.setFitBars(true)
//dimension istandard per 1 set di barre
bdata.barWidth = 0.9f
bdata.setValueTextSize(9f)
mChart.xAxis.setCenterAxisLabels(false)
if (bdata.getDataSetCount() == 2) { //se ci sono più datasets (noi al max 2) raggruppiamo, altrimenti sovrappone
mChart.xAxis.setCenterAxisLabels(true)
val groupSpace = 0.2f
val barSpace = 0.0f
val barWidth = 0.4f
mChart.groupBars(1f, groupSpace, barSpace)
bdata.barWidth = barWidth
bdata.setValueTextSize(6f)
}
mChart.invalidate()
return bdata
}
mChart initiated in onCreate with findviewbyid, xml layout file is the same in both versions
And here the result, with bars not grouped: Graph
Thanks in advance for any suggestion (I know that the code can be improved, first af all let it works as expected!)
I found the solution, in case of multiple datasets i don't have to set barWidth property.