[Schmitzm-commits] r984 - in trunk: src/schmitzm/jfree/feature/style src_junit/schmitzm/jfree/feature/style

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Aug 25 12:55:58 CEST 2010


Author: mojays
Date: 2010-08-25 12:55:58 +0200 (Wed, 25 Aug 2010)
New Revision: 984

Modified:
   trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java
   trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java
Log:
BugFix: redefineSeriesLegendTitle now also deals correctly with multiple renderers/dataset/axis

Modified: trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java	2010-08-23 11:51:39 UTC (rev 983)
+++ trunk/src/schmitzm/jfree/feature/style/FeatureChartUtil.java	2010-08-25 10:55:58 UTC (rev 984)
@@ -344,7 +344,7 @@
               // determine the series index for the current series key
               int seriesIdx = JFreeChartUtil.getSeriesIndexFromDataset(dataset,yAttrName);
               // overwrite the series legend label in chart style
-              redefineSeriesLegendTitle(chartStyle, seriesIdx, f);
+              redefineSeriesLegendTitle(chartStyle, seriesIdx, f, attrIdx);
             }
 
             // Mapping between FID and data index in series
@@ -630,7 +630,7 @@
 
     return zValue;
   }
-
+static int a = 10000;
   /**
    * Creates a {@link CategoryDataset} for 2 (or more) attributes of a
    * {@link FeatureCollection}. CategoryDataset can only be created for
@@ -695,6 +695,7 @@
     // createXYDataset(..))
     FeatureIterator<SimpleFeature> features = null;
     Iterator<SimpleFeature> fi = null;
+    Iterator<SimpleFeature> fi2 = null;
     if (chartStyle.isSortDomainAxis()) {
       // Sorting attribute(s) -> Category attribute
       String[] sortAttr = new String[] {xAttrName};
@@ -706,11 +707,13 @@
       Vector<SimpleFeature> sortedFeatures = FeatureUtil.sortFeatures(fc,sortAttr);
       // Create an Iterator for the sorted Features
       fi = sortedFeatures.iterator();
+      fi2 = sortedFeatures.iterator();
     } else {
       features = fc.features();
       fi = new PipedFeatureIterator(features);
+      fi2 = new PipedFeatureIterator(features);
     }
-
+    
     // Iterate the FeatureCollection and fill the dataset
     try {
       for (int fIdx = 0; fi.hasNext(); fIdx++) {
@@ -758,7 +761,7 @@
             // to the series ID
             if ( seriesID != null )
               yAttrName = yAttrName + "_" + seriesID.toString();
-            dataset.addValue(yValue, yAttrName, catValue);
+            dataset.setValue(yValue, yAttrName, catValue);
 
             //#########################
             // Bei Bar-Charts mit mehreren Achsen ueberlagern sich
@@ -775,7 +778,7 @@
             if ( datasets.length > 1 && ChartType.BAR.equals(chartStyle.getType()) ) {
               for (DefaultCategoryDataset d : datasets)
                 if ( d != dataset ) {
-                  d.addValue(null, JFreeChartUtil.DUMMY_SERIES_PREFIX+yAttrName, catValue);
+                  d.setValue(null, JFreeChartUtil.DUMMY_SERIES_PREFIX+yAttrName, catValue);
                 }
             }
             //#########################
@@ -786,7 +789,7 @@
               // determine the series index for the current series key
               int seriesIdx = JFreeChartUtil.getSeriesIndexFromDataset(dataset,yAttrName);
               // overwrite the series legend label in chart style
-              redefineSeriesLegendTitle(chartStyle, seriesIdx, f);
+              redefineSeriesLegendTitle(chartStyle, seriesIdx, f, attrIdx);
             }
             
             // Mapping between FID and data index in series
@@ -1010,9 +1013,10 @@
    * @param chartStyle the chart style to update the legend title in
    * @param seriesIdx  the series index in the {@link ChartRendererStyle} to update
    * @param f          the feature the legend label is taken from
+   * @param attrIdx    the attribute index in the {@link ChartStyle}
    * @see FeatureChartStyle#getSeriesLegendTitleAttributeName()
    */
