From 9238cac351ae87a837161704d8fd0b10e7c7b18b Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Thu, 9 Jan 2014 21:56:22 +0100 Subject: [PATCH] removed all BigDecimal conversions and usage after realizing simple doubles are just fine. --- .../xchart/demo/charts/date/DateChart01.java | 6 +- .../xchart/demo/charts/date/DateChart02.java | 4 +- .../xchart/demo/charts/date/DateChart03.java | 2 + .../main/java/com/xeiam/xchart/Series.java | 65 ++++++++------- .../java/com/xeiam/xchart/internal/Utils.java | 8 +- .../xeiam/xchart/internal/chartpart/Axis.java | 15 ++-- .../xchart/internal/chartpart/AxisPair.java | 3 +- .../chartpart/AxisTickBarChartCalculator.java | 16 ++-- .../chartpart/AxisTickCalculator.java | 47 ++++++----- .../chartpart/AxisTickDateCalculator.java | 18 ++--- .../AxisTickLogarithmicCalculator.java | 31 ++++--- .../AxisTickNumericalCalculator.java | 29 +++---- .../internal/chartpart/ChartPainter.java | 10 ++- .../internal/chartpart/DateFormatter.java | 9 ++- .../internal/chartpart/NumberFormatter.java | 7 +- .../chartpart/PlotContentBarChart.java | 55 ++++++------- .../chartpart/PlotContentLineChart.java | 76 ++++++++--------- .../xchart/DateAxisTickCalculatorTest.java | 3 +- .../com/xeiam/xchart/DateFormatterTest.java | 81 +++++++++---------- .../xchart/DecimalAxisTickCalculatorTest.java | 3 +- .../com/xeiam/xchart/NumberFormatterTest.java | 61 ++++---------- 21 files changed, 259 insertions(+), 290 deletions(-) diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart01.java index 3a604f56..e67281bd 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart01.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart01.java @@ -60,15 +60,17 @@ public class DateChart01 implements ExampleChart { Collection<Date> xData = new ArrayList<Date>(); Collection<Number> yData = new ArrayList<Number>(); - DateFormat sdf = new SimpleDateFormat("ss.S"); + DateFormat sdf = new SimpleDateFormat("HH:mm:ss.S"); Date date = null; for (int i = 1; i <= 14; i++) { try { - date = sdf.parse("31." + (100 * i + random.nextInt(20))); + date = sdf.parse("23:45:31." + (100 * i + random.nextInt(20))); } catch (ParseException e) { e.printStackTrace(); } + // System.out.println(date.getTime()); + // System.out.println(date.toString()); xData.add(date); yData.add(Math.random() * i); } diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart02.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart02.java index 3cd9695d..f1bd1f2a 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart02.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart02.java @@ -53,11 +53,11 @@ public class DateChart02 implements ExampleChart { Random random = new Random(); - DateFormat sdf = new SimpleDateFormat("mm:ss.SSS"); + DateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); Date date = null; for (int i = 1; i <= 14; i++) { try { - date = sdf.parse("23:" + (5 * i + random.nextInt(2)) + "." + random.nextInt(1000)); + date = sdf.parse("23:45:" + (5 * i + random.nextInt(2)) + "." + random.nextInt(1000)); } catch (ParseException e) { e.printStackTrace(); } diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart03.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart03.java index 9df9deab..76293fcc 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart03.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/date/DateChart03.java @@ -61,6 +61,8 @@ public class DateChart03 implements ExampleChart { } catch (ParseException e) { e.printStackTrace(); } + // System.out.println(date.getTime()); + // System.out.println(date.toString()); xData.add(date); yData.add(Math.random() * i); } diff --git a/xchart/src/main/java/com/xeiam/xchart/Series.java b/xchart/src/main/java/com/xeiam/xchart/Series.java index 1b0fba31..49d7e90c 100644 --- a/xchart/src/main/java/com/xeiam/xchart/Series.java +++ b/xchart/src/main/java/com/xeiam/xchart/Series.java @@ -17,7 +17,6 @@ package com.xeiam.xchart; import java.awt.BasicStroke; import java.awt.Color; -import java.math.BigDecimal; import java.util.Collection; import java.util.Date; import java.util.Iterator; @@ -45,16 +44,16 @@ public class Series { private Collection<? extends Number> errorBars; /** the minimum value of axis range */ - private BigDecimal xMin; + private double xMin; /** the maximum value of axis range */ - private BigDecimal xMax; + private double xMax; /** the minimum value of axis range */ - private BigDecimal yMin; + private double yMin; /** the maximum value of axis range */ - private BigDecimal yMax; + private double yMax; /** Line Style */ private BasicStroke stroke; @@ -107,10 +106,10 @@ public class Series { * @param data * @return */ - private BigDecimal[] findMinMax(Collection<?> data, AxisType axisType) { + private double[] findMinMax(Collection<?> data, AxisType axisType) { - BigDecimal min = null; - BigDecimal max = null; + double min = Double.MAX_VALUE; + double max = Double.MIN_VALUE; for (Object dataPoint : data) { @@ -118,28 +117,28 @@ public class Series { continue; } - BigDecimal bigDecimal = null; + double value = 0.0; if (axisType == AxisType.Number) { - bigDecimal = new BigDecimal(((Number) dataPoint).toString()); + value = ((Number) dataPoint).doubleValue(); } else if (axisType == AxisType.Date) { Date date = (Date) dataPoint; - bigDecimal = new BigDecimal(date.getTime()); + value = date.getTime(); } else if (axisType == AxisType.String) { - return new BigDecimal[] { null, null }; + return new double[] { Double.NaN, Double.NaN }; } - if (min == null || bigDecimal.compareTo(min) < 0) { - min = bigDecimal; + if (value < min) { + min = value; } - if (max == null || bigDecimal.compareTo(max) > 0) { - max = bigDecimal; + if (value > max) { + max = value; } } - return new BigDecimal[] { min, max }; + return new double[] { min, max }; } /** @@ -148,24 +147,24 @@ public class Series { * @param data * @return */ - private BigDecimal[] findMinMaxWithErrorBars(Collection<? extends Number> data, Collection<? extends Number> errorBars) { + private double[] findMinMaxWithErrorBars(Collection<? extends Number> data, Collection<? extends Number> errorBars) { - BigDecimal min = null; - BigDecimal max = null; + double min = Double.MAX_VALUE; + double max = Double.MIN_VALUE; Iterator<? extends Number> itr = data.iterator(); Iterator<? extends Number> ebItr = errorBars.iterator(); while (itr.hasNext()) { - BigDecimal bigDecimal = new BigDecimal(itr.next().doubleValue()); - BigDecimal eb = new BigDecimal(ebItr.next().doubleValue()); - if (min == null || (bigDecimal.subtract(eb)).compareTo(min) < 0) { - min = bigDecimal.subtract(eb); + double bigDecimal = itr.next().doubleValue(); + double eb = ebItr.next().doubleValue(); + if (bigDecimal - eb < min) { + min = bigDecimal - eb; } - if (max == null || (bigDecimal.add(eb)).compareTo(max) > 0) { - max = bigDecimal.add(eb); + if (bigDecimal + eb > max) { + max = bigDecimal + eb; } } - return new BigDecimal[] { min, max }; + return new double[] { min, max }; } /** @@ -260,22 +259,22 @@ public class Series { return errorBars; } - public BigDecimal getxMin() { + public double getxMin() { return xMin; } - public BigDecimal getxMax() { + public double getxMax() { return xMax; } - public BigDecimal getyMin() { + public double getyMin() { return yMin; } - public BigDecimal getyMax() { + public double getyMax() { return yMax; } @@ -320,12 +319,12 @@ public class Series { private void calculateMinMax() { // xData - BigDecimal[] xMinMax = findMinMax(xData, xAxisType); + double[] xMinMax = findMinMax(xData, xAxisType); xMin = xMinMax[0]; xMax = xMinMax[1]; // yData - BigDecimal[] yMinMax = null; + double[] yMinMax = null; if (errorBars == null) { yMinMax = findMinMax(yData, yAxisType); } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/Utils.java b/xchart/src/main/java/com/xeiam/xchart/internal/Utils.java index 8b4cf06c..a26f144e 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/Utils.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/Utils.java @@ -15,7 +15,6 @@ */ package com.xeiam.xchart.internal; -import java.math.BigDecimal; /** * @author timmolter @@ -53,13 +52,14 @@ public class Utils { return (int) (marginSpace / 2.0); } - public static BigDecimal pow(double base, int exponent) { + public static double pow(double base, int exponent) { if (exponent > 0) { - return new BigDecimal(base).pow(exponent); + return Math.pow(base, exponent); } else { - return BigDecimal.ONE.divide(new BigDecimal(base).pow(-exponent)); + return 1.0 / Math.pow(base, -1 * exponent); + } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java index 2d4d6e18..ee940ec4 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java @@ -19,7 +19,6 @@ import java.awt.Graphics2D; import java.awt.font.FontRenderContext; import java.awt.font.TextLayout; import java.awt.geom.Rectangle2D; -import java.math.BigDecimal; import com.xeiam.xchart.StyleManager.LegendPosition; @@ -50,9 +49,9 @@ public class Axis implements ChartPart { /** the axis direction */ private Direction direction; - private BigDecimal min = null; + private double min = Double.MAX_VALUE; - private BigDecimal max = null; + private double max = Double.MIN_VALUE; /** the bounds */ private Rectangle2D bounds; @@ -88,14 +87,14 @@ public class Axis implements ChartPart { * @param min * @param max */ - protected void addMinMax(BigDecimal min, BigDecimal max) { + protected void addMinMax(double min, double max) { // System.out.println(min); // System.out.println(max); - if (this.min == null || min.compareTo(this.min) < 0) { + if (this.min == Double.NaN || min < this.min) { this.min = min; } - if (this.max == null || max.compareTo(this.max) > 0) { + if (this.max == Double.NaN || max > this.max) { this.max = max; } @@ -255,12 +254,12 @@ public class Axis implements ChartPart { return axisType; } - public BigDecimal getMin() { + public double getMin() { return min; } - public BigDecimal getMax() { + public double getMax() { return max; } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java index c4391f66..ee95a148 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisPair.java @@ -22,6 +22,7 @@ import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import com.xeiam.xchart.Series; @@ -96,7 +97,7 @@ public class AxisPair implements ChartPart { series = new Series(seriesName, xData, xAxis.getAxisType(), yData, yAxis.getAxisType(), errorBars, seriesColorMarkerLineStyleCycler.getNextSeriesColorMarkerLineStyle()); } else { // generate xData - Collection<Number> generatedXData = new ArrayList<Number>(); + List<Number> generatedXData = new ArrayList<Number>(); for (int i = 1; i < yData.size() + 1; i++) { generatedXData.add(i); } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java index a77aea5d..0927a728 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickBarChartCalculator.java @@ -15,7 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.math.BigDecimal; import java.util.Date; import java.util.Iterator; import java.util.Set; @@ -42,7 +41,7 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { * @param maxValue * @param styleManager */ - public AxisTickBarChartCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, ChartPainter chart) { + public AxisTickBarChartCalculator(Direction axisDirection, int workingSpace, double minValue, double maxValue, ChartPainter chart) { super(axisDirection, workingSpace, minValue, maxValue, chart.getStyleManager()); calculate(chart); @@ -54,7 +53,7 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { int tickSpace = Utils.getTickSpace(workingSpace); // in plot space // where the tick should begin in the working space in pixels - int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space BigDecimal gridStep = getGridStepForDecimal(tickSpace); + int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); // get all categories Set<Object> categories = new TreeSet<Object>(); @@ -64,10 +63,10 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { while (xItr.hasNext()) { Object x = null; if (chartPainter.getAxisPair().getxAxis().getAxisType() == AxisType.Number) { - x = new BigDecimal(((Number) xItr.next()).doubleValue()); + x = xItr.next(); } else if (chartPainter.getAxisPair().getxAxis().getAxisType() == AxisType.Date) { - x = new BigDecimal(((Date) xItr.next()).getTime()); + x = (double) (((Date) xItr.next()).getTime()); } else if (chartPainter.getAxisPair().getxAxis().getAxisType() == AxisType.String) { x = xItr.next(); @@ -94,13 +93,13 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { int counter = 0; for (Object category : categories) { if (chartPainter.getAxisPair().getxAxis().getAxisType() == AxisType.Number) { - tickLabels.add(numberFormatter.formatNumber((BigDecimal) category)); + tickLabels.add(numberFormatter.formatNumber((Double) category)); } else if (chartPainter.getAxisPair().getxAxis().getAxisType() == AxisType.Date) { - long span = Math.abs(maxValue.subtract(minValue).longValue()); // in data space + long span = (long) Math.abs(maxValue - minValue); // in data space long gridStepHint = (long) (span / (double) tickSpace * styleManager.getXAxisTickMarkSpacingHint()); long timeUnit = dateFormatter.getTimeUnit(gridStepHint); - tickLabels.add(dateFormatter.formatDate((BigDecimal) category, timeUnit)); + tickLabels.add(dateFormatter.formatDate((Double) category, timeUnit)); } else if (chartPainter.getAxisPair().getxAxis().getAxisType() == AxisType.String) { tickLabels.add(category.toString()); @@ -109,5 +108,4 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator { tickLocations.add(tickLabelPosition); } } - } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickCalculator.java index 3c92016d..02ea5497 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickCalculator.java @@ -15,7 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.math.BigDecimal; import java.util.LinkedList; import java.util.List; @@ -39,9 +38,9 @@ public abstract class AxisTickCalculator { protected final int workingSpace; - protected final BigDecimal minValue; + protected final double minValue; - protected final BigDecimal maxValue; + protected final double maxValue; protected final StyleManager styleManager; @@ -54,37 +53,37 @@ public abstract class AxisTickCalculator { * @param maxValue * @param styleManager */ - public AxisTickCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, StyleManager styleManager) { + public AxisTickCalculator(Direction axisDirection, int workingSpace, double minValue, double maxValue, StyleManager styleManager) { // override min/max value for bar charts' Y-Axis - BigDecimal overrideMinValue = minValue; - BigDecimal overrideMaxValue = maxValue; + double overrideMinValue = minValue; + double overrideMaxValue = maxValue; if (styleManager.getChartType() == ChartType.Bar && axisDirection == Direction.Y) { // this is the Y-Axis for a bar chart - if (minValue.compareTo(BigDecimal.ZERO) > 0 && maxValue.compareTo(BigDecimal.ZERO) > 0) { - overrideMinValue = BigDecimal.ZERO; + if (minValue > 0.0 && maxValue > 0.0) { + overrideMinValue = 0.0; } - if (minValue.compareTo(BigDecimal.ZERO) < 0 && maxValue.compareTo(BigDecimal.ZERO) < 0) { - overrideMaxValue = BigDecimal.ZERO; + if (minValue < 0.0 && maxValue < 0.0) { + overrideMaxValue = 0.0; } } if (styleManager.getChartType() == ChartType.Bar && styleManager.isYAxisLogarithmic()) { - int logMin = (int) Math.floor(Math.log10(minValue.doubleValue())); - overrideMinValue = new BigDecimal(Utils.pow(10, logMin).doubleValue()); + int logMin = (int) Math.floor(Math.log10(minValue)); + overrideMinValue = Utils.pow(10, logMin); } // override min and maxValue if specified if (axisDirection == Direction.X && styleManager.getXAxisMin() != null) { - overrideMinValue = new BigDecimal(styleManager.getXAxisMin()); + overrideMinValue = styleManager.getXAxisMin(); } if (axisDirection == Direction.Y && styleManager.getYAxisMin() != null) { - overrideMinValue = new BigDecimal(styleManager.getYAxisMin()); + overrideMinValue = styleManager.getYAxisMin(); } if (axisDirection == Direction.X && styleManager.getXAxisMax() != null) { - overrideMaxValue = new BigDecimal(styleManager.getXAxisMax()); + overrideMaxValue = styleManager.getXAxisMax(); } if (axisDirection == Direction.Y && styleManager.getYAxisMax() != null) { - overrideMaxValue = new BigDecimal(styleManager.getYAxisMax()); + overrideMaxValue = styleManager.getYAxisMax(); } this.axisDirection = axisDirection; this.workingSpace = workingSpace; @@ -93,14 +92,20 @@ public abstract class AxisTickCalculator { this.styleManager = styleManager; } - BigDecimal getFirstPosition(BigDecimal gridStep) { + /** + * Gets the first position + * + * @param gridStep + * @return + */ + double getFirstPosition(double gridStep) { - BigDecimal firstPosition; - if (minValue.remainder(gridStep).doubleValue() <= 0.0) { - firstPosition = minValue.subtract(minValue.remainder(gridStep)); + double firstPosition; + if (minValue % gridStep <= 0.0) { + firstPosition = minValue - (minValue % gridStep); } else { - firstPosition = minValue.subtract(minValue.remainder(gridStep)).add(gridStep); + firstPosition = minValue - (minValue % gridStep) + gridStep; } return firstPosition; } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java index 460b2690..1984e444 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickDateCalculator.java @@ -15,8 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.math.BigDecimal; - import com.xeiam.xchart.StyleManager; import com.xeiam.xchart.internal.Utils; import com.xeiam.xchart.internal.chartpart.Axis.Direction; @@ -39,7 +37,7 @@ public class AxisTickDateCalculator extends AxisTickCalculator { * @param maxValue * @param styleManager */ - public AxisTickDateCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, StyleManager styleManager) { + public AxisTickDateCalculator(Direction axisDirection, int workingSpace, double minValue, double maxValue, StyleManager styleManager) { super(axisDirection, workingSpace, minValue, maxValue, styleManager); dateFormatter = new DateFormatter(styleManager); @@ -52,31 +50,31 @@ public class AxisTickDateCalculator extends AxisTickCalculator { int tickSpace = Utils.getTickSpace(workingSpace); // in plot space // where the tick should begin in the working space in pixels - int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space BigDecimal gridStep = getGridStepForDecimal(tickSpace); + int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); // the span of the data - long span = Math.abs(maxValue.subtract(minValue).longValue()); // in data space + long span = (long) Math.abs(maxValue - minValue); // in data space long gridStepHint = (long) (span / (double) tickSpace * styleManager.getXAxisTickMarkSpacingHint()); long timeUnit = dateFormatter.getTimeUnit(gridStepHint); - BigDecimal gridStep = null; + double gridStep = 0.0; int[] steps = dateFormatter.getValidTickStepsMap().get(timeUnit); for (int i = 0; i < steps.length - 1; i++) { if (gridStepHint < (timeUnit * steps[i] + timeUnit * steps[i + 1]) / 2.0) { - gridStep = new BigDecimal(timeUnit * steps[i]); + gridStep = timeUnit * steps[i]; break; } } - BigDecimal firstPosition = getFirstPosition(gridStep); + double firstPosition = getFirstPosition(gridStep); // generate all tickLabels and tickLocations from the first to last position - for (BigDecimal tickPosition = firstPosition; tickPosition.compareTo(maxValue) <= 0; tickPosition = tickPosition.add(gridStep)) { + for (double tickPosition = firstPosition; tickPosition <= maxValue; tickPosition = tickPosition + gridStep) { tickLabels.add(dateFormatter.formatDate(tickPosition, timeUnit)); // here we convert tickPosition finally to plot space, i.e. pixels - int tickLabelPosition = (int) (margin + ((tickPosition.subtract(minValue)).doubleValue() / (maxValue.subtract(minValue)).doubleValue() * tickSpace)); + int tickLabelPosition = (int) (margin + ((tickPosition - minValue) / (maxValue - minValue) * tickSpace)); tickLocations.add(tickLabelPosition); } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLogarithmicCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLogarithmicCalculator.java index 52f0e8b5..88ab25b5 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLogarithmicCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLogarithmicCalculator.java @@ -15,8 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.math.BigDecimal; - import com.xeiam.xchart.StyleManager; import com.xeiam.xchart.internal.Utils; import com.xeiam.xchart.internal.chartpart.Axis.Direction; @@ -39,7 +37,7 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { * @param maxValue * @param styleManager */ - public AxisTickLogarithmicCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, StyleManager styleManager) { + public AxisTickLogarithmicCalculator(Direction axisDirection, int workingSpace, double minValue, double maxValue, StyleManager styleManager) { super(axisDirection, workingSpace, minValue, maxValue, styleManager); numberFormatter = new NumberFormatter(styleManager); @@ -59,10 +57,10 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { int tickSpace = Utils.getTickSpace(workingSpace); // in plot space // where the tick should begin in the working space in pixels - int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space BigDecimal gridStep = getGridStepForDecimal(tickSpace); + int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); - int logMin = (int) Math.floor(Math.log10(minValue.doubleValue())); - int logMax = (int) Math.ceil(Math.log10(maxValue.doubleValue())); + int logMin = (int) Math.floor(Math.log10(minValue)); + int logMax = (int) Math.ceil(Math.log10(maxValue)); // int logMin = (int) Math.log10(minValue.doubleValue()); // int logMax = (int) Math.log10(maxValue.doubleValue()); // System.out.println("minValue: " + minValue); @@ -83,10 +81,10 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { logMax = (int) (Math.log10(styleManager.getXAxisMax())); // no floor } - // BigDecimal firstPosition = getFirstPosition(tickStep); + // double firstPosition = getFirstPosition(tickStep); // System.out.println("firstPosition: " + firstPosition); - BigDecimal firstPosition = Utils.pow(10, logMin); - BigDecimal tickStep = Utils.pow(10, logMin - 1); + double firstPosition = Utils.pow(10, logMin); + double tickStep = Utils.pow(10, logMin - 1); for (int i = logMin; i <= logMax; i++) { // for each decade @@ -95,23 +93,23 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { // System.out.println("i: " + i); // System.out.println("pow(10, i).doubleValue(): " + pow(10, i).doubleValue()); - for (BigDecimal j = firstPosition; j.doubleValue() <= Utils.pow(10, i).doubleValue(); j = j.add(tickStep)) { + for (double j = firstPosition; j <= Utils.pow(10, i); j = j + tickStep) { // System.out.println("j: " + j); // System.out.println(Math.log10(j.doubleValue()) % 1); - if (j.doubleValue() < minValue.doubleValue()) { + if (j < minValue) { // System.out.println("continue"); continue; } - if (j.doubleValue() > maxValue.doubleValue()) { + if (j > maxValue) { // System.out.println("break"); break; } // only add labels for the decades - if (Math.log10(j.doubleValue()) % 1 == 0.0) { + if (Math.log10(j) % 1 == 0.0) { tickLabels.add(numberFormatter.formatNumber(j)); } else { @@ -119,12 +117,11 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { } // add all the tick marks though - int tickLabelPosition = - (int) (margin + (Math.log10(j.doubleValue()) - Math.log10(minValue.doubleValue())) / (Math.log10(maxValue.doubleValue()) - Math.log10(minValue.doubleValue())) * tickSpace); + int tickLabelPosition = (int) (margin + (Math.log10(j) - Math.log10(minValue)) / (Math.log10(maxValue) - Math.log10(minValue)) * tickSpace); tickLocations.add(tickLabelPosition); } - tickStep = tickStep.multiply(Utils.pow(10, 1)); - firstPosition = tickStep.add(Utils.pow(10, i)); + tickStep = tickStep * Utils.pow(10, 1); + firstPosition = tickStep + Utils.pow(10, i); } } } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickNumericalCalculator.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickNumericalCalculator.java index e7ce366e..e6d273f2 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickNumericalCalculator.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickNumericalCalculator.java @@ -15,8 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.math.BigDecimal; - import com.xeiam.xchart.StyleManager; import com.xeiam.xchart.internal.Utils; import com.xeiam.xchart.internal.chartpart.Axis.Direction; @@ -39,7 +37,7 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator { * @param maxValue * @param styleManager */ - public AxisTickNumericalCalculator(Direction axisDirection, int workingSpace, BigDecimal minValue, BigDecimal maxValue, StyleManager styleManager) { + public AxisTickNumericalCalculator(Direction axisDirection, int workingSpace, double minValue, double maxValue, StyleManager styleManager) { super(axisDirection, workingSpace, minValue, maxValue, styleManager); numberFormatter = new NumberFormatter(styleManager); @@ -59,17 +57,17 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator { int tickSpace = Utils.getTickSpace(workingSpace); // in plot space // where the tick should begin in the working space in pixels - int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space BigDecimal gridStep = getGridStepForDecimal(tickSpace); + int margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace); - BigDecimal gridStep = getGridStep(tickSpace); - BigDecimal firstPosition = getFirstPosition(gridStep); + double gridStep = getGridStep(tickSpace); + double firstPosition = getFirstPosition(gridStep); // generate all tickLabels and tickLocations from the first to last position - for (BigDecimal tickPosition = firstPosition; tickPosition.compareTo(maxValue) <= 0; tickPosition = tickPosition.add(gridStep)) { + for (double tickPosition = firstPosition; tickPosition <= maxValue; tickPosition = tickPosition + gridStep) { tickLabels.add(numberFormatter.formatNumber(tickPosition)); // here we convert tickPosition finally to plot space, i.e. pixels - int tickLabelPosition = (int) (margin + ((tickPosition.subtract(minValue)).doubleValue() / (maxValue.subtract(minValue)).doubleValue() * tickSpace)); + int tickLabelPosition = (int) (margin + ((tickPosition - minValue) / (maxValue - minValue) * tickSpace)); tickLocations.add(tickLabelPosition); } } @@ -80,15 +78,15 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator { * @param tickSpace in plot space * @return */ - private BigDecimal getGridStep(int tickSpace) { + private double getGridStep(int tickSpace) { // this prevents an infinite loop when the plot gets sized really small. if (tickSpace < 10) { - return BigDecimal.ONE; + return 1.0; } // the span of the data - double span = Math.abs(maxValue.subtract(minValue).doubleValue()); // in data space + double span = Math.abs(maxValue - minValue); // in data space int tickMarkSpaceHint = (axisDirection == Direction.X ? styleManager.getXAxisTickMarkSpacingHint() : styleManager.getYAxisTickMarkSpacingHint()); @@ -120,18 +118,18 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator { } // calculate the grid step with hint. - BigDecimal gridStep; + double gridStep; if (significand > 7.5) { // gridStep = 10.0 * 10 ** exponent - gridStep = BigDecimal.TEN.multiply(Utils.pow(10, exponent)); + gridStep = 10.0 * Utils.pow(10, exponent); } else if (significand > 3.5) { // gridStep = 5.0 * 10 ** exponent - gridStep = new BigDecimal(new Double(5).toString()).multiply(Utils.pow(10, exponent)); + gridStep = 5.0 * Utils.pow(10, exponent); } else if (significand > 1.5) { // gridStep = 2.0 * 10 ** exponent - gridStep = new BigDecimal(new Double(2).toString()).multiply(Utils.pow(10, exponent)); + gridStep = 2.0 * Utils.pow(10, exponent); } else { // gridStep = 1.0 * 10 ** exponent @@ -139,5 +137,4 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator { } return gridStep; } - } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPainter.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPainter.java index 65888d32..e0951cfa 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPainter.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartPainter.java @@ -19,7 +19,6 @@ import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.geom.Rectangle2D; -import java.math.BigDecimal; import com.xeiam.xchart.Series; import com.xeiam.xchart.StyleManager; @@ -79,6 +78,11 @@ public class ChartPainter { // calc axis min and max for (Series series : getAxisPair().getSeriesMap().values()) { // add min/max to axis + // System.out.println(series.getxMin()); + // System.out.println(series.getxMax()); + // System.out.println(series.getyMin()); + // System.out.println(series.getyMax()); + // System.out.println("****"); axisPair.getxAxis().addMinMax(series.getxMin(), series.getxMax()); axisPair.getyAxis().addMinMax(series.getyMin(), series.getyMax()); } @@ -87,10 +91,10 @@ public class ChartPainter { if (axisPair.getSeriesMap().isEmpty()) { throw new RuntimeException("No series defined for Chart!!!"); } - if (getStyleManager().isXAxisLogarithmic() && axisPair.getxAxis().getMin().compareTo(BigDecimal.ZERO) <= 0) { + if (getStyleManager().isXAxisLogarithmic() && axisPair.getxAxis().getMin() <= 0.0) { throw new IllegalArgumentException("Series data (accounting for error bars too) cannot be less or equal to zero for a logarithmic X-Axis!!!"); } - if (getStyleManager().isYAxisLogarithmic() && axisPair.getyAxis().getMin().compareTo(BigDecimal.ZERO) <= 0) { + if (getStyleManager().isYAxisLogarithmic() && axisPair.getyAxis().getMin() <= 0.0) { // System.out.println(axisPair.getyAxis().getMin()); throw new IllegalArgumentException("Series data (accounting for error bars too) cannot be less or equal to zero for a logarithmic Y-Axis!!!"); } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/DateFormatter.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/DateFormatter.java index eff2fe79..514b7258 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/DateFormatter.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/DateFormatter.java @@ -15,7 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Map; import java.util.Map.Entry; @@ -58,6 +57,10 @@ public class DateFormatter { validTickStepsMap.put(YEAR_SCALE, new int[] { 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 }); } + /** + * @param gridStepHint + * @return + */ public long getTimeUnit(long gridStepHint) { for (Entry<Long, int[]> entry : validTickStepsMap.entrySet()) { @@ -80,7 +83,7 @@ public class DateFormatter { * @param max * @return */ - public String formatDate(BigDecimal value, long timeUnit) { + public String formatDate(double value, long timeUnit) { String datePattern; @@ -116,7 +119,7 @@ public class DateFormatter { simpleDateformat.setTimeZone(styleManager.getTimezone()); simpleDateformat.applyPattern(datePattern); - return simpleDateformat.format(value.longValueExact()); + return simpleDateformat.format(value); } Map<Long, int[]> getValidTickStepsMap() { diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/NumberFormatter.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/NumberFormatter.java index ac60b6c1..7387214e 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/NumberFormatter.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/NumberFormatter.java @@ -15,7 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.math.BigDecimal; import java.text.DecimalFormat; import java.text.NumberFormat; @@ -42,13 +41,13 @@ public class NumberFormatter { * @param value * @return */ - public String formatNumber(BigDecimal value) { + public String formatNumber(double value) { NumberFormat numberFormat = NumberFormat.getNumberInstance(styleManager.getLocale()); - BigDecimal absoluteValue = value.abs(); + double absoluteValue = Math.abs(value); - if (absoluteValue.compareTo(new BigDecimal("10000.000001")) == -1 && absoluteValue.compareTo(new BigDecimal(".0009999999")) == 1 || BigDecimal.ZERO.compareTo(value) == 0) { + if (absoluteValue < 10000.000001 && absoluteValue > .0009999999 || value == 0) { DecimalFormat normalFormat = (DecimalFormat) numberFormat; normalFormat.applyPattern(styleManager.getNormalDecimalPattern()); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java index 175b0e59..958ceee5 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentBarChart.java @@ -18,7 +18,6 @@ package com.xeiam.xchart.internal.chartpart; import java.awt.Graphics2D; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; -import java.math.BigDecimal; import java.util.Collection; import java.util.Iterator; import java.util.Set; @@ -75,43 +74,41 @@ public class PlotContentBarChart extends PlotContent { Collection<?> xData = series.getXData(); Collection<? extends Number> yData = series.getYData(); - BigDecimal yMin = getChartPainter().getAxisPair().getyAxis().getMin(); - BigDecimal yMax = getChartPainter().getAxisPair().getyAxis().getMax(); + double yMin = getChartPainter().getAxisPair().getyAxis().getMin(); + double yMax = getChartPainter().getAxisPair().getyAxis().getMax(); // if min and max positive, set min to zero - if (yMin.compareTo(BigDecimal.ZERO) > 0 && yMax.compareTo(BigDecimal.ZERO) > 0) { - yMin = BigDecimal.ZERO; + if (yMin > 0.0 && yMax > 0.0) { + yMin = 0.0; } // if min and max negative, set max to zero - if (yMin.compareTo(BigDecimal.ZERO) < 0 && yMax.compareTo(BigDecimal.ZERO) < 0) { - yMax = BigDecimal.ZERO; + if (yMin < 0.0 && yMax < 0.0) { + yMax = 0.0; } // override min and maxValue if specified if (getChartPainter().getStyleManager().getYAxisMin() != null) { - yMin = new BigDecimal(getChartPainter().getStyleManager().getYAxisMin()); + yMin = getChartPainter().getStyleManager().getYAxisMin(); } else if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { // int logMin = (int) Math.floor(Math.log10(getChartPainter().getAxisPair().getyAxis().getMin().doubleValue())); - int logMin = (int) Math.floor(Math.log10(getChartPainter().getAxisPair().getyAxis().getMin().doubleValue())); + int logMin = (int) Math.floor(Math.log10(getChartPainter().getAxisPair().getyAxis().getMin())); // System.out.println("logMin: " + logMin); // System.out.println("min : " + getChartPainter().getAxisPair().getyAxis().getMin().doubleValue()); - // yMin = new BigDecimal(Math.log10(Utils.pow(10, logMin).doubleValue())); - // yMin = new BigDecimal(Utils.pow(10, logMin).doubleValue()); - yMin = new BigDecimal(logMin); + yMin = logMin; } if (getChartPainter().getStyleManager().getYAxisMax() != null) { - yMax = new BigDecimal(getChartPainter().getStyleManager().getYAxisMax()); + yMax = getChartPainter().getStyleManager().getYAxisMax(); } else if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { - yMax = new BigDecimal(Math.log10(yMax.doubleValue())); + yMax = Math.log10(yMax); } // figure out the general form of the chart int chartForm = 1; // 1=positive, -1=negative, 0=span - if (yMin.compareTo(BigDecimal.ZERO) > 0 && yMax.compareTo(BigDecimal.ZERO) > 0) { + if (yMin > 0.0 && yMax > 0.0) { chartForm = 1; // positive chart } - else if (yMin.compareTo(BigDecimal.ZERO) < 0 && yMax.compareTo(BigDecimal.ZERO) < 0) { + else if (yMin < 0.0 && yMax < 0.0) { chartForm = -1; // negative chart } else { @@ -128,32 +125,30 @@ public class PlotContentBarChart extends PlotContent { if (xData.contains(categoryItr.next())) { - BigDecimal y = new BigDecimal(yItr.next().doubleValue()); + double y = ((Number) yItr.next()).doubleValue(); if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { - y = new BigDecimal(Math.log10(y.doubleValue())); + y = Math.log10(y); } - else { - y = new BigDecimal(y.doubleValue()); - } - BigDecimal yTop = null; - BigDecimal yBottom = null; + + double yTop = 0.0; + double yBottom = 0.0; switch (chartForm) { case 1: // positive chart - yTop = new BigDecimal(y.doubleValue()); + yTop = y; yBottom = yMin; break; case -1: // negative chart yTop = yMax; - yBottom = new BigDecimal(y.doubleValue()); + yBottom = y; break; case 0: // span chart - if (y.compareTo(BigDecimal.ZERO) >= 0) { // positive + if (y >= 0.0) { // positive yTop = y; - yBottom = BigDecimal.ZERO; + yBottom = 0.0; } else { - yTop = BigDecimal.ZERO; + yTop = 0.0; yBottom = y; } break; @@ -161,11 +156,11 @@ public class PlotContentBarChart extends PlotContent { break; } - double yTransform = bounds.getHeight() - (yTopMargin + yTop.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace); + double yTransform = bounds.getHeight() - (yTopMargin + (yTop - yMin) / (yMax - yMin) * yTickSpace); double yOffset = bounds.getY() + yTransform + 1; - double zeroTransform = bounds.getHeight() - (yTopMargin + (yBottom.subtract(yMin).doubleValue()) / (yMax.subtract(yMin).doubleValue()) * yTickSpace); + double zeroTransform = bounds.getHeight() - (yTopMargin + (yBottom - yMin) / (yMax - yMin) * yTickSpace); double zeroOffset = bounds.getY() + zeroTransform + 1; // paint bar diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java index ed35878c..8dc99fc3 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContentLineChart.java @@ -20,7 +20,6 @@ import java.awt.Shape; import java.awt.geom.Line2D; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; -import java.math.BigDecimal; import java.util.Collection; import java.util.Date; import java.util.Iterator; @@ -62,35 +61,35 @@ public class PlotContentLineChart extends PlotContent { // data points Collection<?> xData = series.getXData(); - BigDecimal xMin = getChartPainter().getAxisPair().getxAxis().getMin(); - BigDecimal xMax = getChartPainter().getAxisPair().getxAxis().getMax(); + double xMin = getChartPainter().getAxisPair().getxAxis().getMin(); + double xMax = getChartPainter().getAxisPair().getxAxis().getMax(); Collection<? extends Number> yData = series.getYData(); - BigDecimal yMin = getChartPainter().getAxisPair().getyAxis().getMin(); - BigDecimal yMax = getChartPainter().getAxisPair().getyAxis().getMax(); + double yMin = getChartPainter().getAxisPair().getyAxis().getMin(); + double yMax = getChartPainter().getAxisPair().getyAxis().getMax(); // override min and maxValue if specified if (getChartPainter().getStyleManager().getXAxisMin() != null) { - xMin = new BigDecimal(getChartPainter().getStyleManager().getXAxisMin()); + xMin = getChartPainter().getStyleManager().getXAxisMin(); } if (getChartPainter().getStyleManager().getYAxisMin() != null) { - yMin = new BigDecimal(getChartPainter().getStyleManager().getYAxisMin()); + yMin = getChartPainter().getStyleManager().getYAxisMin(); } if (getChartPainter().getStyleManager().getXAxisMax() != null) { - xMax = new BigDecimal(getChartPainter().getStyleManager().getXAxisMax()); + xMax = getChartPainter().getStyleManager().getXAxisMax(); } if (getChartPainter().getStyleManager().getYAxisMax() != null) { - yMax = new BigDecimal(getChartPainter().getStyleManager().getYAxisMax()); + yMax = getChartPainter().getStyleManager().getYAxisMax(); } // logarithmic if (getChartPainter().getStyleManager().isXAxisLogarithmic()) { - xMin = new BigDecimal(Math.log10(xMin.doubleValue())); - xMax = new BigDecimal(Math.log10(xMax.doubleValue())); + xMin = Math.log10(xMin); + xMax = Math.log10(xMax); } if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { - yMin = new BigDecimal(Math.log10(yMin.doubleValue())); - yMax = new BigDecimal(Math.log10(yMax.doubleValue())); + yMin = Math.log10(yMin); + yMax = Math.log10(yMax); } Collection<? extends Number> errorBars = series.getErrorBars(); @@ -108,17 +107,18 @@ public class PlotContentLineChart extends PlotContent { while (xItr.hasNext()) { - BigDecimal x = null; + double x = 0.0; if (getChartPainter().getAxisPair().getxAxis().getAxisType() == AxisType.Number) { - x = new BigDecimal(((Number) xItr.next()).doubleValue()); + x = ((Number) xItr.next()).doubleValue(); + // System.out.println(x); } if (getChartPainter().getAxisPair().getxAxis().getAxisType() == AxisType.Date) { - x = new BigDecimal(((Date) xItr.next()).getTime()); + x = ((Date) xItr.next()).getTime(); // System.out.println(x); } if (getChartPainter().getStyleManager().isXAxisLogarithmic()) { - x = new BigDecimal(Math.log10(x.doubleValue())); + x = Math.log10(x); } Number next = yItr.next(); @@ -132,32 +132,34 @@ public class PlotContentLineChart extends PlotContent { previousY = Integer.MIN_VALUE; continue; } - BigDecimal yOrig = new BigDecimal(next.doubleValue()); - BigDecimal y = null; - BigDecimal eb = BigDecimal.ZERO; + + double yOrig = next.doubleValue(); + double y = 0.0; + double eb = 0.0; if (errorBars != null) { - eb = new BigDecimal(ebItr.next().doubleValue()); + eb = (Double) ebItr.next(); } // System.out.println(y); if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { - y = new BigDecimal(Math.log10(yOrig.doubleValue())); + y = Math.log10(yOrig); } else { - y = new BigDecimal(yOrig.doubleValue()); + y = yOrig; } + // System.out.println(y); - double xTransform = xLeftMargin + (x.subtract(xMin).doubleValue() / xMax.subtract(xMin).doubleValue() * xTickSpace); - double yTransform = bounds.getHeight() - (yTopMargin + y.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace); + double xTransform = xLeftMargin + ((x - xMin) / (xMax - xMin) * xTickSpace); + double yTransform = bounds.getHeight() - (yTopMargin + (y - yMin) / (yMax - yMin) * yTickSpace); // a check if all x data are the exact same values - if (Math.abs(xMax.subtract(xMin).doubleValue()) / 5 == 0.0) { + if (Math.abs(xMax - xMin) / 5 == 0.0) { xTransform = bounds.getWidth() / 2.0; } // a check if all y data are the exact same values - if (Math.abs(yMax.subtract(yMin).doubleValue()) / 5 == 0.0) { + if (Math.abs(yMax - yMin) / 5 == 0.0) { yTransform = bounds.getHeight() / 2.0; } @@ -212,27 +214,27 @@ public class PlotContentLineChart extends PlotContent { g.setColor(getChartPainter().getStyleManager().getErrorBarsColor()); g.setStroke(errorBarStroke); - BigDecimal topValue = null; + double topValue = 0.0; if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { - topValue = yOrig.add(eb); - topValue = new BigDecimal(Math.log10(topValue.doubleValue())); + topValue = yOrig + eb; + topValue = Math.log10(topValue); } else { - topValue = y.add(eb); + topValue = y + eb; } - double topEBTransform = bounds.getHeight() - (yTopMargin + topValue.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace); + double topEBTransform = bounds.getHeight() - (yTopMargin + (topValue - yMin) / (yMax - yMin) * yTickSpace); double topEBOffset = bounds.getY() + topEBTransform; - BigDecimal bottomValue = null; + double bottomValue = 0.0; if (getChartPainter().getStyleManager().isYAxisLogarithmic()) { - bottomValue = yOrig.subtract(eb); + bottomValue = yOrig - eb; // System.out.println(bottomValue); - bottomValue = new BigDecimal(Math.log10(bottomValue.doubleValue())); + bottomValue = Math.log10(bottomValue); } else { - bottomValue = y.subtract(eb); + bottomValue = y - eb; } - double bottomEBTransform = bounds.getHeight() - (yTopMargin + bottomValue.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace); + double bottomEBTransform = bounds.getHeight() - (yTopMargin + (bottomValue - yMin) / (yMax - yMin) * yTickSpace); double bottomEBOffset = bounds.getY() + bottomEBTransform; Shape line = new Line2D.Double(xOffset, topEBOffset, xOffset, bottomEBOffset); diff --git a/xchart/src/test/java/com/xeiam/xchart/DateAxisTickCalculatorTest.java b/xchart/src/test/java/com/xeiam/xchart/DateAxisTickCalculatorTest.java index 037dd9d2..2c754bee 100644 --- a/xchart/src/test/java/com/xeiam/xchart/DateAxisTickCalculatorTest.java +++ b/xchart/src/test/java/com/xeiam/xchart/DateAxisTickCalculatorTest.java @@ -18,7 +18,6 @@ package com.xeiam.xchart; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; -import java.math.BigDecimal; import java.util.Arrays; import java.util.List; @@ -35,7 +34,7 @@ public class DateAxisTickCalculatorTest { @Test public void testDateOneMinuteTimespan() { - AxisTickDateCalculator decimalAxisTickCalculator = new AxisTickDateCalculator(Direction.X, 600, new BigDecimal(1361110661000L), new BigDecimal(1361110721000L), new StyleManager()); + AxisTickDateCalculator decimalAxisTickCalculator = new AxisTickDateCalculator(Direction.X, 600, 1361110661000.0, 1361110721000.0, new StyleManager()); List<String> tickLabels = decimalAxisTickCalculator.getTickLabels(); System.out.println(Arrays.toString(tickLabels.toArray())); diff --git a/xchart/src/test/java/com/xeiam/xchart/DateFormatterTest.java b/xchart/src/test/java/com/xeiam/xchart/DateFormatterTest.java index 4c343dc2..6fd730f4 100644 --- a/xchart/src/test/java/com/xeiam/xchart/DateFormatterTest.java +++ b/xchart/src/test/java/com/xeiam/xchart/DateFormatterTest.java @@ -18,7 +18,6 @@ package com.xeiam.xchart; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; -import java.math.BigDecimal; import java.util.Locale; import java.util.TimeZone; @@ -45,81 +44,81 @@ public class DateFormatterTest { styleManager.setTimezone(timeZone); // ms - BigDecimal value = new BigDecimal(1358108105531L); - BigDecimal min = new BigDecimal(1358108105100L); - BigDecimal max = new BigDecimal(1358108105900L); - long span = Math.abs(max.subtract(min).longValue()); // in data space - long gridStepHint = (long) (span / (double) 1000 * 74); + double value = 1358108105531L; + double min = 1358108105100L; + double max = 1358108105900L; + double span = Math.abs(max - min); // in data space + long gridStepHint = (long) (span / 1000 * 74); long timeUnit = dateFormatter.getTimeUnit(gridStepHint); String stringValue = dateFormatter.formatDate(value, timeUnit); assertThat(stringValue, equalTo("05.531")); // sec - value = new BigDecimal(1358108105000L); - min = new BigDecimal(1358108101000L); - max = new BigDecimal(1358108109000L); - span = Math.abs(max.subtract(min).longValue()); // in data space - gridStepHint = (long) (span / (double) 1000 * 74); + value = 1358108105000L; + min = 1358108101000L; + max = 1358108109000L; + span = Math.abs(max - min); // in data space + gridStepHint = (long) (span / 1000 * 74); timeUnit = dateFormatter.getTimeUnit(gridStepHint); stringValue = dateFormatter.formatDate(value, timeUnit); assertThat(stringValue, equalTo("05.000")); // min - value = new BigDecimal(1358111750000L); - min = new BigDecimal(1358111690000L); - max = new BigDecimal(1358111870000L); - span = Math.abs(max.subtract(min).longValue()); // in data space - gridStepHint = (long) (span / (double) 1000 * 74); + value = 1358111750000L; + min = 1358111690000L; + max = 1358111870000L; + span = Math.abs(max - min); // in data space + gridStepHint = (long) (span / 1000 * 74); timeUnit = dateFormatter.getTimeUnit(gridStepHint); stringValue = dateFormatter.formatDate(value, timeUnit); assertThat(stringValue, equalTo("15:50")); // hour - value = new BigDecimal(1358111870000L); - min = new BigDecimal(1358101070000L); - max = new BigDecimal(1358115470000L); - span = Math.abs(max.subtract(min).longValue()); // in data space - gridStepHint = (long) (span / (double) 1000 * 74); + value = 1358111870000L; + min = 1358101070000L; + max = 1358115470000L; + span = Math.abs(max - min); // in data space + gridStepHint = (long) (span / 1000 * 74); timeUnit = dateFormatter.getTimeUnit(gridStepHint); stringValue = dateFormatter.formatDate(value, timeUnit); assertThat(stringValue, equalTo("21:17")); // day - value = new BigDecimal(1358112317000L); - min = new BigDecimal(1357939517000L); - max = new BigDecimal(1358285117000L); - span = Math.abs(max.subtract(min).longValue()); // in data space - gridStepHint = (long) (span / (double) 1000 * 74); + value = 1358112317000L; + min = 1357939517000L; + max = 1358285117000L; + span = Math.abs(max - min); // in data space + gridStepHint = (long) (span / 1000 * 74); timeUnit = dateFormatter.getTimeUnit(gridStepHint); stringValue = dateFormatter.formatDate(value, timeUnit); assertThat(stringValue, equalTo("13-21")); // week - value = new BigDecimal(1358112317000L); - min = new BigDecimal(1357075517000L); - max = new BigDecimal(1359149117000L); - span = Math.abs(max.subtract(min).longValue()); // in data space - gridStepHint = (long) (span / (double) 1000 * 74); + value = 1358112317000L; + min = 1357075517000L; + max = 1359149117000L; + span = Math.abs(max - min); // in data space + gridStepHint = (long) (span / 1000 * 74); timeUnit = dateFormatter.getTimeUnit(gridStepHint); stringValue = dateFormatter.formatDate(value, timeUnit); assertThat(stringValue, equalTo("01-13")); // month - value = new BigDecimal(1358112838000L); - min = new BigDecimal(1354397638000L); - max = new BigDecimal(1361223238000L); - span = Math.abs(max.subtract(min).longValue()); // in data space - gridStepHint = (long) (span / (double) 1000 * 74); + value = 1358112838000L; + min = 1354397638000L; + max = 1361223238000L; + span = Math.abs(max - min); // in data space + gridStepHint = (long) (span / 1000 * 74); timeUnit = dateFormatter.getTimeUnit(gridStepHint); stringValue = dateFormatter.formatDate(value, timeUnit); assertThat(stringValue, equalTo("01-13")); // year - value = new BigDecimal(1358113402000L); - min = new BigDecimal(1263419002000L); - max = new BigDecimal(1421185402000L); - span = Math.abs(max.subtract(min).longValue()); // in data space - gridStepHint = (long) (span / (double) 1000 * 74); + value = 1358113402000L; + min = 1263419002000L; + max = 1421185402000L; + span = Math.abs(max - min); // in data space + gridStepHint = (long) (span / 1000 * 74); timeUnit = dateFormatter.getTimeUnit(gridStepHint); stringValue = dateFormatter.formatDate(value, timeUnit); assertThat(stringValue, equalTo("2013-01")); diff --git a/xchart/src/test/java/com/xeiam/xchart/DecimalAxisTickCalculatorTest.java b/xchart/src/test/java/com/xeiam/xchart/DecimalAxisTickCalculatorTest.java index b5d2e738..ae145783 100644 --- a/xchart/src/test/java/com/xeiam/xchart/DecimalAxisTickCalculatorTest.java +++ b/xchart/src/test/java/com/xeiam/xchart/DecimalAxisTickCalculatorTest.java @@ -18,7 +18,6 @@ package com.xeiam.xchart; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; -import java.math.BigDecimal; import java.util.Arrays; import java.util.List; @@ -35,7 +34,7 @@ public class DecimalAxisTickCalculatorTest { @Test public void testDateOneMinuteTimespan() { - AxisTickNumericalCalculator decimalAxisTickCalculator = new AxisTickNumericalCalculator(Direction.X, 600, new BigDecimal(-15), new BigDecimal(15), new StyleManager()); + AxisTickNumericalCalculator decimalAxisTickCalculator = new AxisTickNumericalCalculator(Direction.X, 600, -15, 15, new StyleManager()); List<String> tickLabels = decimalAxisTickCalculator.getTickLabels(); System.out.println(Arrays.toString(tickLabels.toArray())); diff --git a/xchart/src/test/java/com/xeiam/xchart/NumberFormatterTest.java b/xchart/src/test/java/com/xeiam/xchart/NumberFormatterTest.java index 2b6376f9..91397cfd 100644 --- a/xchart/src/test/java/com/xeiam/xchart/NumberFormatterTest.java +++ b/xchart/src/test/java/com/xeiam/xchart/NumberFormatterTest.java @@ -18,7 +18,6 @@ package com.xeiam.xchart; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; -import java.math.BigDecimal; import java.util.Locale; import org.junit.Test; @@ -41,87 +40,59 @@ public class NumberFormatterTest { // big styleManager.setLocale(locale); - BigDecimal value = new BigDecimal("1"); - String stringValue = numberFormatter.formatNumber(value); + String stringValue = numberFormatter.formatNumber(1); assertThat(stringValue, equalTo("1")); - value = new BigDecimal(1000L); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(1000); assertThat(stringValue, equalTo("1000")); - value = new BigDecimal("9999"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(9999); assertThat(stringValue, equalTo("9999")); - value = new BigDecimal(20000L); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(20000); assertThat(stringValue, equalTo("2E4")); - value = new BigDecimal("200.23"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(200.23); assertThat(stringValue, equalTo("200.23")); // small - value = new BigDecimal("0.01"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(0.01); assertThat(stringValue, equalTo("0.01")); - value = new BigDecimal("0.001"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(0.001); assertThat(stringValue, equalTo("0.001")); - value = new BigDecimal("0.0012"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(0.0012); assertThat(stringValue, equalTo("0.0012")); - value = new BigDecimal("0.0001"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(0.0001); assertThat(stringValue, equalTo("1E-4")); - value = new BigDecimal(".00012"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(.00012); assertThat(stringValue, equalTo("1.2E-4")); - value = new BigDecimal("0.0"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(0.0); assertThat(stringValue, equalTo("0")); - value = new BigDecimal("0"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(0); assertThat(stringValue, equalTo("0")); - // other case - - // TODO handle these cases better - - // value = new BigDecimal("12228120"); - // stringValue = NumberFormatterUtil.formatNumber(value, null, null, locale); - // assertThat(stringValue, equalTo("0.01")); - - // value = new BigDecimal("0.00000000230000056765"); - // stringValue = NumberFormatterUtil.formatNumber(value, null, null, locale); - // assertThat(stringValue, equalTo("0.01")); - // non-default styleManager.setLocale(Locale.GERMANY); - value = new BigDecimal("0.01"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(0.01); assertThat(stringValue, equalTo("0,01")); - value = new BigDecimal("200.23"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(200.23); assertThat(stringValue, equalTo("200,23")); styleManager.setNormalDecimalPattern("#.#"); - value = new BigDecimal("200.23"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(200.23); assertThat(stringValue, equalTo("200,2")); styleManager.setScientificDecimalPattern("0.#E0"); - value = new BigDecimal("2009764.23"); - stringValue = numberFormatter.formatNumber(value); + stringValue = numberFormatter.formatNumber(2009764.23); assertThat(stringValue, equalTo("2E6")); } -- GitLab