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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Jul 13 21:05:17 CEST 2009


Author: mojays
Date: 2009-07-13 21:05:15 +0200 (Mon, 13 Jul 2009)
New Revision: 215

Added:
   trunk/src/schmitzm/jfree/chart/style/ScatterChartStyle.java
   trunk/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java
Modified:
   trunk/src/schmitzm/jfree/JFreeChartUtil.java
   trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
   trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java
   trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
Log:
new ScatterChartStyle.java

Modified: trunk/src/schmitzm/jfree/JFreeChartUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/JFreeChartUtil.java	2009-07-13 17:30:41 UTC (rev 214)
+++ trunk/src/schmitzm/jfree/JFreeChartUtil.java	2009-07-13 19:05:15 UTC (rev 215)
@@ -280,10 +280,25 @@
    */
   public static JFreeChart createRegressionChart(FeatureCollection fc, String title, String xAttr, String yAttr, boolean regressionLine) {
     XYSeriesCollection  dataset = createXYDataset(fc, title, xAttr, yAttr);
+    return createRegressionChart(dataset, title, xAttr, yAttr, regressionLine);
+  }
+  
+  /**
+   * Creates a {@link JFreeChart} which shows a point for each data iten and
+   * a the appropriate regression line.
+   * @param dataset        the data
+   * @param title          title for the chart
+   * @param xAxisTitle     title for the X-Axis
+   * @param yAxisTitle     title for the Y-Axis
+   * @param regressionLine indicates whether the regression line is shown
+   */
+  public static JFreeChart createRegressionChart(XYDataset dataset, String title, String xAxisTitle, String yAxisTitle, boolean regressionLine) {
+    if ( regressionLine && !(dataset instanceof XYSeriesCollection) )
+      throw new UnsupportedOperationException("Regression line can only be created for XYSeriesCollection!");
     
-    NumberAxis xAxis   = new NumberAxis(xAttr);
+    NumberAxis xAxis   = new NumberAxis(xAxisTitle);
     xAxis.setAutoRangeIncludesZero(false);
-    NumberAxis yAxis   = new NumberAxis(yAttr);
+    NumberAxis yAxis   = new NumberAxis(yAxisTitle);
     yAxis.setAutoRangeIncludesZero(false);
 
     XYPlot             plot             = new XYPlot(dataset, xAxis, yAxis, null);
@@ -304,14 +319,14 @@
     // Regression line
     if ( regressionLine ) {
       // create sample data for curve (plot function directly is not yet available)
-      XYDataset regressionData = createRegressionLineDataset(dataset,0,title+" (RegressionLine)",2);
+      XYDataset regressionData = createRegressionLineDataset((XYSeriesCollection)dataset,0,title+" (RegressionLine)",2);
       addRegressionLineToPlot(plot, regressionData, Color.blue);
     }
     JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, plot, true);
 
     return chart; //ChartFactory.createScatterPlot(title, xAttr, yAttr, dataset, PlotOrientation.HORIZONTAL, true, true, true);
   }
-  
+
   /**
    * Creates sample data for a regression line of an {@link XYDataset}.
    * @param dataset    data the regression line is created for

Modified: trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java	2009-07-13 17:30:41 UTC (rev 214)
+++ trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java	2009-07-13 19:05:15 UTC (rev 215)
@@ -27,6 +27,9 @@
 import org.jfree.chart.plot.CategoryPlot;
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.AbstractRenderer;
+import org.jfree.chart.renderer.category.CategoryItemRenderer;
+import org.jfree.chart.renderer.xy.XYItemRenderer;
 import org.jfree.chart.title.TextTitle;
 import org.jfree.chart.urls.CategoryURLGenerator;
 import org.jfree.chart.urls.StandardCategoryURLGenerator;
@@ -345,14 +348,32 @@
       plot.getRenderer().setURLGenerator( hasURLs() ? URLGEN_XY : null );
       if ( getOrientation() != null )
         plot.setOrientation( getOrientation() );
+      // style renderer
+      for (int i=0; i<plot.getRendererCount(); i++) {
+        XYItemRenderer     renderer      = plot.getRenderer(i);
+        ChartRendererStyle rendererStyle = getRendererStyle(i);
+        if ( renderer != null && rendererStyle != null )
+          rendererStyle.applyToRenderer((AbstractRenderer)renderer);
+      }
     } else if ( chart.getPlot() instanceof CategoryPlot ) {
       CategoryPlot plot = chart.getCategoryPlot();
       plot.getRenderer().setBaseToolTipGenerator( hasTooltips() ? TIPGEN_CAT : null );
       plot.getRenderer().setBaseItemURLGenerator( hasURLs() ? URLGEN_CAT : null );
       if ( getOrientation() != null )
         plot.setOrientation( getOrientation() );
+      // style renderer
+      for (int i=0; i<plot.getRendererCount(); i++) {
+        CategoryItemRenderer renderer      = plot.getRenderer(i);
+        ChartRendererStyle   rendererStyle = getRendererStyle(i);
+        if ( renderer != null && rendererStyle != null )
+          rendererStyle.applyToRenderer((AbstractRenderer)renderer);
+      }
     }
     
+    // style plot
+    if ( getPlotStyle() != null )
+      getPlotStyle().applyToPlot(chart.getPlot());
+
     // Style the legend
     JFreeChartUtil.setLegendVisible(chart, hasLegend());
     

Modified: trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java	2009-07-13 17:30:41 UTC (rev 214)
+++ trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java	2009-07-13 19:05:15 UTC (rev 215)
@@ -203,8 +203,6 @@
   /**
    * Applies the style to an existing chart.
    * @param chart chart the style is applied to
-   * @exception UnsupportedOperationException if the style can not be applied
-   *            to the given dataset
    */
   public void applyToChart(JFreeChart chart) {
     super.applyToChart(chart);

Modified: trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java	2009-07-13 17:30:41 UTC (rev 214)
+++ trunk/src/schmitzm/jfree/chart/style/ChartStyleXMLFactory.java	2009-07-13 19:05:15 UTC (rev 215)
@@ -315,15 +315,15 @@
         style.setDomainGridlinePaint(color);
     }
 
