[Schmitzm-commits] r161 - in trunk/src/schmitzm/jfree/chart: . style

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Jun 22 21:28:49 CEST 2009


Author: mojays
Date: 2009-06-22 21:28:46 +0200 (Mon, 22 Jun 2009)
New Revision: 161

Added:
   trunk/src/schmitzm/jfree/chart/style/
   trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
   trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java
   trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java
   trunk/src/schmitzm/jfree/chart/style/ChartLabelStyle.java
   trunk/src/schmitzm/jfree/chart/style/ChartStyle.java
   trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java
Log:
First version of chart styles in schmitzm.jfree.chart.style.*

Added: trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java	2009-06-22 17:15:45 UTC (rev 160)
+++ trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java	2009-06-22 19:28:46 UTC (rev 161)
@@ -0,0 +1,205 @@
+/*
+ *  SDSS Framework - This file is part of the Spatial Decision Support System Framework Platform
+ *
+ *  AbstractChartStyle.java
+ *
+ *  Responsible developper: Andreas Enders, Geographical Institut, Hydrology Research Group, Meckenheimer Allee 166, 53115 Bonn, Germany
+ *                                          IMPETUS Project, www.impetus.uni-koeln.de
+ *  Contact Information:                    info at andreas-enders.de
+ */
+
+package schmitzm.jfree.chart.style;
+
+import java.awt.Color;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Vector;
+
+import org.apache.log4j.Category;
+import org.apache.log4j.Logger;
+import org.jfree.chart.plot.PlotOrientation;
+
+/**
+ * This class provides an abstract implementation for chart styles.
+ * It implements the variables and methods for maintaining the type, the title(s)
+ * and flags of the {@link ChartStyle} interface.
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
+ * @version 1.0
+ */
+public abstract class AbstractChartStyle implements ChartStyle {
+  /** Logger for this class */
+  protected static final Category LOGGER = Logger.getLogger(AbstractChartStyle.class.getName());
+
+  /** Holds the chart type.
+   *  @see  ChartStyle#ChartType */
+  protected ChartType type = null;
+  /** Holds the chart title. */
+  protected ChartLabelStyle titleStyle = new ChartLabelStyle();
+  /** Holds the styles of the chart axis. */
+  protected Map<Integer,ChartAxisStyle> axisStyle = new HashMap<Integer,ChartAxisStyle>();
+  /** Holds the plot orientation (horizontal/vertical). */
+  protected PlotOrientation orientation = null;
+  /** Stores whether a legend is generated for the chart (Default: true). */
+  protected boolean legend = true;
+  /** Stores whether tooltips are generated for the chart data (Default: true). */
+  protected boolean tooltips = true;
+  /** Stores whether URLs are generated for the chart (Default: true). */
+  protected boolean urls = true;
+
+  /**
+   * Creates a line style with default values.
+   */
+  public AbstractChartStyle() {
+    this( ChartType.LINE );
+  }
+
+  /**
+   * Creates a style with default values.
+   * @param type     style type
+   */
+  public AbstractChartStyle(ChartType type) {
+    this(type,null,null,null,true,true,true);
+  }
+
+  /**
+   * Creates a style.
+   * @param type     style type
+   * @param title    title for the chart
+   * @param xTitle   title for the chart's X-axis
+   * @param yTitle   title for the chart's Y-axis
+   * @param legend   flag whether a legend is generated
+   * @param tooltips flag whether toolstips are generated
+   * @param urls     flag whether URLs are generated
+   */
+  public AbstractChartStyle(ChartType type,
+                            String    title,   String xTitle,    String yTitle,
+                            boolean   legend, boolean tooltips, boolean urls) {
+    setType(type);
+    setLegend(legend);
+    setTooltips(tooltips);
+    setURLs(urls);
+    getTitleStyle().setLabel(title);
+    if ( xTitle != null )
+      setAxisStyle(DOMAIN_AXIS, new ChartAxisStyle(xTitle,Color.BLACK,0,0) );
+    if ( yTitle != null )
+      setAxisStyle(RANGE_AXIS, new ChartAxisStyle(yTitle,Color.BLACK,0,0) );
+  }
+
+  /**
+   * Returns the type of the chart style.
+   */
+  public ChartType getType() {
+    return type;
+  }
+
+  /**
+   * Sets the type of the chart style.
+   * @param type the chart type
+   * @see ChartStyle#ChartType
+   */
+  public void setType(ChartType type) {
+    this.type = type;
+  }
+
+  /**
+   * Returns the orientation of the chart.
+   */
+  public PlotOrientation getOrientation() {
+    return orientation;
+  }
+
+  /**
+   * Sets the orientation of the chart.
+   * @param orientation the orientation of the chart
+   */
+  public void setOrientation(PlotOrientation orientation) {
+    this.orientation = orientation;
+  }
+
+  /**
+   * Returns the style of the charts title.
+   */
+  public ChartLabelStyle getTitleStyle() {
+    return titleStyle;
+  }
+  
+  /**
+   * Sets the style of the charts title.
+   * @param style the new style (if {@code null} the style is
+   *        reset to an empty default style, so that the
+   *        getter method always can be accessed!)
+   */
+  public void setTitleStyle(ChartLabelStyle style) {
+    if ( style == null )
+      style = new ChartLabelStyle();
+    titleStyle = style;
+  }
+
+  /**
+   * Returns whether the chart is configured to generate a legend.
+   */
+  public boolean hasLegend() {
+    return legend;
+  }
+
+  /**
+   * Sets whether the chart is configured to generate a legend.
+   */
+  public void setLegend(boolean legend) {
+    this.legend = legend;
+  }
+
+  /**
+   * Returns whether the chart is configured to generate tooltips for the data.
+   */
+  public boolean hasTooltips() {
+    return tooltips;
+  }
+
+  /**
+   * Sets whether the chart is configured to generate tooltips for the data.
+   */
+  public void setTooltips(boolean tooltips) {
+    this.tooltips = tooltips;
+  }
+
+  /**
+   * Returns whether the chart is configured to generate URLs.
+   */
+  public boolean hasURLs() {
+    return urls;
+  }
+
+  /**
+   * Sets whether the chart is configured to generate URLs.
+   */
+  public void setURLs(boolean urls) {
+    this.urls = urls;
+  }
+
+  /**
+   * Returns the axis count.
+   */
+  public int getAxisCount() {
+    return axisStyle.size();
+  }
+
+  /**
+   * Returns the style of a chart axis.
+   * @param axis axis number (0=domain, 1=range, ...)
+   * @return {@code null} if the chart has no such axis
+   */
+  public ChartAxisStyle getAxisStyle(int axis) {
+    return axisStyle.get(axis);
+  }
+  
+  /**
+   * Sets the style of a chart axis.
+   * @param axis axis number (0=domain, 1=range, ...)
+   * @param style style for the axis
+   */
+  public void setAxisStyle(int axis, ChartAxisStyle style) {
+    axisStyle.put(axis, style);
+  }
+
+}

