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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Aug 26 11:43:13 CEST 2010


Author: mojays
Date: 2010-08-26 11:43:12 +0200 (Thu, 26 Aug 2010)
New Revision: 988

Added:
   trunk/src/schmitzm/jfree/chart/FilteredLegendItemSource.java
Modified:
   trunk/src/schmitzm/jfree/JFreeChartUtil.java
   trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
   trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java
Log:
Legend for 2-axes charts are automatically split in two parts.

Modified: trunk/src/schmitzm/jfree/JFreeChartUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/JFreeChartUtil.java	2010-08-26 08:20:04 UTC (rev 987)
+++ trunk/src/schmitzm/jfree/JFreeChartUtil.java	2010-08-26 09:43:12 UTC (rev 988)
@@ -404,6 +404,36 @@
     
     
     /**
+     * Returns range axis count from a plot.
+     * @param plot a plot
+     */
+    public static int getRangeAxisCountFromPlot(Plot plot) {
+      if ( plot instanceof XYPlot )
+        return ((XYPlot)plot).getRangeAxisCount();
+      if ( plot instanceof CategoryPlot )
+        return ((CategoryPlot)plot).getRangeAxisCount();
+      if ( plot instanceof PiePlot )
+        return 0;
+      LOGGER.warn("Could not determine range axis count for plot: "+LangUtil.getSimpleClassName(plot));
+      return 0;
+    }
+
+    /**
+     * Returns domain axis count from a plot.
+     * @param plot a plot
+     */
+    public static int getDomainAxisCountFromPlot(Plot plot) {
+      if ( plot instanceof XYPlot )
+        return ((XYPlot)plot).getDomainAxisCount();
+      if ( plot instanceof CategoryPlot )
+        return ((CategoryPlot)plot).getDomainAxisCount();
+      if ( plot instanceof PiePlot )
+        return 0;
+      LOGGER.warn("Could not determine domain axis count for plot: "+LangUtil.getSimpleClassName(plot));
+      return 0;
+    }
+
+    /**
      * Returns dataset count from a plot.
      * @param plot a plot
      */

Added: trunk/src/schmitzm/jfree/chart/FilteredLegendItemSource.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/FilteredLegendItemSource.java	2010-08-26 08:20:04 UTC (rev 987)
+++ trunk/src/schmitzm/jfree/chart/FilteredLegendItemSource.java	2010-08-26 09:43:12 UTC (rev 988)
@@ -0,0 +1,88 @@
+package schmitzm.jfree.chart;
+
+import org.jfree.chart.LegendItem;
+import org.jfree.chart.LegendItemCollection;
+import org.jfree.chart.LegendItemSource;
+
+import schmitzm.jfree.JFreeChartUtil;
+
+/**
+ * This {@link LegendItemSource} tunnels an other {@link LegendItemSource} or
+ * a {@link LegendItemCollection} and filters out some {@link LegendItem LegendItems}
+ * by the prefix of their {@linkplain LegendItem#getLabel() labels}.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class FilteredLegendItemSource implements LegendItemSource {
+  /** Holds the filtered legend items. */
+  protected LegendItemCollection filteredLegendItems = null;
+  /** Holds the prefix the original legend items are filtered with. */
+  protected String filterPrefix = null;
+  
+  /**
+   * Creates a new legend item source.
+   * @param legendItems   original legend items
+   * @param filterPrefix  all items with this prefix are filtered out (can be {@code null})
+   */
+  public FilteredLegendItemSource(LegendItemCollection legendItems, String filterPrefix) {
+    this.filterPrefix        = filterPrefix;
+    this.filteredLegendItems = filterLegendItems(legendItems,filterPrefix);
+  }
+  
+  /**
+   * Creates a new legend item source. {@link JFreeChartUtil#DUMMY_SERIES_PREFIX} is
+   * used as filter prefix.
+   * @param legendItems   original legend items 
+   */
+  public FilteredLegendItemSource(LegendItemCollection legendItems) {
+    this(legendItems,JFreeChartUtil.DUMMY_SERIES_PREFIX);
+  }
+
+  /**
+   * Creates a new legend item source. {@link JFreeChartUtil#DUMMY_SERIES_PREFIX} is
+   * used as filter prefix.
+   * @param lis holds the original legend items 
+   * @param filterPrefix  all items with this prefix are filtered out (can be {@code null})
+   */
+  public FilteredLegendItemSource(LegendItemSource lis, String filterPrefix) {
+    this(lis.getLegendItems(), filterPrefix);
+  }
+
+  /**
+   * Creates a new legend item source. {@link JFreeChartUtil#DUMMY_SERIES_PREFIX} is
+   * used as filter prefix.
+   * @param lis holds the original legend items 
+   */
+  public FilteredLegendItemSource(LegendItemSource lis) {
+    this(lis.getLegendItems());
+  }
+  
+  /**
+   * Returns the filtered legend items.  
+   */
+  @Override
+  public LegendItemCollection getLegendItems() {
+    return filteredLegendItems;
+  }
+
+  /**
+   * Filters the items of a {@link LegendItemCollection} by a prefix string
+   * of their labels.
+   * @param legendItems   original legend items
+   * @param filterPrefix  all items with this prefix are filtered out (can be {@code null})
+   * @return the original {@link LegendItemCollection} if {@code filterPrefix} is {@code null}
+   * @see LegendItem#getLabel()
+   */
+  public static LegendItemCollection filterLegendItems(LegendItemCollection legendItems, String filterPrefix) {
+    if ( legendItems == null || filterPrefix == null )
+      return legendItems;
+    
+    LegendItemCollection filteredLegendItems = new LegendItemCollection();
+    for (int j=0; j<legendItems.getItemCount(); j++) {
+      LegendItem lItem = legendItems.get(j);
+      if (!lItem.getLabel().startsWith(filterPrefix))
+        filteredLegendItems.add(lItem);
+    }
+    
+    return filteredLegendItems;
+  }
+}
\ No newline at end of file

