diff --git a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart10.java b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart10.java index a6955a2b6eda870f887279d24c52f755444e7695..331739b161723bc155ea32f37eb94d8c69e4f4dd 100644 --- a/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart10.java +++ b/xchart-demo/src/main/java/org/knowm/xchart/demo/charts/bar/BarChart10.java @@ -34,7 +34,7 @@ import org.knowm.xchart.demo.charts.ExampleChart; * <p> * Demonstrates the following: * <ul> - * <li>Mixed series types - Bar and Line + * <li>Mixed series types - Bar, Line and Scatter * <li>Bar Chart styles - overlapped, bar width */ public class BarChart10 implements ExampleChart { @@ -52,9 +52,12 @@ public class BarChart10 implements ExampleChart { // Create Chart Chart chart = new ChartBuilder().chartType(ChartType.Bar).width(800).height(600).title("Value vs. Letter").xAxisTitle("Letter").yAxisTitle("Value").theme(ChartTheme.GGPlot2).build(); chart.addCategorySeries("China", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 11, 23, 20, 36, 5 }))); - Series series2 = chart.addCategorySeries("World Ave.", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 13, 25, - 22, 38, 7 }))); + Series series2 = chart.addCategorySeries("Korea", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 13, 25, 22, 38, + 7 })), new ArrayList<Number>(Arrays.asList(new Number[] { 1, 3, 2, 1, 2 }))); series2.setSeriesType(SeriesType.Line); + Series series3 = chart.addCategorySeries("World Ave.", new ArrayList<String>(Arrays.asList(new String[] { "A", "B", "C", "D", "E" })), new ArrayList<Number>(Arrays.asList(new Number[] { 20, 22, + 18, 36, 32 }))); + series3.setSeriesType(SeriesType.Scatter); // Customize Chart chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW); diff --git a/xchart/src/main/java/org/knowm/xchart/StyleManager.java b/xchart/src/main/java/org/knowm/xchart/StyleManager.java index deae55c4f36a0336a8bbe2191d74f272c4c1b736..e2ff4585d61bf6d131a26c66565b3045e76f45ad 100644 --- a/xchart/src/main/java/org/knowm/xchart/StyleManager.java +++ b/xchart/src/main/java/org/knowm/xchart/StyleManager.java @@ -930,7 +930,6 @@ public class StyleManager { this.isPlotGridVerticalLinesVisible = isPlotGridLinesVisible; } - @Deprecated public boolean isPlotGridLinesVisible() { return isPlotGridHorizontalLinesVisible && isPlotGridVerticalLinesVisible; diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/Plot.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/Plot.java index 2775a6a20b5a482b5828003c43f6653b02cc644e..fd7abb3c613de709571be64d076cf9cc521c90a8 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/Plot.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/Plot.java @@ -20,6 +20,7 @@ import java.awt.Graphics2D; import java.awt.geom.Rectangle2D; import org.knowm.xchart.StyleManager.ChartType; +import org.knowm.xchart.internal.chartpart.ChartInternal.ChartInternalType; /** * @author timmolter @@ -75,8 +76,14 @@ public class Plot implements ChartPart { // g.draw(bounds); plotSurface.paint(g); - if (getChartInternal().getStyleManager().getChartType() == ChartType.Bar) { - this.plotContent = new PlotContentCategoricalChart(this); + + if (getChartInternal().getChartInternalType() == ChartInternalType.Category) { + if (getChartInternal().getStyleManager().getChartType() == ChartType.Bar) { + this.plotContent = new PlotContentCategoricalChart_Bar(this); + } + else { + this.plotContent = new PlotContentCategoricalChart_Line_Area_Scatter(this); + } } else { this.plotContent = new PlotContentNumericalChart(this); diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart_Bar.java similarity index 91% rename from xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java rename to xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart_Bar.java index 33bc4a10ad31658e6ce06b59f9dd7d733d38f576..50ffed7a22c063ccb9764af43927d2fd194b91c6 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentCategoricalChart_Bar.java @@ -32,14 +32,14 @@ import org.knowm.xchart.internal.Utils; /** * @author timmolter */ -public class PlotContentCategoricalChart extends PlotContent { +public class PlotContentCategoricalChart_Bar extends PlotContent { /** * Constructor * * @param plot */ - protected PlotContentCategoricalChart(Plot plot) { + protected PlotContentCategoricalChart_Bar(Plot plot) { super(plot); } @@ -98,6 +98,11 @@ public class PlotContentCategoricalChart extends PlotContent { int seriesCounter = 0; for (Series series : getChartInternal().getSeriesMap().values()) { + // sanity check + if (Series.SeriesType.Area.equals(series.getSeriesType())) { + throw new RuntimeException("Category-Bar charts only accept Bar, Line and Scatter series types!!!"); + } + // for line series double previousX = -Double.MAX_VALUE; double previousY = -Double.MAX_VALUE; @@ -114,7 +119,6 @@ public class PlotContentCategoricalChart extends PlotContent { double y = yItr.next().doubleValue(); - // TODO only for bar charts necessary double yTop = 0.0; double yBottom = 0.0; switch (chartForm) { @@ -172,6 +176,8 @@ public class PlotContentCategoricalChart extends PlotContent { double barMargin = gridStep * (1 - barWidthPercentage) / 2; xOffset = bounds.getX() + xLeftMargin + gridStep * categoryCounter++ + seriesCounter * barWidth + barMargin; } + + // paint series if (series.getSeriesType() == SeriesType.Bar) { // paint bar g.setColor(series.getStrokeColor()); @@ -189,16 +195,19 @@ public class PlotContentCategoricalChart extends PlotContent { g.draw(path); } } - else if (series.getSeriesType() == SeriesType.Line) { // line series + else { // paint line - if (series.getStroke() != null) { + if (Series.SeriesType.Line.equals(series.getSeriesType())) { - 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); - g.draw(line); + if (series.getStroke() != null) { + + 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); + g.draw(line); + } } } previousX = xOffset + barWidth / 2; @@ -211,10 +220,6 @@ public class PlotContentCategoricalChart extends PlotContent { } } - else { - // TODO probably add this earlier as a sanity check when series are added to charts - throw new RuntimeException("Category charts only accept Bar and Line series types!!!"); - } // paint error bars diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentNumericalChart.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentNumericalChart.java index 472b3181174f0a3e383163ab7144d31acf9e426b..4c092520b1dc92c3c86b083c9eb03212d00625a4 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentNumericalChart.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotContentNumericalChart.java @@ -98,6 +98,11 @@ public class PlotContentNumericalChart extends PlotContent { for (Series series : getChartInternal().getSeriesMap().values()) { + // sanity check + if (Series.SeriesType.Bar.equals(series.getSeriesType())) { + throw new RuntimeException("X-Y charts only accept Line, Scatter, and Area series types!!!"); + } + // data points Collection<?> xData = series.getXData(); Collection<? extends Number> yData = series.getYData(); diff --git a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotSurface.java b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotSurface.java index bc6854a5bf59d1497d05d966d5fe37934d9f0c03..f969f59f466b8b1d94101690179222113f34e9e1 100644 --- a/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotSurface.java +++ b/xchart/src/main/java/org/knowm/xchart/internal/chartpart/PlotSurface.java @@ -22,6 +22,9 @@ import java.awt.geom.Line2D; import java.awt.geom.Rectangle2D; import java.util.List; +import org.knowm.xchart.StyleManager.ChartType; +import org.knowm.xchart.internal.chartpart.ChartInternal.ChartInternalType; + /** * Draws the plot background, the plot border and the horizontal and vertical grid lines * @@ -100,34 +103,38 @@ public class PlotSurface implements ChartPart { } // vertical - if (getChartInternal().getStyleManager().isPlotGridVerticalLinesVisible() || getChartInternal().getStyleManager().isPlotTicksMarksVisible()) { + if ((getChartInternal().getChartInternalType() == ChartInternalType.XY || (getChartInternal().getChartInternalType() == ChartInternalType.Category && getChartInternal().getStyleManager() + .getChartType() != ChartType.Bar))) { - List<Double> xAxisTickLocations = getChartInternal().getAxisPair().getXAxis().getAxisTickCalculator().getTickLocations(); - for (int i = 0; i < xAxisTickLocations.size(); i++) { + if ((getChartInternal().getStyleManager().isPlotGridVerticalLinesVisible() || getChartInternal().getStyleManager().isPlotTicksMarksVisible())) { - double tickLocation = xAxisTickLocations.get(i); - double xOffset = bounds.getX() + tickLocation; + List<Double> xAxisTickLocations = getChartInternal().getAxisPair().getXAxis().getAxisTickCalculator().getTickLocations(); + for (int i = 0; i < xAxisTickLocations.size(); i++) { - if (xOffset > bounds.getX() && xOffset < bounds.getX() + bounds.getWidth()) { + double tickLocation = xAxisTickLocations.get(i); + double xOffset = bounds.getX() + tickLocation; - // draw lines - if (getChartInternal().getStyleManager().isPlotGridVerticalLinesVisible()) { - g.setColor(getChartInternal().getStyleManager().getPlotGridLinesColor()); - g.setStroke(getChartInternal().getStyleManager().getPlotGridLinesStroke()); + if (xOffset > bounds.getX() && xOffset < bounds.getX() + bounds.getWidth()) { - Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + bounds.getHeight()); - g.draw(line); - } - // tick marks - if (getChartInternal().getStyleManager().isPlotTicksMarksVisible()) { + // draw lines + if (getChartInternal().getStyleManager().isPlotGridVerticalLinesVisible()) { + g.setColor(getChartInternal().getStyleManager().getPlotGridLinesColor()); + g.setStroke(getChartInternal().getStyleManager().getPlotGridLinesStroke()); - g.setColor(getChartInternal().getStyleManager().getAxisTickMarksColor()); - g.setStroke(getChartInternal().getStyleManager().getAxisTickMarksStroke()); + Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + bounds.getHeight()); + g.draw(line); + } + // tick marks + if (getChartInternal().getStyleManager().isPlotTicksMarksVisible()) { - Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + getChartInternal().getStyleManager().getAxisTickMarkLength()); - g.draw(line); - line = new Line2D.Double(xOffset, bounds.getY() + bounds.getHeight(), xOffset, bounds.getY() + bounds.getHeight() - getChartInternal().getStyleManager().getAxisTickMarkLength()); - g.draw(line); + g.setColor(getChartInternal().getStyleManager().getAxisTickMarksColor()); + g.setStroke(getChartInternal().getStyleManager().getAxisTickMarksStroke()); + + Shape line = new Line2D.Double(xOffset, bounds.getY(), xOffset, bounds.getY() + getChartInternal().getStyleManager().getAxisTickMarkLength()); + g.draw(line); + line = new Line2D.Double(xOffset, bounds.getY() + bounds.getHeight(), xOffset, bounds.getY() + bounds.getHeight() - getChartInternal().getStyleManager().getAxisTickMarkLength()); + g.draw(line); + } } } }