Added: trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java	2009-06-22 17:15:45 UTC (rev 160)
+++ trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java	2009-06-22 19:28:46 UTC (rev 161)
@@ -0,0 +1,317 @@
+/*
+ *  SDSS Framework - This file is part of the Spatial Decision Support System Framework Platform
+ *
+ *  BasicChartStyle.java
+ *
+ *  Responsible developper: Andreas Enders, Geographical Institut, Hydrology Research Group, Meckenheimer Allee 166, 53115 Bonn, Germany
+ *                                          IMPETUS Project, www.impetus.uni-koeln.de
+ *  Contact Information:                    info at andreas-enders.de
+ */
+
+package schmitzm.jfree.chart.style;
+
+import org.jfree.chart.ChartFactory;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.chart.renderer.category.CategoryStepRenderer;
+import org.jfree.chart.renderer.category.StackedAreaRenderer;
+import org.jfree.chart.renderer.category.StackedBarRenderer;
+import org.jfree.chart.renderer.xy.StackedXYAreaRenderer2;
+import org.jfree.chart.renderer.xy.StackedXYBarRenderer;
+import org.jfree.chart.renderer.xy.XYStepAreaRenderer;
+import org.jfree.chart.renderer.xy.XYStepRenderer;
+import org.jfree.data.category.CategoryDataset;
+import org.jfree.data.general.Dataset;
+import org.jfree.data.xy.IntervalXYDataset;
+import org.jfree.data.xy.TableXYDataset;
+import org.jfree.data.xy.XYBarDataset;
+import org.jfree.data.xy.XYDataset;
+
+/**
+ * This class provides a basis chart style for line, area an bar charts.
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
+ * @version 1.0
+ */
+public class BasicChartStyle extends AbstractChartStyle {
+  /** Stores whether the chart is stacked. Line charts can never be stacked. */
+  protected boolean stacked = false;
+  /** Stores whether the chart is stepped. Bar charts are always stepped. */
+  protected boolean stepped = false;
+
+  /**
+   * Creates a normal line style with default values.
+   */
+  public BasicChartStyle() {
+    this(ChartType.LINE);
+  }
+
+  /**
+   * Creates a normal, vertical line, bar or area style.
+   * @param type type of the chart layout
+   */
+  public BasicChartStyle(ChartType type) {
+    this(type, false, false, PlotOrientation.VERTICAL);
+  }
+
+  /**
+   * Creates a line, area or bar chart style.
+   * @param type        style type
+   * @param stacked     flag whether the chart is stacked (ignored for line charts)
+   * @param stepped     flag whether the chart is stepped (ignored for bar charts)
+   * @param orientation orientation of the chart ({@link PlotOrientation.VERTICAL} or {@link PlotOrientation.HORIZONTAL})
+   */
+  public BasicChartStyle(ChartType type, boolean stacked, boolean stepped, PlotOrientation orientation) {
+    this(type, stacked, stepped, orientation, null, null, null, true, true, true);
+  }
+
+  /**
+   * Creates a chart style.
+   * @param type     style type
+   * @param stacked  flag whether the chart is stacked (ignored for line charts)
+   * @param stepped  flag whether the chart is stepped (ignored for bar charts)
+   * @param orientation orientation of the chart ({@link PlotOrientation.VERTICAL} or {@link PlotOrientation.HORIZONTAL})
+   * @param title    title for the chart
+   * @param xTitle   title for the chart's X-axis
+   * @param yTitle   title for the chart's Y-axis
+   * @param legend   flag whether a legend is generated
+   * @param tooltips flag whether toolstips are generated
+   * @param urls     flag whether URLs are generated
+   */
+  public BasicChartStyle(ChartType type, boolean stacked,  boolean stepped,
+                         PlotOrientation orientation,
+                         String title,   String xTitle,    String yTitle,
+                         boolean legend, boolean tooltips, boolean urls) {
+    super(type, title, xTitle, yTitle, legend, tooltips, urls);
+    setStacked( stacked );
+    setStepped( stepped );
+    setOrientation( orientation );
+  }
+
+  /**
+   * Sets the type of the chart style.
+   * @param type the chart type
+   * @exception IllegalArgumentException if {@link aType} is not of
+   *            {@link ChartType#LINE}, {@link ChartType#AREA} or
+   *            {@link ChartType#BAR}
+   */
+  public void setType(ChartType type) {
+    switch( type ) {
+      // supported types
+      case LINE: stacked = false;
+                 break;
+      case BAR:  stepped = true;
+                 break;
+      case AREA: break;
+      // unsupported types
+      default: throw new IllegalArgumentException("Only ChartType.LINE, ChartType.AREA or ChartType.BAR allowed for BasicChartStyle!");
+    }
+    super.setType(type);
+  }
+
+  /**
+   * Returns whether the chart is stacked.
+   * @return {@code false} for line charts
+   */
+  public boolean isStacked() {
+    return stacked;
+  }
+
+  /**
+   * Sets whether the chart is stacked. Sets the {@link #stacked} property
+   * to {@code false} for line charts, even {@code aStacked} is {@code true}.
+   */
+  public void setStacked(boolean stacked) {
+    if ( type == ChartType.LINE && stacked ) {
+      LOGGER.warn("Stacked property can not be enabled for line charts! Command ignored...");
+      stacked = false;
+    }
+    this.stacked = stacked;
+
+    if ( type == ChartType.AREA && stepped && stacked ) {
+      LOGGER.warn("Area chart style can not be stepped and stacked at the same time. Stepped property automatically disabled...");
+      stepped = false;
+    }
+  }
+
+  /**
+   * Returns whether the chart is stepped.
+   * @return {@code true} for bar charts
+   */
+  public boolean isStepped() {
+    return stepped;
+  }
+
+  /**
+   * Sets whether the chart is stepped. Sets the {@link #stepped} property
+   * to {@code true} for bar charts, even {@code aStepped} is {@code false}.
+   */
+  public void setStepped(boolean stepped) {
+    if ( type == ChartType.BAR && !stepped) {
+      LOGGER.warn("Stepped property can not be disabled for bar charts. Command ignored...");
+      stepped = true;
+    }
+    this.stepped = stepped;
+
+    if ( type == ChartType.AREA && stacked && stepped ) {
+      LOGGER.warn("Area chart style can not be stepped and stacked at the same time. Stacked property automatically disabled...");
+      stacked = false;
+    }
+  }
+
+  /**
+   * Generates a chart using the given data and the style represented by this
+   * class.
+   *
+   * @param dataset the data for the chart
+   * @return JFreeChart
+   * @exception UnsupportedOperationException if the style can not be applied
+   *            to the given dataset
+   */
+  public JFreeChart applyToDataset(Dataset dataset) {
+    JFreeChart chart = null;
+    switch ( type ) {
+      case LINE: chart = createLineChart(dataset);
+                 break;
+      case AREA: chart = createAreaChart(dataset);
+                 break;
+      case BAR:  chart = createBarChart(dataset);
+                 break;
+    }
+    return chart;
+  }
+
+  /**
+   * Creates a line chart from an {@link XYDataset} or {@link CategoryDataset}
+   * @param dataset Dataset
+   */
+  private JFreeChart createLineChart(Dataset dataset) {
+    JFreeChart chart = null;
+    // XYDataset -> XYLineChart
+    if (dataset instanceof XYDataset) {
+      chart = ChartFactory.createXYLineChart(
+          getTitleStyle().getLabel(),
+          getAxisStyle(DOMAIN_AXIS).getLabel(),
+          getAxisStyle(RANGE_AXIS).getLabel(),
+          (XYDataset)dataset,
+          orientation,
+          legend,
+          tooltips,
+          urls
+      );
+      if ( stepped )
+        chart.getXYPlot().setRenderer(  new XYStepRenderer() );
+    }
+    // CategoryDataset -> LineChart
+    if (dataset instanceof CategoryDataset) {
+      chart = ChartFactory.createLineChart(
+          getTitleStyle().getLabel(),
+          getAxisStyle(DOMAIN_AXIS).getLabel(),
+          getAxisStyle(RANGE_AXIS).getLabel(),
+          (CategoryDataset)dataset,
+          orientation,
+          legend,
+          tooltips,
+          urls
+      );
+      if ( stepped )
+        chart.getCategoryPlot().setRenderer(  new CategoryStepRenderer() );
+    }
+    return chart;
+  }
+
+  /**
+   * Creates an area chart from an {@link XYDataset} or {@link CategoryDataset}
+   * @param dataset Dataset
+   */
+  private JFreeChart createAreaChart(Dataset dataset) {
+    JFreeChart chart = null;
+    // XYDataset -> XYAreaChart
+    if (dataset instanceof XYDataset) {
+      chart = ChartFactory.createXYAreaChart(
+          getTitleStyle().getLabel(),
+          getAxisStyle(DOMAIN_AXIS).getLabel(),
+          getAxisStyle(RANGE_AXIS).getLabel(),
+          (XYDataset) dataset,
+          orientation,
+          legend,
+          tooltips,
+          urls
+          );
+      if (stacked) {
+        if (dataset instanceof TableXYDataset)
+          chart.getXYPlot().setRenderer(new StackedXYAreaRenderer2());
+        else
+          throw new UnsupportedOperationException("Chart creation: Stacked area charts can not be applied on XYDataset. TableXYDataset needed!");
+        if (stepped)
+          throw new UnsupportedOperationException("Chart creation: A stacked area chart can not be stepped the same time. Use a stacked bar chart instead!");
+      } else  {
+        if (stepped)
+          chart.getXYPlot().setRenderer(new XYStepAreaRenderer(XYStepAreaRenderer.AREA));
+      }
+    }
+    // CategoryDataset -> AreaChart
+    if (dataset instanceof CategoryDataset) {
+      chart = ChartFactory.createAreaChart(
+          getTitleStyle().getLabel(),
+          getAxisStyle(DOMAIN_AXIS).getLabel(),
+          getAxisStyle(RANGE_AXIS).getLabel(),
+          (CategoryDataset)dataset,
+          orientation,
+          legend,
+          tooltips,
+          urls
+      );
+      if ( stepped )
+        throw new UnsupportedOperationException("Chart creation: Stepped area style can not be applied on CategoryDataset. Use a bar chart instead!");
+      if ( stacked )
+        chart.getCategoryPlot().setRenderer(  new StackedAreaRenderer(false) );
+    }
+
+    return chart;
+  }
+
+  /**
+   * Creates a bar chart from an {@link XYDataset} or {@link CategoryDataset}
+   * @param dataset Dataset
+   */
+  private JFreeChart createBarChart(Dataset dataset) {
+    JFreeChart chart = null;
+    // XYDataset -> XYBarChart
+    if (dataset instanceof XYDataset) {
+      chart = ChartFactory.createXYBarChart(
+          getTitleStyle().getLabel(),
+          getAxisStyle(DOMAIN_AXIS).getLabel(),
+          false,
+          getAxisStyle(RANGE_AXIS).getLabel(),
+          (IntervalXYDataset)(dataset instanceof TableXYDataset ? (TableXYDataset)dataset : new XYBarDataset( (XYDataset) dataset, 0.5d)),
+          PlotOrientation.VERTICAL,
+          true,
+          true,
+          true
+      );
+      if ( stacked ) {
+        if ( dataset instanceof TableXYDataset )
+          chart.getXYPlot().setRenderer(new StackedXYBarRenderer());
+        else
+          throw new UnsupportedOperationException("Chart creation: Stacked bar charts can not be applied on XYDataset. TableXYDataset needed!");
+      }
+    }
+    // CategoryDataset -> BarChart
+    if (dataset instanceof CategoryDataset) {
+      chart = ChartFactory.createBarChart(
+          getTitleStyle().getLabel(),
+          getAxisStyle(DOMAIN_AXIS).getLabel(),
+          getAxisStyle(RANGE_AXIS).getLabel(),
+          (CategoryDataset)dataset,
+          orientation,
+          legend,
+          tooltips,
+          urls
+      );
+      if ( stacked )
+        chart.getCategoryPlot().setRenderer(  new StackedBarRenderer(false) );
+    }
+
+    return chart;
+  }
+}

