Reputation:
Am trying to use Kotlin to display a simple bar chart using mpandroidchart library. Most of the examples I've been finding online are using java, so copy the code, paste in android studio to convert to Kotlin, then try to fix the errors were necessary. But i just can't seem to fix the one appearing when using the BarEntry as shown in the image. Any help is appreciated. Thanks
Upvotes: 4
Views: 10062
Reputation: 11
Unresolved reference: barChart and Variable expected
private fun setBarChart() {
val entries = ArrayList<BarEntry>()
entries.add(BarEntry(8f, 0))
entries.add(BarEntry(2f, 1))
entries.add(BarEntry(5f, 2))
entries.add(BarEntry(20f, 3))
entries.add(BarEntry(15f, 4))
entries.add(BarEntry(19f, 5))
val barDataSet = BarDataSet(entries, "Cells")
val labels = ArrayList<String>()
labels.add("18-Jan")
labels.add("19-Jan")
labels.add("20-Jan")
labels.add("21-Jan")
labels.add("22-Jan")
labels.add("23-Jan")
val data = BarData(labels, barDataSet)
barChart.data = data // set the data and list of lables into chart
barChart.setDescription("Bar Chart") // set the description
//barDataSet.setColors(ColorTemplate.COLORFUL_COLORS)
barDataSet.color = resources.getColor(R.color.colorAccent)
barChart.animateY(5000)
}
}
Upvotes: -1
Reputation: 229
step 1:
dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v2.2.4'
}
step 2:
repositories {
maven { url "https://jitpack.io" }
}
step 3:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setBarChart()
}
private fun setBarChart() {
val entries = ArrayList<BarEntry>()
entries.add(BarEntry(8f, 0))
entries.add(BarEntry(2f, 1))
entries.add(BarEntry(5f, 2))
entries.add(BarEntry(20f, 3))
entries.add(BarEntry(15f, 4))
entries.add(BarEntry(19f, 5))
val barDataSet = BarDataSet(entries, "Cells")
val labels = ArrayList<String>()
labels.add("18-Jan")
labels.add("19-Jan")
labels.add("20-Jan")
labels.add("21-Jan")
labels.add("22-Jan")
labels.add("23-Jan")
val data = BarData(labels, barDataSet)
barChart.data = data // set the data and list of lables into chart
barChart.setDescription("Set Bar Chart Description") // set the description
//barDataSet.setColors(ColorTemplate.COLORFUL_COLORS)
barDataSet.color = resources.getColor(R.color.colorAccent)
barChart.animateY(5000)
}
}
step 4 :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barChart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 6
Reputation: 15423
BarEntry only accept Float
and FloatArray
. You are passing Int
instead of Float
. Use f
after each value like:
val barEntry = BarEntry(8f, 0f)
Or convert your value to Float
using toFloat()
provided by Kotlin like:
val barEntry = BarEntry(8f, 0.toFloat())
Upvotes: 3