Skip to content

Instantly share code, notes, and snippets.

@lecho
Created July 9, 2014 18:29
Show Gist options
  • Save lecho/7f0aa7e8cb4afc37d6a4 to your computer and use it in GitHub Desktop.
Save lecho/7f0aa7e8cb4afc37d6a4 to your computer and use it in GitHub Desktop.
//Automatically calculates Y axis values.
private Axis calculateYAxis(int numberOfSteps) {
if (numberOfSteps < 2) {
throw new
IllegalArgumentException("Number or steps have to be grater or equal 2");
}
List<Float> values = new ArrayList<Float>();
final float range = mData.getMaxYValue() - mData.getMinYValue();
final float tickRange = range / (numberOfSteps - 1);
final float x = (float) Math.ceil(Math.log10(tickRange) - 1);
final float pow10x = (float) Math.pow(10, x);
final float roundedTickRange = (float) Math.ceil(tickRange / pow10x) *
pow10x;
float value = mData.getMinYValue();
while (value <= mData.getMaxYValue()) {
values.add(value);
value += roundedTickRange;
}
Axis yAxis = new Axis();
yAxis.setValues(values);
return yAxis;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment