[Schmitzm-commits] r1976 - in trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling: . chartsymbols

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed May 2 16:01:20 CEST 2012


Author: keeb
Date: 2012-05-02 16:01:19 +0200 (Wed, 02 May 2012)
New Revision: 1976

Modified:
   trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java
   trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphic.java
   trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphicLegendVisitor.java
Log:


Modified: trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java
===================================================================
--- trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java	2012-05-02 11:43:01 UTC (rev 1975)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java	2012-05-02 14:01:19 UTC (rev 1976)
@@ -1085,52 +1085,15 @@
 					PointSymbolizer pointSymbolizer = (PointSymbolizer) rule
 							.symbolizers().get(0);
 
+
 					ChartGraphic cg = new ChartGraphic(
 							pointSymbolizer.getGraphic());
-
 					{
-						Symbolizer fixSymbolizer = ChartGraphic
-								.getFixDataSymbolizer(pointSymbolizer);
-
-						Double maxValue = cg.getMaxValue();
-
 						// TODO https://trac.wikisquare.de/gp/ticket/79
-						// fixSymbolizer = ChartGraphic.addLegendLabels(
-						// fixSymbolizer, maxValue);
+						 BufferedImage newimage = ChartGraphic.renderLegendImage(pointSymbolizer);
 
-						SimpleFeatureType ftpoint = FeatureUtil
-								.createFeatureType(Point.class);
-
-						int chartLegendIconSize = 80; // pointSymbolizer.getGraphic().getSize().toString()
-														// ?
-						final BufferedImage chartImage = LegendIconFeatureRenderer
-								.getInstance().createImageForSymbolizer(
-										fixSymbolizer,
-										ftpoint,
-										new Dimension(chartLegendIconSize,
-												chartLegendIconSize));
-
-//						// HIER
-//						// WENN horiz-chart
-//						int dX = 50;
-//						BufferedImage newimage = new BufferedImage(
-//								chartImage.getWidth() + dX,
-//								chartImage.getHeight(),
-//								BufferedImage.TYPE_4BYTE_ABGR);
-//						Graphics2D g2 = newimage.createGraphics();
-//
-//						int stringWidth = g2.getFontMetrics().stringWidth("sdsd");
-//												
-//						g2.drawString("Helklo Word", 5, 5);
-//						g2.drawString("" + maxValue, 5, 5);
-//						g2.drawString("" + 0, 5, 5);
-//						g2.drawImage(chartImage, 0, 0, null);
-//						g2.dispose();
-
-//						final JLabel iconLabel = new JLabel(new ImageIcon(
-//								newimage));
 						final JLabel iconLabel = new JLabel(new ImageIcon(
-								chartImage));
+								newimage));
 
 						// One bigger Icon to span over both columns
 						panel.add(iconLabel, "span 2");

Modified: trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphic.java
===================================================================
--- trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphic.java	2012-05-02 11:43:01 UTC (rev 1975)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphic.java	2012-05-02 14:01:19 UTC (rev 1976)
@@ -1,7 +1,9 @@
 package de.schmitzm.geotools.styling.chartsymbols;
 
 import java.awt.Color;
-import java.awt.Font;
+import java.awt.Dimension;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -10,7 +12,6 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import javax.swing.JPanel;
 import javax.swing.SwingUtilities;
 
 import org.apache.log4j.Logger;
@@ -20,10 +21,16 @@
 import org.geotools.styling.Symbolizer;
 import org.geotools.styling.visitor.DuplicatingStyleVisitor;
 import org.geotools.util.WeakHashSet;
+import org.opengis.feature.simple.SimpleFeatureType;
 import org.opengis.style.GraphicalSymbol;
 
+import com.vividsolutions.jts.geom.Point;
+
+import de.schmitzm.geotools.LegendIconFeatureRenderer;
+import de.schmitzm.geotools.feature.FeatureUtil;
 import de.schmitzm.geotools.gui.GeotoolsGUIUtil;
 import de.schmitzm.geotools.styling.StylingUtil;
+import de.schmitzm.i18n.Translation;
 import de.schmitzm.regex.RegexCache;
 import de.schmitzm.swing.SwingUtil;
 
@@ -548,88 +555,163 @@
 	 *            Bar entspricht.
 	 * 
 	 */
