From 515c9bd0f2a8ebec06082fbe66fe5e3f0e42e47c Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Sat, 26 Apr 2014 14:26:03 +0200 Subject: [PATCH] number formatter - fixed for decimal numbers less than 1 --- .../xchart/demo/charts/line/LineChart04.java | 2 +- .../internal/chartpart/NumberFormatter.java | 17 ++- .../com/xeiam/xchart/NumberFormatterTest.java | 103 ------------------ 3 files changed, 14 insertions(+), 108 deletions(-) delete mode 100644 xchart/src/test/java/com/xeiam/xchart/NumberFormatterTest.java diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java index c36284fa..5ca26d73 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java @@ -48,7 +48,7 @@ public class LineChart04 implements ExampleChart { chart.getStyleManager().setLegendVisible(false); for (int i = 0; i < 200; i++) { - Series series = chart.addSeries("A" + i, new double[] { Math.random(), Math.random() }, new double[] { Math.random(), Math.random() }); + Series series = chart.addSeries("A" + i, new double[] { Math.random(), Math.random() }, new double[] { Math.random() / 1000, Math.random() / 1000 }); series.setLineColor(SeriesColor.BLUE); series.setLineStyle(SeriesLineStyle.SOLID); series.setMarker(SeriesMarker.CIRCLE); 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 dc526b56..a8c97832 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 @@ -49,7 +49,13 @@ public class NumberFormatter { else { placeOfDifference = (int) Math.floor(Math.log(difference) / Math.log(10)); } - int placeOfValue = (int) Math.floor(Math.log(value) / Math.log(10)); + int placeOfValue; + if (value == 0.0) { + placeOfValue = 0; + } + else { + placeOfValue = (int) Math.floor(Math.log(value) / Math.log(10)); + } System.out.println("difference: " + difference); System.out.println("placeOfDifference: " + placeOfDifference); @@ -57,7 +63,7 @@ public class NumberFormatter { if (placeOfDifference <= 4 && placeOfDifference >= -4) { System.out.println("getNormalDecimalPattern"); - return getNormalDecimalPattern(placeOfValue, placeOfDifference); + return getNormalDecimalPatternPositive(placeOfValue, placeOfDifference); } else { System.out.println("getScientificDecimalPattern"); @@ -65,13 +71,16 @@ public class NumberFormatter { } } - private String getNormalDecimalPattern(int placeOfValue, int placeOfDifference) { + private String getNormalDecimalPatternPositive(int placeOfValue, int placeOfDifference) { int maxNumPlaces = 15; StringBuilder sb = new StringBuilder(); for (int i = maxNumPlaces - 1; i >= -1 * maxNumPlaces; i--) { - if (i < placeOfValue && i >= placeOfDifference) { + if (i >= 0 && (i < placeOfValue && i >= placeOfDifference)) { + sb.append("0"); + } + else if (i < 0 && (i > placeOfValue && i >= placeOfDifference)) { sb.append("0"); } else { diff --git a/xchart/src/test/java/com/xeiam/xchart/NumberFormatterTest.java b/xchart/src/test/java/com/xeiam/xchart/NumberFormatterTest.java deleted file mode 100644 index bec78474..00000000 --- a/xchart/src/test/java/com/xeiam/xchart/NumberFormatterTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/** - * Copyright 2011 - 2014 Xeiam LLC. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.xeiam.xchart; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; - -import java.util.Locale; - -import org.junit.Test; - -import com.xeiam.xchart.internal.chartpart.NumberFormatter; - -/** - * @author timmolter - */ -public class NumberFormatterTest { - - private final Locale locale = Locale.US; - - @Test - public void testNumberFormatting() { - - StyleManager styleManager = new StyleManager(); - NumberFormatter numberFormatter = new NumberFormatter(styleManager); - - // big - styleManager.setLocale(locale); - - String stringValue = numberFormatter.formatNumber(1); - assertThat(stringValue, equalTo("1")); - - stringValue = numberFormatter.formatNumber(1000); - assertThat(stringValue, equalTo("1,000")); - - stringValue = numberFormatter.formatNumber(9999); - assertThat(stringValue, equalTo("9,999")); - - stringValue = numberFormatter.formatNumber(20000); - assertThat(stringValue, equalTo("2E4")); - - stringValue = numberFormatter.formatNumber(200.23); - assertThat(stringValue, equalTo("200.23")); - - // small - - stringValue = numberFormatter.formatNumber(0.01); - assertThat(stringValue, equalTo("0.01")); - - stringValue = numberFormatter.formatNumber(0.001); - assertThat(stringValue, equalTo("0.001")); - - stringValue = numberFormatter.formatNumber(0.0012); - assertThat(stringValue, equalTo("0.0012")); - - stringValue = numberFormatter.formatNumber(0.0001); - assertThat(stringValue, equalTo("1E-4")); - - stringValue = numberFormatter.formatNumber(.00012); - assertThat(stringValue, equalTo("1.2E-4")); - - stringValue = numberFormatter.formatNumber(0.0); - assertThat(stringValue, equalTo("0")); - - stringValue = numberFormatter.formatNumber(0); - assertThat(stringValue, equalTo("0")); - - // non-default - styleManager.setLocale(Locale.GERMANY); - - stringValue = numberFormatter.formatNumber(0.01); - assertThat(stringValue, equalTo("0,01")); - - stringValue = numberFormatter.formatNumber(10000); - assertThat(stringValue, equalTo("10.000")); - - stringValue = numberFormatter.formatNumber(200.23); - assertThat(stringValue, equalTo("200,23")); - - styleManager.setDecimalPattern("#.#"); - stringValue = numberFormatter.formatNumber(200.23); - assertThat(stringValue, equalTo("200,2")); - - styleManager.setScientificDecimalPattern("0.#E0"); - stringValue = numberFormatter.formatNumber(2009764.23); - assertThat(stringValue, equalTo("2E6")); - - } - -} -- GitLab