[Schmitzm-commits] r286 - trunk/src/schmitzm/jfree/feature/style

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Aug 3 14:45:21 CEST 2009


Author: alfonx
Date: 2009-08-03 14:45:21 +0200 (Mon, 03 Aug 2009)
New Revision: 286

Modified:
   trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java
Log:
* GP-Feature-Charts: Implemented normalization for range axes in category data-sets.

Modified: trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java	2009-08-03 11:51:53 UTC (rev 285)
+++ trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java	2009-08-03 12:45:21 UTC (rev 286)
@@ -83,12 +83,6 @@
 	public static final FeatureChartStyleXMLFactory FEATURE_CHART_STYLE_FACTORY = new FeatureChartStyleXMLFactory();
 
 	/**
-	 * Holds the statistics needed to normalize the attribute values. Key =
-	 * attributeName
-	 */
-	private static HashMap<String,StaticBin1D> attribStats = new HashMap<String, StaticBin1D>();
-
-	/**
 	 * Returns all {@link DatasetSelectionModel DatasetSelectionModels} that can
 	 * be reached via the renderers of a chart.
 	 * 
@@ -252,53 +246,9 @@
 		for (int i = 1; i < xySeries.length; i++)
 			dataset.addSeries(xySeries[i]);
 		
-		// NORMALIZATION:
-		// We have to create a descriptive statistic of all items before we can
-		// transform the first one. So we iterate over all items before we do
-		// optional sorting and insertion.
-		{
+		// Calculate any statistics needed for normalization. 
+		HashMap<String,StaticBin1D> statisticsForNormalization = calcStatisticsForNormalization(fc, chartStyle);
 
-			// First check if any attribute needs normalization. If not we can
-			// skip the whole iteration
-			boolean doNormalization = false;
-			for (int attrIdx = 0; attrIdx < attrCount; attrIdx++) {
-				if (chartStyle.isAttributeNormalized(attrIdx)) {
-					doNormalization = true;
-					break;
-				}
-			}
-			
-			if (doNormalization) {
-				final FeatureIterator fIt = fc.features();
-				// Loop over all features, and collect the attribute values we are interested in.
-				while (fIt.hasNext()){
-					Feature f = fIt.next();
-
-					for (int attrIdx = 0; attrIdx < attrCount; attrIdx++) {
-	
-					
-						if (chartStyle.isAttributeNormalized(attrIdx)) {
-							String attrName = chartStyle.getAttributeName(attrIdx);
-							StaticBin1D stat = attribStats.get(attrName);
-							if (stat == null){
-								stat = new DynamicBin1D();
-								attribStats.put(attrName, stat);
-							}
-//							// TODO: here also filtering the several NULL aliases and do not
-//							// insert them into the statistics calculation
-//							// if ( yValue is a NULL alias )
-//							// yValue = null;
-							final Double doubleValue = new Double(((Number) f.getAttribute(attrName)).doubleValue());
-							stat.add(doubleValue);				
-//							LOGGER.debug("Adding "+doubleValue+" and sd = "+stat.standardDeviation()+" and mean = "+stat.mean());				
-						}
-					}
-				} // Featuer iterator
-			} // doNormlization
-
-		} // Normalization stuff block
-		
-
 		// If dataset should be sorted, the features must be sorted first
 		// according to the domain attribute. The "autoSort" functionality
 		// of XYSeries can NOT BE USED because of the following reason:
@@ -339,11 +289,11 @@
 
 				/* Normalization of a range axis value */
 				if (chartStyle.isAttributeNormalized(attrIdx))
-					yValue = normalize(yValue, yAttrName);
+					yValue = normalize(yValue, yAttrName, statisticsForNormalization);
 				
 				/* Normalization of the domain axis value (if they are not handled as categories) */
 				if (chartStyle.isAttributeNormalized(ChartStyle.DOMAIN_AXIS))
-					xValue = normalize(xValue, xAttrName);
+					xValue = normalize(xValue, xAttrName, statisticsForNormalization);
 
 				xySeries[attrIdx].add(xValue, yValue);
 
