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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sat Aug 1 12:59:20 CEST 2009


Author: mojays
Date: 2009-08-01 12:59:19 +0200 (Sat, 01 Aug 2009)
New Revision: 264

Modified:
   trunk/src/schmitzm/jfree/chart/style/ScatterChartStyle.java
   trunk/src/schmitzm/jfree/feature/style/FeatureBasicChartStyle.java
   trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java
   trunk/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java
Log:
some structural changes in dataset generation

Modified: trunk/src/schmitzm/jfree/chart/style/ScatterChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/ScatterChartStyle.java	2009-07-31 19:35:11 UTC (rev 263)
+++ trunk/src/schmitzm/jfree/chart/style/ScatterChartStyle.java	2009-08-01 10:59:19 UTC (rev 264)
@@ -58,7 +58,7 @@
   public ScatterChartStyle(String id) {
     super(id, ChartType.SCATTER);
   }
-  
+
   /**
    * Returns whether a regression line is shown.
    */

Modified: trunk/src/schmitzm/jfree/feature/style/FeatureBasicChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/feature/style/FeatureBasicChartStyle.java	2009-07-31 19:35:11 UTC (rev 263)
+++ trunk/src/schmitzm/jfree/feature/style/FeatureBasicChartStyle.java	2009-08-01 10:59:19 UTC (rev 264)
@@ -184,13 +184,10 @@
      */
     @Override
     public JFreeChart applyToFeatureCollection(FeatureCollection fc) {
-        //    Dataset dataset = null; // TODO: Create dataset from FeatureCollection by utility method
-        Dataset dataset = FeatureChartUtil.createXYDataset(
-            fc,
-            isSortDomainAxis(),
-            getAttributeName(DOMAIN_AXIS),
-            getAttributeName(RANGE_AXIS)
-        );
-        return applyToDataset(dataset);
+      Dataset dataset = FeatureChartUtil.createXYDataset(
+          fc,
+          this
+      );  
+      return applyToDataset(dataset);
     }
 }

Modified: trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java	2009-07-31 19:35:11 UTC (rev 263)
+++ trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java	2009-08-01 10:59:19 UTC (rev 264)
@@ -162,30 +162,37 @@
   
   /**
    * Creates a {@link XYDataset} for 2 (or more) attributes of a {@link FeatureCollection}.
+   * XYDateset can only be created for <b>numeric</b> attributes.
    * @param fc    a {@link FeatureCollection}
-   * @param seriesKey   a (unique) key for the {@link XYSeries} in the dataset
-   * @param xAttr feature attribute used for the X-value
-   * @param yAttr feature attribute(s) used for the Y-value (for each a series
-   *              is created in the dataset)
+   * @param style defines the attributes used to create the dataset from, as well
+   *              as the sorting and normalization properties
+   * @throws IllegalArgumentException if less then 2 attributes are specified
+   * @throws UnsupportedOperationException if attributes are not numeric
    */
