Abhishek Sabbarwal
Abhishek Sabbarwal

Reputation: 3808

Add padding on first and last label on x-axis in MPAndroidChart

I am using a Scatter Chart (MPAndroidChart library) and it looks like below presently.enter image description here

Everything works well so far except for the values on the first and last label (Su and S) wherein the circles are cut off. Is there a way to add padding so that Su has some padding on the left on the X Axis and renders the circle completely and S has some padding on the right so the circle values for S render completely. I have tried to manually set padding going through numerous examples but nothing has worked correctly so far. Is someone has any ideas and can point in the right direction it will be great.. Thanks!

Upvotes: 8

Views: 4723

Answers (4)

Brolin Michael
Brolin Michael

Reputation: 31

Use: xAxis.setAvoidFirstLastClipping(true)

If set to true, the chart will avoid that the first and last label entry in the chart "clip" off the edge of the chart or the screen.

Upvotes: 3

Andras_v
Andras_v

Reputation: 67

I would try setting offset on chart. This let me draw the full labels and dots on both sides, however the grid lines ends where the last dot is.

    mLineChart.setExtraLeftOffset(36);
    mLineChart.setExtraRightOffset(36);

Upvotes: 3

Ryan
Ryan

Reputation: 287

Edit: This is probably not the best approach in most cases. See my newer answer for a better method.

I handled this by setting the x-axis AxisMinimum and AxisMaximum to be just a little bit outside the bounds of my least and greatest index values.

The key is that even though the x-axis labels are based on integer indices, the values of the axis itself are floats.

Assuming the indices in your example are 0-6, then this should work:

xAxis.setAxisMinimum(-0.1f);
xAxis.setAxisMaximum(6.1f);

You may need to tweak those values a little, and if your indices are based on the size of an array, you'll need to adjust accordingly.

Upvotes: 1

Ryan
Ryan

Reputation: 287

I just realized my previous answer isn't the best solution, but I'm going to leave it intact in case it better addresses certain situations or similar needs.

A better way to handle this is to use the SpaceMax and SpaceMin properties, which do exactly what you need without doing potentially fragile things with indices:

xAxis.setSpaceMax(0.1f);
xAxis.setSpaceMin(0.1f);

That will add the specified spacing on the ends in a much more clear and concise way.

Upvotes: 5

Related Questions