13

I am trying to make a horizontal barchart that covers the entire parent layout but its not. Below is my code -

HorizontalBarChart barchart = new HorizontalBarChart(activity);
barchart.setLayoutParams(new LinearLayout.LayoutParams(0, 110, weight));

ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
entries.add(new BarEntry(86.0f, 0));

BarDataSet dataset = new BarDataSet(entries, "");
dataset.setColor(Color.parseColor("#E0E0E0"));

ArrayList<String> labels = new ArrayList<String>();
labels.add("86.0"); 

BarData bardata = new BarData(labels, dataset);
barchart.setData(bardata);
barchart.setDescription("");

Legend legend = barchart.getLegend();
legend.setEnabled(false);

YAxis topAxis = barchart.getAxisLeft();
topAxis.setDrawLabels(false);

YAxis bottomAxis = barchart.getAxisRight();
bottomAxis.setDrawLabels(false);

XAxis rightAxis = barchart.getXAxis();
rightAxis.setDrawLabels(false);
bottomAxis.setDrawLabels(false);

barchart.setPadding(-1, -1, -1, -1);
barchart.setBackgroundColor(Color.CYAN);

return barchart;

I want my horizontal barchart (barchart) to fill the entire blue area. Can someone please help.

EDIT : @PhilippJahoda i tried your solution, but at first launch it shows up the same way it was, when i click/touch the chart then only it covers the entire area. Can you please tell me why i have to touch the chart to make it fill the entire space.

At first launch it looks like -

Screen shot: enter image description here

After clicking it looks like -

Screen shot2: enter image description here

Agr1909
  • 385
  • 1
  • 5
  • 17

5 Answers5

26

Update to the latest version of the library if you have not already. Then, just remove all offsets from the chart. It's in the documentation.

Call:

chart.setViewPortOffsets(0f, 0f, 0f, 0f);
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
  • 2
    It's the same problem I'm having. I'm calling chart.setViewPortOffset(100f, 0f, 100f, 0f) after setting chart Data.. but my edit takes effect only after I tap on the chart. Any solution @PhilippJahoda? I've got the latest mpchart lib version. – andrea.rinaldi Sep 21 '15 at 15:10
  • 1
    Don't have method setViewPortOffsets for piechart – SANAT Sep 14 '16 at 05:45
  • @SANAT try using setExtraOffsets with negative values – Daivid Oct 07 '16 at 23:59
  • For some reason, setViewPortOffsets(0f, 0f, 0f, 0f) not working in API<24 – nnyerges Dec 14 '19 at 14:33
  • setViewPortOffsets(0f, 0f, 0f, 0f) only works well when it's all zeroes. If you try something like setViewPortOffsets(0f, 0f, 0f, 10f) it won't work until interaciton with the chart. Even if you call invalidate(). – Slobodan Antonijević Jul 30 '20 at 09:19
12

The padding removal works with

chart.setViewPortOffsets(0f, 0f, 0f, 0f);

but if the effect appears only after touch, try to invalidate chart view (after you set data) this way:

post(new Runnable() {
    @Override
    public void run() {
        chartView.invalidate();
    }
});
Nik Kober
  • 868
  • 11
  • 16
5
    YAxis yl = chart.getAxisLeft();
    yl.setSpaceTop(20f);
Hagakurje
  • 1,002
  • 2
  • 11
  • 17
3

Best way to control the padding what I feel is as:

  1. Set all the offsets to 0

    chart.setViewPortOffsets(0f, 0f, 0f, 0f);

  2. Control the padding from the XML

    com.github.mikephil.charting.charts.LineChart android:id="@+id/chart" android:layout_width="match_parent" android:layout_height="500dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_marginBottom="10dp"

Amit
  • 3,422
  • 3
  • 28
  • 39
0

When you first set your ViePortOffsets and after that moveViewTo then it works.

Graph.setViewPortOffsets(0F,0F,0F,0F)
Graph.moveViewTo(0f,0f,YAxis.AxisDependency.RIGHT)
Graph.invalidate()