-    // domain grid line color and visibility
-    Element rangeGridElem = element.getChild("domainGridline");
+    // range grid line color and visibility
+    Element rangeGridElem = element.getChild("rangeGridline");
     if ( rangeGridElem != null ) {
       Boolean visible = XMLUtil.getBooleanAttribute(rangeGridElem, "visible");
       if ( visible != null )
-        style.setDomainGridlineVisible(visible);
+        style.setRangeGridlineVisible(visible);
       Paint color = XMLUtil.getColorAttribute(rangeGridElem, "paint");
       if ( color != null )
-        style.setDomainGridlinePaint(color);
+        style.setRangeGridlinePaint(color);
     }
 
     return style;

Added: trunk/src/schmitzm/jfree/chart/style/ScatterChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ScatterChartStyle.java	2009-07-13 17:30:41 UTC (rev 214)
+++ trunk/src/schmitzm/jfree/chart/style/ScatterChartStyle.java	2009-07-13 19:05:15 UTC (rev 215)
@@ -0,0 +1,111 @@
+/** 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 org.jfree.chart.JFreeChart;
+import org.jfree.chart.renderer.xy.XYItemRenderer;
+import org.jfree.data.general.Dataset;
+import org.jfree.data.xy.XYDataset;
+import org.jfree.data.xy.XYSeriesCollection;
+
+import schmitzm.jfree.JFreeChartUtil;
+import schmitzm.lang.LangUtil;
+
+/**
+ * This class provides a chart style for scatter chart, which shows the data as
+ * points. Besides the points it is possible to show a regression line ("line of best
+ * fit") for the data points.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class ScatterChartStyle extends AbstractChartStyle {
+  /** Stores whether a regression line ("line of best fit") for the
+   *  data points is shown. */
+  protected boolean regrLineVisible = true;
+
+  /**
+   * Creates a scatter chart style a regression line shown.
+   * @param id   a (unique) ID for the style
+   */
+  public ScatterChartStyle(String id) {
+    super(id, ChartType.SCATTER);
+  }
+  
+  /**
+   * Returns whether a regression line is shown.
+   */
+  public boolean isRegressionLineVisible() {
+    return regrLineVisible;
+  }
+  
+  /**
+   * Sets whether a regression line is shown.
+   */
+  public void setRegressionLineVisible(boolean visible) {
+    this.regrLineVisible = visible;
+  }
+  
+
+  /**
+   * Creates a scatter chart for the given {@link Dataset} and applies the style.
+   * @param dataset the data for the chart
+   * @exception UnsupportedOperationException if the style can not be applied
+   *            to the given dataset
+   */
+  @Override
+  public JFreeChart applyToDataset(Dataset dataset) {
+    if ( !(dataset instanceof XYDataset) )
+      throw new UnsupportedOperationException("ScatterChartStyle can only be applied to XYDataset: "+LangUtil.getSimpleClassName(dataset));
+
+    // create default scatter chart
+    JFreeChart chart = JFreeChartUtil.createRegressionChart(
+        (XYDataset)dataset, "", "", "", isRegressionLineVisible()
+    );
+    
+    // apply style
+    applyToChart(chart);
+    return chart;
+  }
+
+  /**
+   * Applies the style to an existing chart.
+   * @param chart chart the style is applied to
+   * @exception UnsupportedOperationException if the style can not be applied
+   *            to the given dataset
+   */
+  public void applyToChart(JFreeChart chart) {
+    if ( chart.getXYPlot() == null )
+      throw new UnsupportedOperationException("ScatterChartStyle can only be applied to XYPlot: "+LangUtil.getSimpleClassName(chart.getPlot()));
+
+    // Create regression line if not already existing
+    if ( isRegressionLineVisible() && chart.getXYPlot().getRendererCount() < 2 ) {
+      // Create sample data for regression line for primary dataset (primary series)
+      // -> plot function directly is not yet available in JFreeChart
+      XYSeriesCollection dataset         = (XYSeriesCollection)chart.getXYPlot().getDataset();
+      String             regressionTitle = getTitleStyle().getLabel() + " (RegressionLine)";
+      XYDataset          regressionData  = JFreeChartUtil.createRegressionLineDataset(
+                                                 dataset, 0, regressionTitle, 2);
+      // Add regression line to plot (with default color)
+      JFreeChartUtil.addRegressionLineToPlot(chart.getXYPlot(), regressionData, Color.blue);
+    }
+
+    // apply "normal" style properties
+    super.applyToChart(chart);
+
+    // apply visiblity of regression line
+    XYItemRenderer regrLineRenderer = chart.getXYPlot().getRenderer(1);
+    if ( regrLineRenderer != null )
+      regrLineRenderer.setSeriesVisible(0, isRegressionLineVisible() );
+  }
+
+}

