Developers often face the challenge of presenting data in a visually appealing manner in their Android applications. Data visualization not only enhances user experience but also makes comprehension much easier. Fortunately, there are several chart libraries designed specifically for Kotlin Android, making it easier to integrate beautiful, interactive charts into your apps. In this tutorial, we will explore some of the best chart libraries, understand their features, learn how to install them, and see code examples to get started.
1. MPAndroidChart
MPAndroidChart is one of the most popular and comprehensive chart libraries for Android. Created by Philipp Jahoda, it offers a wide variety of chart types and is known for its performance and easy integration.
List of Features
- Support for various chart types: Line, Bar, Pie, Scatter, Candle, Bubble, and Radar charts.
- Real-time and interactive chart capabilities.
- Zooming and panning functionalities.
- Customizable styling.
- Support for animations.
- Easy data handling and updating.
Installation
To integrate MPAndroidChart into your project, add the following dependency to your build.gradle
file:.
dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}
Code Example
Adding a Line Chart
- XML Layout:.
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/lineChart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- Kotlin Code:.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
class MainActivity : AppCompatActivity() {
lateinit var lineChart: LineChart
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lineChart = findViewById(R.id.lineChart)
val entries = ArrayList<Entry>()
entries.add(Entry(0f, 1f))
entries.add(Entry(1f, 2f))
entries.add(Entry(2f, 0f))
entries.add(Entry(3f, 4f))
val dataSet = LineDataSet(entries, "Label")
val lineData = LineData(dataSet)
lineChart.data = lineData
lineChart.invalidate() // Refresh the chart
}
}
Explanation:.
- XML Layout: Defines a
LineChart
view. - Kotlin Code: Creates
Entry
objects with X and Y values. These entries are added to aLineDataSet
, which is a collection of entries that will be drawn as a line. Finally, aLineData
object is created with the dataset and set to theLineChart
.invalidate
method refreshes the chart with the new data.
Comparison
MPAndroidChart is highly versatile and widely used due to its extensive features and regular updates. Compared to others, it offers the most comprehensive set of chart types and functionalities.
Reference Link
2. AnyChart
AnyChart is a versatile JavaScript charting library that provides numerous features and supports multiple platforms, including Kotlin Android. This library is known for its beautiful, interactive charts and ease of use.
List of Features
- Wide variety of charts: Line, Bar, Bubble, Funnel, Heat Map, Maps, etc.
- Cross-platform support.
- Detailed documentation and examples.
- Highly customizable options.
- Free and premium versions available.
Installation
Add the AnyChart dependency to your build.gradle
file:.
dependencies {
implementation 'com.github.AnyChart:AnyChart-Android:1.1.2'
}
Code Example
Adding a Bar Chart
- XML Layout:.
<com.anychart.AnyChartView
android:id="@+id/any_chart_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- Kotlin Code:.
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.anychart.AnyChart
import com.anychart.chart.common.dataentry.ValueDataEntry
import com.anychart.charts.Cartesian
import com.anychart.enums.Anchor
import com.anychart.enums.Position
import com.anychart.enums.TooltipPositionMode
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val anyChartView: AnyChartView = findViewById(R.id.any_chart_view)
val cartesian: Cartesian = AnyChart.column()
val data: MutableList<ValueDataEntry> = ArrayList()
data.add(ValueDataEntry("A", 10))
data.add(ValueDataEntry("B", 20))
data.add(ValueDataEntry("C", 30))
val column = cartesian.column(data)
column.tooltip()
.titleFormat("{%X}")
.position(Position.CENTER_BOTTOM)
.anchor(Anchor.CENTER_BOTTOM)
.offsetX(0.0)
.offsetY(5.0)
.format("\${%Value}{groupsSeparator: }")
cartesian.animation(true)
cartesian.title("Sample Bar Chart")
cartesian.yScale().minimum(0.0)
cartesian.tooltip().positionMode(TooltipPositionMode.POINT)
cartesian.xAxis(0).title("Category")
cartesian.yAxis(0).title("Value")
anyChartView.setChart(cartesian)
}
}
Explanation:.
- XML Layout: Defines an
AnyChartView
for displaying the chart. - Kotlin Code: Creates
ValueDataEntry
objects for data points.AnyChart.column
method creates a column chart. Tooltip settings and axis titles are configured, and the chart is assigned to theAnyChartView
.
Comparison
AnyChart shines with cross-platform support and detailed customization options. While not as feature-heavy as MPAndroidChart, it is excellent for developers who are looking for beautiful and highly customizable visualizations.
Reference Link
3. HelloCharts
HelloCharts is another powerful Android charting library, designed to be lightweight and easy to use. It supports a range of chart types and is known for its simplicity and quick setup.
List of Features
- Support for Line, Column, Pie, Bubble, Combo, and Preview charts.
- Smooth animations for data changes.
- Flawless zooming and scrolling.
- Customizable data points and labels.
- Lightweight and fast performance.
Installation
To use HelloCharts in your project, add the following dependency to the build.gradle
file:.
dependencies {
implementation 'com.github.lecho:hellocharts-android:v1.5.8'
}
Code Example
Adding a Pie Chart
- XML Layout:.
<lecho.lib.hellocharts.view.PieChartView
android:id="@+id/pieChart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
- Kotlin Code:.
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import lecho.lib.hellocharts.model.PieChartData
import lecho.lib.hellocharts.model.SliceValue
import lecho.lib.hellocharts.view.PieChartView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val pieChartView: PieChartView = findViewById(R.id.pieChart)
val values = ArrayList<SliceValue>()
values.add(SliceValue(30f, Color.RED).setLabel("Red"))
values.add(SliceValue(20f, Color.BLUE).setLabel("Blue"))
values.add(SliceValue(50f, Color.GREEN).setLabel("Green"))
val pieChartData = PieChartData(values)
pieChartData.setHasLabels(true)
pieChartView.pieChartData = pieChartData
}
}
Explanation:.
- XML Layout: Defines a
PieChartView
for showing the pie chart. - Kotlin Code: Creates
SliceValue
objects with values and colors. These slices are added to aPieChartData
object, which represents the pie chart data. Labels are enabled, and the data is set to thePieChartView
.
Comparison
HelloCharts is lightweight and easy to use, making it perfect for simple, quick chart implementations. While it might not offer as many customization options as MPAndroidChart or AnyChart, its simplicity is its greatest strength.
Reference Link
4. EazeGraph
EazeGraph is a lightweight library for creating beautiful and simple charts in Android applications. It is a go-to for minimalistic designs and straightforward implementations.
Features
Easy implementation for basic charts (Bar, Pie, Line, Stacked Bar).
Lightweight and performance-friendly.
Clean and modern visual styles.
XML attributes customization.
Installation
Add the following dependency to your build.gradle
file:.
dependencies {
implementation 'com.github.blackfizz:eazegraph:1.2.2'
}
Code Examples
Creating a Bar Chart
import com.db.chart.model.Bar
val barChart = findViewById<BarChartView>(R.id.barchart)
val bar = Bar("January", 5f)
bar.color = Color.parseColor("#FF6200EE")
barChart.addBar(bar)
barChart.show()
Explanation:.
- Imports: Essential class for the Bar chart is imported.
- FindViewById: Bar chart view is retrieved from the layout.
- Bar: A bar with a label and value is created and styled.
- AddBar & Show: Bar is added to the chart and displayed.
Comparison
EazeGraph is ideal for developers focused on simplicity and performance. However, it lacks some of the extensive features and chart types seen in more comprehensive libraries like MPAndroidChart and AnyChart.
Reference Link
MPAndroidChart stands out for its extensive features and variety, making it suitable for most complex needs. AnyChart offers beautiful, cross-platform charts with great customization options, whereas HelloCharts provides a simple and lightweight solution for quicker tasks.
Thanks for reading.