[Schmitzm-commits] r991 - in trunk: doc src/schmitzm/jfree src/schmitzm/jfree/chart src/schmitzm/jfree/chart/style src/schmitzm/jfree/table/style src_junit/schmitzm/jfree/feature/style

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Aug 26 19:05:37 CEST 2010


Author: mojays
Date: 2010-08-26 19:05:35 +0200 (Thu, 26 Aug 2010)
New Revision: 991

Added:
   trunk/src/schmitzm/jfree/chart/StyledToolTipGenerator.java
Modified:
   trunk/doc/Chart style XML structure.pdf
   trunk/src/schmitzm/jfree/JFreeChartUtil.java
   trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
   trunk/src/schmitzm/jfree/table/style/TableChartAxisStyle.java
   trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java
Log:
- XML documentation updated
- "nicer" tooltips automatically generated with axis label and series legend title

Modified: trunk/doc/Chart style XML structure.pdf
===================================================================
(Binary files differ)

Modified: trunk/src/schmitzm/jfree/JFreeChartUtil.java
===================================================================
--- trunk/src/schmitzm/jfree/JFreeChartUtil.java	2010-08-26 12:08:35 UTC (rev 990)
+++ trunk/src/schmitzm/jfree/JFreeChartUtil.java	2010-08-26 17:05:35 UTC (rev 991)
@@ -467,7 +467,49 @@
       return 0;
     }
 
+    public static int getDatasetIndex(Plot plot, Dataset dataset) {
+      int idx = -1;
+      if ( plot instanceof CategoryPlot ) {
+        CategoryPlot catPlot = (CategoryPlot)plot;
+        for (int i=0; i<catPlot.getDatasetCount(); i++)
+          if ( dataset == catPlot.getDataset(i) ) {
+            idx = i;
+            break;
+          }
+      } else if ( plot instanceof XYPlot ) {
+        XYPlot xyPlot = (XYPlot)plot;
+        for (int i=0; i<xyPlot.getDatasetCount(); i++)
+          if ( dataset == xyPlot.getDataset(i) ) {
+            idx = i;
+            break;
+          }
+      } else if ( plot instanceof PiePlot ) {
+        PiePlot piePlot = (PiePlot)plot;
+        if ( dataset == piePlot.getDataset() )
+          idx = 0;
+      } else
+        LOGGER.warn("Plot does not contain dataset!");
+      
+      return idx;
+    }
+    
     /**
+     * Returns range axis count from a plot.
+     * @param plot a plot
+     */
+    public static ValueAxis getRangeAxisForDataset(Plot plot, Dataset dataset) {
+      int idx = getDatasetIndex(plot, dataset);
+      
+      if ( plot instanceof XYPlot )
+        return ((XYPlot)plot).getRangeAxisForDataset(idx);
+      if ( plot instanceof CategoryPlot )
+        return ((CategoryPlot)plot).getRangeAxisForDataset(idx);
+      
+      LOGGER.warn("Could not determine range axis for plot: "+LangUtil.getSimpleClassName(plot));
+      return null;
+    }
+
+    /**
      * Returns domain axis count from a plot.
      * @param plot a plot
      */
@@ -501,11 +543,13 @@
      * Returns the dataset from a plot.
      * @param plot a plot
      */
