/*
 * Copyright (c) 2009 Philipp Meinen <philipp@bind.ch>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the Software
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
 * THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package ch.bind.philib;

public final class Intervals {

	private Intervals() {
	}

	public static int chooseInterval(int maxValue, int maxSegments) {
		// try to bring maxSegments or less lines on to the chart
		int interval = 1;
		int segments = maxValue;

		// use these intervals:
		// 1, 2, 5,
		// 10, 25, 50
		// 100, 250, 500
		// ... and so on
		int intervalNum = 0;
		while (segments > maxSegments) {
			intervalNum++;

			// 0 for 1, 2, 5
			// 1 for 10, 25, 50
			// 2 for 100, 250, 500
			int power = (intervalNum / 3);

			double multiply = Math.pow(10, power);
			int num = intervalNum % 3;
			if (num == 0) { // 1, 10, 100, 100, ...
				interval = (int) (1.0 * multiply);
			} else if (num == 1) { // 2, 25, 250, 2500, ...
				interval = (int) (2.5 * multiply);
			} else { // 5, 50, 500, 5000, ...
				interval = (int) (5.0 * multiply);
			}

			segments = maxValue / interval;
			if (segments * interval < maxValue)
				segments++;
		}

		return interval;
	}
}

@@ -17,16 +17,21 @@
 (c) 2009
+-2010
  Philipp
@@ -1220,16 +1220,76 @@
 ) %7B%0A%09%7D%0A%0A
+%09private static final double%5B%5D COEFFS = %7B 1.0, 2.5, 5.0 %7D;%0A%0A
 %09public 
@@ -1729,17 +1729,16 @@
 m / 3);%0A
-%0A
 %09%09%09doubl
@@ -1808,191 +1808,130 @@
 %0A%09%09%09
-if (num == 0) %7B // 1, 10, 100, 100, ...%0A%09%09%09%09interval = (int) (1.0 * multiply);%0A%09%09%09%7D else if (num == 1) %7B // 2, 25, 250, 2500, ...%0A%09%09%09%09interval = (int) (2.5 * multiply);%0A%09%09%09%7D else %7B //
+double coeff = COEFFS%5Bnum%5D;%0A%0A%09%09%09// for num=0: 1, 10, 100, 100, ...%0A%09%09%09// for num=1: 2, 25, 250, 2500, ...%0A%09%09%09// for num=2:
  5, 
@@ -1952,17 +1952,16 @@
  ...%0A%09%09%09
-%09
 interval
@@ -1970,19 +1970,21 @@
  (int) (
-5.0
+coeff
  * multi
@@ -1992,13 +1992,8 @@
 ly);
-%0A%09%09%09%7D
 %0A%0A%09%09

replace branching by a table-lookup