diff gnv-artifacts/src/main/java/de/intevation/gnv/chart/XMLChartTheme.java @ 304:a16d337c5678

The style of charts can be configured with ChartThemes using XML configuration files. gnv-artifacts/trunk@362 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 23 Nov 2009 17:01:28 +0000
parents
children 22a6493e8460
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/chart/XMLChartTheme.java	Mon Nov 23 17:01:28 2009 +0000
@@ -0,0 +1,250 @@
+package de.intevation.gnv.chart;
+
+import java.awt.Font;
+import java.awt.Color;
+import java.lang.NumberFormatException;
+
+import org.apache.log4j.Logger;
+
+import org.jfree.chart.StandardChartTheme;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.ui.RectangleInsets;
+import org.jfree.util.UnitType;
+
+
+import org.w3c.dom.Document;
+
+import de.intevation.artifactdatabase.Config;
+
+/**
+ * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
+ */
+public class XMLChartTheme
+extends      StandardChartTheme
+{
+    private static final Color DEFAULT_COLOR = Color.BLACK;
+
+    private Logger log = Logger.getLogger(XMLChartTheme.class);
+
+    protected boolean domainCrosshairVisible;
+    protected boolean rangeCrosshairVisible;
+
+
+    public XMLChartTheme(String name) {
+        super(name);
+    }
+
+
+    public void setDomainCrosshairVisible(boolean visible) {
+        this.domainCrosshairVisible = visible;
+    }
+
+
+    public boolean getDomainCrosshairVisible() {
+        return domainCrosshairVisible;
+    }
+
+
+    public boolean getRangeCrosshairVisible() {
+        return rangeCrosshairVisible;
+    }
+
+
+    public void setRangeCrosshairVisible(boolean visible) {
+        this.rangeCrosshairVisible = visible;
+    }
+
+
+    public void applyXMLConfiguration(Document document) {
+        log.debug("create XMLChartTheme");
+
+        init(document);
+    }
+
+
+    private void init(Document document) {
+        log.debug("init XMLChartTheme parameters");
+
+        initChartParameters(document);
+        initTitleParameters(document);
+        initSubtitleParameters(document);
+        initPlotParameters(document);
+        initAxisParameters(document);
+        initLegendParameters(document);
+    }
+
+
+    private void initTitleParameters(Document document) {
+        log.debug("init title parameters.");
+
+        int     size  = getInt(document, "theme/title/font/size/@value");
+        String  type  = getString(document, "theme/title/font/type/@value");
+        boolean bold  = getBool(document, "theme/title/font/bold/@value");
+
+        setExtraLargeFont(createFont(type, size, bold));
+
+        String color = getString(document, "theme/title/font/color/@value");
+        Color  c     = decodeColor(color);
+        if (c != null)
+            setTitlePaint(c);
+    }
+
+
+    private void initSubtitleParameters(Document document) {
+        log.debug("init title parameters.");
+
+        int     size  = getInt(document, "theme/subtitle/font/size/@value");
+        String  type  = getString(document, "theme/subtitle/font/type/@value");
+        boolean bold  = getBool(document, "theme/subtitle/font/bold/@value");
+
+        setLargeFont(createFont(type, size, bold));
+
+        String col  = getString(document, "theme/subtitle/font/color/@value");
+        setSubtitlePaint(Color.decode(col));
+    }
+
+
+    private void initChartParameters(Document document) {
+        log.debug("init chart parameters.");
+
+        String bg = getString(document, "theme/chart/background/color/@value");
+        Color  c  = decodeColor(bg);
+        if (c != null)
+            setChartBackgroundPaint(c);
+    }
+
+
+    private void initPlotParameters(Document document) {
+        log.debug("init plot parameters.");
+
+        String tmp = null;
+        tmp        = getString(document, "theme/plot/background/color/@value");
+        Color  c   = decodeColor(tmp);
+        if (c != null)
+            setPlotBackgroundPaint(c);
+
+        tmp = getString(document, "theme/plot/outline/color/@value");
+        c   = decodeColor(tmp);
+        if (c != null)
+            setPlotOutlinePaint(c);
+
+        tmp = getString(document, "theme/plot/domaingridline/color/@value");
+        c   = decodeColor(tmp);
+        if (c != null)
+            setDomainGridlinePaint(c);
+
+        tmp = getString(document, "theme/plot/rangegridline/color/@value");
+        c   = decodeColor(tmp);
+        if (c != null)
+            setRangeGridlinePaint(c);
+
+        boolean rangeCrosshairVisible = getBool(
+            document, "theme/plot/rangecrosshair/visible/@value");
+        setRangeCrosshairVisible(rangeCrosshairVisible);
+
+        boolean domainCrosshairVisible = getBool(
+            document, "theme/plot/domaincrosshair/visible/@value");
+        setDomainCrosshairVisible(domainCrosshairVisible);
+
+        int top    = getInt(document, "theme/plot/offset/top/@value");
+        int bottom = getInt(document, "theme/plot/offset/bottom/@value");
+        int left   = getInt(document, "theme/plot/offset/left/@value");
+        int right  = getInt(document, "theme/plot/offset/right/@value");
+        setAxisOffset(new RectangleInsets(
+            UnitType.RELATIVE,
+            top, left, bottom, right)
+        );
+    }
+
+
+    private void initAxisParameters(Document document) {
+        log.debug("init axis parameters.");
+
+        String tmp = null;
+        tmp        = getString(document, "theme/axis/label/color/@value");
+        Color c    = decodeColor(tmp);
+        if (c != null)
+            setAxisLabelPaint(c);
+
+        tmp = getString(document, "theme/axis/ticklabel/color/@value");
+        c   = decodeColor(tmp);
+        if (c != null)
+            setTickLabelPaint(c);
+    }
+
+
+    private void initLegendParameters(Document document) {
+        log.debug("init legend parameters.");
+
+        String tmp = null;
+        tmp        = getString(document, "theme/legend/font/color/@value");
+        Color c    = decodeColor(tmp);
+        if (c != null)
+            setLegendItemPaint(c);
+
+        tmp = getString(document, "theme/legend/background/color/@value");
+        c   = decodeColor(tmp);
+        if (c != null)
+            setLegendBackgroundPaint(c);
+    }
+
+
+    private static String getString(Document document, String xpath) {
+        return Config.getStringXPath(document, xpath);
+    }
+
+
+    private static int getInt(Document document, String xpath) {
+        String tmp = getString(document, xpath);
+
+        if (tmp != null)
+            return Integer.parseInt(tmp);
+        else
+            return 0;
+    }
+
+
+    private static boolean getBool(Document document, String xpath) {
+        String tmp = getString(document, xpath);
+
+        if (tmp != null)
+            return Boolean.parseBoolean(tmp);
+        else
+            return false;
+    }
+
+
+    protected Color decodeColor(String color) {
+        try {
+            if (color == null)
+                return null;
+
+            return Color.decode(color);
+        }
+        catch (NumberFormatException nfe) {
+            log.warn("Error while parsing color: " + color, nfe);
+        }
+
+        return null;
+    }
+
+
+    protected Font createFont(String type, int size, boolean bold) {
+        Font font = null;
+        if (bold)
+            font = new Font(type, Font.BOLD, size);
+        else
+            font = new Font(type, Font.PLAIN, size);
+
+        return font;
+    }
+
+
+    protected void applyToXYPlot(XYPlot plot) {
+        log.debug("apply theme parameter to XYPlot");
+
+        super.applyToXYPlot(plot);
+        plot.setDomainCrosshairVisible(this.domainCrosshairVisible);
+        plot.setRangeCrosshairVisible(this.rangeCrosshairVisible);
+    }
+}

http://dive4elements.wald.intevation.org