From 124235072babed495c047586dae5ed628c20c80d Mon Sep 17 00:00:00 2001 From: Tim Molter <tim.molter@gmail.com> Date: Sun, 3 Feb 2013 20:39:22 +0100 Subject: [PATCH] title font --- .../xeiam/xchart/demo/charts/Example9.java | 6 +- .../src/main/java/com/xeiam/xchart/Chart.java | 4 +- .../internal/chartpart/AxisTickLabels.java | 2 +- .../internal/chartpart/AxisTickMarks.java | 6 +- .../xchart/internal/chartpart/AxisTitle.java | 2 +- .../xchart/internal/chartpart/ChartTitle.java | 11 +-- .../xchart/internal/chartpart/Legend.java | 4 +- .../internal/chartpart/PlotContent.java | 2 +- .../internal/chartpart/PlotSurface.java | 2 +- .../com/xeiam/xchart/style/StyleManager.java | 69 ++++++++++++++----- .../com/xeiam/xchart/style/theme/Theme.java | 3 + .../xeiam/xchart/style/theme/XChartTheme.java | 7 ++ 12 files changed, 78 insertions(+), 40 deletions(-) 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 933a0c4a..41be965a 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 @@ -76,10 +76,10 @@ public class Example9 implements ExampleChart { chart.setYAxisTitle("Y"); chart.setForegroundColor(ChartColor.getAWTColor(ChartColor.GREY)); chart.setGridLinesColor(new Color(255, 255, 255)); - chart.getStyleManager().setBackgroundColor(Color.WHITE); + chart.getStyleManager().setChartBackgroundColor(Color.WHITE); chart.setLegendBackgroundColor(Color.PINK); - chart.getStyleManager().setBordersColor(Color.GREEN); - chart.getStyleManager().setFontColor(Color.MAGENTA); + chart.getStyleManager().setChartBordersColor(Color.GREEN); + chart.getStyleManager().setChartFontColor(Color.MAGENTA); chart.setTitleFont(new Font(Font.MONOSPACED, Font.BOLD, 24)); chart.setLegendFont(new Font(Font.SERIF, Font.PLAIN, 18)); chart.setAxisTitleFont(new Font(Font.SANS_SERIF, Font.ITALIC, 18)); diff --git a/xchart/src/main/java/com/xeiam/xchart/Chart.java b/xchart/src/main/java/com/xeiam/xchart/Chart.java index 8f16cf5c..61509807 100644 --- a/xchart/src/main/java/com/xeiam/xchart/Chart.java +++ b/xchart/src/main/java/com/xeiam/xchart/Chart.java @@ -104,7 +104,7 @@ public class Chart { } g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // global rendering hint - g.setColor(styleManager.getBackgroundColor()); + g.setColor(styleManager.getChartBackgroundColor()); g.fillRect(0, 0, width, height); chartTitle.paint(g); @@ -378,7 +378,7 @@ public class Chart { */ public void setTitleFont(Font font) { - this.chartTitle.font = font; + styleManager.setTitleFont(font); } /** diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java index f0696f14..8087358d 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickLabels.java @@ -60,7 +60,7 @@ public class AxisTickLabels implements IChartPart { bounds = new Rectangle(); g.setFont(font); - g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getFontColor()); + g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getChartFontColor()); if (axisTick.axis.direction == Axis.Direction.Y) { // Y-Axis diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java index a0dcfa1f..db9eebd9 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTickMarks.java @@ -60,7 +60,7 @@ public class AxisTickMarks implements IChartPart { bounds = new Rectangle(); - g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getBordersColor()); + g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getChartBordersColor()); if (axisTick.axis.direction == Axis.Direction.Y) { // Y-Axis @@ -72,7 +72,7 @@ public class AxisTickMarks implements IChartPart { int tickLocation = axisTick.tickLocations.get(i); - g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getBordersColor()); + g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getChartBordersColor()); g.setStroke(stroke); g.drawLine(xOffset, yOffset + (int) (axisTick.axis.getPaintZone().getHeight() - tickLocation), xOffset + TICK_LENGTH, yOffset + (int) (axisTick.axis.getPaintZone().getHeight() - tickLocation)); @@ -96,7 +96,7 @@ public class AxisTickMarks implements IChartPart { int tickLocation = axisTick.tickLocations.get(i); - g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getBordersColor()); + g.setColor(axisTick.axis.axisPair.chart.getStyleManager().getChartBordersColor()); g.setStroke(stroke); g.drawLine(xOffset + tickLocation, yOffset, xOffset + tickLocation, yOffset - TICK_LENGTH); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java index 2d5dde1e..70f0937d 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/AxisTitle.java @@ -100,7 +100,7 @@ public class AxisTitle implements IChartPart, IHideable { bounds = new Rectangle(); - g.setColor(axis.axisPair.chart.getStyleManager().getFontColor()); + g.setColor(axis.axisPair.chart.getStyleManager().getChartFontColor()); g.setFont(font); if (axis.direction == Axis.Direction.Y) { diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java index ac50ea78..ddf9ec70 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/ChartTitle.java @@ -15,7 +15,6 @@ */ package com.xeiam.xchart.internal.chartpart; -import java.awt.Font; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.font.FontRenderContext; @@ -39,9 +38,6 @@ public class ChartTitle implements IChartPart, IHideable { /** the visibility state of title */ protected boolean isVisible = false; // default to false - /** the font */ - public Font font; - /** the bounds */ private Rectangle bounds; @@ -53,7 +49,6 @@ public class ChartTitle implements IChartPart, IHideable { public ChartTitle(Chart chart) { this.chart = chart; - font = new Font(Font.SANS_SERIF, Font.BOLD, 14); // default font } public void setText(String text) { @@ -76,12 +71,12 @@ public class ChartTitle implements IChartPart, IHideable { public void paint(Graphics2D g) { bounds = new Rectangle(); - g.setFont(font); + g.setFont(chart.getStyleManager().getChartTitleFont()); if (isVisible) { FontRenderContext frc = g.getFontRenderContext(); - TextLayout textLayout = new TextLayout(text, font, frc); + TextLayout textLayout = new TextLayout(text, chart.getStyleManager().getChartTitleFont(), frc); Rectangle rectangle = textLayout.getPixelBounds(null, 0, 0); int xOffset = (int) ((chart.width - rectangle.getWidth()) / 2.0); int yOffset = (int) ((isVisible ? (Chart.CHART_PADDING - rectangle.getY()) : 0)); @@ -90,7 +85,7 @@ public class ChartTitle implements IChartPart, IHideable { // g.setColor(Color.green); // g.draw(bounds); - g.setColor(chart.getStyleManager().getFontColor()); + g.setColor(chart.getStyleManager().getChartFontColor()); textLayout.draw(g, xOffset, yOffset); } 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 ed577be3..2de0d137 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 @@ -111,7 +111,7 @@ public class Legend implements IChartPart, IHideable { 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().getBordersColor()); + g.setColor(chart.getStyleManager().getChartBordersColor()); g.drawRect(xOffset, yOffset, legendBoxWidth, legendBoxHeight); g.setColor(backgroundColor); g.fillRect(xOffset + 1, yOffset + 1, legendBoxWidth - 1, legendBoxHeight - 1); @@ -134,7 +134,7 @@ public class Legend implements IChartPart, IHideable { } // paint series name - g.setColor(chart.getStyleManager().getFontColor()); + 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; diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java index 62bf88cf..b9749e41 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotContent.java @@ -145,7 +145,7 @@ public class PlotContent implements IChartPart { // paint errorbar if (errorBars != null) { - g.setColor(plot.chart.getStyleManager().getBordersColor()); + g.setColor(plot.chart.getStyleManager().getChartBordersColor()); g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL)); int bottom = (int) (-1 * bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue())); int top = (int) (bounds.getHeight() * eb / (yMax.subtract(yMin).doubleValue())); diff --git a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java index 8eb5e205..2942fdc9 100644 --- a/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java +++ b/xchart/src/main/java/com/xeiam/xchart/internal/chartpart/PlotSurface.java @@ -74,7 +74,7 @@ public class PlotSurface implements IChartPart, IHideable { g.setColor(foregroundColor); g.fill(backgroundRectangle); Rectangle borderRectangle = new Rectangle((int) bounds.getX() - 1, (int) bounds.getY(), (int) (bounds.getWidth()), (int) bounds.getHeight()); - g.setColor(plot.chart.getStyleManager().getBordersColor()); + g.setColor(plot.chart.getStyleManager().getChartBordersColor()); g.draw(borderRectangle); // paint grid lines 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 944444fb..2374e400 100644 --- a/xchart/src/main/java/com/xeiam/xchart/style/StyleManager.java +++ b/xchart/src/main/java/com/xeiam/xchart/style/StyleManager.java @@ -22,6 +22,7 @@ package com.xeiam.xchart.style; import java.awt.Color; +import java.awt.Font; import com.xeiam.xchart.style.theme.Theme; import com.xeiam.xchart.style.theme.XChartTheme; @@ -31,20 +32,29 @@ import com.xeiam.xchart.style.theme.XChartTheme; */ public class StyleManager { + /** the default Theme */ private Theme theme = new XChartTheme(); - private Color backgroundColor; - public Color bordersColor; - public Color fontColor; + private Color chartBackgroundColor; + public Color chartBordersColor; + public Color chartFontColor; + + private Font chartTitleFont; /** * Constructor */ public StyleManager() { - backgroundColor = theme.getChartBackgroundColor(); - bordersColor = theme.getChartBordersColor(); - fontColor = theme.getChartFontColor(); + setAllStyles(); + } + + private void setAllStyles() { + + chartBackgroundColor = theme.getChartBackgroundColor(); + chartBordersColor = theme.getChartBordersColor(); + chartFontColor = theme.getChartFontColor(); + chartTitleFont = theme.getChartTitleFont(); } /** @@ -55,16 +65,24 @@ public class StyleManager { public void setTheme(Theme theme) { this.theme = theme; + setAllStyles(); } + // Chart Style /////////////////////////////// + /** * Set the chart background color - the part around the edge of the chart * * @param color */ - public void setBackgroundColor(Color backgroundColor) { + public void setChartBackgroundColor(Color color) { + + this.chartBackgroundColor = color; + } + + public Color getChartBackgroundColor() { - this.backgroundColor = backgroundColor; + return chartBackgroundColor; } /** @@ -72,9 +90,14 @@ public class StyleManager { * * @param color */ - public void setBordersColor(Color bordersColor) { + public void setChartBordersColor(Color color) { - this.bordersColor = bordersColor; + this.chartBordersColor = color; + } + + public Color getChartBordersColor() { + + return chartBordersColor; } /** @@ -82,24 +105,34 @@ public class StyleManager { * * @param color */ - public void setFontColor(Color fontColor) { + public void setChartFontColor(Color color) { - this.fontColor = fontColor; + this.chartFontColor = color; } - public Color getBackgroundColor() { + public Color getChartFontColor() { - return backgroundColor; + return chartFontColor; } - public Color getBordersColor() { + // Chart Title /////////////////////////////// + + /** + * Set the chart title font + * + * @param font + */ + public void setTitleFont(Font font) { - return bordersColor; + this.chartTitleFont = font; } - public Color getFontColor() { + public Font getChartTitleFont() { - return fontColor; + return chartTitleFont; } + // Chart Legend /////////////////////////////// + // 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 f6893b2a..7170e105 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 @@ -22,6 +22,7 @@ package com.xeiam.xchart.style.theme; import java.awt.Color; +import java.awt.Font; /** * @author timmolter @@ -34,4 +35,6 @@ public interface Theme { public Color getChartFontColor(); + public Font getChartTitleFont(); + } 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 94b4d07b..e266bb50 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 @@ -22,6 +22,7 @@ package com.xeiam.xchart.style.theme; import java.awt.Color; +import java.awt.Font; import com.xeiam.xchart.style.ChartColor; @@ -48,4 +49,10 @@ public class XChartTheme implements Theme { return ChartColor.getAWTColor(ChartColor.BLACK); } + @Override + public Font getChartTitleFont() { + + return new Font(Font.SANS_SERIF, Font.BOLD, 14); + } + } -- GitLab