-    public static Dataset getDatasetFromPlot(Plot plot) {
+    public static Dataset getDatasetFromPlot(Plot plot, int... datasetIdx) {
+      int idx = datasetIdx.length > 0 ? datasetIdx[0] : 0;
+      
       if ( plot instanceof XYPlot )
-        return ((XYPlot)plot).getDataset();
+        return ((XYPlot)plot).getDataset(idx);
       if ( plot instanceof CategoryPlot )
-        return ((CategoryPlot)plot).getDataset();
+        return ((CategoryPlot)plot).getDataset(idx);
       if ( plot instanceof PiePlot )
         return ((PiePlot)plot).getDataset();
       LOGGER.warn("Could not determine dataset for plot: "+LangUtil.getSimpleClassName(plot));

Added: trunk/src/schmitzm/jfree/chart/StyledToolTipGenerator.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/StyledToolTipGenerator.java	2010-08-26 12:08:35 UTC (rev 990)
+++ trunk/src/schmitzm/jfree/chart/StyledToolTipGenerator.java	2010-08-26 17:05:35 UTC (rev 991)
@@ -0,0 +1,163 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Krüger - additional utility classes
+ ******************************************************************************/
+
+package schmitzm.jfree.chart;
+
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.ValueAxis;
+import org.jfree.chart.labels.CategoryToolTipGenerator;
+import org.jfree.chart.labels.PieToolTipGenerator;
+import org.jfree.chart.labels.XYToolTipGenerator;
+import org.jfree.chart.plot.CategoryPlot;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.category.AbstractCategoryItemRenderer;
+import org.jfree.chart.renderer.xy.XYItemRenderer;
+import org.jfree.data.category.CategoryDataset;
+import org.jfree.data.general.PieDataset;
+import org.jfree.data.xy.XYDataset;
+
+import schmitzm.jfree.JFreeChartUtil;
+import schmitzm.jfree.chart.style.ChartStyle;
+
+/**
+ * A tooltip generator for data items, which uses the axis and series legend label in
+ * the tooltip, instead of the simple "series key", as the standard
+ * tooltip generators do.<br>
+ * Furthermore this tooltip generator can be used for XY-datasets as well
+ * as for category-datasets and pie-datasets. 
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ *
+ */
+public class StyledToolTipGenerator implements CategoryToolTipGenerator,
+    XYToolTipGenerator, PieToolTipGenerator {
+
+  /** Holds the chart to access the axis and legend. */
+  protected JFreeChart chart;
+  /** Holds the chart style to determine axis and legend labels directly
+   *  from the style (currently not used!). */
+  protected ChartStyle chartStyle;
+  
+  /**
+   * Creates a new tooltip generator.
+   * @param chart  the chart to access the axis and legend
+   * @param style  the chart style to determine axis and legend labels directly
+   *               from the style (currently not used!)
+   */
+  public StyledToolTipGenerator(JFreeChart chart, ChartStyle style) {
+    this.chart      = chart;
+    this.chartStyle = style;
+  }
+  
+  /**
+   * Combines the axis label, domain value, series key and range value to
+   * a complete tooltip string.
+   * @param axisLabel   the axis description
+   * @param domainValue the domain value of the data item
+   * @param seriesKey   the series key of the data item
+   * @param rangeValue  the range value of the data item
+   */
+  private String generateToolTip(String axisLabel, Object domainValue, Object seriesKey, Object rangeValue) {
+    StringBuffer sb = new StringBuffer();
+    // Append Range Axis label
+    if ( axisLabel != null && !"".equals(axisLabel.trim()) )
+      sb.append(axisLabel.trim());
+    // Append domain value (X or Category)
+    if ( domainValue != null && !"".equals(domainValue.toString().trim()) ) {
+      if ( sb.length() > 0 )
+        sb.append(", ");
+      sb.append(domainValue.toString().trim());
+    }
+    // Append series
+    if ( seriesKey != null && !"".equals(seriesKey.toString().trim()) ) {
+      if ( sb.length() > 0 )
+        sb.append(", ");
+      sb.append(seriesKey.toString().trim());
+    }
+    // Append range value
+    if ( sb.length() > 0 )
+      sb.append(": ");
+    sb.append(rangeValue);
+    
+    return sb.toString();
+
+  }
+  
+  
+  /**
+   * Generates a tooltip for a data item.
+   * @param dataset the dataset
+   * @param row     the row index (zero-based)
+   * @param col     the column index (zero-based)
+   */
+  @Override
+  public String generateToolTip(CategoryDataset dataset, int row, int col) {
+    CategoryPlot plot = chart.getCategoryPlot();
+    AbstractCategoryItemRenderer rend = (AbstractCategoryItemRenderer)plot.getRendererForDataset(dataset);
+    ValueAxis rangeAxis = JFreeChartUtil.getRangeAxisForDataset(plot, dataset);  
+
+    String rangeAxisLabel = rangeAxis.getLabel();
+    Object domainValue    = dataset.getColumnKey(col);
+    String seriesValue    = rend.getLegendItemLabelGenerator().generateLabel(dataset, row);
+    Object rangeValue     = dataset.getValue(row, col);
+    
+    return generateToolTip(rangeAxisLabel,domainValue,seriesValue,rangeValue);
+  }
+
+  /**
+   * Generates a tooltip for a data item.
+   * @param dataset the dataset
+   * @param series  the series index (zero-based)
+   * @param item    the item index (zero-based)   
+   */
+  @Override
+  public String generateToolTip(XYDataset dataset, int series, int item) {
+    XYPlot plot = chart.getXYPlot();
+    XYItemRenderer rend = (XYItemRenderer)plot.getRendererForDataset(dataset);
+    ValueAxis rangeAxis = JFreeChartUtil.getRangeAxisForDataset(plot, dataset);  
+
+    String rangeAxisLabel = rangeAxis.getLabel();
+    Object domainValue    = dataset.getX(series, item);
+    String seriesValue    = rend.getLegendItemLabelGenerator().generateLabel(dataset, series);
+    Object rangeValue     = dataset.getY(series, item);
+    
+    return generateToolTip(rangeAxisLabel,domainValue,seriesValue,rangeValue);
+  }
+
+  /**
+   * Generates a tooltip for a data item.
+   * @param dataset the dataset
+   * @param key     the pie data key
+   */
+  @Override
+  public String generateToolTip(PieDataset dataset, Comparable key) {
+    throw new UnsupportedOperationException("StyledToolTipGenerator.generateToolTip(PieDataset,Comparable) not yet implemented!");
+  }
+
+}

Modified: trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java	2010-08-26 12:08:35 UTC (rev 990)
+++ trunk/src/schmitzm/jfree/chart/style/AbstractChartStyle.java	2010-08-26 17:05:35 UTC (rev 991)
@@ -82,6 +82,7 @@
 
 import schmitzm.jfree.JFreeChartUtil;
 import schmitzm.jfree.chart.FilteredLegendItemSource;
+import schmitzm.jfree.chart.StyledToolTipGenerator;
 import schmitzm.lang.LangUtil;
 import skrueger.geotools.CopyableUtil;
 
@@ -475,7 +476,8 @@
       // so reduce the renderer count for these properties.
       int rendCount = reducedCountForSpecialStyle(this, plot, plot.getRendererCount());
       for (int i=0; i<rendCount; i++) {
-        plot.getRenderer(i).setBaseToolTipGenerator( hasTooltips() ? TIPGEN_XY : null );
+        plot.getRenderer(i).setBaseToolTipGenerator(
+            hasTooltips() ? new StyledToolTipGenerator(chart,this) : null );
         plot.getRenderer(i).setURLGenerator( hasURLs() ? URLGEN_XY : null );
       }
       if ( getOrientation() != null )
@@ -492,7 +494,8 @@
       CategoryPlot plot = chart.getCategoryPlot();
       int rendCount = reducedCountForSpecialStyle(this, plot, plot.getRendererCount());
       for (int i=0; i<rendCount; i++) {
-        plot.getRenderer(i).setBaseToolTipGenerator( hasTooltips() ? TIPGEN_CAT : null );
+        plot.getRenderer(i).setBaseToolTipGenerator(
+            hasTooltips() ? new StyledToolTipGenerator(chart,this) : null );
         plot.getRenderer(i).setBaseItemURLGenerator( hasURLs() ? URLGEN_CAT : null );
       }
       if ( getOrientation() != null )

Modified: trunk/src/schmitzm/jfree/table/style/TableChartAxisStyle.java
===================================================================
--- trunk/src/schmitzm/jfree/table/style/TableChartAxisStyle.java	2010-08-26 12:08:35 UTC (rev 990)
+++ trunk/src/schmitzm/jfree/table/style/TableChartAxisStyle.java	2010-08-26 17:05:35 UTC (rev 991)
@@ -122,7 +122,7 @@
     String normStr = null; 
     for (int i=0; i<chartStyle.getAxisCount(); i++)
       if ( chartStyle.getAxisStyle(i) == this ) {
-        // ToDo: ??? von welchem Attribut die Aggr.Function und
+        // TODO: ??? von welchem Attribut die Aggr.Function und
         //       Normalisierung verwenden. Ist 'i' richtig?
         if ( chartStyle.getAttributeAggregation(i) != null )
           funcStr = chartStyle.getAttributeAggregation(i).getTitle();

Modified: trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java
===================================================================
--- trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java	2010-08-26 12:08:35 UTC (rev 990)
+++ trunk/src_junit/schmitzm/jfree/feature/style/FeatureChartStyleTest.java	2010-08-26 17:05:35 UTC (rev 991)
@@ -29,6 +29,9 @@
 import org.jfree.chart.axis.NumberTickUnit;
 import org.jfree.chart.axis.TickUnits;
 import org.jfree.chart.axis.ValueTick;
+import org.jfree.chart.labels.CategoryToolTipGenerator;
+import org.jfree.chart.plot.CategoryPlot;
+import org.jfree.chart.renderer.category.AbstractCategoryItemRenderer;
 import org.jfree.chart.renderer.category.BarRenderer;
 import org.jfree.data.category.CategoryDataset;
 import org.jfree.data.category.DefaultCategoryDataset;
@@ -41,6 +44,7 @@
 
 import schmitzm.geotools.styling.TestingUtil2;
 import schmitzm.io.IOUtil;
+import schmitzm.jfree.JFreeChartUtil;
 import schmitzm.jfree.chart.style.BasicChartStyle;
 import schmitzm.jfree.chart.style.ChartAxisStyle;
 import schmitzm.jfree.chart.style.ChartLabelStyle;
@@ -292,7 +296,7 @@
 	@Test
 	public void testKRECKbarChart() throws IOException, InterruptedException {
 		String titleAttName = "jahr";
-		String xAxisAttName = "code";
+		String xAxisAttName = "title"; //"code";
 		String yAxisAttName = "dm_u3";
 		String yAxisAttName2 = "w_bev";
 		String groupAttName = "jahr";
@@ -302,7 +306,7 @@
 
 		log.info("BarChart für " + features.size() + " Features wird erstellt");
 
-		// Fürs testing ausgeben, wie die Werteder featurecollection sind.
+		// Fürs testing ausgeben, wie die Werte der featurecollection sind.
 		{
 			Iterator<SimpleFeature> iterator = features.iterator();
 			while (iterator.hasNext()) {
@@ -314,14 +318,17 @@
 		}
 
 		ChartAxisStyle xAxisStyle = new ChartAxisStyle();
+		xAxisStyle.setLabel("Regionen");
 		ChartAxisStyle yAxisStyle = new ChartAxisStyle();
-		ChartAxisStyle yAxisStyle2 = new ChartAxisStyle();
+        yAxisStyle.setLabel("Deutsch, Männlich unter 3 Jahre");
         yAxisStyle.setValuesFont( SwingUtil.DEFAULT_FONT.deriveFont(20f) );
         yAxisStyle.setValuesPaint( Color.RED );
+		ChartAxisStyle yAxisStyle2 = new ChartAxisStyle();
+        yAxisStyle2.setLabel("Weiblich");
         yAxisStyle2.setValuesFont( SwingUtil.DEFAULT_FONT.deriveFont(8f).deriveFont(Font.BOLD+Font.ITALIC) );
         yAxisStyle2.setValuesPaint( Color.BLUE );
 
-		FeatureBasicChartStyle chartStyle = new FeatureBasicChartStyle(
+		final FeatureBasicChartStyle chartStyle = new FeatureBasicChartStyle(
 				"keckBarChart", ChartType.BAR);
 
 		// Domain Axis
@@ -342,7 +349,11 @@
 
 		// Farben aus Palette zuweisen
         ChartRendererStyle chartRendererStyle = new ChartRendererStyle();
+        chartRendererStyle.setSeriesLegendTooltip(0, new ChartLabelStyle("test"));
+        chartRendererStyle.setDefaultLegendTooltip(new ChartLabelStyle("TEST"));
         ChartRendererStyle chartRendererStyle2 = new ChartRendererStyle();
+        chartRendererStyle2.setSeriesLegendTooltip(0, new ChartLabelStyle("test2"));
+        chartRendererStyle2.setDefaultLegendTooltip(new ChartLabelStyle("TEST2"));
         chartStyle.setRendererStyle(0, chartRendererStyle);
         chartStyle.setRendererStyle(1, chartRendererStyle2);
 
@@ -355,14 +366,14 @@
 
 //		chartStyle.setLegend(false);
 
-		JFreeChart barChart = chartStyle.applyToFeatureCollection(features);
+		final JFreeChart barChart = chartStyle.applyToFeatureCollection(features);
 //		((CategoryPlot)barChart.getPlot()).setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);
+		
+//		// Maximale Breite der Bars einstellen
+//		BarRenderer renderer = (BarRenderer) barChart.getCategoryPlot()
+//				.getRenderer();
+//		renderer.setMaximumBarWidth(0.1);
 
-		// Maximale Breite der Bars einstellen
-		BarRenderer renderer = (BarRenderer) barChart.getCategoryPlot()
-				.getRenderer();
-		renderer.setMaximumBarWidth(0.1);
-
 		if (INTERACTIVE) {
 			ChartFrame chartFrame = new ChartFrame(
 					"KECK, 4 Vergelichregionen, 2 Variablen", barChart);
@@ -376,7 +387,7 @@
 	}
 
 	@Test
-	@Ignore
+//	@Ignore
 	public void testBarChartDualAxis() throws IOException, CQLException,
 			InterruptedException {
 
@@ -675,7 +686,7 @@
 		}
 	}
 
-	@Ignore
+//	@Ignore
 	@Test
 	public void testLineChartAsCategories() throws IOException, CQLException,
 			InterruptedException {
@@ -802,7 +813,7 @@
 				writtenXML, IOUtil.readFileAsString(testFile2));
 	}
 
-	@Ignore
+//	@Ignore
 	@Test
 	public void testLineChartNOTCategories() throws IOException, CQLException,
 			InterruptedException {



More information about the Schmitzm-commits mailing list