Empty Brain
Empty Brain

Reputation: 627

Labels and Bars are not aligned in MPAndroidchart Bar chart

I'm using MPAndroidChart to display a Bar chart in my android app. But xAxis labels and bars are not aligned.

I used MPAndroidChart:v3.0.3. In the screenshot, bars are not aligned with labels. I follow the git example. Tried some solution but nothing actually works. Can't identify the problem. The bar width is .5f

chart setup

    barChart.setDrawBarShadow(false);
    barChart.setDrawValueAboveBar(true);
    barChart.getDescription().setEnabled(false);
    barChart.setMaxVisibleValueCount(60);
    barChart.setPinchZoom(false);
    barChart.setScaleEnabled(false);
    barChart.setDrawGridBackground(false);
    barChart.setFitBars(true);
    barChart.animateY(2500);

xAxis setup

    XAxis xAxis = barChart.getXAxis();
    xAxis.setSpaceMax(0.5f);
    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
    xAxis.setDrawAxisLine(true);
    xAxis.setCenterAxisLabels(false);
    xAxis.setGranularity(1f);
    xAxis.setAxisMinimum(0f);
    xAxis.setAxisMaximum(xAxisKeys.size());
    xAxis.setLabelCount(xAxisKeys.size(), true);
    IAxisValueFormatter xAxisFormatter = new CustomFormatter(xAxisKeys);
    xAxis.setValueFormatter(xAxisFormatter);

enter image description here

Upvotes: 3

Views: 3020

Answers (2)

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

Try removing lines below from your code. Comment them and check for change:

xAxis.setAxisMinimum(0f);
xAxis.setAxisMaximum(xAxisKeys.size());
xAxis.setLabelCount(xAxisKeys.size(), true);

And follow example below for basic graph after that you can customize it by changing properties as per your requirement:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BarChart barChart = (BarChart) findViewById(R.id.chart1);
    BarData data = barData();

    barChart.setData(data);
    barChart.setFitBars(true);

    // xAxis customization
    XAxis xAxis = barChart.getXAxis();

    // Following code have no effect but you can change it if required
    xAxis.setGranularity(1f);
    xAxis.setGranularityEnabled(true);
    xAxis.setCenterAxisLabels(false);

    // Setting position of xAxis
    xAxis.setPosition(XAxis.XAxisPosition.BOTH_SIDED);

    // Setting labels to xAxis
    xAxis.setValueFormatter(new IndexAxisValueFormatter(getXAxisValues()));

}

// creating list of x-axis values 
private ArrayList<String> getXAxisValues()
{
    ArrayList<String> labels = new ArrayList<String> ();

    labels.add( "JAN");
    labels.add( "FEB");
    labels.add( "MAR");
    labels.add( "APR");
    labels.add( "MAY");
    labels.add( "JUN");
    return labels;
}

// this method is used to create data for Bar graph 
public BarData barData()
{
    ArrayList<BarEntry> group1 = new ArrayList<BarEntry>();
    group1.add(new BarEntry(0, 3));
    group1.add(new BarEntry(1, 1));
    group1.add(new BarEntry(2, 4));
    group1.add(new BarEntry(3, 7));
    group1.add(new BarEntry(4, 3));
    group1.add(new BarEntry(5, 8));

    BarDataSet barDataSet = new BarDataSet(group1, "Brand 1");
    barDataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
    barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

    BarData barData = new BarData(barDataSet);

    return barData;
}
} 

Upvotes: -1

Paresh Rajput
Paresh Rajput

Reputation: 147

Please try to set this property

xAxis.setAvoidFirstLastClipping(false);

Upvotes: -1

Related Questions