-  private static boolean redefineSeriesLegendTitle(FeatureChartStyle chartStyle, int seriesIdx, SimpleFeature f) {
+  private static boolean redefineSeriesLegendTitle(FeatureChartStyle chartStyle, int seriesIdx, SimpleFeature f, int attrIdx) {
     if ( chartStyle.getSeriesLegendTitleAttributeName() == null )
       return false;
     
@@ -1025,19 +1029,22 @@
     if ( "".equals(seriesLabelStr) )
       return false;
     
+    // Determine the renderer (primary or secondary range axis!) for
+    // the series
+    int                rendID = determineDatasetIndexForAttribute(chartStyle, attrIdx);
+    ChartRendererStyle rendStyle = chartStyle.getRendererStyle(rendID);
     // Create a renderer style if it does not exist yet
-    if ( chartStyle.getRendererStyle(0) == null )
-      chartStyle.setRendererStyle(0, new ChartRendererStyle());
+    if ( rendStyle == null ) {
+      rendStyle = new ChartRendererStyle();
+      chartStyle.setRendererStyle(rendID, rendStyle);
+    }
     // Do not change the label if the existing label equals the
     // new label
-    if ( seriesLabel.equals( chartStyle.getRendererStyle(0).getSeriesLegendLabel(seriesIdx) ) )
+    if ( seriesLabel.equals( rendStyle.getSeriesLegendLabel(seriesIdx) ) )
       return false;
      
     // redefine the legend label for the given series
-    chartStyle.getRendererStyle(0).setSeriesLegendLabel(
-                                                        seriesIdx,
-                                                        new ChartLabelStyle(seriesLabelStr)   
-    );
+    rendStyle.setSeriesLegendLabel( seriesIdx, new ChartLabelStyle(seriesLabelStr) );
     return true;
   }
   
@@ -1168,13 +1175,24 @@
   }
 
   /**
-   * Determines the dataset 
+   * Determines the dataset for an attribute of the style.
    * @param chartStyle the chart style which defines the attribute
    * @param attrIdx    the attribute for which the dataset should be
    *                   determined
    * @param datasets   the available datasets
    */
   private static <D extends Dataset> D determineDatasetForAttribute(FeatureChartStyle chartStyle, int attrIdx, D[] datasets) {
+    int datasetIdx = determineDatasetIndexForAttribute(chartStyle, attrIdx);
+    return datasets[datasetIdx];
+  }
+
+  /**
+   * Determines the dataset index for an attribute of the style.
+   * @param chartStyle the chart style which defines the attribute
+   * @param attrIdx    the attribute for which the dataset should be
+   *                   determined
+   */
+  private static int determineDatasetIndexForAttribute(FeatureChartStyle chartStyle, int attrIdx) {
     int axis = chartStyle.getAttributeAxis(attrIdx);
     int datasetIdx = 0;
     switch ( axis ) {
@@ -1188,8 +1206,9 @@
         throw new UnsupportedOperationException("Unknown range axis: "+axis);
     }
     
-    return datasets[datasetIdx];
+    return datasetIdx;
   }
+
   /**
    * Determines the range attribute value from a feature and transforms it
    * according to the (optional) aggregation function or normalization.

Modified: trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java
===================================================================
--- trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java	2010-08-23 11:51:39 UTC (rev 983)
+++ trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java	2010-08-25 10:55:58 UTC (rev 984)
@@ -28,6 +28,8 @@
 import org.jfree.chart.axis.NumberTickUnit;
 import org.jfree.chart.axis.TickUnits;
 import org.jfree.chart.axis.ValueTick;
+import org.jfree.chart.plot.CategoryPlot;
+import org.jfree.chart.plot.DatasetRenderingOrder;
 import org.jfree.chart.renderer.category.BarRenderer;
 import org.jfree.data.category.CategoryDataset;
 import org.jfree.data.category.DefaultCategoryDataset;
@@ -308,7 +310,7 @@
 
 	@Test
 	public void testKRECKbarChart() throws IOException, InterruptedException {
-		String titleAttName = "title";
+		String titleAttName = "jahr";
 		String xAxisAttName = "code";
 		String yAxisAttName = "dm_u3";
 		String yAxisAttName2 = "w_bev";
@@ -337,14 +339,14 @@
 		FeatureBasicChartStyle chartStyle = new FeatureBasicChartStyle(
 				"keckBarChart", ChartType.BAR);
 
+		// Domain Axis
 		chartStyle.setAttributeName(0, xAxisAttName);
+        chartStyle.setAxisStyle(0, xAxisStyle);
+		// Range Axis
 		chartStyle.setAttributeName(1, yAxisAttName);
 		chartStyle.setSeriesAttributeName(groupAttName);
-
-		chartStyle.setAxisStyle(0, xAxisStyle);
 		chartStyle.setAxisStyle(1, yAxisStyle);
-
-
+		// Range Axis 2
 		chartStyle.setAxisStyle(2, yAxisStyle2);
 		chartStyle.setAttributeName(2, yAxisAttName2);
 		chartStyle.setAttributeAxis(2, ChartStyle.RANGE_AXIS2);
@@ -354,10 +356,11 @@
 		chartStyle.setForceCategories(true);
 
 		// Farben aus Palette zuweisen
-		ChartRendererStyle chartRendererStyle = new ChartRendererStyle();
+        ChartRendererStyle chartRendererStyle = new ChartRendererStyle();
+        ChartRendererStyle chartRendererStyle2 = new ChartRendererStyle();
+        chartStyle.setRendererStyle(0, chartRendererStyle);
+        chartStyle.setRendererStyle(1, chartRendererStyle2);
 
-		chartStyle.setRendererStyle(0, chartRendererStyle);
-
 		// Hintergrund auf weiss stellen
 		ChartPlotStyle chartPlotStyle = new ChartPlotStyle();
 		chartPlotStyle.setBackgroundPaint(Color.white);
@@ -365,9 +368,10 @@
 		chartPlotStyle.setRangeGridlinePaint(Color.darkGray);
 		chartStyle.setPlotStyle(chartPlotStyle);
 
-		chartStyle.setLegend(false);
+//		chartStyle.setLegend(false);
 
 		JFreeChart barChart = chartStyle.applyToFeatureCollection(features);
+//		((CategoryPlot)barChart.getPlot()).setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
 
 		// Maximale Breite der Bars einstellen
 		BarRenderer renderer = (BarRenderer) barChart.getCategoryPlot()



More information about the Schmitzm-commits mailing list