diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example1.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example1.java index 40588fc699e9c0552a747eb10ec8a695c28af616..086baf75bc7eefbd14c6c9eed3e0e9dcd8fcc92e 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example1.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example1.java @@ -76,7 +76,7 @@ public class Example1 implements ExampleChart { // Customize Chart chart.setTitle("Example1"); chart.getStyleManager().setChartTitleVisible(false); - chart.setLegendVisible(false); + chart.getStyleManager().setChartLegendVisible(false); // Series 1 Series series1 = chart.addSeries("data", xData, yData); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example10.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example10.java index 969667519286e7013764a4d1b29514f8683f8022..cfad83f1c059688027222e000f0f2476bb84d6d7 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example10.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example10.java @@ -46,7 +46,7 @@ public class Example10 implements ExampleChart { chart.setTitle("Example10"); chart.setXAxisTitle("X"); chart.setYAxisTitle("Y"); - chart.setLegendVisible(false); + chart.getStyleManager().setChartLegendVisible(false); for (int i = 0; i < 200; i++) { Series series = chart.addSeries("", new double[] { Math.random(), Math.random() }, new double[] { Math.random(), Math.random() }); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example2.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example2.java index 9da85d2cf0147d0532f508042a37fc04b328afe9..4f70acfbc2b528bc95224964efacc47d79ae1ea0 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example2.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example2.java @@ -57,7 +57,7 @@ public class Example2 implements ExampleChart { // Customize Chart chart.getStyleManager().setChartTitleVisible(false); - chart.setLegendVisible(false); + chart.getStyleManager().setChartLegendVisible(false); // Series 1 Series series1 = chart.addSeries("y=sin(x)", xData1, yData1); diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example8.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example8.java index b6d9036aced6fb4799eab05342bb3ecd743e374b..3b00784eb0e964bd2c80240a659f8f4162701358 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example8.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example8.java @@ -58,7 +58,7 @@ public class Example8 implements ExampleChart { // Customize Chart chart.getStyleManager().setChartTitleVisible(false); - chart.setLegendVisible(false); + chart.getStyleManager().setChartLegendVisible(false); chart.setAxisTitlesVisible(false); // Series 1 diff --git a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example9.java b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example9.java index f53a8e148767b24e07e7ee0f013430490b14ad74..c8f4a3564ebf80172b0f56d255ae851a18987dc6 100644 --- a/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example9.java +++ b/xchart-demo/src/main/java/com/xeiam/xchart/demo/charts/Example9.java @@ -77,11 +77,11 @@ public class Example9 implements ExampleChart { chart.setForegroundColor(ChartColor.getAWTColor(ChartColor.GREY)); chart.setGridLinesColor(new Color(255, 255, 255)); chart.getStyleManager().setChartBackgroundColor(Color.WHITE); - chart.setLegendBackgroundColor(Color.PINK); + chart.getStyleManager().setChartLegendBackgroundColor(Color.PINK); chart.getStyleManager().setChartBordersColor(Color.GREEN); chart.getStyleManager().setChartFontColor(Color.MAGENTA); chart.getStyleManager().setTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 24)); - chart.setLegendFont(new Font(Font.SERIF, Font.PLAIN, 18)); + chart.getStyleManager().setChartLegendFont(new Font(Font.SERIF, Font.PLAIN, 18)); chart.setAxisTitleFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); chart.setTickLabelFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); chart.setTickLabelFont(new Font(Font.SERIF, Font.PLAIN, 11)); diff --git a/xchart/src/main/java/com/xeiam/xchart/Chart.java b/xchart/src/main/java/com/xeiam/xchart/Chart.java index 332a5e148cc174512b15c6ae01598f014f484711..f335b748e2c89025ca30f77faa1750939aec4d1c 100644 --- a/xchart/src/main/java/com/xeiam/xchart/Chart.java +++ b/xchart/src/main/java/com/xeiam/xchart/Chart.java @@ -26,8 +26,8 @@ import java.util.Locale; import java.util.TimeZone; import com.xeiam.xchart.internal.chartpart.AxisPair; +import com.xeiam.xchart.internal.chartpart.ChartLegend; import com.xeiam.xchart.internal.chartpart.ChartTitle; -import com.xeiam.xchart.internal.chartpart.Legend; import com.xeiam.xchart.internal.chartpart.Plot; import com.xeiam.xchart.style.Series; import com.xeiam.xchart.style.StyleManager; @@ -49,7 +49,7 @@ public class Chart { // Chart Parts public ChartTitle chartTitle = new ChartTitle(this); - public Legend chartLegend = new Legend(this); + public ChartLegend chartLegend = new ChartLegend(this); public AxisPair axisPair = new AxisPair(this); protected Plot plot = new Plot(this); @@ -77,7 +77,7 @@ public class Chart { setTitle(chartBuilder.title); setXAxisTitle(chartBuilder.xAxisTitle); setYAxisTitle(chartBuilder.yAxisTitle); - setLegendVisible(chartBuilder.isLegendVisible); + styleManager.setChartLegendVisible(chartBuilder.isLegendVisible); } /** @@ -280,16 +280,6 @@ public class Chart { this.axisPair.yAxis.getAxisTitle().setVisible(isVisible); } - /** - * Set the chart legend visibility - * - * @param isVisible - */ - public void setLegendVisible(boolean isVisible) { - - this.chartLegend.setVisible(isVisible); - } - /** * Set the x- and y-axis tick marks and labels visibility * @@ -351,26 +341,6 @@ public class Chart { this.plot.plotSurface.setGridLinesColor(color); } - /** - * Set the chart legend color - * - * @param color - */ - public void setLegendBackgroundColor(Color color) { - - this.chartLegend.backgroundColor = color; - } - - /** - * Set the chart legend font - * - * @param font - */ - public void setLegendFont(Font font) { - - this.chartLegend.font = font; - } - /** * Set the x- and y-axis title font * diff --git a/xchart/src/main/java/com/xeiam/xchart/QuickChart.java b/xchart/src/main/java/com/xeiam/xchart/QuickChart.java index 452dc79e24a89fbee091c9112e6b0ede67516281..8340528505539c7b11af6b3219dc74103c999880 100644 --- a/xchart/src/main/java/com/xeiam/xchart/QuickChart.java +++ b/xchart/src/main/java/com/xeiam/xchart/QuickChart.java @@ -85,7 +85,7 @@ public final class QuickChart { if (seriesNames != null) { series = chart.addSeries(seriesNames[i], xData, yData[i]); } else { - chart.setLegendVisible(false); + chart.getStyleManager().setChartLegendVisible(false); series = chart.addSeries(" " + i, xData, yData[i]); } series.setMarker(SeriesMarker.NONE); 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 7eacf787be4859cfbf2f4030bf4dae7e599e665c..b37075379d27ca63a1c7ec8cac54dcaa7f4b87fb 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 @@ -206,7 +206,8 @@ public class Axis implements IChartPart { int xOffset = (int) (axisPair.yAxis.getBounds().getWidth() + (axisPair.yAxis.axisTick.isVisible ? Plot.PLOT_PADDING : 0) + Chart.CHART_PADDING); int yOffset = (int) (axisPair.yAxis.getBounds().getY() + axisPair.yAxis.getBounds().getHeight()); - int width = (int) (axisPair.chart.width - axisPair.yAxis.getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - (axisPair.chart.chartLegend.isVisible ? 3 : 2) * Chart.CHART_PADDING); + int width = (int) (axisPair.chart.width - axisPair.yAxis.getBounds().getWidth() - axisPair.getChartLegendBounds().getWidth() - (axisPair.chart.getStyleManager().isChartLegendVisisble() ? 3 : 2) + * Chart.CHART_PADDING); int height = this.getSizeHint(); Rectangle xAxisRectangle = new Rectangle(xOffset, yOffset, width, height); this.paintZone = xAxisRectangle; diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartLegend.java similarity index 73% rename from xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java rename to xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartLegend.java index 2de0d137258e7b93d11819f0ca11db8ca12c504e..092ad9aacddbeed5af23c55d57dc8be70ceadeeb 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/Legend.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartLegend.java @@ -15,8 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.awt.Color; -import java.awt.Font; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.font.FontRenderContext; @@ -27,28 +25,16 @@ import com.xeiam.xchart.Chart; import com.xeiam.xchart.internal.interfaces.IChartPart; import com.xeiam.xchart.internal.interfaces.IHideable; import com.xeiam.xchart.internal.markers.Marker; -import com.xeiam.xchart.style.ChartColor; import com.xeiam.xchart.style.Series; /** * @author timmolter */ -public class Legend implements IChartPart, IHideable { - - private static final int LEGEND_PADDING = 10; +public class ChartLegend implements IChartPart, IHideable { /** parent */ private final Chart chart; - /** the visibility state of legend */ - protected boolean isVisible = true; // default to true - - /** the font */ - public Font font; - - /** the background color */ - public Color backgroundColor; - /** the bounds */ private Rectangle bounds; @@ -57,26 +43,23 @@ public class Legend implements IChartPart, IHideable { * * @param chart */ - public Legend(Chart chart) { + public ChartLegend(Chart chart) { this.chart = chart; - backgroundColor = ChartColor.getAWTColor(ChartColor.LIGHT_GREY); // default background color - font = new Font(Font.SANS_SERIF, Font.PLAIN, 11); // default font } @Override public void setVisible(boolean isVisible) { - this.isVisible = isVisible; } @Override public void paint(Graphics2D g) { bounds = new Rectangle(); - g.setFont(font); + g.setFont(chart.getStyleManager().getChartLegendFont()); - if (isVisible) { + if (chart.getStyleManager().isChartLegendVisisble()) { Map<Integer, Series> seriesMap = chart.axisPair.seriesMap; @@ -86,7 +69,7 @@ public class Legend implements IChartPart, IHideable { for (Integer seriesId : seriesMap.keySet()) { Series series = seriesMap.get(seriesId); - TextLayout textLayout = new TextLayout(series.name, font, new FontRenderContext(null, true, false)); + TextLayout textLayout = new TextLayout(series.name, chart.getStyleManager().getChartLegendFont(), new FontRenderContext(null, true, false)); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); // System.out.println(rectangle); if (rectangle.getWidth() > legendTextContentMaxWidth) { @@ -100,25 +83,25 @@ public class Legend implements IChartPart, IHideable { // determine legend content height int legendContentHeight = 0; int maxContentHeight = Math.max(legendTextContentMaxHeight, Marker.SIZE); - legendContentHeight = maxContentHeight * seriesMap.size() + LEGEND_PADDING * (seriesMap.size() - 1); + legendContentHeight = maxContentHeight * seriesMap.size() + chart.getStyleManager().getChartLegendPadding() * (seriesMap.size() - 1); // determine legend content width - int legendContentWidth = (int) (3.0 * Marker.SIZE + LEGEND_PADDING + legendTextContentMaxWidth); + int legendContentWidth = (int) (3.0 * Marker.SIZE + chart.getStyleManager().getChartLegendPadding() + legendTextContentMaxWidth); // Draw Legend Box - int legendBoxWidth = legendContentWidth + 2 * LEGEND_PADDING; - int legendBoxHeight = legendContentHeight + 2 * LEGEND_PADDING; + int legendBoxWidth = legendContentWidth + 2 * chart.getStyleManager().getChartLegendPadding(); + int legendBoxHeight = legendContentHeight + 2 * chart.getStyleManager().getChartLegendPadding(); int xOffset = chart.width - legendBoxWidth - Chart.CHART_PADDING; int yOffset = (int) ((chart.height - legendBoxHeight) / 2.0 + chart.chartTitle.getBounds().getY() + chart.chartTitle.getBounds().getHeight()); g.setColor(chart.getStyleManager().getChartBordersColor()); g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight); - g.setColor(backgroundColor); + g.setColor(chart.getStyleManager().getChartLegendBackgroundColor()); g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1); // Draw legend content inside legend box - int startx = xOffset + LEGEND_PADDING; - int starty = yOffset + LEGEND_PADDING; + int startx = xOffset + chart.getStyleManager().getChartLegendPadding(); + int starty = yOffset + chart.getStyleManager().getChartLegendPadding(); for (Integer seriesId : seriesMap.keySet()) { Series series = seriesMap.get(seriesId); // paint line @@ -135,9 +118,9 @@ public class Legend implements IChartPart, IHideable { // paint series name g.setColor(chart.getStyleManager().getChartFontColor()); - TextLayout layout = new TextLayout(series.name, font, new FontRenderContext(null, true, false)); - layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + LEGEND_PADDING), (starty + Marker.SIZE)); - starty = starty + legendTextContentMaxHeight + LEGEND_PADDING; + TextLayout layout = new TextLayout(series.name, chart.getStyleManager().getChartLegendFont(), new FontRenderContext(null, true, false)); + layout.draw(g, (float) (startx + Marker.SIZE + (Marker.SIZE * 1.5) + chart.getStyleManager().getChartLegendPadding()), (starty + Marker.SIZE)); + starty = starty + legendTextContentMaxHeight + chart.getStyleManager().getChartLegendPadding(); } // bounds 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 b08bc3eac52fa2b430de0cbce3a9767805e8a5b9..58b00ae581606aef3000b8d332327d0dfdd89cff 100644 --- a/xchart/src/main/java/com/xeiam/xchart/style/StyleManager.java +++ b/xchart/src/main/java/com/xeiam/xchart/style/StyleManager.java @@ -40,9 +40,13 @@ public class StyleManager { public Color chartFontColor; private Font chartTitleFont; - private boolean isChartTitleVisible; + private boolean isChartLegendVisisble; + private Color chartLegendBackgroundColor; + private Font chartLegendFont; + private int chartLegendPadding; + /** * Constructor */ @@ -53,11 +57,18 @@ public class StyleManager { private void setAllStyles() { + // chart chartBackgroundColor = theme.getChartBackgroundColor(); chartBordersColor = theme.getChartBordersColor(); chartFontColor = theme.getChartFontColor(); + // chart title chartTitleFont = theme.getChartTitleFont(); isChartTitleVisible = theme.isChartTitleVisible(); + // chart legend + isChartLegendVisisble = theme.isChartLegendVisible(); + chartLegendBackgroundColor = theme.getChartLegendBackgroundColor(); + chartLegendFont = theme.getChartLegendFont(); + chartLegendPadding = theme.getChartLegendPadding(); } /** @@ -151,6 +162,67 @@ public class StyleManager { } // Chart Legend /////////////////////////////// + + /** + * Set the chart legend color + * + * @param color + */ + public void setChartLegendBackgroundColor(Color color) { + + this.chartLegendBackgroundColor = color; + } + + public Color getChartLegendBackgroundColor() { + + return chartLegendBackgroundColor; + } + + /** + * Set the chart legend font + * + * @param font + */ + public void setChartLegendFont(Font font) { + + this.chartLegendFont = font; + } + + public Font getChartLegendFont() { + + return chartLegendFont; + } + + /** + * Set the chart legend visibility + * + * @param isChartLegendVisisble + */ + public void setChartLegendVisible(boolean isChartLegendVisisble) { + + this.isChartLegendVisisble = isChartLegendVisisble; + } + + public boolean isChartLegendVisisble() { + + return isChartLegendVisisble; + } + + /** + * Set the chart legend padding + * + * @param chartLegendPadding + */ + public void setChartLegendPadding(int chartLegendPadding) { + + this.chartLegendPadding = chartLegendPadding; + } + + public int getChartLegendPadding() { + + return chartLegendPadding; + } + // Chart Title /////////////////////////////// } 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 f3388869e62ce860e415a091650be1a84a54ef1f..1498d3b95a028b2fcc4663805e04cad1e6dc1551 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 @@ -43,4 +43,14 @@ public interface Theme { public boolean isChartTitleVisible(); + // Chart Legend /////////////////////////////// + + public Font getChartLegendFont(); + + public boolean isChartLegendVisible(); + + public Color getChartLegendBackgroundColor(); + + public int getChartLegendPadding(); + } 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 f541f4f75eda4ec78ec368e035d41c2cedbb243f..c3b091848cbfd663a347d2713eb6c5648b100db1 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 @@ -31,6 +31,8 @@ import com.xeiam.xchart.style.ChartColor; */ public class XChartTheme implements Theme { + // Chart Style /////////////////////////////// + @Override public Color getChartBackgroundColor() { @@ -49,6 +51,8 @@ public class XChartTheme implements Theme { return ChartColor.getAWTColor(ChartColor.BLACK); } + // Chart Title /////////////////////////////// + @Override public Font getChartTitleFont() { @@ -61,4 +65,30 @@ public class XChartTheme implements Theme { return false; } + // Chart Legend /////////////////////////////// + + @Override + public Font getChartLegendFont() { + + return new Font(Font.SANS_SERIF, Font.PLAIN, 11); + } + + @Override + public boolean isChartLegendVisible() { + + return true; + } + + @Override + public Color getChartLegendBackgroundColor() { + + return ChartColor.getAWTColor(ChartColor.LIGHT_GREY); + } + + @Override + public int getChartLegendPadding() { + + return 10; + } + }