Saturday 4 March 2017

Android Grouped Bar Chart customized X axis label with mpandroidchart


To use the library, you need to do this in Gradle dependency:

  • Add the following to your project level build.gradle:
allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}
  • Add this to your app build.gradle:
dependencies {
    compile 'com.github.PhilJay:MPAndroidChart:v3.0.1'
}
Next go to your layout file (constraint layout format) create the mpandroidchart graph view with the following code:

<com.github.mikephil.charting.charts.BarChart
        android:id="@+id/barChart"
        android:layout_width="0dp"
        android:layout_marginEnd="20dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginRight="20dp"
        android:layout_marginStart="20dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="20dp" />


Next go in to your java file create these few variables field first:

private BarChart chart;
float barWidth;
float barSpace;
float groupSpace;

barWidth = 0.3f;
barSpace = 0f;
groupSpace = 0.4f;

Next go in the override onCreate method

Set the chart setting with the below following code

chart = (BarChart)findViewById(R.id.barChart);
chart.setDescription(null);
chart.setPinchZoom(false);
chart.setScaleEnabled(false);
chart.setDrawBarShadow(false);
chart.setDrawGridBackground(false);

Next create the dummy data for display the graph

int groupCount = 6;

 ArrayList xVals = new ArrayList();

 xVals.add("Jan"); 
 xVals.add("Feb");
 xVals.add("Mar"); 
 xVals.add("Apr"); 
 xVals.add("May");
 xVals.add("Jun");

 ArrayList yVals1 = new ArrayList();
 ArrayList yVals2 = new ArrayList(); 

 yVals1.add(new BarEntry(1, (float) 1));
 yVals2.add(new BarEntry(1, (float) 2)); 
 yVals1.add(new BarEntry(2, (float) 3));
 yVals2.add(new BarEntry(2, (float) 4)); 
 yVals1.add(new BarEntry(3, (float) 5));
 yVals2.add(new BarEntry(3, (float) 6));
 yVals1.add(new BarEntry(4, (float) 7)); 
 yVals2.add(new BarEntry(4, (float) 8)); 
 yVals1.add(new BarEntry(5, (float) 9)); 
 yVals2.add(new BarEntry(5, (float) 10));
 yVals1.add(new BarEntry(6, (float) 11));
 yVals2.add(new BarEntry(6, (float) 12));

Next, to draw the graph

BarDataSet set1, set2;
set1 = new BarDataSet(yVals1, "A");
set1.setColor(Color.RED);
set2 = new BarDataSet(yVals2, "B");
set2.setColor(Color.BLUE);
BarData data = new BarData(set1, set2);
data.setValueFormatter(new LargeValueFormatter());
chart.setData(data);
chart.getBarData().setBarWidth(barWidth);
chart.getXAxis().setAxisMinimum(0);
chart.getXAxis().setAxisMaximum(0 + chart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
chart.groupBars(0, groupSpace, barSpace);
chart.getData().setHighlightEnabled(false);
chart.invalidate();

Draw the indicator

Legend l = chart.getLegend();
l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
l.setDrawInside(true);
l.setYOffset(20f);
l.setXOffset(0f);
l.setYEntrySpace(0f);
l.setTextSize(8f);


Draw the X-Axis and Y-Axis

//X-axis
XAxis xAxis = chart.getXAxis();
xAxis.setGranularity(1f);
xAxis.setGranularityEnabled(true);
xAxis.setCenterAxisLabels(true);
xAxis.setDrawGridLines(false);
xAxis.setAxisMaximum(6);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setValueFormatter(new IndexAxisValueFormatter(xVals));
//Y-axis
chart.getAxisRight().setEnabled(false);
YAxis leftAxis = chart.getAxisLeft();
leftAxis.setValueFormatter(new LargeValueFormatter());
leftAxis.setDrawGridLines(true);
leftAxis.setSpaceTop(35f);
leftAxis.setAxisMinimum(0f);

Output:


Video tutorial: click here

7 comments:

  1. I have followed your tutorial but the labels are not corresponding with the grouped bars on the graph. Checkout this image https://hamsoftug.com/pega/graph-out-put.png and kindly help me out.

    ReplyDelete
  2. If i am adding bar "C" in each group, xAxis labels are not showing properly below every group.

    ReplyDelete
    Replies
    1. Remember adjust the bar width, space and group space...... try to understand the code and also read the documentation....

      Delete