Added: trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java	2009-06-22 17:15:45 UTC (rev 160)
+++ trunk/src/schmitzm/jfree/chart/style/ChartAxisStyle.java	2009-06-22 19:28:46 UTC (rev 161)
@@ -0,0 +1,104 @@
+/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM)
+
+    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
+    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+    You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
+
+    Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
+    Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
+    Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
+ **/
+
+package schmitzm.jfree.chart.style;
+
+import java.awt.Color;
+import java.awt.Paint;
+import java.text.Format;
+
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.Axis;
+import org.jfree.data.general.Dataset;
+
+/**
+ * This interface is a general super class for a design style of a chart
+ * {@link Axis}.
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
+ * @version 1.0
+ */
+public class ChartAxisStyle extends ChartLabelStyle {
+  /** Holds the angle (in degrees) the axis values are rotated by. */
+  protected double valuesAngleDegr = 0;
+  /** Holds whether the axis is visible. */
+  protected boolean visible = true;
+  /** Holds the number format to display the axis values. */
+  protected Format valuesFormat = null;
+  
+  /**
+   * Creates a new style with default values (empty label, color black, angle 0).
+   */
+  public ChartAxisStyle() {
+    this("",Color.BLACK, 0.0 ,0.0);
+  }
+
+  /**
+   * Creates a new style.
+   * @param title axis title
+   * @param color text color for the axis title
+   * @param labelAngle angel (in degrees) the axis label is rotated by
+   * @param valuesAngle angel (in degrees) the axis values are rotated by
+   */
+  public ChartAxisStyle(String title, Paint color, double labelAngle, double valuesAngle) {
+    super(title,color,labelAngle);
+    setValuesAngle(valuesAngle);
+  }
+
+  /**
+   * Returns the angle (in degrees) the axis values are rotated by.
+   */
+  public double getValuesAngle() {
+    return valuesAngleDegr;
+  }
+
+  /**
+   * Returns the angle (in radian) the axis values are rotated by.
+   */
+  public double getValuesAngleRadian() {
+    return valuesAngleDegr * Math.PI / 180;
+  }
+
+  /**
+   * Sets the angle (in degrees) the label text is rotated by.
+   * @param color Color for the label text
+   */
+  public void setValuesAngle(double angel) {
+    this.valuesAngleDegr = angel;
+  }
+
+  /**
+   * Returns whether the axis is visible.
+   */
+  public boolean isVisible() {
+    return visible;
+  }
+
+  /**
+   * Sets whether the axis is visible.
+   */
+  public void setVisible(boolean visible) {
+    this.visible = visible;
+  }
+
+  /**
+   * Returns the (number) format for the axis values.
+   */
+  public Format getValuesFormat() {
+    return valuesFormat;
+  }
+
+  /**
+   * Sets the (number) format for the axis values.
+   */
+  public void setValuesFormat(Format format) {
+    this.valuesFormat = format;
+  }
+}

