How to draw BubbleChart using MPAndroidChart in Android

In this android programming source code example, we are going to draw BubbleChart using MPAndroidChart in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to draw BubbleChart using MPAndroidChart in Android.

activity_main.xml

<?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=".AndroidChart.AndroidChartActivity7">

    <com.github.mikephil.charting.charts.BubbleChart
        android:id="@+id/bubbleChart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.github.mikephil.charting.charts.BubbleChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BubbleData;
import com.github.mikephil.charting.data.BubbleDataSet;
import com.github.mikephil.charting.data.BubbleEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

public class AndroidChartActivity7 extends AppCompatActivity {
    private BubbleChart bubbleChart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_chart7);
        bubbleChart = (BubbleChart)findViewById(R.id.bubbleChart);
        BubbleDataSet bubbleDataSet = new BubbleDataSet(getData(), "Inducesmile");
        bubbleDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
        BubbleData scatterData = new BubbleData(bubbleDataSet);
        XAxis xAxis = bubbleChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        final String[] months = new String[]{"Jan", "Feb", "Mar", "Apr", "May", "Jun"};
        IndexAxisValueFormatter formatter = new IndexAxisValueFormatter(months);
        xAxis.setGranularity(1f);
        xAxis.setValueFormatter(formatter);
        bubbleChart.setData(scatterData);
        bubbleChart.animateXY(5000, 5000);
        bubbleChart.invalidate();
    }

    private ArrayList getData(){
        ArrayList<BubbleEntry> entries = new ArrayList<>();
        entries.add(new BubbleEntry(0f, 1f, 0.21f));
        entries.add(new BubbleEntry(1f, 2f, 0.12f));
        entries.add(new BubbleEntry(2f, 3f, 0.20f));
        entries.add(new BubbleEntry(3f, 4f, 0.52f));
        entries.add(new BubbleEntry(4f, 5f, 0.29f));
        entries.add(new BubbleEntry(5f, 6f, 0.62f));
        return entries;
    }
}

app/build.gradle

implementation 'com.github.Philjay:MPAndroidChart:v3.1.0'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.