Volodymyr Khodonovych Volodymyr Khodonovych - 1 year ago 140
Android Question

Bar chart android

I need to plot simple Bar chart in android (something like that bar chart). I have found many libs but they very complex not really useful. Maybe someone already uses bar chart and know a good one?

Answer Source

I have very simple and easy solution for Bar Chart in android using MpAndroidChart Library.

Add library in build.gradle file:

compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'

MainActivity.java:

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

    ArrayList<BarEntry> entries = new ArrayList<>();
    entries.add(new BarEntry(38f, 0));
    entries.add(new BarEntry(52f, 1));
    entries.add(new BarEntry(65f, 2));
    entries.add(new BarEntry(30f, 3));
    entries.add(new BarEntry(85f, 4));
    entries.add(new BarEntry(19f, 5));
    entries.add(new BarEntry(75f, 6));

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

    ArrayList<String> labels = new ArrayList<String>();
    labels.add("Mon");
    labels.add("Tue");
    labels.add("Wed");
    labels.add("Thus");
    labels.add("Fri");
    labels.add("Sat");
    labels.add("Sun");

    BarData data = new BarData(labels, bardataset);
    barChart.setData(data); // set the data and list of lables into chart

    barChart.setDescription("Description");  // set the description

    bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

    barChart.animateY(5000);

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.example.jatin.bargraph.MainActivity">
    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/barchart"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download