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

clean up AxisTickCategoryChartCalculator

parent de86d5c8
No related branches found
No related tags found
No related merge requests found
......@@ -22,6 +22,7 @@ import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.List;
import org.knowm.xchart.StyleManager.LegendPosition;
import org.knowm.xchart.internal.chartpart.ChartInternal.ChartInternalType;
......@@ -349,13 +350,13 @@ public class Axis implements ChartPart {
if (getChartInternal().getChartInternalType() == ChartInternalType.Category) {
// No need to pass in min and max
// pass in axis type instead of ChartInternal
return new AxisTickCategoryChartCalculator(getDirection(), workingSpace, getChartInternal());
List<?> categories = (List<?>) getChartInternal().getSeriesMap().values().iterator().next().getXData();
AxisType axisType = getChartInternal().getAxisPair().getXAxis().getAxisType();
return new AxisTickCategoryChartCalculator(getDirection(), workingSpace, categories, axisType, getChartInternal().getStyleManager());
}
else if (getChartInternal().getChartInternalType() == ChartInternalType.XY && getAxisType() == AxisType.Date) {
// TODO don't pass in style manager
return new AxisTickDateCalculator(getDirection(), workingSpace, getChartInternal().getxAxisMin(), getChartInternal().getxAxisMax(), getChartInternal().getStyleManager());
}
else if (getChartInternal().getStyleManager().isXAxisLogarithmic()) {
......
......@@ -21,7 +21,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.knowm.xchart.Series;
import org.knowm.xchart.StyleManager;
import org.knowm.xchart.internal.Utils;
import org.knowm.xchart.internal.chartpart.Axis.AxisType;
import org.knowm.xchart.internal.chartpart.Axis.Direction;
......@@ -42,14 +42,14 @@ public class AxisTickCategoryChartCalculator extends AxisTickCalculator {
* @param maxValue
* @param styleManager
*/
public AxisTickCategoryChartCalculator(Direction axisDirection, double workingSpace, ChartInternal chart) {
public AxisTickCategoryChartCalculator(Direction axisDirection, double workingSpace, List<?> categories, AxisType axisType, StyleManager styleManager) {
super(axisDirection, workingSpace, Double.NaN, Double.NaN, chart.getStyleManager());
super(axisDirection, workingSpace, Double.NaN, Double.NaN, styleManager);
calculate(chart);
calculate(categories, axisType);
}
private void calculate(ChartInternal chartInternal) {
private void calculate(List<?> categories, AxisType axisType) {
// tick space - a percentage of the working space available for ticks
int tickSpace = (int) (styleManager.getAxisTickSpacePercentage() * workingSpace); // in plot space
......@@ -57,18 +57,6 @@ public class AxisTickCategoryChartCalculator extends AxisTickCalculator {
// where the tick should begin in the working space in pixels
double margin = Utils.getTickStartOffset(workingSpace, tickSpace);
List<?> categories = (List<?>) chartInternal.getSeriesMap().values().iterator().next().getXData();
// verify all series have exactly the same xAxis
if (chartInternal.getSeriesMap().size() > 1) {
for (Series series : chartInternal.getSeriesMap().values()) {
if (!series.getXData().equals(categories)) {
throw new IllegalArgumentException("X-Axis data must exactly match all other Series X-Axis data for Bar Charts!!");
}
}
}
// generate all tickLabels and tickLocations from the first to last position
double gridStep = (tickSpace / (double) categories.size());
double firstPosition = gridStep / 2.0;
......@@ -76,10 +64,10 @@ public class AxisTickCategoryChartCalculator extends AxisTickCalculator {
// set up String formatters that may be encountered
NumberFormatter numberFormatter = null;
SimpleDateFormat simpleDateformat = null;
if (chartInternal.getAxisPair().getXAxis().getAxisType() == AxisType.Number) {
if (axisType == AxisType.Number) {
numberFormatter = new NumberFormatter(styleManager);
}
else if (chartInternal.getAxisPair().getXAxis().getAxisType() == AxisType.Date) {
else if (axisType == AxisType.Date) {
if (styleManager.getDatePattern() == null) {
throw new RuntimeException("You need to set the Date Formatting Pattern!!!");
}
......@@ -90,15 +78,15 @@ public class AxisTickCategoryChartCalculator extends AxisTickCalculator {
int counter = 0;
for (Object category : categories) {
if (chartInternal.getAxisPair().getXAxis().getAxisType() == AxisType.String) {
if (axisType == AxisType.String) {
tickLabels.add(category.toString());
double tickLabelPosition = margin + firstPosition + gridStep * counter++;
tickLocations.add(tickLabelPosition);
}
else if (chartInternal.getAxisPair().getXAxis().getAxisType() == AxisType.Number) {
else if (axisType == AxisType.Number) {
tickLabels.add(numberFormatter.formatNumber(new BigDecimal(category.toString()), minValue, maxValue, axisDirection));
}
else if (chartInternal.getAxisPair().getXAxis().getAxisType() == AxisType.Date) {
else if (axisType == AxisType.Date) {
tickLabels.add(simpleDateformat.format((((Date) category).getTime())));
}
......
......@@ -163,6 +163,13 @@ public class ChartInternal {
if (xData.size() != yData.size()) {
throw new IllegalArgumentException("X and Y-Axis sizes are not the same!!!");
}
// verify all series have exactly the same xAxis
if (seriesMap.size() > 0) { // there was already a series added
if (!seriesMap.entrySet().iterator().next().getValue().getXData().equals(xData)) {
throw new IllegalArgumentException("X-Axis data must exactly match all other Series X-Axis data for Category Charts!!");
}
}
// TODO make sure pie charts only have one series!
// inspect the series to see what kind of data it contains (Number, Date, String)
setXAxisType(xData);
......@@ -304,6 +311,7 @@ public class ChartInternal {
if (getSeriesMap().isEmpty()) {
throw new RuntimeException("No series defined for Chart!!!");
}
xAxisMin = axisPair.getXAxis().getMin();
xAxisMax = axisPair.getXAxis().getMax();
yAxisMin = axisPair.getYAxis().getMin();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment