Create Barchart in Android Studio

Kartik
2 min readJun 21, 2019

--

I will keep the code simple and let's jump into the coding part.

Setting up build.gradle(Module: app) file

Add the following code in your file.

repositories {
maven { url "https://jitpack.io" }
}
dependencies {
//...
//...
implementation 'com.github.PhilJay:MPAndroidChart:v2.2.4'
}

and click on the Sync option which pops up.

Building activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context="Android.in.barchart.MainActivity"
>

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

</RelativeLayout>

Coding MainActivity.java file

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

BarChart barChart = (BarChart) findViewById(R.id.barchart);

ArrayList<BarEntry> entries = new ArrayList<>();
entries.add(new BarEntry(8f, 0));
entries.add(new BarEntry(2f, 1));
entries.add(new BarEntry(5f, 2));
entries.add(new BarEntry(20f, 3));
entries.add(new BarEntry(15f, 4));
entries.add(new BarEntry(19f, 5));

BarDataSet bardataset = new BarDataSet(entries, "Cells");

ArrayList<String> labels = new ArrayList<String>();
labels.add("2016");
labels.add("2015");
labels.add("2014");
labels.add("2013");
labels.add("2012");
labels.add("2011");

BarData data = new BarData(labels, bardataset);
barChart.setData(data); // set the data and list of labels into chart
barChart.setDescription("Set Bar Chart Description Here"); // set the description
bardataset.setColors(ColorTemplate.COLORFUL_COLORS);
barChart.animateY(5000);

}
}

Play with entries.add() and labels.add() functions. You will understand how plotting is done.

Basically,

entries.add(new BarEntry(8f, 0);

Here 8f is the value and 0 is the index value which is mapping to

labels.add("2016");

which is at 0th index.

Source and Credits: https://github.com/PhilJay/MPAndroidChart

which is A powerful 🚀 Android chart view/graph view library, supporting line- bar- pie- radar- bubble- and candlestick charts as well as scaling, dragging and animations.

Clone/Download the repository and play with the code. There is so many chart example available.

Thank You.

--

--

Kartik

Onboarding people to NFT | Developer 👨‍💻 JPEG Collector | Love Spreading Knowledge | Are you new to NFT? Always happy to help.