-	public static Symbolizer addLegendLabels(Symbolizer symbolizer,
-			final Double maxValue) {
+	// public static Symbolizer addLegendLabels(Symbolizer symbolizer,
+	// final Double maxValue) {
+	//
+	// DuplicatingStyleVisitor visitor = new DuplicatingStyleVisitor() {
+	// @Override
+	// protected ExternalGraphic copy(ExternalGraphic eg) {
+	// // https://trac.wikisquare.de/gp/ticket/79
+	//
+	// String url2;
+	// try {
+	//
+	// ChartGraphic cg = new ChartGraphic(eg);
+	// ChartGraphicTyp cgt = cg.getChartType();
+	// if (cgt.isBar()) {
+	//
+	// url2 = eg.getLocation().toString();
+	//
+	// if (!cgt.isHorizontal()) {
+	// url2 += "&chxt=y,x,r";
+	// } else {
+	// url2 += "&chxt=x,y,r";
+	// }
+	//
+	// url2 += "&chtt=%20";
+	//
+	// String midValueString = maxValue / 2 + "";
+	// if (midValueString.endsWith(".0"))
+	// midValueString = midValueString.substring(0,
+	// midValueString.length() - 2);
+	//
+	// String maxValueString = maxValue + "";
+	// if (maxValueString.endsWith(".0"))
+	// maxValueString = maxValueString.substring(0,
+	// maxValueString.length() - 2);
+	//
+	// url2 += "&chxl=0:|0|" + maxValueString + "|1:||2:||";
+	//
+	// url2 += "&chxs=2,0000DD,13,-1,t,FF0000";
+	// // url2 += "&chxl=0:|0|" + midValueString + "|"
+	// // + maxValueString + "|1:|";
+	//
+	// // breite des Charts vergrößern
+	// // url2 = url2.replaceAll(
+	// // "chs=" + cg.getImageWidth(),
+	// // "chs="
+	// // + (cg.getImageWidth() + new JPanel()
+	// // .getFontMetrics(
+	// // new JPanel().getFont())
+	// // .stringWidth(maxValueString)));
+	//
+	// ExternalGraphic externalGraphic2 = StylingUtil.STYLE_BUILDER
+	// .createExternalGraphic(url2, eg.getFormat());
+	//
+	// return externalGraphic2;
+	// }
+	// return eg;
+	// } catch (Exception e) {
+	// Logger.getLogger(ChartGraphicPreviewFixStyleVisitor.class)
+	// .error(e, e);
+	// return eg;
+	// }
+	// }
+	//
+	// };
+	// symbolizer.accept(visitor);
+	// Symbolizer copy = (Symbolizer) visitor.getCopy();
+	// // if (copy instanceof PointSymbolizer) {
+	// // PointSymbolizer ps = (PointSymbolizer) copy;
+	// // ps.getGraphic().setSize(StylingUtil.ff.literal(120));
+	// // }
+	// return copy;
+	// }
 
-		DuplicatingStyleVisitor visitor = new DuplicatingStyleVisitor() {
-			@Override
-			protected ExternalGraphic copy(ExternalGraphic eg) {
-				// https://trac.wikisquare.de/gp/ticket/79
+	// /**
+	// * Liefert <code>true</code>, wenn es sich um ein Bar-Chart handelt
+	// */
+	// public static boolean isBar(String url) {
+	// return false;
+	// }
+	//
+	// public static boolean isHorizontalBar(String url2) {
+	// // TODO Auto-generated method stub
+	// return false;
+	// }
 
-				String url2;
-				try {
+	public static BufferedImage renderLegendImage(
+			PointSymbolizer pointSymbolizer) {
+		ChartGraphicLegendVisitor visitor = new ChartGraphicLegendVisitor();
+		pointSymbolizer.accept(visitor);
+		pointSymbolizer = (PointSymbolizer) visitor.getCopy();
+		ChartGraphic cg = new ChartGraphic(pointSymbolizer.getGraphic());
 
-					ChartGraphic cg = new ChartGraphic(eg);
-					ChartGraphicTyp cgt = cg.getChartType();
-					if (cgt.isBar()) {
+		Double maxValue = cg.getMaxValue();
+		String maxValueString = maxValue + "";
+		if (maxValueString.endsWith(".0"))
+			maxValueString = maxValueString.substring(0,
+					maxValueString.length() - 2);
+		String minValueString = "0";
 
-						url2 = eg.getLocation().toString();
+		ChartGraphicTyp cgt = cg.getChartType();
+		Symbolizer fixSymbolizer = ChartGraphic
+				.getFixDataSymbolizer(pointSymbolizer);
 
-						if (!cgt.isHorizontal()) {
-							url2 += "&chxt=y,x,r";
-						} else {
-							url2 += "&chxt=x,y,r";
-						}
+		SimpleFeatureType ftpoint = FeatureUtil.createFeatureType(Point.class);
 
-						url2 += "&chtt=%20";
+		int chartLegendIconSize = 80; // pointSymbolizer.getGraphic().getSize().toString()
+		// ?
+		final BufferedImage chartImage = LegendIconFeatureRenderer
+				.getInstance()
+				.createImageForSymbolizer(fixSymbolizer, ftpoint,
+						new Dimension(chartLegendIconSize, chartLegendIconSize));
+		BufferedImage newimage = chartImage;
+		if (cgt.isBar()) {
 
-						String midValueString = maxValue / 2 + "";
-						if (midValueString.endsWith(".0"))
-							midValueString = midValueString.substring(0,
-									midValueString.length() - 2);
+			if (cgt.isHorizontal()) {
+				int dX = 12;
+				int dY = 10;
+				newimage = new BufferedImage(chartImage.getWidth() + dX,
+						chartImage.getHeight() + dY,
+						BufferedImage.TYPE_4BYTE_ABGR);
+				Graphics2D g2 = newimage.createGraphics();
+				g2.setColor(new Color(000));
 
-						String maxValueString = maxValue + "";
-						if (maxValueString.endsWith(".0"))
-							maxValueString = maxValueString.substring(0,
-									maxValueString.length() - 2);
+				int stringWidth = g2.getFontMetrics().stringWidth(
+						maxValueString);
+				g2.drawString(minValueString, dX, newimage.getHeight());
+				g2.drawString(maxValueString, chartImage.getWidth()
+						- stringWidth, newimage.getHeight());
+				g2.setColor(Color.gray);
+				g2.drawLine(dX+1,chartImage.getHeight()-5,dX+1,chartImage.getHeight());
+				g2.drawLine(chartImage.getWidth()-dX+2,chartImage.getHeight()-5,chartImage.getWidth()-dX+2,chartImage.getHeight());
+				g2.drawImage(chartImage, 0, 0, null);
+				g2.dispose();
+			} else {
 
-						url2 += "&chxl=0:|0|" + maxValueString + "|1:||2:||";
+//				int dX = 40;
+				int dX=chartImage.createGraphics().getFontMetrics().stringWidth(maxValueString);
+				int dY = 4;
+				newimage = new BufferedImage(chartImage.getWidth() + dX,
+						chartImage.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
+				Graphics2D g2 = newimage.createGraphics();
+				g2.setColor(new Color(000));
 
-						url2 += "&chxs=2,0000DD,13,-1,t,FF0000";
-						// url2 += "&chxl=0:|0|" + midValueString + "|"
-						// + maxValueString + "|1:|";
-
-						// breite des Charts vergrößern
-						// url2 = url2.replaceAll(
-						// "chs=" + cg.getImageWidth(),
-						// "chs="
-						// + (cg.getImageWidth() + new JPanel()
-						// .getFontMetrics(
-						// new JPanel().getFont())
-						// .stringWidth(maxValueString)));
-
-						ExternalGraphic externalGraphic2 = StylingUtil.STYLE_BUILDER
-								.createExternalGraphic(url2, eg.getFormat());
-
-						return externalGraphic2;
-					}
-					return eg;
-				} catch (Exception e) {
-					Logger.getLogger(ChartGraphic.class)
-							.error(e, e);
-					return eg;
-				}
+				int stringWidth = g2.getFontMetrics().stringWidth(
+						maxValueString);
+				g2.drawString(minValueString, dX
+						- g2.getFontMetrics().stringWidth(minValueString)+7,
+						newimage.getHeight());
+				g2.drawString(maxValueString, dX - stringWidth+7, g2.getFontMetrics().getHeight());
+				g2.setColor(Color.gray);
+				g2.drawLine(dX+10,newimage.getHeight()-dY,dX+7,newimage.getHeight()-dY);
+				g2.drawLine(dX+10,dY+3,dX+7,dY+3);
+				g2.drawImage(chartImage, dX, dY, null);
+//				g2.drawRect(dX,0,chartImage.getWidth(), chartImage.getHeight());
+				g2.dispose();
 			}
-
-		};
-		symbolizer.accept(visitor);
-		Symbolizer copy = (Symbolizer) visitor.getCopy();
-		// if (copy instanceof PointSymbolizer) {
-		// PointSymbolizer ps = (PointSymbolizer) copy;
-		// ps.getGraphic().setSize(StylingUtil.ff.literal(120));
-		// }
-		return copy;
+		}
+		return newimage;
 	}
-
-	/**
-	 * Liefert <code>true</code>, wenn es sich um ein Bar-Chart handelt
-	 */
-	public static boolean isBar(String url) {
-		return false;
-	}
-
-	public static boolean isHorizontalBar(String url2) {
-		// TODO Auto-generated method stub
-		return false;
-	}
 }

Modified: trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphicLegendVisitor.java
===================================================================
--- trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphicLegendVisitor.java	2012-05-02 11:43:01 UTC (rev 1975)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphicLegendVisitor.java	2012-05-02 14:01:19 UTC (rev 1976)
@@ -9,6 +9,8 @@
 /**
  * When a dynamic Chart Symvol is previewed, the freemarker(?) expressions (like
  * "${COUNTHOUSES}") are replaced with fixed values;
+ * 
+ * Sets opacity to 1 and a fixed size, so we have a decent Icon in the legend
  */
 public class ChartGraphicLegendVisitor extends DuplicatingStyleVisitor {
 
@@ -25,7 +27,7 @@
 			g = super.copy(g);
 
 			g.setOpacity(StylingUtil.STYLE_BUILDER.literalExpression(1.));
-			g.setSize(StylingUtil.STYLE_BUILDER.literalExpression(90));
+			g.setSize(StylingUtil.STYLE_BUILDER.literalExpression(80));
 
 		} catch (Exception e) {
 			Logger.getLogger(ChartGraphicLegendVisitor.class).error(e, e);



More information about the Schmitzm-commits mailing list