title: How to add bar per hour and align per top label in mp chart in android
body: <pre><code>package com.example.stackanswer;
  
  import android.graphics.Color;
  import android.graphics.Typeface;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.widget.Toast;
  
  import com.github.mikephil.charting.charts.BarChart;
  import com.github.mikephil.charting.components.Legend;
  import com.github.mikephil.charting.components.XAxis;
  import com.github.mikephil.charting.components.YAxis;
  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.formatter.IndexAxisValueFormatter;
  import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
  
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  
  public class MainActivity extends AppCompatActivity {
  
      private static final String TAG = MainActivity.class.getName();
      BarChart barChart;
      ArrayList&lt;IBarDataSet&gt; dataSets = new ArrayList&lt;&gt;();
      float defaultBarWidth = -1;
      List&lt;String&gt; xAxisValues1 = new ArrayList&lt;&gt;(Arrays.asList(&quot;12Am-6am&quot;, &quot;6am-12pm&quot;, &quot;12pm-6pm&quot;, &quot;6pm-12am&quot;));
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          barChart = findViewById(R.id.barchart);
          setChart(4);
      }
  
  
      private void setChart(int size) {
  
          List&lt;BarEntry&gt; data1 = getListOf12am6am(6);
          List&lt;BarEntry&gt; data2 = getListOf6am12pm(6);
          List&lt;BarEntry&gt; data3 = getListOf12pm6pm(6);
          List&lt;BarEntry&gt; data4 = getListOf6pm12am(6);
  
          dataSets = new ArrayList&lt;&gt;();
          BarDataSet set1, set2,set3,set4;
  
          set1 = new BarDataSet(data1, &quot;midnight&quot;);
          set1.setColor(Color.parseColor(&quot;#7ffbde&quot;));
          set1.setValueTextColor(Color.rgb(55, 70, 73));
          set1.setValueTextSize(10f);
  
          set2 = new BarDataSet(data2, &quot;morning&quot;);
          set2.setColors(Color.parseColor(&quot;#7ffbde&quot;));
          set2.setValueTextColor(Color.rgb(55, 70, 73));
          set2.setValueTextSize(10f);
  
          set3 = new BarDataSet(data3, &quot;afternoon&quot;);
          set3.setColor(Color.parseColor(&quot;#7ffbde&quot;));
          set3.setValueTextColor(Color.rgb(55, 70, 73));
          set3.setValueTextSize(10f);
  
          set4 = new BarDataSet(data4, &quot;evening&quot;);
          set4.setColors(Color.parseColor(&quot;#7ffbde&quot;));
          set4.setValueTextColor(Color.rgb(55, 70, 73));
          set4.setValueTextSize(10f);
  
  
  
          dataSets.add(set1);
          dataSets.add(set2);
          dataSets.add(set3);
          dataSets.add(set4);
  
          BarData data = new BarData(dataSets);
          barChart.setData(data);
          barChart.getAxisLeft().setAxisMinimum(0);
  
          barChart.getDescription().setEnabled(false);
          barChart.getAxisRight().setAxisMinimum(0);
          barChart.setDrawBarShadow(false);
          barChart.setDrawValueAboveBar(true);
          barChart.setMaxVisibleValueCount(10);
          barChart.setPinchZoom(false);
          barChart.setDrawGridBackground(false);
  
  
          Legend l = barChart.getLegend();
          l.setWordWrapEnabled(true);
          l.setTextSize(14);
          l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
          l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
          l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
          l.setDrawInside(false);
          l.setForm(Legend.LegendForm.CIRCLE);
  
          XAxis xAxis = barChart.getXAxis();
          xAxis.setGranularity(1f);
          xAxis.setCenterAxisLabels(true);
          xAxis.setDrawGridLines(false);
          xAxis.setLabelRotationAngle(-45);
          xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
          xAxis.setAxisMaximum(getListOf12am6am(size).size());
          barChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(xAxisValues1));
  
          YAxis leftAxis = barChart.getAxisLeft();
          leftAxis.removeAllLimitLines();
          leftAxis.setTypeface(Typeface.DEFAULT);
          leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
          leftAxis.setLabelCount(3);
          leftAxis.setAxisMaximum(60);
          leftAxis.setAxisMinimum(0);
  
          leftAxis.setTextColor(Color.BLACK);
          leftAxis.setDrawGridLines(false);
          barChart.getAxisRight().setEnabled(false);
  
          setBarWidth(data, size);
          barChart.invalidate();
  
      }
  
      private List&lt;BarEntry&gt; getListOf12am6am(int size) {
          ArrayList&lt;BarEntry&gt; expenseEntries = new ArrayList&lt;&gt;();
          expenseEntries.add(new BarEntry(1, 10));
          expenseEntries.add(new BarEntry(2, 15));
          expenseEntries.add(new BarEntry(3, 2));
          expenseEntries.add(new BarEntry(4, 24));
          expenseEntries.add(new BarEntry(5, 18));
          expenseEntries.add(new BarEntry(6, 12));
          return expenseEntries.subList(0, size);
      }
  
      private List&lt;BarEntry&gt; getListOf6am12pm(int size) {
          ArrayList&lt;BarEntry&gt; expenseEntries = new ArrayList&lt;&gt;();
          expenseEntries.add(new BarEntry(1, 65));
          expenseEntries.add(new BarEntry(2, 54));
          expenseEntries.add(new BarEntry(3, 15));
          expenseEntries.add(new BarEntry(4, 12));
          expenseEntries.add(new BarEntry(15, 12));
          expenseEntries.add(new BarEntry(16, 6));
          return expenseEntries.subList(0, size);
      }
  
      private List&lt;BarEntry&gt; getListOf12pm6pm(int size) {
          ArrayList&lt;BarEntry&gt; incomeEntries = new ArrayList&lt;&gt;();
  
          incomeEntries.add(new BarEntry(1, 11));
          incomeEntries.add(new BarEntry(2, 13));
          incomeEntries.add(new BarEntry(3, 11));
          incomeEntries.add(new BarEntry(4, 72));
          incomeEntries.add(new BarEntry(5, 47));
          incomeEntries.add(new BarEntry(6, 4500));
          return incomeEntries.subList(0, size);
      }
  
      private List&lt;BarEntry&gt; getListOf6pm12am(int size) {
          ArrayList&lt;BarEntry&gt; incomeEntries = new ArrayList&lt;&gt;();
          incomeEntries.add(new BarEntry(7, 20));
          incomeEntries.add(new BarEntry(8, 30));
          incomeEntries.add(new BarEntry(9, 43));
          incomeEntries.add(new BarEntry(10, 57));
          incomeEntries.add(new BarEntry(11, 43));
          incomeEntries.add(new BarEntry(12, 16));
          return incomeEntries.subList(0, size);
      }
  
      private void setBarWidth(BarData barData, int size) {
          if (dataSets.size() &gt; 1) {
              float barSpace = 0.02f;
              float groupSpace = 0.3f;
              defaultBarWidth = (1 - groupSpace) / dataSets.size() - barSpace;
              if (defaultBarWidth &gt;= 0) {
                  barData.setBarWidth(defaultBarWidth);
              } else {
                  Toast.makeText(getApplicationContext(), &quot;Default Barwdith &quot; + defaultBarWidth, Toast.LENGTH_SHORT).show();
              }
              int groupCount = getListOf12am6am(size).size();
              if (groupCount != -1) {
                  barChart.getXAxis().setAxisMinimum(0);
                  barChart.getXAxis().setAxisMaximum(0 + barChart.getBarData().getGroupWidth(groupSpace, barSpace) * groupCount);
                  barChart.getXAxis().setCenterAxisLabels(true);
              } else {
                  Toast.makeText(getApplicationContext(), &quot;no of bar groups is &quot; + groupCount, Toast.LENGTH_SHORT).show();
              }
  
              barChart.groupBars(0, groupSpace, barSpace); // perform the &quot;explicit&quot; grouping
              barChart.invalidate();
          }
      }
  
  }
  </code></pre>
  <p>This my code using this code i am able to below graph</p>
  <p><a href="https://i.stack.imgur.com/thD4T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/thD4T.png" alt="enter image description here" /></a></p>
  <p>while i am trying plot graph like below screen</p>
  <p><a href="https://i.stack.imgur.com/HMfku.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HMfku.png" alt="enter image description here" /></a></p>
  <p>but i am facing two problem</p>
  <p>-- in single group i want add 6 bar but it display  only 4 bar
  -- at top <strong>morning,afternoon, evevning , midnight</strong>  i want align with <strong>&quot;12Am-6am&quot;, &quot;6am-12pm&quot;, &quot;12pm-6pm&quot;, &quot;6pm-12am&quot;</strong></p>
  <p>i am using MPchart Library.
  please help me to  how to plot with given graph of code to expected graph. i just want bottom graph in given image .</p>