14
  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;
Matt
  • 1,017
  • 2
  • 11
  • 27

4 Answers4

31

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.

TR4Android
  • 3,200
  • 16
  • 21
  • 4
    It's a sad world we live when you have to manually set something that should have been already set by default. – Mateus Viccari Sep 08 '16 at 23:55
  • 4
    How can we achieve this with a CombinedChart? – stefana Nov 23 '16 at 10:54
  • 1
    @Nfear Have you got any solution for Combined charts? – Prabs Feb 06 '17 at 09:29
  • http://stackoverflow.com/questions/42064664/mpandroidchart-combinedchart-first-and-last-bars-arent-visible-completely – Prabs Feb 06 '17 at 09:54
  • 3
    @Nfear @Prabs I've updated the answer with a solution that should work with `CombinedChart`s. – TR4Android Feb 06 '17 at 10:10
  • I'm not going to talk about how much time I wasted trying to hack this using min and max values. – Brad Jun 23 '18 at 16:03
  • @TR4Android could you help me with CustomCombinedChartRenderer? I've created a custom class which rotates the label, it is working fine with bar and line, but in combined, labels aren't rotated – Prabs May 27 '20 at 10:59
3

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.

David Rawson
  • 20,912
  • 7
  • 88
  • 124
garrett
  • 31
  • 1
3

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:

David Rawson
  • 20,912
  • 7
  • 88
  • 124
1

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

  barChart.setFitBars(true);
  barChart.setVisibleXRangeMinimum(barEntries.size());
ingyesid
  • 2,864
  • 2
  • 23
  • 21