diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java index 905e7bf55f084a27360cb66413ae2f05689877f4..af87da336bee71b6c35dd3540d9c923903534346 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart01.java @@ -83,7 +83,7 @@ public class LineChart01 implements ExampleChart { Chart chart = new LineChart(800, 600); // Customize Chart - chart.setChartTitle("Example1"); + chart.setChartTitle("LineChart01"); chart.getStyleManager().setChartTitleVisible(false); chart.getStyleManager().setLegendVisible(false); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart03.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart03.java index 70b276295577efc79db46bcfc8e2397c022d479e..f7d1669b19ac1fa9ec64fa5665bd1bc9d50b233b 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart03.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart03.java @@ -52,7 +52,7 @@ public class LineChart03 implements ExampleChart { } // Customize Chart - chart.setChartTitle("Example3"); + chart.setChartTitle("LineChart03"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java index 6b76afb1b8765e587da6c6a7a03518abd88c57f7..ae07ba4a2079eabca51b045dba85a0771f4e3184 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart04.java @@ -27,6 +27,7 @@ import com.xeiam.xchart.Chart; import com.xeiam.xchart.LineChart; import com.xeiam.xchart.SwingWrapper; import com.xeiam.xchart.style.Series; +import com.xeiam.xchart.style.StyleManager.LegendPosition; /** * Date Axis @@ -64,10 +65,11 @@ public class LineChart04 implements ExampleChart { } // Customize Chart - chart.setChartTitle("Example4"); + chart.setChartTitle("LineChart04"); chart.setXAxisTitle("time of day"); chart.setYAxisTitle("gigawatts"); chart.getValueFormatter().setTimezone(TimeZone.getTimeZone("UTC")); + chart.getStyleManager().setLegendPosition(LegendPosition.InsideNW); Series series = chart.addDateSeries("value", xData, yData); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java index 433eb26a74c1d1d456efa2a659ceda746fbf8d9c..ca77de2eee0816203fd1b8e22b9385099777fd19 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart05.java @@ -47,7 +47,7 @@ public class LineChart05 implements ExampleChart { // new LineChart(800, 600); // Customize Chart - chart.setChartTitle("Example5"); + chart.setChartTitle("LineChart05"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart06.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart06.java index 8e741ab718fd0cf11287087c30124678f9920335..fb586405706078a9a352dcdaddf3e5dfa193ae48 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart06.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart06.java @@ -38,7 +38,7 @@ public class LineChart06 implements ExampleChart { Chart chart = new LineChart(800, 600); // Customize Chart - chart.setChartTitle("Example6"); + chart.setChartTitle("LineChart06"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart07.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart07.java index 4516337b8e1cd999c169aa0745cc27b820d05b25..4efa76fe5db4d0c58c5b72b3c11eb572152a2b53 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart07.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart07.java @@ -42,7 +42,7 @@ public class LineChart07 implements ExampleChart { // Create Chart Chart chart = new LineChart(800, 600); - chart.setChartTitle("Example7"); + chart.setChartTitle("LineChart07"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); chart.addSeries("y(x)", xData, yData); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart09.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart09.java index 46a619c359646410695967487984a6434d85fa6c..fb44c79335da5b5425094a1015a4b13e77e97fb8 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart09.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart09.java @@ -70,7 +70,7 @@ public class LineChart09 implements ExampleChart { } // Customize Chart - chart.setChartTitle("Sample Chart With Customization"); + chart.setChartTitle("LineChart09"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); chart.getStyleManager().setPlotBackgroundColor(ChartColor.getAWTColor(ChartColor.GREY)); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart10.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart10.java index 929eee301bd9602b6ce1e2084d6d8acb4c4e0806..6b628ee856507d0204ed4ea8918c0f445e714497 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart10.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart10.java @@ -42,7 +42,7 @@ public class LineChart10 implements ExampleChart { Chart chart = new LineChart(800, 600); // Customize Chart - chart.setChartTitle("Example10"); + chart.setChartTitle("LineChart10"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); chart.getStyleManager().setLegendVisible(false); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart11.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart11.java index 6c809dc5f72ee296f10b28032e0d545ae54ae525..e42a0195f479e848d07a05f6d2b37f8c4f00fb47 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart11.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/line/LineChart11.java @@ -35,7 +35,7 @@ public class LineChart11 implements ExampleChart { public Chart getChart() { // Create Chart - Chart chart = new ChartBuilder().width(800).height(600).title("My Title").xAxisTitle("X").yAxisTitle("Y").build(); + Chart chart = new ChartBuilder().width(800).height(600).title("LineChart11").xAxisTitle("X").yAxisTitle("Y").build(); chart.addSeries("x", new double[] { 1, 2, 5, 7 }, new double[] { -3, 6, 9, 0 }); return chart; diff --git a/xchart/src/main/java/com/xeiam/xchart/Chart.java b/xchart/src/main/java/com/xeiam/xchart/Chart.java index 4d8c2a6db755ea96f7779e4020d7bd60012b9076..223ee965914d7b31c6f274cfa1ca6cf92f132f5d 100644 --- a/xchart/src/main/java/com/xeiam/xchart/Chart.java +++ b/xchart/src/main/java/com/xeiam/xchart/Chart.java @@ -103,10 +103,10 @@ public abstract class Chart { g.setColor(styleManager.getChartBackgroundColor()); g.fillRect(0, 0, width, height); - chartLegend.paint(g); axisPair.paint(g); plot.paint(g); chartTitle.paint(g); + chartLegend.paint(g); g.dispose(); } diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java index 68c60199b026cba11bc8a9841dd57dcd36e476fd..dde87027eefe0490fb5fd670d1d60d67f923b024 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Axis.java @@ -22,6 +22,7 @@ import java.awt.font.TextLayout; import java.math.BigDecimal; import com.xeiam.xchart.Chart; +import com.xeiam.xchart.style.StyleManager.LegendPosition; /** * Axis @@ -195,8 +196,13 @@ public class Axis implements ChartPart { int xOffset = (int) (axisPair.getyAxis().getBounds().getWidth() + (getChart().getStyleManager().isyAxisTicksVisible() ? getChart().getStyleManager().getPlotPadding() : 0) + getChart() .getStyleManager().getChartPadding()); int yOffset = (int) (axisPair.getyAxis().getBounds().getY() + axisPair.getyAxis().getBounds().getHeight()); - int width = (int) (getChart().getWidth() - axisPair.getyAxis().getBounds().getWidth() - getChart().getChartLegend().getBounds().getWidth() - (getChart().getStyleManager().isLegendVisible() ? 3 - : 2) + + int chartLegendWidth = 0; + if (getChart().getStyleManager().getLegendPosition() == LegendPosition.OutsideW) { + chartLegendWidth = getChart().getChartLegend().getSizeHint()[0]; + } + + int width = (int) (getChart().getWidth() - axisPair.getyAxis().getBounds().getWidth() - chartLegendWidth - (getChart().getStyleManager().isLegendVisible() ? 3 : 2) * getChart().getStyleManager().getChartPadding()); int height = this.getSizeHint(); Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height); 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 c423c03491f5dc760216abf9f6355b880d85a984..70c99f172a5b646e80f6b6012418ebadcf878e97 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 @@ -30,6 +30,8 @@ import com.xeiam.xchart.style.Series; */ public class Legend implements ChartPart { + private static final int LEGEND_MARGIN = 6; + /** parent */ private final Chart chart; @@ -46,11 +48,12 @@ public class Legend implements ChartPart { this.chart = chart; } - @Override - public void paint(Graphics2D g) { - - bounds = new Rectangle(); - g.setFont(chart.getStyleManager().getLegendFont()); + /** + * get the width of the chart legend + * + * @return + */ + protected int[] getSizeHint() { if (chart.getStyleManager().isLegendVisible()) { @@ -84,8 +87,54 @@ public class Legend implements ChartPart { // Draw Legend Box int legendBoxWidth = legendContentWidth + 2 * chart.getStyleManager().getLegendPadding(); int legendBoxHeight = legendContentHeight + 2 * chart.getStyleManager().getLegendPadding(); - int xOffset = chart.getWidth() - legendBoxWidth - chart.getStyleManager().getChartPadding(); - int yOffset = (int) ((chart.getHeight() - legendBoxHeight) / 2.0 + chart.getChartTitle().getSizeHint()); + return new int[] { legendBoxWidth, legendBoxHeight, legendTextContentMaxHeight }; + } else { + return new int[] { 0, 0, 0 }; + } + } + + @Override + public void paint(Graphics2D g) { + + bounds = new Rectangle(); + g.setFont(chart.getStyleManager().getLegendFont()); + + if (chart.getStyleManager().isLegendVisible()) { + + Map<Integer, Series> seriesMap = chart.getAxisPair().getSeriesMap(); + + int legendBoxWidth = getSizeHint()[0]; + int legendBoxHeight = getSizeHint()[1]; + int legendTextContentMaxHeight = getSizeHint()[2]; + + // legend draw position + int xOffset = 0; + int yOffset = 0; + switch (chart.getStyleManager().getLegendPosition()) { + case OutsideW: + xOffset = chart.getWidth() - legendBoxWidth - chart.getStyleManager().getChartPadding(); + yOffset = (int) ((chart.getHeight() - legendBoxHeight) / 2.0 + chart.getChartTitle().getSizeHint()); + break; + case InsideNW: + xOffset = (int) (chart.getPlot().getBounds().getX() + LEGEND_MARGIN); + yOffset = (int) (chart.getPlot().getBounds().getY() + LEGEND_MARGIN); + break; + case InsideNE: + xOffset = (int) (chart.getPlot().getBounds().getX() + chart.getPlot().getBounds().getWidth() - legendBoxWidth - LEGEND_MARGIN); + yOffset = (int) (chart.getPlot().getBounds().getY() + LEGEND_MARGIN); + break; + case InsideSE: + xOffset = (int) (chart.getPlot().getBounds().getX() + chart.getPlot().getBounds().getWidth() - legendBoxWidth - LEGEND_MARGIN); + yOffset = (int) (chart.getPlot().getBounds().getY() + chart.getPlot().getBounds().getHeight() - legendBoxHeight - LEGEND_MARGIN); + break; + case InsideSW: + xOffset = (int) (chart.getPlot().getBounds().getX() + LEGEND_MARGIN); + yOffset = (int) (chart.getPlot().getBounds().getY() + chart.getPlot().getBounds().getHeight() - legendBoxHeight - LEGEND_MARGIN); + break; + + default: + break; + } g.setColor(chart.getStyleManager().getLegendBorderColor()); g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight); diff --git a/xchart/src/main/java/com/xeiam/xchart/style/StyleManager.java b/xchart/src/main/java/com/xeiam/xchart/style/StyleManager.java index def9dc9f4be314630d1badac4589441fc6164420..26c91023804e79889408b8c199e5116ca6e39626 100644 --- a/xchart/src/main/java/com/xeiam/xchart/style/StyleManager.java +++ b/xchart/src/main/java/com/xeiam/xchart/style/StyleManager.java @@ -33,6 +33,10 @@ import com.xeiam.xchart.style.theme.XChartTheme; */ public class StyleManager { + public enum LegendPosition { + OutsideW, InsideNW, InsideNE, InsideSE, InsideSW + } + /** the default Theme */ private Theme theme = new XChartTheme(); @@ -54,6 +58,7 @@ public class StyleManager { private Color legendBorderColor; private Font legendFont; private int legendPadding; + private LegendPosition legendPosition; // Chart Axes /////////////////////////////// private boolean xAxisTitleVisible; @@ -110,6 +115,7 @@ public class StyleManager { legendBorderColor = theme.getLegendBorderColor(); legendFont = theme.getLegendFont(); legendPadding = theme.getLegendPadding(); + legendPosition = theme.getLegendPosition(); // axes xAxisTitleVisible = theme.isXAxisTitleVisible(); @@ -351,6 +357,21 @@ public class StyleManager { return legendPadding; } + /** + * sets the legend position + * + * @param legendPosition + */ + public void setLegendPosition(LegendPosition legendPosition) { + + this.legendPosition = legendPosition; + } + + public LegendPosition getLegendPosition() { + + return legendPosition; + } + // Chart Axes /////////////////////////////// /** diff --git a/xchart/src/main/java/com/xeiam/xchart/style/theme/GGPlot2Theme.java b/xchart/src/main/java/com/xeiam/xchart/style/theme/GGPlot2Theme.java index 05144636fe4a5910a235c6657389f5b66bbd76df..57e7318ffc054f2b530ecfd310b4240f009a10c9 100644 --- a/xchart/src/main/java/com/xeiam/xchart/style/theme/GGPlot2Theme.java +++ b/xchart/src/main/java/com/xeiam/xchart/style/theme/GGPlot2Theme.java @@ -27,6 +27,7 @@ import java.awt.Font; import java.awt.Stroke; import com.xeiam.xchart.style.ChartColor; +import com.xeiam.xchart.style.StyleManager.LegendPosition; /** * @author timmolter @@ -117,6 +118,12 @@ public class GGPlot2Theme implements Theme { return 10; } + @Override + public LegendPosition getLegendPosition() { + + return LegendPosition.OutsideW; + } + // Chart Axes /////////////////////////////// @Override diff --git a/xchart/src/main/java/com/xeiam/xchart/style/theme/Theme.java b/xchart/src/main/java/com/xeiam/xchart/style/theme/Theme.java index dd039e861cc6f274555a5f0bd3ef7633dba7d9ee..60e307e521d69ae05fd816e2333f42350899cbdb 100644 --- a/xchart/src/main/java/com/xeiam/xchart/style/theme/Theme.java +++ b/xchart/src/main/java/com/xeiam/xchart/style/theme/Theme.java @@ -25,6 +25,8 @@ import java.awt.Color; import java.awt.Font; import java.awt.Stroke; +import com.xeiam.xchart.style.StyleManager.LegendPosition; + /** * @author timmolter */ @@ -62,6 +64,8 @@ public interface Theme { public int getLegendPadding(); + public LegendPosition getLegendPosition(); + // Chart Axes /////////////////////////////// public boolean isXAxisTitleVisible(); diff --git a/xchart/src/main/java/com/xeiam/xchart/style/theme/XChartTheme.java b/xchart/src/main/java/com/xeiam/xchart/style/theme/XChartTheme.java index 03351c0d479ea60586c7f80cd054dc6dfb470472..6405bae5ea58fb2f98db1547b92521b097dd2cec 100644 --- a/xchart/src/main/java/com/xeiam/xchart/style/theme/XChartTheme.java +++ b/xchart/src/main/java/com/xeiam/xchart/style/theme/XChartTheme.java @@ -27,6 +27,7 @@ import java.awt.Font; import java.awt.Stroke; import com.xeiam.xchart.style.ChartColor; +import com.xeiam.xchart.style.StyleManager.LegendPosition; /** * @author timmolter @@ -118,6 +119,12 @@ public class XChartTheme implements Theme { return 10; } + @Override + public LegendPosition getLegendPosition() { + + return LegendPosition.OutsideW; + } + // Chart Axes /////////////////////////////// @Override