Dietmar
Dietmar

Reputation: 568

Androidplot: Set padding/margin to (perfectly) accommodate axis labels

In an androidplot XYPlot, the tick labels on the Y axis get clipped if you have large values (many digits) and/or a large font size. This (and similar issues on the X axis) has been discussed before in these questions:

The answer is always to add more padding and/or margin in order to have enough space for the labels.

My question is: How can I set the padding/margin to have just the right amount of space?

Simply hardcoding the left margin to some value is a suboptimal solution. Let's assume I want to plot some data and I don't know in advance if the Y values will be in the range [0,9] or in the range [10000,20000]. How can I set the padding/margin to avoid both clipping and unnecessary empty space?

Upvotes: 4

Views: 1232

Answers (3)

u2tall
u2tall

Reputation: 490

In case anyone needs it, here is how I set dynamic padding for the range labels with the current version of AndroidPlot (1.5.7)

 String largestString = getLargestLabelOnRange();
 float rangeLabelPadding = largestString.length() * 9;
 plot.getGraph().setPaddingLeft(rangeLabelPadding);

Upvotes: 0

Dietmar
Dietmar

Reputation: 568

The best workaround I could find was this:

int digits = (int) Math.floor(Math.log10(absmax)) + 1;
float leftpad = digits * fontsize/2;
myplot.getGraphWidget().setPaddingLeft(leftpad);

Where absmax is the greatest absolute value in the data.

Upvotes: 2

Ifor
Ifor

Reputation: 2835

I have something like this which I think will do the trick. My setup is more complex as I overlay multiple plots on top of each other to get a multi range affect.

left_pad = mTripPlot.getGraphWidget().getRangeLabelWidth() - 1;
mTripPlot.getGraphWidget().setPadding(left_pad, top_pad, right_pad, bottom_pad);

Upvotes: 0

Related Questions