Added: trunk/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java	2009-07-13 17:30:41 UTC (rev 214)
+++ trunk/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java	2009-07-13 19:05:15 UTC (rev 215)
@@ -0,0 +1,98 @@
+/** 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.feature.style;
+
+import org.geotools.feature.FeatureCollection;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.data.general.Dataset;
+
+import schmitzm.jfree.JFreeChartUtil;
+import schmitzm.jfree.chart.style.BasicChartStyle;
+import schmitzm.jfree.chart.style.ScatterChartStyle;
+
+/**
+ * This class extends the {@link ScatterChartStyle} with the properties
+ * of the {@link FeatureChartStyle}.<br>
+ * After instantiation the 2 feature attributes used to define the chart
+ * data must be set by {@link #setAttributeName(int, String)}. Without
+ * setting the attributes the style can not be applied to a feature collection!
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class FeatureScatterChartStyle extends ScatterChartStyle implements FeatureChartStyle {
+  /** Used to maintain the {@link FeatureChartStyle} properties. */
+  protected Dummy dummyFeatureChartStyle = null;
+  
+  /**
+   * Creates a scatter chart style a regression line shown.
+   * @param id   a (unique) ID for the style
+   */
+  public FeatureScatterChartStyle(String id) {
+    super(id);
+  }
+
+  /**
+   * Returns the attribute count needed to specify the chart data
+   * from feature collection.
+   * @return always 2
+   */
+  @Override
+  public int getAttributeCount() {
+    return dummyFeatureChartStyle.getAttributeCount();
+  }
+
+  /**
+   * Returns the name of a feature attribute needed to create a
+   * chart for this style.
+   * @param idx axis index
+   * @see ChartStyle#DOMAIN_AXIS
+   * @see ChartStyle#RANGE_AXIS
+   * @see ChartStyle#RANGE_AXIS2
+   */
+  public String getAttributeName(int idx) {
+    return dummyFeatureChartStyle.getAttributeName(idx);
+  }
+
+  /**
+   * Sets the name of a feature attribute needed to create a
+   * chart for this style.
+   * @param idx axis index
+   * @param attrName feature attribute name 
+   * @see ChartStyle#DOMAIN_AXIS
+   * @see ChartStyle#RANGE_AXIS
+   * @see ChartStyle#RANGE_AXIS2
+   */
+  public void setAttributeName(int idx, String attrName) {
+    dummyFeatureChartStyle.setAttributeName(idx,attrName);
+  }
+
+  /**
+   * Creates an appropriate {@link Dataset} for the attributes defined
+   * by this style (according to the attributes types in the given
+   * {@link FeatureCollection}) and calls {@link #applyToDataset(Dataset)}.
+   * @see schmitzm.jfree.feature.style.FeatureChartStyle#applyToFeatureCollection(org.geotools.feature.FeatureCollection)
+   */
+  @Override
+  public JFreeChart applyToFeatureCollection(FeatureCollection fc) {
+    // TODO: Check the attribute data and create a CategoryDataset if
+    //       a non-numeric column is specified
+    Dataset dataset = JFreeChartUtil.createXYDataset(
+        fc,
+        getTitleStyle().getLabel(),
+        getAttributeName(0),
+        getAttributeName(1)
+    );
+    return applyToDataset(dataset);
+  }
+
+
+}



More information about the Schmitzm-commits mailing list