Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 746
Matt Avatar asked Aug 08 '16 20:08

Matt


4 Answers

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.

like image 143
TR4Android Avatar answered Oct 16 '22 19:10

TR4Android


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.

like image 3
garrett Avatar answered Oct 16 '22 19:10

garrett


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:

like image 3
2 revs Avatar answered Oct 16 '22 18:10

2 revs


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

  barChart.setFitBars(true);
  barChart.setVisibleXRangeMinimum(barEntries.size());
like image 1
ingyesid Avatar answered Oct 16 '22 17:10

ingyesid