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

logarithmic axis bar charts

parent 15a14784
Branches
No related tags found
No related merge requests found
...@@ -27,6 +27,7 @@ import java.util.List; ...@@ -27,6 +27,7 @@ import java.util.List;
import com.xeiam.xchart.StyleManager; import com.xeiam.xchart.StyleManager;
import com.xeiam.xchart.StyleManager.ChartType; import com.xeiam.xchart.StyleManager.ChartType;
import com.xeiam.xchart.internal.Utils;
import com.xeiam.xchart.internal.chartpart.Axis.Direction; import com.xeiam.xchart.internal.chartpart.Axis.Direction;
/** /**
...@@ -78,6 +79,12 @@ public abstract class AxisTickCalculator { ...@@ -78,6 +79,12 @@ public abstract class AxisTickCalculator {
overrideMaxValue = BigDecimal.ZERO; overrideMaxValue = BigDecimal.ZERO;
} }
} }
if (styleManager.getChartType() == ChartType.Bar && styleManager.isYAxisLogarithmic()) {
int logMin = (int) Math.floor(Math.log10(minValue.doubleValue()));
overrideMinValue = new BigDecimal(Utils.pow(10, logMin).doubleValue());
}
// override min and maxValue if specified // override min and maxValue if specified
if (axisDirection == Direction.X && styleManager.getXAxisMin() != null) { if (axisDirection == Direction.X && styleManager.getXAxisMin() != null) {
overrideMinValue = new BigDecimal(styleManager.getXAxisMin()); overrideMinValue = new BigDecimal(styleManager.getXAxisMin());
...@@ -91,23 +98,14 @@ public abstract class AxisTickCalculator { ...@@ -91,23 +98,14 @@ public abstract class AxisTickCalculator {
if (axisDirection == Direction.Y && styleManager.getYAxisMax() != null) { if (axisDirection == Direction.Y && styleManager.getYAxisMax() != null) {
overrideMaxValue = new BigDecimal(styleManager.getYAxisMax()); overrideMaxValue = new BigDecimal(styleManager.getYAxisMax());
} }
this.axisDirection = axisDirection; this.axisDirection = axisDirection;
this.workingSpace = workingSpace; this.workingSpace = workingSpace;
this.minValue = overrideMinValue; this.minValue = overrideMinValue;
// this.minValue = new BigDecimal(10000);
this.maxValue = overrideMaxValue; this.maxValue = overrideMaxValue;
this.styleManager = styleManager; this.styleManager = styleManager;
} }
BigDecimal pow(double base, int exponent) {
if (exponent > 0) {
return new BigDecimal(base).pow(exponent);
} else {
return BigDecimal.ONE.divide(new BigDecimal(base).pow(-exponent));
}
}
BigDecimal getFirstPosition(BigDecimal gridStep) { BigDecimal getFirstPosition(BigDecimal gridStep) {
BigDecimal firstPosition; BigDecimal firstPosition;
......
...@@ -24,6 +24,7 @@ package com.xeiam.xchart.internal.chartpart; ...@@ -24,6 +24,7 @@ package com.xeiam.xchart.internal.chartpart;
import java.math.BigDecimal; import java.math.BigDecimal;
import com.xeiam.xchart.StyleManager; import com.xeiam.xchart.StyleManager;
import com.xeiam.xchart.internal.Utils;
import com.xeiam.xchart.internal.chartpart.Axis.Direction; import com.xeiam.xchart.internal.chartpart.Axis.Direction;
/** /**
...@@ -90,8 +91,8 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { ...@@ -90,8 +91,8 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator {
// BigDecimal firstPosition = getFirstPosition(tickStep); // BigDecimal firstPosition = getFirstPosition(tickStep);
// System.out.println("firstPosition: " + firstPosition); // System.out.println("firstPosition: " + firstPosition);
BigDecimal firstPosition = pow(10, logMin); BigDecimal firstPosition = Utils.pow(10, logMin);
BigDecimal tickStep = pow(10, logMin - 1); BigDecimal tickStep = Utils.pow(10, logMin - 1);
for (int i = logMin; i <= logMax; i++) { // for each decade for (int i = logMin; i <= logMax; i++) { // for each decade
...@@ -100,7 +101,7 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { ...@@ -100,7 +101,7 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator {
// System.out.println("i: " + i); // System.out.println("i: " + i);
// System.out.println("pow(10, i).doubleValue(): " + pow(10, i).doubleValue()); // System.out.println("pow(10, i).doubleValue(): " + pow(10, i).doubleValue());
for (BigDecimal j = firstPosition; j.doubleValue() <= pow(10, i).doubleValue(); j = j.add(tickStep)) { for (BigDecimal j = firstPosition; j.doubleValue() <= Utils.pow(10, i).doubleValue(); j = j.add(tickStep)) {
// System.out.println("j: " + j); // System.out.println("j: " + j);
// System.out.println(Math.log10(j.doubleValue()) % 1); // System.out.println(Math.log10(j.doubleValue()) % 1);
...@@ -127,8 +128,8 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator { ...@@ -127,8 +128,8 @@ public class AxisTickLogarithmicCalculator extends AxisTickCalculator {
* tickSpace); * tickSpace);
tickLocations.add(tickLabelPosition); tickLocations.add(tickLabelPosition);
} }
tickStep = tickStep.multiply(pow(10, 1)); tickStep = tickStep.multiply(Utils.pow(10, 1));
firstPosition = tickStep.add(pow(10, i)); firstPosition = tickStep.add(Utils.pow(10, i));
} }
} }
} }
...@@ -24,6 +24,7 @@ package com.xeiam.xchart.internal.chartpart; ...@@ -24,6 +24,7 @@ package com.xeiam.xchart.internal.chartpart;
import java.math.BigDecimal; import java.math.BigDecimal;
import com.xeiam.xchart.StyleManager; import com.xeiam.xchart.StyleManager;
import com.xeiam.xchart.internal.Utils;
import com.xeiam.xchart.internal.chartpart.Axis.Direction; import com.xeiam.xchart.internal.chartpart.Axis.Direction;
/** /**
...@@ -121,16 +122,16 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator { ...@@ -121,16 +122,16 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator {
BigDecimal gridStep; BigDecimal gridStep;
if (significand > 7.5) { if (significand > 7.5) {
// gridStep = 10.0 * 10 ** exponent // gridStep = 10.0 * 10 ** exponent
gridStep = BigDecimal.TEN.multiply(pow(10, exponent)); gridStep = BigDecimal.TEN.multiply(Utils.pow(10, exponent));
} else if (significand > 3.5) { } else if (significand > 3.5) {
// gridStep = 5.0 * 10 ** exponent // gridStep = 5.0 * 10 ** exponent
gridStep = new BigDecimal(new Double(5).toString()).multiply(pow(10, exponent)); gridStep = new BigDecimal(new Double(5).toString()).multiply(Utils.pow(10, exponent));
} else if (significand > 1.5) { } else if (significand > 1.5) {
// gridStep = 2.0 * 10 ** exponent // gridStep = 2.0 * 10 ** exponent
gridStep = new BigDecimal(new Double(2).toString()).multiply(pow(10, exponent)); gridStep = new BigDecimal(new Double(2).toString()).multiply(Utils.pow(10, exponent));
} else { } else {
// gridStep = 1.0 * 10 ** exponent // gridStep = 1.0 * 10 ** exponent
gridStep = pow(10, exponent); gridStep = Utils.pow(10, exponent);
} }
return gridStep; return gridStep;
} }
......
...@@ -93,11 +93,20 @@ public class PlotContentBarChart extends PlotContent { ...@@ -93,11 +93,20 @@ public class PlotContentBarChart extends PlotContent {
// override min and maxValue if specified // override min and maxValue if specified
if (getChartPainter().getStyleManager().getYAxisMin() != null) { if (getChartPainter().getStyleManager().getYAxisMin() != null) {
yMin = new BigDecimal(getChartPainter().getStyleManager().getYAxisMin()); yMin = new BigDecimal(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()));
// 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);
} }
if (getChartPainter().getStyleManager().getYAxisMax() != null) { if (getChartPainter().getStyleManager().getYAxisMax() != null) {
yMax = new BigDecimal(getChartPainter().getStyleManager().getYAxisMax()); yMax = new BigDecimal(getChartPainter().getStyleManager().getYAxisMax());
} else if (getChartPainter().getStyleManager().isYAxisLogarithmic()) {
yMax = new BigDecimal(Math.log10(yMax.doubleValue()));
} }
// figure out the general form of the chart // figure out the general form of the chart
int chartForm = 1; // 1=positive, -1=negative, 0=span int chartForm = 1; // 1=positive, -1=negative, 0=span
if (yMin.compareTo(BigDecimal.ZERO) > 0 && yMax.compareTo(BigDecimal.ZERO) > 0) { if (yMin.compareTo(BigDecimal.ZERO) > 0 && yMax.compareTo(BigDecimal.ZERO) > 0) {
...@@ -107,6 +116,8 @@ public class PlotContentBarChart extends PlotContent { ...@@ -107,6 +116,8 @@ public class PlotContentBarChart extends PlotContent {
} else { } else {
chartForm = 0;// span chart chartForm = 0;// span chart
} }
// System.out.println(yMin);
// System.out.println(yMax);
Iterator<?> categoryItr = categories.iterator(); Iterator<?> categoryItr = categories.iterator();
Iterator<Number> yItr = yData.iterator(); Iterator<Number> yItr = yData.iterator();
...@@ -117,6 +128,11 @@ public class PlotContentBarChart extends PlotContent { ...@@ -117,6 +128,11 @@ public class PlotContentBarChart extends PlotContent {
if (xData.contains(categoryItr.next())) { if (xData.contains(categoryItr.next())) {
BigDecimal y = new BigDecimal(yItr.next().doubleValue()); BigDecimal y = new BigDecimal(yItr.next().doubleValue());
if (getChartPainter().getStyleManager().isYAxisLogarithmic()) {
y = new BigDecimal(Math.log10(y.doubleValue()));
} else {
y = new BigDecimal(y.doubleValue());
}
BigDecimal yTop = null; BigDecimal yTop = null;
BigDecimal yBottom = null; BigDecimal yBottom = null;
...@@ -142,15 +158,10 @@ public class PlotContentBarChart extends PlotContent { ...@@ -142,15 +158,10 @@ public class PlotContentBarChart extends PlotContent {
break; break;
} }
// if (yTop.compareTo(yMax) > 0) {
// yTop = yMax;
// }
int yTransform = (int) (bounds.getHeight() - (yTopMargin + yTop.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace)); int yTransform = (int) (bounds.getHeight() - (yTopMargin + yTop.subtract(yMin).doubleValue() / yMax.subtract(yMin).doubleValue() * yTickSpace));
int yOffset = (int) (bounds.getY() + yTransform) + 1; int yOffset = (int) (bounds.getY() + yTransform) + 1;
// if (yBottom.compareTo(yMin) > 0) {
// yBottom = yMin;
// }
int zeroTransform = (int) (bounds.getHeight() - (yTopMargin + (yBottom.subtract(yMin).doubleValue()) / (yMax.subtract(yMin).doubleValue()) * yTickSpace)); int zeroTransform = (int) (bounds.getHeight() - (yTopMargin + (yBottom.subtract(yMin).doubleValue()) / (yMax.subtract(yMin).doubleValue()) * yTickSpace));
int zeroOffset = (int) (bounds.getY() + zeroTransform) + 1; int zeroOffset = (int) (bounds.getY() + zeroTransform) + 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment