5

Create Bar Chart Graph using MpAndroidChart Library Android Studio example tutorial

How to make chart with multiple values inside android application programmatically.

Mp Android Chart Library is developed by PhilJay and available on Github for every android developer who wish to create simple Graph chart inside their android applications. This library allow us to create beautiful charts to show our data into well settled format inside android apps. So here is the complete step by step tutorial for Create Bar Chart Graph using MpAndroidChart Library Android Studio example tutorial.

android-project-download-code-button

Please add below following repositories and dependencies code to your build.gradle( Module:app ) file and You don’t need to download manually the whole library because after adding below code it will automatically download and install the Mp Android Library into your project.

build-gradle

NOTE : Add this to your build.gradle( Module:app ) file.

repositories {
 maven { url "https://jitpack.io" }
}

dependencies {
 compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
}

Here is the screenshot of my build.gradle( Module:app ) file after adding above code .

mp-android-chart

How to Create Bar Chart Graph using MpAndroidChart Library Android Studio example tutorial.

Code for MainActivity.java file.

package com.android_examples.barchart_android_examplescom;
import java.util.ArrayList;
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;

public class MainActivity extends AppCompatActivity {

    BarChart chart ;
    ArrayList<BarEntry> BARENTRY ;
    ArrayList<String> BarEntryLabels ;
    BarDataSet Bardataset ;
    BarData BARDATA ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        chart = (BarChart) findViewById(R.id.chart1);

        BARENTRY = new ArrayList<>();

        BarEntryLabels = new ArrayList<String>();

        AddValuesToBARENTRY();

        AddValuesToBarEntryLabels();

        Bardataset = new BarDataSet(BARENTRY, "Projects");

        BARDATA = new BarData(BarEntryLabels, Bardataset);

        Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

        chart.setData(BARDATA);

        chart.animateY(3000);

    }

    public void AddValuesToBARENTRY(){

        BARENTRY.add(new BarEntry(2f, 0));
        BARENTRY.add(new BarEntry(4f, 1));
        BARENTRY.add(new BarEntry(6f, 2));
        BARENTRY.add(new BarEntry(8f, 3));
        BARENTRY.add(new BarEntry(7f, 4));
        BARENTRY.add(new BarEntry(3f, 5));

    }

    public void AddValuesToBarEntryLabels(){

        BarEntryLabels.add("January");
        BarEntryLabels.add("February");
        BarEntryLabels.add("March");
        BarEntryLabels.add("April");
        BarEntryLabels.add("May");
        BarEntryLabels.add("June");

    }
}

Code for activity_main.xml layout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.barchart_android_examplescom.MainActivity">

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

</RelativeLayout>

Screenshots :

Bar-Graph-1

Create Bar Chart Graph using MpAndroidChart Library Android Studio example tutorial

Click here to download Create Bar Chart Graph using MpAndroidChart Library Android Studio example tutorial project with source code

5 Comments

  1. I entered code exactly like your example.
    Compile error – BARDATA = new BarData(BarEntryLabels, Bardataset);

    Error:(35, 19) error: constructor BarData in class BarData cannot be applied to given types;
    required: IBarDataSet[]
    found: ArrayList,BarDataSet
    reason: varargs mismatch; ArrayList cannot be converted to IBarDataSet

    • Hello. Same problem here, the solution could be the following:
      IBarDataSet Bardataset ;
      Bardataset = new BarDataSet(BARENTRY, “Projects”);

Leave a Reply

Your email address will not be published. Required fields are marked *