Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/org/dive4elements/river/exports/LegendProcessor.java @ 5831:bd047b71ab37
Repaired internal references
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:06:39 +0200 |
parents | flys-artifacts/src/main/java/de/intevation/flys/exports/LegendProcessor.java@3dc26ec2558d |
children |
comparison
equal
deleted
inserted
replaced
5830:160f53ee0870 | 5831:bd047b71ab37 |
---|---|
1 package org.dive4elements.river.exports; | |
2 | |
3 import java.awt.geom.Line2D; | |
4 import java.util.ArrayList; | |
5 import java.util.HashMap; | |
6 import java.util.Iterator; | |
7 import java.util.LinkedHashMap; | |
8 import java.util.List; | |
9 | |
10 import org.jfree.chart.LegendItem; | |
11 import org.jfree.chart.LegendItemCollection; | |
12 import org.jfree.chart.plot.XYPlot; | |
13 | |
14 | |
15 /** Class to process Plots legends. */ | |
16 public abstract class LegendProcessor { | |
17 | |
18 /** (Empty) shape for aggregated Legend Items. */ | |
19 private static final Line2D.Double SPACE = new Line2D.Double(0,0,0,0); | |
20 | |
21 | |
22 /** Prevent instantiations. */ | |
23 private LegendProcessor() { | |
24 } | |
25 | |
26 | |
27 /** | |
28 * Create a hash from a legenditem. | |
29 * This hash can then be used to merge legend items labels. | |
30 * @return hash for given legenditem to identify mergeables. | |
31 */ | |
32 protected static String legendItemHash(LegendItem li) { | |
33 // TODO Do proper implementation. Ensure that only mergable sets are created. | |
34 // getFillPaint() | |
35 // getFillPaintTransformer() | |
36 // getLabel() | |
37 // getLine() | |
38 // getLinePaint() | |
39 // getLineStroke() | |
40 // getOutminePaint() | |
41 // getOutlineStroke() | |
42 // Shape getShape() | |
43 // String getToolTipText() | |
44 // String getURLText() | |
45 // boolean isLineVisible() | |
46 // boolean isShapeFilled() | |
47 // boolean isShapeOutlineVisible() | |
48 // boolean isShapeVisible() | |
49 String hash = li.getLinePaint().toString(); | |
50 // XXX: DEAD CODE // String label = li.getLabel(); | |
51 /*if (label.startsWith("W (") || label.startsWith("W(")) { | |
52 hash += "-W-"; | |
53 } | |
54 else if (label.startsWith("Q(") || label.startsWith("Q (")) { | |
55 hash += "-Q-"; | |
56 }*/ | |
57 | |
58 // WQ.java holds example of using regex Matcher/Pattern. | |
59 | |
60 return hash; | |
61 } | |
62 | |
63 | |
64 /** | |
65 * Create new legend entries, dependent on settings. | |
66 * @param plot The plot for which to modify the legend. | |
67 * @param threshold How many items are needed for aggregation to | |
68 * be triggered? | |
69 */ | |
70 public static void aggregateLegendEntries(XYPlot plot, int threshold) { | |
71 LegendItemCollection old = plot.getLegendItems(); | |
72 // Find "similar" entries if aggregation is enabled. | |
73 | |
74 int maxListSize = 0; | |
75 int AGGR_THRESHOLD = threshold; | |
76 | |
77 if (AGGR_THRESHOLD > old.getItemCount() || AGGR_THRESHOLD <= 0){ | |
78 return; | |
79 } | |
80 | |
81 HashMap<String, List<LegendItem>> entries = new LinkedHashMap<String, List<LegendItem>>(); | |
82 for (Iterator<LegendItem> i = old.iterator(); i.hasNext();) { | |
83 LegendItem item = i.next(); | |
84 String hash = legendItemHash(item); | |
85 List<LegendItem> itemList = entries.get(hash); | |
86 if (itemList == null) { | |
87 itemList = new ArrayList<LegendItem>(); | |
88 entries.put(hash, itemList); | |
89 } | |
90 itemList.add(item); | |
91 | |
92 if (itemList.size() > maxListSize) { | |
93 maxListSize = itemList.size(); | |
94 } | |
95 } | |
96 | |
97 if (maxListSize < AGGR_THRESHOLD) { | |
98 // No need to do anything. | |
99 return; | |
100 } | |
101 | |
102 // Run over collected entries, merge their names and create new | |
103 // entry if needed. | |
104 LegendItemCollection newLegend = new LegendItemCollection(); | |
105 for (List<LegendItem> itemList: entries.values()) { | |
106 if (itemList.size() >= AGGR_THRESHOLD) { | |
107 // Now do merging. | |
108 // XXX: DEAD CODE // LegendItem item = itemList.get(0); | |
109 // Unfortunately we cannot clone and just setDescription, as this | |
110 // method was added in JFreeChart 1.0.14 (we are at .13). | |
111 | |
112 // Remove the shapes of all but the first items, | |
113 // to prevent "overfill" of legenditemblock. | |
114 for (int i = 0; i < itemList.size(); i++) { | |
115 if (i != 0) { | |
116 LegendItem litem = itemList.get(i); | |
117 | |
118 // Make shape and line really small. | |
119 LegendItem merged = new LegendItem( | |
120 "," + litem.getLabel(), litem.getDescription(), litem.getToolTipText(), | |
121 litem.getURLText(), false, SPACE, | |
122 false, litem.getFillPaint(), false, | |
123 litem.getOutlinePaint(), litem.getOutlineStroke(), false, | |
124 SPACE, litem.getLineStroke(), litem.getLinePaint()); | |
125 newLegend.add(merged); | |
126 } | |
127 else { | |
128 newLegend.add(itemList.get(i)); | |
129 } | |
130 } | |
131 } | |
132 else { | |
133 // Do not merge entries. | |
134 for (LegendItem li: itemList) { | |
135 newLegend.add(li); | |
136 } | |
137 } | |
138 } | |
139 | |
140 plot.setFixedLegendItems (newLegend); | |
141 } | |
142 } | |
143 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |