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

misc. rearranging, comments, cleanup, renaming stuff

parent 3cbbe093
Branches
No related tags found
No related merge requests found
Showing
with 97 additions and 106 deletions
......@@ -32,6 +32,9 @@ import org.knowm.xchart.internal.style.Theme;
*/
public class Chart {
/**
* Hides ugly details mostly related to painting the chart and managing the added series. Not to be exposed to outside.
*/
private final ChartPainter chartPainter;
/**
......
......@@ -23,7 +23,6 @@ import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import org.knowm.xchart.StyleManager.ChartType;
import org.knowm.xchart.StyleManager.LegendPosition;
/**
......@@ -121,7 +120,7 @@ public class Axis implements ChartPart {
protected void setAxisType(AxisType axisType) {
if (this.axisType != null && this.axisType != axisType) {
throw new IllegalArgumentException("Date and Number Axes cannot be mixed on the same chart!! ");
throw new IllegalArgumentException("Different Axes (Date, Number, String) cannot be mixed on the same chart!!");
}
this.axisType = axisType;
}
......@@ -343,9 +342,9 @@ public class Axis implements ChartPart {
private AxisTickCalculator getAxisTickCalculator(double workingSpace) {
if (getDirection() == Direction.X && getChartPainter().getStyleManager().getChartType() == ChartType.Bar) {
if (getDirection() == Direction.X && getAxisType() == AxisType.String) {
return new AxisTickBarChartCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter());
return new AxisTickCategoryChartCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter());
}
else if (getDirection() == Direction.X && getChartPainter().getStyleManager().isXAxisLogarithmic() && getAxisType() != AxisType.Date) {
......@@ -365,7 +364,7 @@ public class Axis implements ChartPart {
}
else { // number
return new AxisTickNumericalCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter().getStyleManager());
return new AxisTickNumberCalculator(getDirection(), workingSpace, getMin(), getMax(), getChartPainter().getStyleManager());
}
}
......
......@@ -26,7 +26,6 @@ import java.util.List;
import java.util.Map;
import org.knowm.xchart.Series;
import org.knowm.xchart.StyleManager.ChartType;
import org.knowm.xchart.internal.chartpart.Axis.AxisType;
import org.knowm.xchart.internal.style.SeriesColorMarkerLineStyleCycler;
......@@ -103,13 +102,10 @@ public class AxisPair implements ChartPart {
xAxis.setAxisType(AxisType.Date);
}
else if (dataPoint instanceof String) {
if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar) {
throw new RuntimeException("X-Axis data types of String can only be used for Bar Charts!!!");
}
xAxis.setAxisType(AxisType.String);
}
else {
throw new RuntimeException("Series data must be either Number, Date or String type!!!");
throw new IllegalArgumentException("Series data must be either Number, Date or String type!!!");
}
yAxis.setAxisType(AxisType.Number);
series = new Series(seriesName, xData, xAxis.getAxisType(), yData, yAxis.getAxisType(), errorBars, seriesColorMarkerLineStyleCycler.getNextSeriesColorMarkerLineStyle());
......
......@@ -25,8 +25,6 @@ import java.util.LinkedList;
import java.util.List;
import org.knowm.xchart.StyleManager;
import org.knowm.xchart.StyleManager.ChartType;
import org.knowm.xchart.internal.Utils;
import org.knowm.xchart.internal.chartpart.Axis.Direction;
/**
......@@ -44,9 +42,9 @@ public abstract class AxisTickCalculator {
protected final double workingSpace;
protected final double minValue;
protected double minValue;
protected final double maxValue;
protected double maxValue;
protected final StyleManager styleManager;
......@@ -61,31 +59,16 @@ public abstract class AxisTickCalculator {
*/
public AxisTickCalculator(Direction axisDirection, double workingSpace, double minValue, double maxValue, StyleManager styleManager) {
// override min/max value for bar charts' Y-Axis
// override min and maxValue if specified
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 > 0.0 && maxValue > 0.0) {
overrideMinValue = 0.0;
}
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));
overrideMinValue = Utils.pow(10, logMin);
}
// override min and maxValue if specified
if (axisDirection == Direction.X && styleManager.getXAxisMin() != null && styleManager.getChartType() != ChartType.Bar) { // bar chart cannot have a max or min
if (axisDirection == Direction.X && styleManager.getXAxisMin() != null) {
overrideMinValue = styleManager.getXAxisMin();
}
if (axisDirection == Direction.Y && styleManager.getYAxisMin() != null) {
overrideMinValue = styleManager.getYAxisMin();
}
if (axisDirection == Direction.X && styleManager.getXAxisMax() != null && styleManager.getChartType() != ChartType.Bar) { // bar chart cannot have a max or min
if (axisDirection == Direction.X && styleManager.getXAxisMax() != null) {
overrideMaxValue = styleManager.getXAxisMax();
}
if (axisDirection == Direction.Y && styleManager.getYAxisMax() != null) {
......
......@@ -27,11 +27,11 @@ import org.knowm.xchart.internal.chartpart.Axis.AxisType;
import org.knowm.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
* This class encapsulates the logic to generate the axis tick mark and axis tick label data for rendering the axis ticks for String axes
*
* @author timmolter
*/
public class AxisTickBarChartCalculator extends AxisTickCalculator {
public class AxisTickCategoryChartCalculator extends AxisTickCalculator {
/**
* Constructor
......@@ -42,9 +42,30 @@ public class AxisTickBarChartCalculator extends AxisTickCalculator {
* @param maxValue
* @param styleManager
*/
public AxisTickBarChartCalculator(Direction axisDirection, double workingSpace, double minValue, double maxValue, ChartPainter chart) {
public AxisTickCategoryChartCalculator(Direction axisDirection, double workingSpace, double minValue, double maxValue, ChartPainter chart) {
super(axisDirection, workingSpace, minValue, maxValue, chart.getStyleManager());
// override min/max value for bar charts' Y-Axis
double overrideMinValue = minValue;
double overrideMaxValue = maxValue;
if (axisDirection == Direction.Y) { // this is the Y-Axis for a bar chart
if (minValue > 0.0 && maxValue > 0.0) {
overrideMinValue = 0.0;
}
if (minValue < 0.0 && maxValue < 0.0) {
overrideMaxValue = 0.0;
}
}
if (styleManager.isYAxisLogarithmic()) {
int logMin = (int) Math.floor(Math.log10(minValue));
overrideMinValue = Utils.pow(10, logMin);
}
this.minValue = overrideMinValue;
this.maxValue = overrideMaxValue;
calculate(chart);
}
......
......@@ -28,7 +28,7 @@ import org.knowm.xchart.internal.chartpart.Axis.Direction;
*
* @author timmolter
*/
public class AxisTickNumericalCalculator extends AxisTickCalculator {
public class AxisTickNumberCalculator extends AxisTickCalculator {
NumberFormatter numberFormatter = null;
......@@ -41,7 +41,7 @@ public class AxisTickNumericalCalculator extends AxisTickCalculator {
* @param maxValue
* @param styleManager
*/
public AxisTickNumericalCalculator(Direction axisDirection, double workingSpace, double minValue, double maxValue, StyleManager styleManager) {
public AxisTickNumberCalculator(Direction axisDirection, double workingSpace, double minValue, double maxValue, StyleManager styleManager) {
super(axisDirection, workingSpace, minValue, maxValue, styleManager);
numberFormatter = new NumberFormatter(styleManager);
......
......@@ -76,10 +76,10 @@ public class Plot implements ChartPart {
plotSurface.paint(g);
if (getChartPainter().getStyleManager().getChartType() == ChartType.Bar) {
this.plotContent = new PlotContentBarChart(this);
this.plotContent = new PlotContentCategoricalChart(this);
}
else {
this.plotContent = new PlotContentLineChart(this);
this.plotContent = new PlotContentNumericalChart(this);
}
plotContent.paint(g);
......
......@@ -33,14 +33,14 @@ import org.knowm.xchart.internal.Utils;
/**
* @author timmolter
*/
public class PlotContentBarChart extends PlotContent {
public class PlotContentCategoricalChart extends PlotContent {
/**
* Constructor
*
* @param plot
*/
protected PlotContentBarChart(Plot plot) {
protected PlotContentCategoricalChart(Plot plot) {
super(plot);
}
......@@ -78,8 +78,8 @@ public class PlotContentBarChart extends PlotContent {
for (Series series : getChartPainter().getAxisPair().getSeriesMap().values()) {
// for line series
double previousX = Integer.MIN_VALUE;
double previousY = Integer.MIN_VALUE;
double previousX = -Double.MAX_VALUE;
double previousY = -Double.MAX_VALUE;
Collection<? extends Number> yData = series.getYData();
double yMin = getChartPainter().getAxisPair().getYAxis().getMin();
......@@ -225,7 +225,7 @@ public class PlotContentBarChart extends PlotContent {
// paint line
if (series.getStroke() != null) {
if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) {
if (previousX != -Double.MAX_VALUE && previousY != -Double.MAX_VALUE) {
g.setColor(series.getStrokeColor());
g.setStroke(series.getStroke());
Shape line = new Line2D.Double(previousX, previousY, xOffset + barWidth / 2, yOffset);
......
......@@ -34,14 +34,14 @@ import org.knowm.xchart.internal.chartpart.Axis.AxisType;
/**
* @author timmolter
*/
public class PlotContentLineChart extends PlotContent {
public class PlotContentNumericalChart extends PlotContent {
/**
* Constructor
*
* @param plot
*/
protected PlotContentLineChart(Plot plot) {
protected PlotContentNumericalChart(Plot plot) {
super(plot);
}
......@@ -120,8 +120,8 @@ public class PlotContentLineChart extends PlotContent {
yMax = Math.log10(yMax);
}
double previousX = Integer.MIN_VALUE;
double previousY = Integer.MIN_VALUE;
double previousX = -Double.MAX_VALUE;
double previousY = -Double.MAX_VALUE;
Iterator<?> xItr = xData.iterator();
Iterator<? extends Number> yItr = yData.iterator();
......@@ -155,8 +155,8 @@ public class PlotContentLineChart extends PlotContent {
closePath(g, path, previousX, bounds, yTopMargin);
path = null;
previousX = Integer.MIN_VALUE;
previousY = Integer.MIN_VALUE;
previousX = -Double.MAX_VALUE;
previousY = -Double.MAX_VALUE;
continue;
}
......@@ -196,7 +196,7 @@ public class PlotContentLineChart extends PlotContent {
if (series.getStroke() != null) {
if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) {
if (previousX != -Double.MAX_VALUE && previousY != -Double.MAX_VALUE) {
g.setColor(series.getStrokeColor());
g.setStroke(series.getStroke());
Shape line = new Line2D.Double(previousX, previousY, xOffset, yOffset);
......@@ -208,7 +208,7 @@ public class PlotContentLineChart extends PlotContent {
// paint area
if (Series.SeriesType.Area.equals(series.getSeriesType())) {
if (previousX != Integer.MIN_VALUE && previousY != Integer.MIN_VALUE) {
if (previousX != -Double.MAX_VALUE && previousY != -Double.MAX_VALUE) {
g.setColor(series.getFillColor());
double yBottomOfArea = bounds.getY() + bounds.getHeight() - yTopMargin;
......
......@@ -22,8 +22,6 @@ import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import org.knowm.xchart.StyleManager.ChartType;
/**
* Draws the plot background, the plot border and the horizontal and vertical grid lines
*
......@@ -68,12 +66,9 @@ public class PlotSurface implements ChartPart {
}
// paint grid lines and/or inner plot ticks
if (getChartPainter().getStyleManager().isPlotGridHorizontalLinesVisible()
|| getChartPainter().getStyleManager().isPlotGridVerticalLinesVisible()
|| getChartPainter().getStyleManager().isPlotTicksMarksVisible()
) {
// horizontal
if (getChartPainter().getStyleManager().isPlotGridHorizontalLinesVisible() || getChartPainter().getStyleManager().isPlotTicksMarksVisible()) {
List<Double> yAxisTickLocations = getChartPainter().getAxisPair().getYAxis().getAxisTickCalculator().getTickLocations();
for (int i = 0; i < yAxisTickLocations.size(); i++) {
......@@ -102,15 +97,10 @@ public class PlotSurface implements ChartPart {
}
}
}
}
// vertical
if (getChartPainter().getStyleManager().getChartType() != ChartType.Bar
&& (getChartPainter().getStyleManager().isPlotGridVerticalLinesVisible()
|| getChartPainter().getStyleManager().isPlotTicksMarksVisible())
) {
if (getChartPainter().getStyleManager().isPlotGridVerticalLinesVisible() || getChartPainter().getStyleManager().isPlotTicksMarksVisible()) {
List<Double> xAxisTickLocations = getChartPainter().getAxisPair().getXAxis().getAxisTickCalculator().getTickLocations();
for (int i = 0; i < xAxisTickLocations.size(); i++) {
......@@ -143,7 +133,6 @@ public class PlotSurface implements ChartPart {
}
}
}
}
@Override
public ChartPainter getChartPainter() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment