Matt
Matt

Reputation: 1047

MPAndroidChart - First and last bars not rendering correctly

  1. In the bar chart I am creating the first and last rows are consistently being cut in half (even if I add additional bars). This also causes the values above the bars to be out of place. I am inflating this within a fragment.

  2. The axis are also only increasing by 0.9 instead of 1. To fix this do I need to implement the AxisValueFormatter interface?

Image:

enter image description here

Code: .java

chart = (BarChart) view.findViewById(R.id.chart1);

// Chart settings
chart.setDrawGridBackground(false);
chart.setHighlightFullBarEnabled(true);
chart.setDrawBarShadow(false);
chart.setDrawValueAboveBar(true);
chart.setDescription("");

// Settings for X-Axis
XAxis xAxis = chart.getXAxis();
xAxis.setDrawGridLines(false);
xAxis.setEnabled(true);
xAxis.setDrawLabels(true);
xAxis.setPosition(XAxisPosition.BOTTOM);

// Settings for Y-Axis
YAxis leftAxis = chart.getAxisLeft();
YAxis rightAxis = chart.getAxisRight();

leftAxis.setAxisMinValue(0f);
rightAxis.setAxisMinValue(0f);

BARENTRY = new ArrayList<>();

BarEntryLabels = new ArrayList<String>();

BARENTRY.add(new BarEntry(2f, 1));
BARENTRY.add(new BarEntry(3f, 2));
BARENTRY.add(new BarEntry(4f, 3));
BARENTRY.add(new BarEntry(5f, 4));
BARENTRY.add(new BarEntry(6f, 5));

Bardataset = new BarDataSet(BARENTRY, "Projects");

Bardataset.setColors(ColorTemplate.COLORFUL_COLORS);

BARDATA = new BarData(Bardataset);

chart.setData(BARDATA);
chart.invalidate(); // refresh

chart.animateY(1500);

return view;

Upvotes: 16

Views: 8095

Answers (4)

ingyesid
ingyesid

Reputation: 2884

i just fixed it,settings the x range min, using

  barChart.setFitBars(true);
  barChart.setVisibleXRangeMinimum(barEntries.size());

Upvotes: 1

David Rawson
David Rawson

Reputation: 21497

If the problem is with the first bar only, you can use negative values for the axis minimum:

xAxis.setAxisMinimum(-0.5f);

Like in the answer to this question here:

Upvotes: 3

garrett
garrett

Reputation: 31

Please use setAxisMinimum and setAxisMaximum, it will work perfectly!

After your BarData is instantiated, you just need

chart.getXAxis().setAxisMinimum(-data.getBarWidth() / 2);
chart.getXAxis().setAxisMaximum(count-data.getBarWidth() / 2);`

There is no need for setFitBars, and the count is BARENTRY size.

Upvotes: 3

TR4Android
TR4Android

Reputation: 3246

To answer your questions:

  1. In order to properly show your bars you need to fit the bars, like this (don't ask me why it's not enabled by default):

    chart.setFitBars(true)
    

    You can find more information in the BarChart javadocs:

    setFitBars(boolean enabled): Adds half of the bar width to each side of the x-axis range in order to allow the bars of the barchart to be fully displayed.

    If you have a CombinedChart you could use setSpaceMin() and setSpaceMax() to add additional spacing on both ends of the axis:

    XAxis xAxis = chart.getXAxis();
    xAxis.setSpaceMin(barData.getBarWidth() / 2f);
    xAxis.setSpaceMax(barData.getBarWidth() / 2f);
    
  2. It is currently impossible to infuence the value positions rendered on the axis. Creating a ValueFormatter only changes the displayed text, not the actual position of the label.

Upvotes: 32

Related Questions