@@ -358,16 +308,84 @@
 	}
 
 	/**
+	 * Calculates statistics needed to normalize data. If normalization is not
+	 * used, this function returns an empty map.
+	 * 
+	 * @param fc
+	 *            {@link FeatureCollection} where the data comes from
+	 * @param chartStyle
+	 *            {@link ChartStyle} to determine which attributes shall be
+	 *            normalized.
+	 */
+	public static HashMap<String,StaticBin1D> calcStatisticsForNormalization(FeatureCollection fc,
+			FeatureChartStyle chartStyle) {
+		// NORMALIZATION:
+		// We have to create a descriptive statistic of all items before we can
+		// transform the first one. So we iterate over all items before we do
+		// optional sorting and insertion.
+			/**
+			 * Holds the statistics needed to normalize the attribute values. Key =
+			 * attributeName
+			 */
+			HashMap<String,StaticBin1D> attribStats = new HashMap<String, StaticBin1D>();
+
+
+			// First check if any attribute needs normalization. If not we can
+			// skip the whole iteration
+			boolean doNormalization = false;
+			for (int attrIdx = 0; attrIdx < chartStyle.getAttributeCount(); attrIdx++) {
+				if (chartStyle.isAttributeNormalized(attrIdx)) {
+					doNormalization = true;
+					break;
+				}
+			}
+			
+			if (doNormalization) {
+				final FeatureIterator fIt = fc.features();
+				// Loop over all features, and collect the attribute values we are interested in.
+				while (fIt.hasNext()){
+					Feature f = fIt.next();
+
+					for (int attrIdx = 0; attrIdx < chartStyle.getAttributeCount(); attrIdx++) {
+	
+					
+						if (chartStyle.isAttributeNormalized(attrIdx)) {
+							String attrName = chartStyle.getAttributeName(attrIdx);
+							StaticBin1D stat = attribStats.get(attrName);
+							if (stat == null){
+								stat = new DynamicBin1D();
+								attribStats.put(attrName, stat);
+							}
+//							// TODO: here also filtering the several NULL aliases and do not
+//							// insert them into the statistics calculation
+//							// if ( yValue is a NULL alias )
+//							// yValue = null;
+							final Double doubleValue = new Double(((Number) f.getAttribute(attrName)).doubleValue());
+							stat.add(doubleValue);				
+//							LOGGER.debug("Adding "+doubleValue+" and sd = "+stat.standardDeviation()+" and mean = "+stat.mean());				
+						}
+					}
+				} // Featuer iterator
+			} // doNormlization
+			
+			return attribStats;
+	}
+
+	/**
 	 * Do a z-transformation on the data item. 
+	 * 
+	 * @param statisticsForNormalization The statistics needed for normalization.   
+	 * 
+	 * @see #calcStatisticsForNormalization(FeatureCollection, FeatureChartStyle, int)
 	 */
-	private static Number normalize(Number yValue, String attrName) {
+	private static Number normalize(Number yValue, String attrName, HashMap<String,StaticBin1D> statisticsForNormalization) {
 		Number zValue;
 
-		if (attribStats.get(attrName) == null) {
+		if (statisticsForNormalization.get(attrName) == null) {
 			throw new IllegalArgumentException("The statistics needed for the normlization need to be created before normalizing.");
 		}
 
-		StaticBin1D stats = attribStats.get(attrName);
+		StaticBin1D stats = statisticsForNormalization.get(attrName);
 
 		zValue = (yValue.doubleValue() - stats.mean())
 				/ stats.standardDeviation();
@@ -406,6 +424,10 @@
 		for (int i = 1; i < attrCount; i++)
 			checkAttributeType(fc.getSchema(), chartStyle.getAttributeName(i),
 					Number.class, "Range attribute", "CategoryDataset");
+		
+		
+		// Calculate any statistics needed for normalization. 
+		HashMap<String,StaticBin1D> statisticsForNormalization = calcStatisticsForNormalization(fc, chartStyle);
 
 		// Create a new dataset and insert the series
 		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
@@ -439,6 +461,11 @@
 			for (int attrIdx = 1; attrIdx < attrCount; attrIdx++) {
 				String yAttrName = chartStyle.getAttributeName(attrIdx);
 				Number yValue = (Number) f.getAttribute(yAttrName);
+				
+				/* Normalization of a range axis value */
+				if (chartStyle.isAttributeNormalized(attrIdx))
+					yValue = normalize(yValue, yAttrName, statisticsForNormalization);
+	
 				// // TODO: here maybe filtering the several NULL aliases
 				// if ( yValue is a NULL alias )
 				// yValue = null;
@@ -447,7 +474,7 @@
 				// if ( yValue == null )
 				// continue;
 
-				/* Note: Normalization does not make sense for a category dataset! */
+				/* Note: Normalization does not make sense for a DOMAIN axis in a category dataset! */
 
 				// Add data to dataset
 				dataset.addValue(yValue, yAttrName, catValue);



More information about the Schmitzm-commits mailing list