Added: trunk/src/schmitzm/jfree/chart/style/ChartLabelStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartLabelStyle.java	2009-06-22 17:15:45 UTC (rev 160)
+++ trunk/src/schmitzm/jfree/chart/style/ChartLabelStyle.java	2009-06-22 19:28:46 UTC (rev 161)
@@ -0,0 +1,105 @@
+/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM)
+
+    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
+    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+    You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
+
+    Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
+    Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
+    Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
+ **/
+
+package schmitzm.jfree.chart.style;
+
+import java.awt.Color;
+import java.awt.Paint;
+
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.Axis;
+import org.jfree.data.general.Dataset;
+
+/**
+ * This interface is a general super class for a design style of a chart
+ * {@link Axis}.
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
+ * @version 1.0
+ */
+public class ChartLabelStyle {
+  /** Holds the label text. */
+  protected String label = null;
+  /** Holds the text color of the label. */
+  protected Paint paint = null;
+  /** Holds the angle (in degrees) the label is rotated by. */
+  protected double angleDegr = 0;
+  
+  /**
+   * Creates a new style with default values (empty label, color black, angle 0).
+   */
+  public ChartLabelStyle() {
+    this("",Color.BLACK, 0.0);
+  }
+
+  /**
+   * Creates a new style.
+   * @param color text color
+   * @param angle angle (in degrees) the text is rotated by
+   */
+  public ChartLabelStyle(String label, Paint color, double angle) {
+    setLabel(label);
+    setPaint(color);
+    setAngle(angle);
+  }
+  
+  /**
+   * Returns the label text.
+   */
+  public String getLabel() {
+    return label;
+  }
+
+  /**
+   * Sets the label text.
+   * @param label the text for the label
+   */
+  public void setLabel(String label) {
+    this.label = label;
+  }
+  
+  /**
+   * Returns the color for the label text.
+   */
+  public Paint getPaint() {
+    return paint;
+  }
+
+  /**
+   * Sets the color for the label text.
+   * @param color Color for the label text
+   */
+  public void setPaint(Paint color) {
+    this.paint = color;
+  }
+  
+  /**
+   * Returns the angle (in degrees) the label text is rotated by.
+   */
+  public double getAngle() {
+    return angleDegr;
+  }
+
+  /**
+   * Returns the angle (in radian) the label text is rotated by.
+   */
+  public double getAngleRadian() {
+    return angleDegr * Math.PI / 180;
+  }
+
+  /**
+   * Sets the angle (in degrees) the label text is rotated by.
+   * @param color Color for the label text
+   */
+  public void setAngle(double angle) {
+    this.angleDegr = angle;
+  }
+  
+}