Modified: trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java	2010-08-26 08:20:04 UTC (rev 987)
+++ trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java	2010-08-26 09:43:12 UTC (rev 988)
@@ -46,8 +46,15 @@
 
 import org.apache.log4j.Category;
 import org.jfree.chart.JFreeChart;
+import org.jfree.chart.LegendItem;
+import org.jfree.chart.LegendItemCollection;
+import org.jfree.chart.LegendItemSource;
 import org.jfree.chart.axis.Axis;
 import org.jfree.chart.axis.NumberAxis;
+import org.jfree.chart.block.BlockBorder;
+import org.jfree.chart.block.BlockContainer;
+import org.jfree.chart.block.BorderArrangement;
+import org.jfree.chart.block.EmptyBlock;
 import org.jfree.chart.labels.CategoryToolTipGenerator;
 import org.jfree.chart.labels.PieSectionLabelGenerator;
 import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
@@ -61,6 +68,8 @@
 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.CompositeTitle;
+import org.jfree.chart.title.LegendTitle;
 import org.jfree.chart.title.TextTitle;
 import org.jfree.chart.urls.CategoryURLGenerator;
 import org.jfree.chart.urls.PieURLGenerator;
@@ -68,8 +77,11 @@
 import org.jfree.chart.urls.StandardPieURLGenerator;
 import org.jfree.chart.urls.StandardXYURLGenerator;
 import org.jfree.chart.urls.XYURLGenerator;
+import org.jfree.ui.RectangleEdge;
+import org.jfree.ui.RectangleInsets;
 
 import schmitzm.jfree.JFreeChartUtil;
+import schmitzm.jfree.chart.FilteredLegendItemSource;
 import schmitzm.lang.LangUtil;
 import skrueger.geotools.CopyableUtil;
 
@@ -589,6 +601,34 @@
       else
         LOGGER.warn("Style contains no style definition for axis "+i);
     }