-  public static XYSeriesCollection createXYDataset(FeatureCollection fc, boolean sort, String xAttr, String... yAttr) {
+  public static XYSeriesCollection createXYDataset(FeatureCollection fc, FeatureChartStyle chartStyle) {
+    int attrCount = chartStyle.getAttributeCount();
+    if ( attrCount < 2 )
+      throw new IllegalArgumentException("FeatureChartStyle must define at least 2 attributes to create XYDataset: "+attrCount);
+    String xAttrName = chartStyle.getAttributeName(0);
     // check data types of X attribute
-    checkAttributeNumeric(fc.getSchema(), xAttr, "Domain attribute", "XYDataset");
+    checkAttributeNumeric(fc.getSchema(), xAttrName, "Domain attribute", "XYDataset");
     // check data types of Y attribute
-    for (String attr : yAttr)
-      checkAttributeNumeric(fc.getSchema(), attr, "Range attribute", "XYDataset");
+    for (int i=1; i<attrCount; i++)
+      checkAttributeNumeric(fc.getSchema(), chartStyle.getAttributeName(i), "Range attribute", "XYDataset");
     
-    // Create a series for each range attribute
-    XYSeries[] xySeries = new XYSeries[yAttr.length];
-    for (int i=0; i<xySeries.length; i++)
-      xySeries[i] = new XYSeries(yAttr[i], false, true); // IMPORTANT: Do not use autosort!!
+    // Create a series for each range attribute (note: the 0
+    // array element useless, but it is more comfortable to
+    // use an array of the same size as "attributes"
+    XYSeries[] xySeries = new XYSeries[attrCount];
+    for (int i=1; i<xySeries.length; i++)
+      xySeries[i] = new XYSeries(chartStyle.getAttributeName(i), false, true); // IMPORTANT: Do not use autosort!!
     
     // Create a new dataset and insert the series
     XYSeriesCollection           dataset   = new XYSeriesCollection();
     Feature2SeriesDatasetMapping mapping   = new Feature2SeriesDatasetMapping(fc,dataset);
     dataset.setGroup( new FeatureDatasetMetaData(mapping) );
-    for (XYSeries series : xySeries)
-      dataset.addSeries(series);
+    for (int i=1; i<xySeries.length; i++)
+      dataset.addSeries(xySeries[i]);
 
     // If dataset should be sorted, the features must be sorted first
     // according to the domain attribute. The "autoSort" functionality 
@@ -199,23 +206,24 @@
     // Only solution: Pre-sort the features so that the XYSeries internal
     //                order does not change!
     Iterator<Feature> fi = null; 
-    if ( sort ) {
-      Vector<Feature> sortedFeatures = FeatureUtil.sortFeatures(fc.features(), xAttr);
+    if ( chartStyle.isSortDomainAxis() ) {
+      Vector<Feature> sortedFeatures = FeatureUtil.sortFeatures(fc.features(), xAttrName);
       // Create an Iterator for the sorted Features
       fi = sortedFeatures.iterator();
     } else
       fi = new PipedFeatureIterator( fc.features() );
     
     // Iterate the FeatureCollection and fill the series 
-    for (int i=0; fi.hasNext(); i++) {
+    for (int fIdx=0; fi.hasNext(); fIdx++) {
       Feature f = fi.next();
       // Determine X value (NULL not permitted for XYDateset!)
-      Number xValue = (Number)f.getAttribute(xAttr);
+      Number xValue = (Number)f.getAttribute(xAttrName);
       if ( xValue == null )
         continue;
       // Determine the Y values and fill the series
-      for (int attrIdx=0; attrIdx<yAttr.length; attrIdx++) {
-        Number yValue = (Number)f.getAttribute(yAttr[attrIdx]);
+      for (int attrIdx=1; attrIdx<attrCount; attrIdx++) {
+        String yAttrName = chartStyle.getAttributeName(attrIdx);
+        Number yValue    = (Number)f.getAttribute(yAttrName);
 //      // TODO: here maybe filtering the several NULL aliases
 //      if ( yValue is a NULL alias )
 //        yValue = null;
@@ -225,7 +233,7 @@
         xySeries[attrIdx].add(xValue, yValue);
 
         // Mapping between FID and data index in series
-        mapping.setMapping(f.getID(), yAttr[attrIdx], i);
+        mapping.setMapping(f.getID(), yAttrName, fIdx);
 
         //LOGGER.debug(f.getID() + " --> "+i);
       }
@@ -233,22 +241,4 @@
     }
     return dataset;
   }
-  
-  /**
-   * Creates a {@link JFreeChart} which shows a point for each feature and
-   * a the appropriate regression line.
-   * @param fc      a {@link FeatureCollection}
-   * @param title   title for the chart (also used as key for the {@link XYSeries})
-   * @param xAttr   feature attribute used for the X-value
-   * @param yAttr   feature attribute used for the Y-value
-   * @param regressionLine indicates whether the regression line is shown
-   */
-  public static JFreeChart createRegressionChart(FeatureCollection fc, String title, String xAttr, String yAttr, boolean regressionLine) {
-    XYSeriesCollection  dataset = createXYDataset(fc, false, xAttr, yAttr);
-    return JFreeChartUtil.createRegressionChart(dataset, title, xAttr, yAttr, regressionLine);
-  }
-
-
-
-
 }

Modified: trunk/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java	2009-07-31 19:35:11 UTC (rev 263)
+++ trunk/src/schmitzm/jfree/feature/style/FeatureScatterChartStyle.java	2009-08-01 10:59:19 UTC (rev 264)
@@ -59,6 +59,20 @@
   }
 
   /**
+   * Creates a scatter chart style a regression line shown.
+   * @param id         a (unique) ID for the style
+   * @param title      title for the chart
+   * @param xAttrName  feature attribute used for domain axis
+   * @param yAttrName  feature attribute used for range axis
+   */
+  public FeatureScatterChartStyle(String id, String title, String xAttrName, String yAttrName) {
+    this(id);
+    setAttributeName(0, xAttrName);
+    setAttributeName(1, yAttrName);
+    getTitleStyle().setLabel(title);
+  }
+
+  /**
    * Returns the maximum number of feature attributes that can be
    * specified by this style.
    * @return always 2
@@ -177,9 +191,7 @@
     //       a non-numeric column is specified
     Dataset dataset = FeatureChartUtil.createXYDataset(
         fc,
-        isSortDomainAxis(),
-        getAttributeName(0),
-        getAttributeName(1)
+        this
     );
     return applyToDataset(dataset);
   }



More information about the Schmitzm-commits mailing list