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