4

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?

Community
  • 1
  • 1
Dietmar
  • 506
  • 3
  • 10

3 Answers3

2

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.

Dietmar
  • 506
  • 3
  • 10
0

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);
Ifor
  • 2,825
  • 1
  • 22
  • 25
  • For me, `getRangeLabelWidth()` always returns 41.0, irrespective of the values on the Y axis and of the font size I set using `getGraphWidget().getRangeLabelPaint().setTextSize()`. – Dietmar Mar 08 '15 at 17:15
0

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);
u2tall
  • 450
  • 1
  • 7
  • 12