Added: trunk/src/schmitzm/jfree/chart/style/ChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyle.java	2009-06-22 17:15:45 UTC (rev 160)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyle.java	2009-06-22 19:28:46 UTC (rev 161)
@@ -0,0 +1,148 @@
+/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM)
+
+    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
+    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+    You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
+
+    Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
+    Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
+    Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
+ **/
+
+package schmitzm.jfree.chart.style;
+
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.Plot;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.data.general.Dataset;
+
+/**
+ * This interface is a general super class for a design style of a {@link JFreeChart}.
+ * Sub classes should read the style from an xml element or something like that.
+ * Maybe there could be a general implementation representing a style for
+ * basic charts (bar, line and area charts) and special implementations for
+ * special chart types (e.g. financial, statistical oder spider charts).
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
+ * @version 1.0
+ */
+public interface ChartStyle {
+  /**
+   * Enum representing the style type of a chart (bar, line, area)
+   */
+  public enum ChartType {
+    /** Represents all bar chart styles */
+    BAR,
+    /** Represents all line chart styles */
+    LINE,
+    /** Represents all area chart styles */
+    AREA,
+    /** Represents all point chart styles */
+    POINT,
+    /** Represents all pie chart styles */
+    PIE,
+    /** Represents all gantt chart styles */
+    GANTT,
+    /** Represents all timeseries chart styles */
+    TIMESERIES, 
+    /** Represents all spider chart styles */
+    SPIDER,
+    /** Represents all scatter chart styles */
+    SCATTER
+  };
+  
+  /** Constant for the domain axis (X). */
+  public static final int DOMAIN_AXIS = 0;
+  /** Constant for the range axis (Y). */
+  public static final int RANGE_AXIS  = 1;
+  /** Constant for the second range axis (Y). */
+  public static final int RANGE_AXIS2 = 2;
+
+  /**
+   * Returns the type of the chart style.
+   */
+  public ChartType getType();
+
+  /**
+   * Returns the orientation of the chart.
+   * @see PlotOrientation#HORIZONTAL
+   * @see PlotOrientation#VERTICAL
+   */
+  public PlotOrientation getOrientation();
+  
+  /**
+   * Sets the orientation of the chart.
+   * @param orientation the orientation of the chart
+   */
+  public void setOrientation(PlotOrientation orientation);
+
+  /**
+   * Returns the style of the chart title.
+   */
+  public ChartLabelStyle getTitleStyle();
+
+  /**
+   * Sets the title for the chart.
+   * @param titleStyle the new chart title style
+   */
+  public void setTitleStyle(ChartLabelStyle titleStyle);
+
+  /**
+   * Returns whether the chart is configured to generate a legend.
+   */
+  public boolean hasLegend();
+
+  /**
+   * Sets whether the chart is configured to generate a legend.
+   */
+  public void setLegend(boolean legend);
+
+  /**
+   * Returns whether the chart is configured to generate tooltips for the data.
+   */
+  public boolean hasTooltips();
+
+  /**
+   * Sets whether the chart is configured to generate tooltips for the data.
+   */
+  public void setTooltips(boolean tooltips);
+
+  /**
+   * Returns whether the chart is configured to generate URLs.
+   */
+  public boolean hasURLs();
+
+  /**
+   * Sets whether the chart is configured to generate URLs.
+   */
+  public void setURLs(boolean urls);
+
+  /**
+   * Returns count of axis of the chart.
+   */
+  public int getAxisCount();
+
+  /**
+   * Returns the style of a chart axis.<br>
+   * 0 = domain axis (X)<br>
+   * 1 = first (right) range axis (Y)<br>
+   * 2 = second (left) range axis (Y) if available<br>
+   */
+  public ChartAxisStyle getAxisStyle(int axis);
+
+  /**
+   * Sets the style of a chart axis.
+   * @param axis axis number (0=domain, 1=range, ...)
+   * @param style style for the axis
+   */
+  public void setAxisStyle(int axis, ChartAxisStyle style);
+
+  /**
+   * Generates a chart using the given data and the style represented by
+   * this class.
+   * @param dataset the data for the chart
+   * @exception UnsupportedOperationException if the style can not be applied
+   *            to the given dataset
+   */
+  public JFreeChart applyToDataset(Dataset dataset);
+
+}

