[Schmitzm-commits] r1969 - 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 Apr 25 19:04:55 CEST 2012


Author: alfonx
Date: 2012-04-25 19:04:55 +0200 (Wed, 25 Apr 2012)
New Revision: 1969

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
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-04-25 16:36:22 UTC (rev 1968)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/StyledLayerUtil.java	2012-04-25 17:04:55 UTC (rev 1969)
@@ -1092,9 +1092,12 @@
 						Symbolizer fixSymbolizer = ChartGraphic
 								.getFixDataLegendSymbolizer(pointSymbolizer);
 
+						Double maxValue = new ChartGraphic(
+								pointSymbolizer.getGraphic()).getMaxValue();
+
 						// TODO https://trac.wikisquare.de/gp/ticket/79
-						fixSymbolizer = ChartGraphic
-								.addLegendLabels(fixSymbolizer);
+						fixSymbolizer = ChartGraphic.addLegendLabels(
+								fixSymbolizer, maxValue);
 
 						SimpleFeatureType ftpoint = FeatureUtil
 								.createFeatureType(Point.class);
@@ -1105,7 +1108,7 @@
 								.getInstance().createImageForSymbolizer(
 										fixSymbolizer,
 										ftpoint,
-										new Dimension(chartLegendIconSize,
+										new Dimension(chartLegendIconSize + 20,
 												chartLegendIconSize));
 
 						final JLabel iconLabel = new JLabel(new ImageIcon(

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-04-25 16:36:22 UTC (rev 1968)
+++ trunk/schmitzm-gt/src/main/java/de/schmitzm/geotools/styling/chartsymbols/ChartGraphic.java	2012-04-25 17:04:55 UTC (rev 1969)
@@ -1,6 +1,7 @@
 package de.schmitzm.geotools.styling.chartsymbols;
 
 import java.awt.Color;
+import java.awt.Font;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -9,11 +10,13 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import javax.swing.JPanel;
 import javax.swing.SwingUtilities;
 
 import org.apache.log4j.Logger;
 import org.geotools.styling.ExternalGraphic;
 import org.geotools.styling.Graphic;
+import org.geotools.styling.PointSymbolizer;
 import org.geotools.styling.Symbolizer;
 import org.geotools.styling.visitor.DuplicatingStyleVisitor;
 import org.geotools.util.WeakHashSet;
@@ -35,12 +38,24 @@
 		// bvo, Vertical bar chart in which bars are stacked
 		;
 
+		public boolean isBar() {
+			return this.toString().startsWith("b");
+		}
+
 		/**
 		 * @return a human-readable and translated Title
 		 */
 		public String getTitle() {
 			return GeotoolsGUIUtil.R("ChartGraphicTyp." + this.toString());
 		}
+
+		public boolean isHorizontal() {
+			return this.toString().contains("h");
+		}
+
+		public boolean isStacked() {
+			return this.toString().endsWith("s");
+		}
 	}
 
 	public ExternalGraphic getChartGraphic() {
@@ -202,6 +217,10 @@
 		importChartFromGraphic(importThis);
 	}
 
+	public ChartGraphic(ExternalGraphic importThis) {
+		importChartFromExternalGraphic(importThis);
+	}
+
 	/**
 	 * Tries to find a gt-chart defined in this Graphic object. If a chart
 	 * definition can be found, its settings are stored in this
@@ -216,81 +235,93 @@
 			if (gs instanceof ExternalGraphic) {
 
 				ExternalGraphic eg = (ExternalGraphic) gs;
-				try {
-					if (eg.getLocation() == null)
-						continue;
-					String url = eg.getLocation().toString();
+				if (importChartFromExternalGraphic(eg))
+					break;
 
-					/*
-					 * Parse the URL-Parameters, but do NOT CALL THE SETTERS!
-					 */
+			}
+		}
 
-					// Read the Chart-Type!
-					Pattern typePattern = RegexCache.getInstance().getPattern(
-							"cht=(.*?)(&.*|^)");
-					Matcher m = typePattern.matcher(url);
-					if (m.find() && m.groupCount() > 0) {
-						chartType = ChartGraphicTyp.valueOf(m.group(1));
-					}
+	}
 
