[Schmitzm-commits] r2270 - in trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree: . chart

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon Mar 4 17:45:08 CET 2013


Author: mojays
Date: 2013-03-04 17:45:08 +0100 (Mon, 04 Mar 2013)
New Revision: 2270

Modified:
   trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/JFreeChartUtil.java
   trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/ChartMouseSelectionTracker.java
Log:
ChartMouseSelectionTracker: BugFix to handle transformation between screen and chart coordinates
JFreeChartUtil: methods to extend chart legend

Modified: trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/JFreeChartUtil.java
===================================================================
--- trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/JFreeChartUtil.java	2013-03-03 14:50:47 UTC (rev 2269)
+++ trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/JFreeChartUtil.java	2013-03-04 16:45:08 UTC (rev 2270)
@@ -246,8 +246,37 @@
   	  legends.setPosition(RectangleEdge.BOTTOM);
   	  chart.addSubtitle(legends);
   	}
+
+  	/**
+  	 * Extends the legend of a chart with the legend items of another chart.
+  	 * @param sourceLegend     legend to extend
+  	 * @param additionalLegend items to extends {@code sourceLegend} with
+  	 */
+  	public static void extendLegendItemSource(LegendTitle sourceLegend, LegendTitle additionalLegend) {
+  	  LegendItemSource[] sourceLegendItemSources = sourceLegend != null ? sourceLegend.getSources() : null;
+  	  LegendItemSource[] additionalLegendItemSources = additionalLegend != null ? additionalLegend.getSources() : null;
+
+  	  // if there are no new legend items, nothing has to be changed
+  	  if ( additionalLegendItemSources == null )
+  	    return;
+      
+  	  LegendItemSource[] newLegendItemSources = additionalLegendItemSources;
+  	  if ( sourceLegendItemSources != null )
+  	    newLegendItemSources = LangUtil.extendArray(sourceLegendItemSources, additionalLegendItemSources);
+  	  
+  	  sourceLegend.setSources(newLegendItemSources);
+  	}
   	
     /**
+     * Extends the (primary) legend of a chart with the (primary) legend items of another chart.
+     * @param sourceChart      chart to extend the (primary) legend for
+     * @param additionalLegend items to extends {@code sourceLegend} with
+     */
+    public static void extendLegendItemSource(JFreeChart sourceChart, JFreeChart additionalLegend) {
+      extendLegendItemSource(sourceChart.getLegend(), additionalLegend.getLegend());
+    }
+
+    /**
      * When all datasets for a range axis are empty, JFreeChart
      * shows a default range with tick labels between 0.0 and 1.0.
      * That looks not nice for us (especially Stefan!). We (Stefan) want

Modified: trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/ChartMouseSelectionTracker.java
===================================================================
--- trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/ChartMouseSelectionTracker.java	2013-03-03 14:50:47 UTC (rev 2269)
+++ trunk/schmitzm-jfree/src/main/java/de/schmitzm/jfree/chart/ChartMouseSelectionTracker.java	2013-03-04 16:45:08 UTC (rev 2270)
@@ -29,6 +29,7 @@
  ******************************************************************************/
 package de.schmitzm.jfree.chart;
 
+import java.awt.Point;
 import java.awt.geom.Point2D;
 import java.awt.geom.Rectangle2D;
 import java.util.HashSet;
@@ -50,6 +51,7 @@
  * @author <a href="mailto:Martin.Schmitz at koeln.de">Martin Schmitz</a>
  */
 public abstract class ChartMouseSelectionTracker extends MouseSelectionTracker {
+  
   /** Holds the panel connected to this listener. */
   protected ChartPanel chartPanel = null;
   
@@ -80,8 +82,18 @@
    */
   @Override
   protected void selectionPerformed(int ox, int oy, int px, int py) {
+    // Transform screen coordinates (of selection) to coordinates in
+    // ChartPanel
+    Point2D oPointTransformed = chartPanel.translateScreenToJava2D(new Point(ox,oy));
+    Point2D pPointTransformed = chartPanel.translateScreenToJava2D(new Point(px,py));
+    ox = (int)Math.round(oPointTransformed.getX());
+    oy = (int)Math.round(oPointTransformed.getY());
+    px = (int)Math.round(pPointTransformed.getX());
+    py = (int)Math.round(pPointTransformed.getY());
+    
+    
     // Create rectangle for selection window
-    Rectangle2D selectedArea = new Rectangle2D.Double(
+    Rectangle2D selectedArea = new Rectangle2D.Double( 
         Math.min(ox,px),
         Math.min(oy,py),
         Math.abs(ox-px),
@@ -96,7 +108,7 @@
     if ( selectedPoint == null &&
          SelectionMode.ONLY_SINGLE_CLICK.equals(getSelectionMode()) )
        return;
-
+    
     // Check every chart data points if the selection area
     // contains it (or it contains the selected point)
     HashSet<ChartEntity> selectedEntities = new HashSet<ChartEntity>();



More information about the Schmitzm-commits mailing list