Added: trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java	2009-06-22 17:15:45 UTC (rev 160)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleUtil.java	2009-06-22 19:28:46 UTC (rev 161)
@@ -0,0 +1,187 @@
+/** SCHMITZM - This file is part of the java library of Martin O.J. Schmitz (SCHMITZM)
+
+    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
+    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+    You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
+
+    Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
+    Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
+    Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
+ **/
+
+package schmitzm.jfree.chart.style;
+
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
+import java.text.SimpleDateFormat;
+
+import org.jdom.Element;
+import org.jfree.chart.plot.PlotOrientation;
+
+
+import schmitzm.jfree.chart.style.ChartStyle.ChartType;
+import schmitzm.swing.SwingUtil;
+
+/**
+ * This class contains static utility methods related to chart styling.
+ * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
+ * @version 1.0
+ */
+public class ChartStyleUtil {
+  
+  /**
+   * Gets the attribute value from element.
+   * @param element  element where the attribute is determined from
+   * @param attrName name of the attribute
+   * @param defValue default value returned if attribute is not found (or empty)
+   */
+  private static String getAttributeValue(Element element, String attrName, String... defValue) {
+    String attrValue = element.getAttributeValue(attrName);
+    if ( defValue.length >= 1 && (attrValue == null || "".equals(attrValue)) )
+      return defValue[0];
+    return attrValue;
+  }
+
+  
+  public static ChartStyle createStyleFromXML(Element element) {
+    if (!"style".equals(element.getName()))
+      throw new IllegalArgumentException("Element is no style element: "+element.getName());
+    
+    // Determine the chart type
+    String               typeStr   = getAttributeValue(element, "type");
+    ChartStyle.ChartType chartType = null;
+    if ("area".equalsIgnoreCase(typeStr))
+      chartType = ChartType.AREA;
+    else if ("bar".equalsIgnoreCase(typeStr))
+      chartType = ChartType.BAR;
+    else if ("line".equalsIgnoreCase(typeStr))
+      chartType = ChartType.LINE;
+    else if ("point".equalsIgnoreCase(typeStr))
+      chartType = ChartType.POINT;
+    else if ("pie".equalsIgnoreCase(typeStr))
+      chartType = ChartType.PIE;
+    else if ("gantt".equalsIgnoreCase(typeStr))
+      chartType = ChartType.GANTT;
+    else if( "timeseries".equalsIgnoreCase( typeStr ))
+      chartType = ChartType.TIMESERIES;
+    else if( "spider".equalsIgnoreCase( typeStr ))
+      chartType = ChartType.SPIDER;
+    else if( "scatter".equalsIgnoreCase( typeStr ))
+      chartType = ChartType.SCATTER;
+    else
+      throw new UnsupportedOperationException("Style style not supported: "+typeStr);
+   
+    // create a style according to the type
+    ChartStyle chartStyle = createDefaultChartStyle(chartType);
+    
+    // common attributes
+    chartStyle.setLegend(   "true".equalsIgnoreCase( getAttributeValue(element,"legend","true") ) );
+    chartStyle.setTooltips( "true".equalsIgnoreCase( getAttributeValue(element,"tooltips","true") ) );
+    chartStyle.setURLs(     "true".equalsIgnoreCase( getAttributeValue(element,"urls","true") ) );
+
+    // Plot orientation
+    String orientationStr = getAttributeValue(element,"orientation","horzontal");
+    if ("horizontal".equalsIgnoreCase(orientationStr))
+      chartStyle.setOrientation( PlotOrientation.HORIZONTAL );
+    else if ("vertical".equalsIgnoreCase(orientationStr))
+      chartStyle.setOrientation( PlotOrientation.VERTICAL );
+
+    if ( element.getChild("title") != null );
+      chartStyle.setTitleStyle( createTitleStyleFromXML(element.getChild("title") ) );
+    if ( element.getChild("domainAxis") != null );
+      chartStyle.setAxisStyle( ChartStyle.DOMAIN_AXIS,createAxisStyleFromXML(element.getChild("domainAxis") ) );
+    if ( element.getChild("rangeAxis") != null );
+      chartStyle.setAxisStyle( ChartStyle.RANGE_AXIS,createAxisStyleFromXML(element.getChild("rangeAxis") ) );
+    if ( element.getChild("rangeAxis2") != null );
+      chartStyle.setAxisStyle( ChartStyle.RANGE_AXIS2,createAxisStyleFromXML(element.getChild("rangeAxis2") ) );
+
+    return chartStyle;
+  }
+  
+  /**
+   * Creates a default style for a chart type.
+   * @param type a chart type
+   */
+  public static ChartStyle createDefaultChartStyle(ChartType type) {
+    switch ( type ) {
+      case AREA:
+      case LINE:
+      case POINT: break;
+    }
+    throw new UnsupportedOperationException("Style for this chart type not yet supported: "+type);
+  }
+  
+  /**
+   * Reads the {@link ChartLabelStyle} properties from an XML element.
+   * <ul>
+   *   <li>Attribute "paint": color of the label</li>
+   *   <li>Attribute "angle": angle of the label</li>
+   *   <li>Child element "label": the label content</li>
+   * </ul>
+   * @param element
+   * @param style
+   */
+  private static void applyLabelStyleFromXML(Element element, ChartLabelStyle style) {
+    // color in attribute
+    String colorStr = getAttributeValue(element, "paint");
+    if ( colorStr != null )
+      style.setPaint( SwingUtil.parseColor(colorStr) );
+    
+    // angle in attribute
+    String angleStr = getAttributeValue(element, "angle");
+    if ( angleStr != null )
+      style.setAngle( Double.parseDouble(angleStr) );
+    
+    // label in child element <label>
+    Element labelElem = element.getChild("label");
+    if ( labelElem != null )
+      style.setLabel( labelElem.getTextTrim() ); 
+    
+  }
+  
+  /**
+   * Creates a {@link ChartLabelStyle} from XML element.
+   * @param element an element
+   */
+  public static ChartLabelStyle createTitleStyleFromXML(Element element) {
+    ChartLabelStyle style = new ChartLabelStyle();
+    
+    // read the common ChartLabelStyle properties
+    applyLabelStyleFromXML(element,style);
+    
+    return style;
+  }
+  
+  /**
+   * Creates a {@link ChartLabelStyle} from XML element.
+   * @param element an element
+   */
+  public static ChartAxisStyle createAxisStyleFromXML(Element element) {
+    ChartAxisStyle style = new ChartAxisStyle();
+    
+    // read the common ChartLabelStyle properties
+    applyLabelStyleFromXML(element,style);
+    
+    // read "visible" property from attribute
+    String visibleStr = getAttributeValue(element, "visible");
+    if ( visibleStr != null )
+      style.setVisible( "true".equalsIgnoreCase(visibleStr) );
+
+    // values format and angle from child element <valueLabels>
+    Element valuesElem = element.getChild("valueLabels");
+    if ( valuesElem != null ) {
+      String angleStr = getAttributeValue(valuesElem, "angle");
+      if ( angleStr != null )
+        style.setValuesAngle( Double.parseDouble(angleStr) );
+      String formatStr = getAttributeValue(valuesElem, "numberFormat");
+      if ( formatStr != null )
+        style.setValuesFormat(new DecimalFormat(formatStr) ); 
+      formatStr = getAttributeValue(valuesElem, "dateFormat");
+      if ( formatStr != null )
+        style.setValuesFormat(new SimpleDateFormat(formatStr) ); 
+    }
+    
+    return style;
+  }
+  
+}



More information about the Schmitzm-commits mailing list