-					// Read the ATTRIBUTE NAMES from the Style
-					Pattern nextAtt = RegexCache.getInstance().getPattern(
-							"\\$\\{([^ ]*?)( .*?|)\\}");
-					m = nextAtt.matcher(url);
-					while (m.find() && m.groupCount() > 0) {
-						attributes.add(m.group(1));
-					}
+	public boolean importChartFromExternalGraphic(ExternalGraphic eg) {
 
-					// Read the COLORS from the Style
-					Pattern colorPart = RegexCache.getInstance().getPattern(
-							"chco=([^&]*?)(&.*|$)");
-					m = colorPart.matcher(url);
-					if (m.find() && m.groupCount() == 2) {
-						String colorstr = m.group(1);
-						Pattern nextColor = RegexCache.getInstance()
-								.getPattern("([a-f,A-F,0-9]{6,6})(?:[&,]|$)?");
-						m = nextColor.matcher(colorstr);
-						int count = 0;
-						while (m.find() && m.groupCount() == 1) {
-							if (attributes.size() >= count + 1)
-								colors.put(attributes.get(count),
-										Color.decode("#" + m.group(1)));
-							count++;
-						}
-					}
+		if (eg == null)
+			return false;
 
-					// Read any size from the Style
-					Pattern sizePart = RegexCache.getInstance().getPattern(
-							"chs=(\\d+)x(\\d+)");
-					m = sizePart.matcher(url);
-					if (m.find() && m.groupCount() == 2) {
-						imageWidth = Integer.valueOf(m.group(1));
-						imageHeight = Integer.valueOf(m.group(2));
-					}
+		try {
 
-					// Try to find a maxValue
-					Pattern maxValuePattern = RegexCache
-							.getInstance()
-							.getPattern(
-									"\\$\\{([^ \\*]*?) \\* 100\\. / ([^ \\*]*)\\}");
-					m = maxValuePattern.matcher(url);
-					if (m.find() && m.groupCount() == 2) {
-						maxValue = Double.valueOf(m.group(2));
-					}
+			if (eg.getLocation() == null)
+				return false;
 
-					break;
+			String url = eg.getLocation().toString();
 
-				} catch (MalformedURLException e) {
-					LOGGER.warn(e, e);
-					continue;
+			/*
+			 * Parse the URL-Parameters, but do NOT CALL THE SETTERS!
+			 */
+
+			// Read the Chart-Type!
+			Pattern typePattern = RegexCache.getInstance().getPattern(
+					"cht=(.*?)(&.*|^)");
+			Matcher m = typePattern.matcher(url);
+			if (m.find() && m.groupCount() > 0) {
+				chartType = ChartGraphicTyp.valueOf(m.group(1));
+			}
+
+			// Read the ATTRIBUTE NAMES from the Style
+			Pattern nextAtt = RegexCache.getInstance().getPattern(
+					"\\$\\{([^ ]*?)( .*?|)\\}");
+			m = nextAtt.matcher(url);
+			while (m.find() && m.groupCount() > 0) {
+				attributes.add(m.group(1));
+			}
+
+			// Read the COLORS from the Style
+			Pattern colorPart = RegexCache.getInstance().getPattern(
+					"chco=([^&]*?)(&.*|$)");
+			m = colorPart.matcher(url);
+			if (m.find() && m.groupCount() == 2) {
+				String colorstr = m.group(1);
+				Pattern nextColor = RegexCache.getInstance().getPattern(
+						"([a-f,A-F,0-9]{6,6})(?:[&,]|$)?");
+				m = nextColor.matcher(colorstr);
+				int count = 0;
+				while (m.find() && m.groupCount() == 1) {
+					if (attributes.size() >= count + 1)
+						colors.put(attributes.get(count),
+								Color.decode("#" + m.group(1)));
+					count++;
 				}
 			}
+
+			// Read any size from the Style
+			Pattern sizePart = RegexCache.getInstance().getPattern(
+					"chs=(\\d+)x(\\d+)");
+			m = sizePart.matcher(url);
+			if (m.find() && m.groupCount() == 2) {
+				imageWidth = Integer.valueOf(m.group(1));
+				imageHeight = Integer.valueOf(m.group(2));
+			}
+
+			// Try to find a maxValue
+			Pattern maxValuePattern = RegexCache.getInstance().getPattern(
+					"\\$\\{([^ \\*]*?) \\* 100\\. / ([^ \\*]*)\\}");
+			m = maxValuePattern.matcher(url);
+			if (m.find() && m.groupCount() == 2) {
+				maxValue = Double.valueOf(m.group(2));
+			}
+
+			return true;
+
+		} catch (Exception e) {
+			LOGGER.warn(e, e);
+
+			return false;
 		}
 
 	}
 
 	public ChartGraphic() {
-		this(null);
+		this((ExternalGraphic) null);
 	}
 
 	private ChartGraphicTyp chartType = ChartGraphicTyp.bvg;
@@ -517,8 +548,14 @@
 	 * (depending whether the chart is Horizontal or Vertical)
 	 * 
 	 * https://trac.wikisquare.de/gp/ticket/79
+	 * 
+	 * @param maxValue
+	 *            <code>null</code> oder bei Barchart der Wert, der dem höchsten
+	 *            Bar entspricht.
+	 * 
 	 */
-	public static Symbolizer addLegendLabels(Symbolizer symbolizer) {
+	public static Symbolizer addLegendLabels(Symbolizer symbolizer,
+			final Double maxValue) {
 
 		DuplicatingStyleVisitor visitor = new DuplicatingStyleVisitor() {
 			@Override
@@ -527,35 +564,78 @@
 
 				String url2;
 				try {
-					url2 = eg.getLocation().toString();
 
-					// chxt=x,y,r,x
-					// chxl=0:|0|55|110|
+					ChartGraphic cg = new ChartGraphic(eg);
+					ChartGraphicTyp cgt = cg.getChartType();
+					if (cgt.isBar()) {
 
-					// TODO Bei vertical und horizontal anders, TODO Nur bei
-					// BAR?!
-					// url2 += "&chxt=x";
-					// Label an Axis style:
-					url2 += "&chxs=0,ff0000,12,1,lt";
-					// Which Axis to show
-					url2 += "&chxt=y";
-					url2 += "&chdlp=t|t";
-					// Which Labels to show:
-					url2 += "&chxl=0:|0|20|111|";
-					url2 += "&chma=30,30,30,30";
+						url2 = eg.getLocation().toString();
 
-					ExternalGraphic externalGraphic2 = StylingUtil.STYLE_BUILDER
-							.createExternalGraphic(url2, eg.getFormat());
+						if (!cgt.isHorizontal()) {
+							url2 += "&chxt=y,x,r";
+						} else {
+							url2 += "&chxt=x,y,r";
+						}
 
-					return externalGraphic2;
+						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);
-		return (Symbolizer) visitor.getCopy();
+		Symbolizer copy = (Symbolizer) visitor.getCopy();
+		// if (copy instanceof PointSymbolizer) {
+		// PointSymbolizer ps = (PointSymbolizer) copy;
+		// ps.getGraphic().setSize(StylingUtil.ff.literal(120));
+		// }
+		return copy;
 	}
+
+	/**
+	 * 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;
+	}
 }



More information about the Schmitzm-commits mailing list