diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/scatter/ScatterChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/scatter/ScatterChart01.java index 7862a72a859653fda9286fe1ab22c488f33b1266..2357bd6a586a27136ab871c8d4b58bb55afefb29 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/scatter/ScatterChart01.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/scatter/ScatterChart01.java @@ -32,6 +32,7 @@ import com.xeiam.xchart.demo.charts.ExampleChart; * <ul> * <li>ChartType.Scatter * <li>Series data as a Set + * <li>Setting marker size */ public class ScatterChart01 implements ExampleChart { @@ -61,6 +62,7 @@ public class ScatterChart01 implements ExampleChart { // Customize Chart chart.getStyleManager().setChartTitleVisible(false); chart.getStyleManager().setLegendPosition(LegendPosition.InsideSW); + chart.getStyleManager().setMarkerSize(16); // Series chart.addSeries("Gaussian Blob", xData, yData); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/theme/ThemeChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/theme/ThemeChart01.java index 9993b409c53c7db8665ab538fd64f5c48b3cdded..ec92f5f7c444b7dceff11741aaacf6c609a999a6 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/theme/ThemeChart01.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/theme/ThemeChart01.java @@ -24,6 +24,10 @@ import com.xeiam.xchart.demo.charts.ExampleChart; /** * Default XChart Theme + * <p> + * Demonstrates the following: + * <ul> + * <li>Setting marker size */ public class ThemeChart01 implements ExampleChart { @@ -58,6 +62,7 @@ public class ThemeChart01 implements ExampleChart { String seriesName = "y=" + 2 * i + "x-" + i * b + "b"; chart.addSeries(seriesName, xData, yData); + chart.getStyleManager().setMarkerSize(11); } return chart; diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java index 70bc61698535c2315192c8170570fb62db24fa3e..5e7192996d6a7b9025271c6be591f216895fffc0 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java @@ -76,13 +76,15 @@ public class Legend implements ChartPart { // determine legend text content max width double legendTextContentMaxWidth = 0; - double legendTextContentMaxHeight = 0; + // double legendTextContentMaxHeight = 0; // determine legend content height double legendContentHeight = 0; for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) { - List<Map.Entry<String, Rectangle2D>> seriesBounds = getSeriesBounds(series, g); + + List<Map.Entry<String, Rectangle2D>> seriesBounds = getSeriesNameBounds(series, g); + double blockHeight = 0; for (Map.Entry<String, Rectangle2D> entry : seriesBounds) { blockHeight += entry.getValue().getHeight(); @@ -91,14 +93,9 @@ public class Legend implements ChartPart { blockHeight = Math.max(blockHeight, isBar ? BOX_SIZE : getChartPainter().getStyleManager().getMarkerSize()); - legendTextContentMaxHeight = Math.max(legendTextContentMaxHeight, blockHeight); - legendContentHeight += blockHeight; + legendContentHeight += blockHeight + styleManager.getLegendPadding(); } - // vertical padding between items - double paddingSize = isBar ? styleManager.getLegendPadding() : fontMetrics.getDescent(); - legendContentHeight += paddingSize * (chartPainter.getAxisPair().getSeriesMap().size() - 1); - // determine legend content width double legendContentWidth = 0; if (!isBar) { @@ -109,11 +106,11 @@ public class Legend implements ChartPart { } // Legend Box double legendBoxWidth = legendContentWidth + 2 * styleManager.getLegendPadding(); - double legendBoxHeight = legendContentHeight + 2 * styleManager.getLegendPadding(); + double legendBoxHeight = legendContentHeight + 1 * styleManager.getLegendPadding(); return new double[] { legendBoxWidth, legendBoxHeight }; } - private List<Map.Entry<String, Rectangle2D>> getSeriesBounds(Series series, Graphics2D g) { + private List<Map.Entry<String, Rectangle2D>> getSeriesNameBounds(Series series, Graphics2D g) { String lines[] = series.getName().split("\\n"); List<Map.Entry<String, Rectangle2D>> stringBounds = new ArrayList<Map.Entry<String, Rectangle2D>>(lines.length); @@ -188,12 +185,16 @@ public class Legend implements ChartPart { double starty = yOffset + styleManager.getLegendPadding(); for (Series series : chartPainter.getAxisPair().getSeriesMap().values()) { - List<Map.Entry<String, Rectangle2D>> seriesBounds = getSeriesBounds(series, g); + + List<Map.Entry<String, Rectangle2D>> seriesNameBounds = getSeriesNameBounds(series, g); + float blockHeight = 0; - for (Map.Entry<String, Rectangle2D> entry : seriesBounds) { + for (Map.Entry<String, Rectangle2D> entry : seriesNameBounds) { blockHeight += entry.getValue().getHeight(); } + blockHeight = Math.max(blockHeight, styleManager.getChartType() == ChartType.Bar ? BOX_SIZE : getChartPainter().getStyleManager().getMarkerSize()); + if (styleManager.getChartType() != ChartType.Bar) { // paint line if (styleManager.getChartType() != ChartType.Scatter && series.getStroke() != null) { @@ -224,22 +225,21 @@ public class Legend implements ChartPart { float itemOffsetY = -fontMetrics.getDescent(); if (styleManager.getChartType() != ChartType.Bar) { final float x = (float) (startx + styleManager.getLegendSeriesLineLength() + styleManager.getLegendPadding()); - for (Map.Entry<String, Rectangle2D> entry : seriesBounds) { + for (Map.Entry<String, Rectangle2D> entry : seriesNameBounds) { g.drawString(entry.getKey(), x, (float) (starty + entry.getValue().getHeight()) + itemOffsetY); itemOffsetY += entry.getValue().getHeight(); } - itemOffsetY = Math.max(itemOffsetY, getChartPainter().getStyleManager().getMarkerSize()); - starty += blockHeight + fontMetrics.getDescent(); + starty += blockHeight + styleManager.getLegendPadding(); } else { final float x = (float) (startx + BOX_SIZE + styleManager.getLegendPadding()); - for (Map.Entry<String, Rectangle2D> entry : seriesBounds) { + for (Map.Entry<String, Rectangle2D> entry : seriesNameBounds) { double height = entry.getValue().getHeight(); double centerOffsetY = (Math.max(BOX_SIZE, height) - height) / 2.0; g.drawString(entry.getKey(), x, (float) (starty + height + itemOffsetY + centerOffsetY)); itemOffsetY += height; } - starty += Math.max(BOX_SIZE, blockHeight) + styleManager.getLegendPadding(); + starty += blockHeight + styleManager.getLegendPadding(); } }