Skip to content
Snippets Groups Projects
Commit 3c61c6f1 authored by Tim Molter's avatar Tim Molter
Browse files

fixed numerical axis tick label decimal formatter

parent 8f6d7040
Branches
No related tags found
No related merge requests found
Showing
with 37 additions and 27 deletions
......@@ -73,7 +73,7 @@ public class BarChart02 implements ExampleChart {
e.printStackTrace();
}
xData.add(date);
yData.add(-1 * ((random.nextInt(i) + 1)));
yData.add(-1 * 0.00000001 * ((random.nextInt(i) + 1)));
}
Series series = chart.addSeries("Model 77", xData, yData);
series.setLineColor(SeriesColor.RED);
......
......@@ -64,7 +64,7 @@ public class DateChart03 implements ExampleChart {
// System.out.println(date.getTime());
// System.out.println(date.toString());
xData.add(date);
yData.add(Math.random() * i);
yData.add(Math.random() * i * 1000000000);
}
chart.addSeries("blah", xData, yData);
......
......@@ -52,7 +52,7 @@ public class ScatterChart01 implements ExampleChart {
Random random = new Random();
int size = 1000;
for (int i = 0; i < size; i++) {
xData.add(random.nextGaussian());
xData.add(random.nextGaussian() / 1000);
yData.add(-1000000 + random.nextGaussian());
}
......
......@@ -51,11 +51,11 @@ public class ScatterChart04 implements ExampleChart {
// generates data
int size = 10;
List<Integer> xData = new ArrayList<Integer>();
List<Double> xData = new ArrayList<Double>();
List<Double> yData = new ArrayList<Double>();
List<Double> errorBars = new ArrayList<Double>();
for (int i = 0; i <= size; i++) {
xData.add(i);
xData.add(((double) i) / 100000000);
yData.add(10 * Math.exp(-i));
errorBars.add(Math.random() + .3);
}
......
......@@ -15,6 +15,7 @@
*/
package com.xeiam.xchart.internal.chartpart;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
......@@ -27,14 +28,14 @@ import com.xeiam.xchart.internal.chartpart.Axis.Direction;
/**
* This class encapsulates the logic to generate the axis tick mark and axis tick label data for rendering the axis ticks for decimal axes
*
*
* @author timmolter
*/
public class AxisTickBarChartCalculator extends AxisTickCalculator {
/**
* Constructor
*
*
* @param axisDirection
* @param workingSpace
* @param minValue
......@@ -118,7 +119,7 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator {
for (Object category : categories) {
if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) {
tickLabels.add(numberFormatter.formatNumber((Double) category, minValue, maxValue));
tickLabels.add(numberFormatter.formatNumber(BigDecimal.valueOf((Double) category), minValue, maxValue));
}
else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Date) {
long span = (long) Math.abs(maxValue - minValue); // in data space
......@@ -149,7 +150,7 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator {
for (double tickPosition = firstPosition; tickPosition <= maxValue; tickPosition = tickPosition + gridStep) {
if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Number) {
tickLabels.add(numberFormatter.formatNumber(tickPosition, minValue, maxValue));
tickLabels.add(numberFormatter.formatNumber(BigDecimal.valueOf(tickPosition), minValue, maxValue));
}
else if (chartPainter.getAxisPair().getXAxis().getAxisType() == AxisType.Date) {
long span = (long) Math.abs(maxValue - minValue); // in data space
......
......@@ -15,13 +15,15 @@
*/
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;
/**
* This class encapsulates the logic to generate the axis tick mark and axis tick label data for rendering the axis ticks for logarithmic axes
*
*
* @author timmolter
*/
public class AxisTickLogarithmicCalculator extends AxisTickCalculator {
......@@ -30,7 +32,7 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator {
/**
* Constructor
*
*
* @param axisDirection
* @param workingSpace
* @param minValue
......@@ -48,7 +50,7 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator {
// a check if all axis data are the exact same values
if (minValue == maxValue) {
tickLabels.add(numberFormatter.formatNumber(maxValue, minValue, maxValue));
tickLabels.add(numberFormatter.formatNumber(BigDecimal.valueOf(maxValue), minValue, maxValue));
tickLocations.add(workingSpace / 2.0);
return;
}
......
......@@ -24,7 +24,7 @@ import com.xeiam.xchart.internal.chartpart.Axis.Direction;
/**
* This class encapsulates the logic to generate the axis tick mark and axis tick label data for rendering the axis ticks for decimal axes
*
*
* @author timmolter
*/
public class AxisTickNumericalCalculator extends AxisTickCalculator {
......@@ -33,7 +33,7 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator {
/**
* Constructor
*
*
* @param axisDirection
* @param workingSpace
* @param minValue
......@@ -51,7 +51,7 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator {
// a check if all axis data are the exact same values
if (minValue == maxValue) {
tickLabels.add(numberFormatter.formatNumber(maxValue, minValue, maxValue));
tickLabels.add(numberFormatter.formatNumber(BigDecimal.valueOf(maxValue), minValue, maxValue));
tickLocations.add(workingSpace / 2.0);
return;
}
......@@ -63,14 +63,19 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator {
double margin = Utils.getTickStartOffset(workingSpace, tickSpace); // in plot space double gridStep = getGridStepForDecimal(tickSpace);
BigDecimal gridStep = BigDecimal.valueOf(getNumericalGridStep(tickSpace));
BigDecimal firstPosition = BigDecimal.valueOf(getFirstPosition(gridStep.doubleValue()));
BigDecimal scaledfirstPosition = firstPosition.setScale(16, RoundingMode.HALF_UP);
// System.out.println("scaledfirstPosition: " + scaledfirstPosition); // chop off any double imprecision
// System.out.println("***gridStep: " + gridStep);
BigDecimal cleanedGridStep = gridStep.setScale(16, RoundingMode.HALF_UP); // chop off any double imprecision
// System.out.println("cleanedGridStep: " + cleanedGridStep);
BigDecimal firstPosition = BigDecimal.valueOf(getFirstPosition(cleanedGridStep.doubleValue()));
// System.out.println("firstPosition: " + firstPosition); // chop off any double imprecision
BigDecimal cleanedFirstPosition = firstPosition.setScale(16, RoundingMode.HALF_UP); // chop off any double imprecision
// System.out.println("scaledfirstPosition: " + cleanedFirstPosition);
// generate all tickLabels and tickLocations from the first to last position
for (BigDecimal tickPosition = scaledfirstPosition; tickPosition.compareTo(BigDecimal.valueOf(maxValue + gridStep.doubleValue() * 0.2)) <= 0; tickPosition = tickPosition.add(gridStep)) {
for (BigDecimal tickPosition = cleanedFirstPosition; tickPosition.compareTo(BigDecimal.valueOf(maxValue + cleanedGridStep.doubleValue() * 0.2)) <= 0; tickPosition =
tickPosition.add(cleanedGridStep)) {
tickLabels.add(numberFormatter.formatNumber(tickPosition.doubleValue(), minValue, maxValue));
tickLabels.add(numberFormatter.formatNumber(tickPosition, minValue, maxValue));
// here we convert tickPosition finally to plot space, i.e. pixels
double tickLabelPosition = margin + ((tickPosition.doubleValue() - minValue) / (maxValue - minValue) * tickSpace);
tickLocations.add(tickLabelPosition);
......
......@@ -15,6 +15,7 @@
*/
package com.xeiam.xchart.internal.chartpart;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
......@@ -35,7 +36,7 @@ public class NumberFormatter {
this.styleManager = styleManager;
}
public String getFormatPattern(double value, double min, double max) {
public String getFormatPattern(BigDecimal value, double min, double max) {
// System.out.println("value: " + value);
// System.out.println("min: " + min);
......@@ -50,11 +51,11 @@ public class NumberFormatter {
placeOfDifference = (int) Math.floor(Math.log(difference) / Math.log(10));
}
int placeOfValue;
if (value == 0.0) {
if (value.doubleValue() == 0.0) {
placeOfValue = 0;
}
else {
placeOfValue = (int) Math.floor(Math.log(value) / Math.log(10));
placeOfValue = (int) Math.floor(Math.log(value.doubleValue()) / Math.log(10));
}
// System.out.println("difference: " + difference);
......@@ -104,11 +105,11 @@ public class NumberFormatter {
/**
* Format a number value, if the override patterns are null, it uses defaults
*
*
* @param value
* @return
*/
public String formatNumber(double value, double min, double max) {
public String formatNumber(BigDecimal value, double min, double max) {
NumberFormat numberFormat = NumberFormat.getNumberInstance(styleManager.getLocale());
......@@ -130,7 +131,7 @@ public class NumberFormatter {
/**
* Format a log number value for log Axes which show only decade tick labels. if the override patterns are null, it uses defaults
*
*
* @param value
* @return
*/
......
......@@ -37,7 +37,7 @@ public class PlotContentLineChart extends PlotContent {
/**
* Constructor
*
*
* @param plot
*/
protected PlotContentLineChart(Plot plot) {
......@@ -66,6 +66,7 @@ public class PlotContentLineChart extends PlotContent {
// data points
Collection<?> xData = series.getXData();
// System.out.println(xData);
double xMin = getChartPainter().getAxisPair().getXAxis().getMin();
double xMax = getChartPainter().getAxisPair().getXAxis().getMax();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment