From 2145230aa9eb7f298e12098a2d0372225d037b8e Mon Sep 17 00:00:00 2001 From: Martin Pernollet Date: Sun, 5 Oct 2014 13:09:01 +0200 Subject: [PATCH] Elapsed time tick formatter --- .../src/api/org/jzy3d/chart2d/Chart2d.java | 74 ++-- jzy3d-api/src/api/org/jzy3d/maths/Utils.java | 403 ++++++++++-------- .../layout/renderers/DateTickRenderer.java | 3 +- .../renderers/ElapsedTimeTickRenderer.java | 23 + .../src/tests/org/jzy3d/maths/TestUtils.java | 12 + 5 files changed, 296 insertions(+), 219 deletions(-) create mode 100644 jzy3d-api/src/api/org/jzy3d/plot3d/primitives/axes/layout/renderers/ElapsedTimeTickRenderer.java create mode 100644 jzy3d-api/src/tests/org/jzy3d/maths/TestUtils.java diff --git a/jzy3d-api/src/api/org/jzy3d/chart2d/Chart2d.java b/jzy3d-api/src/api/org/jzy3d/chart2d/Chart2d.java index 7715a01d..23121452 100644 --- a/jzy3d-api/src/api/org/jzy3d/chart2d/Chart2d.java +++ b/jzy3d-api/src/api/org/jzy3d/chart2d/Chart2d.java @@ -12,7 +12,7 @@ import org.jzy3d.chart2d.primitives.LineSerie2d; import org.jzy3d.chart2d.primitives.Serie2d; import org.jzy3d.maths.BoundingBox3d; import org.jzy3d.plot3d.primitives.axes.layout.IAxeLayout; -import org.jzy3d.plot3d.primitives.axes.layout.renderers.DateTickRenderer; +import org.jzy3d.plot3d.primitives.axes.layout.renderers.ElapsedTimeTickRenderer; import org.jzy3d.plot3d.rendering.canvas.Quality; import org.jzy3d.plot3d.rendering.view.View; import org.jzy3d.plot3d.rendering.view.ViewportMode; @@ -26,11 +26,43 @@ import org.jzy3d.plot3d.rendering.view.modes.ViewPositionMode; // Interface de LineSerie fournie par Chart2d package, using x, y float args public class Chart2d extends AWTChart{ - - public Chart2d() { - super(); + public Chart2d(){ + this(new Chart2dComponentFactory(), Quality.Intermediate, Toolkit.newt.toString()); + + IAxeLayout axe = getAxeLayout(); + axe.setZAxeLabelDisplayed(false); + axe.setTickLineDisplayed(false); + + View view = getView(); + view.setViewPositionMode(ViewPositionMode.TOP); + view.setSquared(true); + view.getCamera().setViewportMode(ViewportMode.STRETCH_TO_FILL); } + public void asTimeChart(float timeMax, float ymin, float ymax, String xlabel, String ylabel){ + IAxeLayout axe = getAxeLayout(); + axe.setYAxeLabel(ylabel); + axe.setXAxeLabel( xlabel ); + axe.setXTickRenderer( new ElapsedTimeTickRenderer() ); + + View view = getView(); + view.setBoundManual(new BoundingBox3d(0, timeMax, ymin, ymax, -1, 1)); + } + + public Serie2d getSerie(String name){ + Serie2d s = null; + if(!series.keySet().contains(name)){ + s = new LineSerie2d(name); + addDrawable(s.getDrawable()); + } + else{ + s = series.get(name); + } + return s; + } + + /* */ + public Chart2d(IChartComponentFactory factory, Quality quality, String windowingToolkit, GLCapabilities capabilities) { super(factory, quality, windowingToolkit, capabilities); } @@ -54,38 +86,8 @@ public class Chart2d extends AWTChart{ public Chart2d(String windowingToolkit) { super(windowingToolkit); } - - public Chart2d(float timeMax, float ymin, float ymax, String xlabel, String ylabel){ - this(new Chart2dComponentFactory(), Quality.Intermediate, Toolkit.newt.toString()); - - IAxeLayout axe = getAxeLayout(); - axe.setYAxeLabel(ylabel); - axe.setXAxeLabel( xlabel ); - axe.setZAxeLabelDisplayed(false); - - axe.setTickLineDisplayed(false); - - axe.setXTickRenderer( new DateTickRenderer( "mm:ss" ) ); - - - View view = getView(); - view.setViewPositionMode(ViewPositionMode.TOP); - view.setBoundManual(new BoundingBox3d(0, timeMax, ymin, ymax, -1, 1)); - view.setSquared(true); - view.getCamera().setViewportMode(ViewportMode.STRETCH_TO_FILL); - } - public Serie2d getSerie(String name){ - Serie2d s = null; - if(!series.keySet().contains(name)){ - s = new LineSerie2d(name); - addDrawable(s.getDrawable()); - } - else{ - s = series.get(name); - } - return s; - } + /* */ - Map series = new HashMap(); + protected Map series = new HashMap(); } diff --git a/jzy3d-api/src/api/org/jzy3d/maths/Utils.java b/jzy3d-api/src/api/org/jzy3d/maths/Utils.java index 36ad5f03..70582d3e 100644 --- a/jzy3d-api/src/api/org/jzy3d/maths/Utils.java +++ b/jzy3d-api/src/api/org/jzy3d/maths/Utils.java @@ -4,232 +4,271 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; - public class Utils { - /** Convert a number into a string, with precision number of - * meaningfull digits. - * - * 'd' integral The result is formatted as a decimal integer - * 'o' integral The result is formatted as an octal integer - * 'x', 'X' integral The result is formatted as a hexadecimal integer - * 'e', 'E' floating point The result is formatted as a decimal number in computerized scientific notation - * 'f' floating point The result is formatted as a decimal number - * 'g', 'G' floating point The result is formatted using computerized scientific notation or decimal format, depending on the precision and the value after rounding. - * - * @see http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax - * @see String.format - */ - public static String num2str( char parseMode, double num, int precision ){ - return String.format( "%." + precision + parseMode, new Double(num) ); - } - - /** Same as {@link num2str(char parseMode, double num, int precision)} but - * does not query precision.*/ - public static String num2str( char parseMode, double num){ - return String.format( "%" + parseMode, new Double(num) ); + /** + * Convert a number into a string, with precision number of + * meaningfull digits. + * + * 'd' integral The result is formatted as a decimal integer 'o' integral + * The result is formatted as an octal integer 'x', 'X' integral The result + * is formatted as a hexadecimal integer 'e', 'E' floating point The result + * is formatted as a decimal number in computerized scientific notation 'f' + * floating point The result is formatted as a decimal number 'g', 'G' + * floating point The result is formatted using computerized scientific + * notation or decimal format, depending on the precision and the value + * after rounding. + * + * @see http + * ://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax + * @see String.format + */ + public static String num2str(char parseMode, double num, int precision) { + return String.format("%." + precision + parseMode, new Double(num)); + } + + /** + * Same as {@link num2str(char parseMode, double num, int precision)} but + * does not query precision. + */ + public static String num2str(char parseMode, double num) { + return String.format("%" + parseMode, new Double(num)); + } + + public static String num2str(double num, int precision) { + return num2str('g', num, precision); + } + + /** Convert a number into a string. */ + public static String num2str(double num) { + return Double.toString(num); + } + + /*************************************************************/ + + /** Convert a date to the format "dd/MM/yyyy HH:mm:ss". */ + public static String dat2str(Date date) { + return dat2str(date, "dd/MM/yyyy HH:mm:ss"); + } + + /** + * Some example format dd.MM.yy 09.04.98 yyyy.MM.dd G 'at' hh:mm:ss z + * 1998.04.09 AD at 06:15:55 PDT EEE, MMM d, ''yy Thu, Apr 9, '98 h:mm a + * 6:15 PM H:mm 18:15 H:mm:ss:SSS 18:15:55:624 K:mm a,z 6:15 PM,PDT + * yyyy.MMMMM.dd GGG hh:mm aaa 1998.April.09 AD 06:15 PM + * + * @see http + * ://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat. + * html + * @param date + * @param format + * @return + */ + public static String dat2str(Date date, String format) { + if (date == null) + return ""; + SimpleDateFormat formatter; + formatter = new SimpleDateFormat(format, Locale.getDefault()); + return formatter.format(date); + } + + /*************************************************************/ + + public static long dat2num(Date date) { + if (date == null) + return 0; + return date.getTime(); + } + + public static Date num2dat(long value) { + return new Date(value); + } + + public static String time2str(long milli) { + StringBuilder buf = new StringBuilder(20); + String sgn = ""; + + if (milli < 0) { + sgn = "-"; + milli = Math.abs(milli); + } + + append(buf, sgn, 0, (milli / 3600000)); + append(buf, ":", 2, ((milli % 3600000) / 60000)); + append(buf, ":", 2, ((milli % 60000) / 1000)); + append(buf, ".", 3, (milli % 1000)); + return buf.toString(); + } + + /** + * Append a right-aligned and zero-padded numeric value to a + * `StringBuilder`. + */ + private static void append(StringBuilder tgt, String pfx, int dgt, long val) { + tgt.append(pfx); + if (dgt > 1) { + int pad = (dgt - 1); + for (long xa = val; xa > 9 && pad > 0; xa /= 10) { + pad--; + } + for (int xa = 0; xa < pad; xa++) { + tgt.append('0'); + } + } + tgt.append(val); + } + + public static String blanks(int length) { + String b = ""; + for (int i = 0; i < length; i++) + b += " "; + return b; } - public static String num2str( double num, int precision ){ - return num2str( 'g', num, precision ); - } - - /** Convert a number into a string.*/ - public static String num2str( double num ){ - return Double.toString(num); - } - - /*************************************************************/ - - /** Convert a date to the format "dd/MM/yyyy HH:mm:ss".*/ - public static String dat2str(Date date){ - return dat2str(date, "dd/MM/yyyy HH:mm:ss"); - } - - /** - * Some example format - * dd.MM.yy 09.04.98 - * yyyy.MM.dd G 'at' hh:mm:ss z 1998.04.09 AD at 06:15:55 PDT - * EEE, MMM d, ''yy Thu, Apr 9, '98 - * h:mm a 6:15 PM - * H:mm 18:15 - * H:mm:ss:SSS 18:15:55:624 - * K:mm a,z 6:15 PM,PDT - * yyyy.MMMMM.dd GGG hh:mm aaa 1998.April.09 AD 06:15 PM - * - * @see http://java.sun.com/docs/books/tutorial/i18n/format/simpleDateFormat.html - * @param date - * @param format - * @return - */ - public static String dat2str(Date date, String format){ - if(date==null) - return ""; - SimpleDateFormat formatter; - formatter = new SimpleDateFormat(format, Locale.getDefault()); - return formatter.format(date); - } - - /*************************************************************/ - - public static long dat2num(Date date){ - if(date==null) - return 0; - return date.getTime(); - } - - public static Date num2dat(long value){ - return new Date(value); - } - - public static String blanks(int length){ - String b = ""; - for (int i = 0; i < length; i++) - b += " "; - return b; - } - /*****************************************************************************/ - - /** - * Computes the absolute values of an array of doubles. - * - * @param values - * @return the sum of input values - */ - public static double[] abs(double[] values){ - double[] output = new double[values.length]; - - for( int i=0; i