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.
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.
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 .
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 :