+    
+    // Split the legend when multiple range axis are used
+    if ( JFreeChartUtil.getRangeAxisCountFromPlot(chart.getPlot()) > 1 ) {
+      chart.removeLegend();
+      LegendItemSource lis1 = new FilteredLegendItemSource(
+          (LegendItemSource)JFreeChartUtil.getRendererForDataset(chart.getPlot(), 0)
+      );
+      LegendTitle legend1 = new LegendTitle(lis1);
+      legend1.setWidth(100);
+      legend1.setMargin(new RectangleInsets(2, 2, 2, 2));
+      legend1.setFrame(new BlockBorder());
+
+      LegendItemSource lis2 = new FilteredLegendItemSource(
+          (LegendItemSource)JFreeChartUtil.getRendererForDataset(chart.getPlot(), 1)
+      );
+      LegendTitle legend2 = new LegendTitle(lis2);
+      legend2.setMargin(new RectangleInsets(2, 2, 2, 2));
+      legend2.setFrame(new BlockBorder());
+
+      BlockContainer container = new BlockContainer(new BorderArrangement());
+      container.add(legend1, RectangleEdge.LEFT);
+      container.add(legend2, RectangleEdge.RIGHT);
+      container.add(new EmptyBlock(2000, 0));
+      CompositeTitle legends = new CompositeTitle(container);
+      legends.setPosition(RectangleEdge.BOTTOM);
+      chart.addSubtitle(legends);
+    }
+
   }
   
   /**

Modified: trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java	2010-08-26 08:20:04 UTC (rev 987)
+++ trunk/src/schmitzm/jfree/chart/style/BasicChartStyle.java	2010-08-26 09:43:12 UTC (rev 988)
@@ -43,6 +43,7 @@
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.LegendItem;
 import org.jfree.chart.LegendItemCollection;
+import org.jfree.chart.LegendItemSource;
 import org.jfree.chart.plot.CategoryPlot;
 import org.jfree.chart.plot.Plot;
 import org.jfree.chart.plot.PlotOrientation;
@@ -296,31 +297,33 @@
     //       effect the new (special stacked/stepped) renderer!!
     super.applyToChart(chart);
     
-    
-    // Workaround (aehnlich zu JFreeChart-Demo "DualAxisDemo5"):
-    // Bei Bar-Charts mit mehreren Achsen wurden Dummy-Series
-    // in die Datasets eingefuegt. Diese sollen nicht in der Legende
-    // erscheinen.
-    // -> Legende in diesem Fall manuell setzen
-    int datasetCount = JFreeChartUtil.getDatasetCountFromPlot(chart.getPlot());
-    if ( chart.getPlot() instanceof CategoryPlot &&
-         datasetCount > 1 &&
-         ChartType.BAR.equals(getType()) ) {
-      LegendItemCollection newLegendItems = new LegendItemCollection();
-      CategoryPlot         cPlot          = chart.getCategoryPlot();
-      int                  rendererCount  = cPlot.getRendererCount();
-      LegendItemCollection legendItems    = cPlot.getLegendItems();
-      
-      for (int j=0; j<legendItems.getItemCount(); j++) {
-        LegendItem lItem = legendItems.get(j);
-        if (!lItem.getLabel().startsWith(JFreeChartUtil.DUMMY_SERIES_PREFIX))
-          newLegendItems.add(lItem);
-      }
-      cPlot.setFixedLegendItems(newLegendItems);
-    }
+//--> fuer Charts mit 2 Achsen wird die Legende in "AbstractChartStyle"
+//    gesplittet, hierbei wird die Standard-Legende ersetzt und die  
+//    Eintraege fuer die Dummy-Series bereits entfernt!
+//--> hier nicht mehr notwendig!
+//    // Workaround (aehnlich zu JFreeChart-Demo "DualAxisDemo5"):
+//    // Bei Bar-Charts mit mehreren Achsen wurden Dummy-Series
+//    // in die Datasets eingefuegt. Diese sollen nicht in der Legende
+//    // erscheinen.
+//    // -> Legende in diesem Fall manuell setzen
+//    int datasetCount = JFreeChartUtil.getDatasetCountFromPlot(chart.getPlot());
+//    if ( chart.getPlot() instanceof CategoryPlot &&
+//         datasetCount > 1 &&
+//         ChartType.BAR.equals(getType()) ) {
+//      LegendItemCollection newLegendItems = new LegendItemCollection();
+//      CategoryPlot         cPlot          = chart.getCategoryPlot();
+//      int                  rendererCount  = cPlot.getRendererCount();
+//      LegendItemCollection legendItems    = cPlot.getLegendItems();
+//      
+//      for (int j=0; j<legendItems.getItemCount(); j++) {
+//        LegendItem lItem = legendItems.get(j);
+//        if (!lItem.getLabel().startsWith(JFreeChartUtil.DUMMY_SERIES_PREFIX))
+//          newLegendItems.add(lItem);
+//      }
+//      cPlot.setFixedLegendItems(newLegendItems);
+//    }
 
   }
-  
 
   /**
    * Creates a default line chart from an {@link XYDataset} or {@link CategoryDataset}



More information about the Schmitzm-commits mailing list