MPAndroidChart - the first and last lines do not display correctly

  • On the histogram, I create the first and last lines that are sequentially cut in half (even if I add additional bars). This also leads to the fact that the values โ€‹โ€‹above the bars are not true. I inflate it inside the fragment.

  • The axis also increases by only 0.9 instead of 1. To fix this, do I need to implement the AxisValueFormatter interface?

Picture

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; 
+7
android mpandroidchart
source share
3 answers

To answer your questions:

  • For your bars to display correctly, you need to fit in bars like this (don't ask me why it's not turned on by default):

     chart.setFitBars(true) 

    More information can be found in BarChart javadocs:

    setFitBars(boolean enabled) : adds half the width of the bar to each side of the range along the x axis to allow barcode bars to display fully.

    If you have CombinedChart , you can use setSpaceMin() and setSpaceMax() to add extra distance at both ends of the axis:

     XAxis xAxis = chart.getXAxis(); xAxis.setSpaceMin(barData.getBarWidth() / 2f); xAxis.setSpaceMax(barData.getBarWidth() / 2f); 
  • Currently, it is not possible to influence the positions of values โ€‹โ€‹displayed on the axis. Creating a ValueFormatter only changes the displayed text, not the actual position of the label.

+17
source share

Use setAxisMinimum and setAxisMaximum , it will work setAxisMaximum fine!

After creating an instance of BarData you just need to

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

There is no need for setFitBars , but the value is count BARENTRY .

+1
source share

If the problem is only in the first bar, you can use negative values โ€‹โ€‹for the minimum axis:

 xAxis.setAxisMinimum(-0.5f); 

As the answer to this question is here :

+1
source share

All Articles