MPAndroidChart for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

MPAndroidChart for Kotlin: Complete Guide with Examples

MPAndroidChart is a powerful, open-source library for creating charts and graphs in Android apps. Written in Java, it integrates seamlessly with Kotlin thanks to full interoperability. This library is the de facto standard for data visualization in the Android ecosystem, offering a wide range of chart types—from simple line and bar charts to complex pie, bubble, and combined charts.

Why use MPAndroidChart? In modern mobile development, data visualization is key. Whether you're displaying financial metrics, workout results, app usage stats, or temperature trends, a well-designed chart makes information clear and actionable. MPAndroidChart handles all the heavy lifting—rendering, animations, touch handling, and zooming—so you can focus on your app's logic. It supports thousands of data points, runs smoothly even on older devices, and offers dozens of customization options.

In this guide, we'll walk through installation, core classes and methods, build several working examples in Kotlin, and share performance optimization tips. By the end, you'll confidently integrate any chart type into your Android app.

1. Installation

MPAndroidChart is distributed via Maven Central. To install, add the dependency to your module's build.gradle file (usually app/build.gradle).

Gradle command:

dependencies {
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
}

After adding the line, sync your project (Sync Now). The library is ready to use.

Important: Make sure your root build.gradle (project-level) includes the Maven Central repository:

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

If you're using the newer configuration (Gradle 7+), add the repository in settings.gradle.kts:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

2. Quick Start — Minimal Working Example

Let's create a simple line chart with three data points. First, add a LineChart to your XML layout:

<!-- activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lineChart"
        android:layout_width="match_parent"
        android:layout_height="400dp" />

</LinearLayout>

Now the code in your Activity (Kotlin):

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
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() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val lineChart: LineChart = findViewById(R.id.lineChart)

        // 1. Create data entries
        val entries = listOf(
            Entry(0f, 10f),   // x=0, y=10
            Entry(1f, 20f),   // x=1, y=20
            Entry(2f, 15f)    // x=2, y=15                    

Recommendations