comparison flys-artifacts/src/main/java/de/intevation/flys/exports/LegendProcessor.java @ 3183:05c84d65988a

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

http://dive4elements.wald.intevation.org