comparison flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.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 f1e09a003f78
children 12440c6b8b66
comparison
equal deleted inserted replaced
3182:f1e09a003f78 3183:05c84d65988a
9 9
10 import java.text.NumberFormat; 10 import java.text.NumberFormat;
11 11
12 import java.util.ArrayList; 12 import java.util.ArrayList;
13 import java.util.HashMap; 13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List; 14 import java.util.List;
16 import java.util.LinkedHashMap;
17 import java.util.Map; 15 import java.util.Map;
18 16
19 import org.w3c.dom.Document; 17 import org.w3c.dom.Document;
20 18
21 import org.apache.log4j.Logger; 19 import org.apache.log4j.Logger;
258 256
259 // These have to go after the autozoom. 257 // These have to go after the autozoom.
260 addAnnotationsToRenderer(plot); 258 addAnnotationsToRenderer(plot);
261 259
262 aggregateLegendEntries(plot); 260 aggregateLegendEntries(plot);
261 // LegendItemAggregator benutzen und so umbauen, dass er eine Liste von LegendItems frisst.
263 262
264 return chart; 263 return chart;
265 } 264 }
266 265
267 266
1303 /** 1302 /**
1304 * Create new legend entries, dependent on settings. 1303 * Create new legend entries, dependent on settings.
1305 * @param plot The plot for which to modify the legend. 1304 * @param plot The plot for which to modify the legend.
1306 */ 1305 */
1307 public void aggregateLegendEntries(XYPlot plot) { 1306 public void aggregateLegendEntries(XYPlot plot) {
1308 LegendItemCollection old = plot.getLegendItems();
1309 // Find "similar" entries if aggregation is enabled.
1310
1311 int maxListSize = 0;
1312 int AGGR_THRESHOLD = 0; 1307 int AGGR_THRESHOLD = 0;
1313 1308
1314 Integer threshold = getChartSettings().getLegendSection() 1309 Integer threshold = getChartSettings().getLegendSection()
1315 .getAggregationThreshold(); 1310 .getAggregationThreshold();
1316 1311
1317 AGGR_THRESHOLD = (threshold != null) ? threshold.intValue() : 0; 1312 AGGR_THRESHOLD = (threshold != null) ? threshold.intValue() : 0;
1318 1313
1319 if (AGGR_THRESHOLD > old.getItemCount() || AGGR_THRESHOLD <= 0){ 1314 LegendProcessor.aggregateLegendEntries(plot, AGGR_THRESHOLD);
1320 return;
1321 }
1322
1323 HashMap<String, List<LegendItem>> entries = new LinkedHashMap<String, List<LegendItem>>();
1324 for (Iterator i = old.iterator(); i.hasNext();) {
1325 LegendItem item = (LegendItem) i.next();
1326 String hash = legendItemHash(item);
1327 List<LegendItem> itemList = entries.get(hash);
1328 if (itemList == null) {
1329 itemList = new ArrayList<LegendItem>();
1330 }
1331 itemList.add(item);
1332
1333 if (itemList.size() > maxListSize) {
1334 maxListSize = itemList.size();
1335 }
1336
1337 entries.put(legendItemHash(item), itemList);
1338 }
1339
1340 if (maxListSize < AGGR_THRESHOLD) {
1341 // No need to do anything.
1342 return;
1343 }
1344
1345 // Run over collected entries, merge their names and create new
1346 // entry if needed.
1347 LegendItemCollection newLegend = new LegendItemCollection();
1348 for (Map.Entry<String, List<LegendItem>> cursor: entries.entrySet()) {
1349 List<LegendItem> itemList = cursor.getValue();
1350 if (itemList.size() >= AGGR_THRESHOLD) {
1351 // Now do merging.
1352 LegendItem item = (LegendItem) itemList.get(0);
1353 // Unfortunately we cannot clone and just setDescription, as this
1354 // method was added in JFreeChart 1.0.14 (we are at .13).
1355
1356 // Remove the shapes of all but the first items,
1357 // to prevent "overfill" of legenditemblock.
1358 for (int i = 0; i < itemList.size(); i++) {
1359 if (i != 0) {
1360 LegendItem litem = itemList.get(i);
1361
1362 // Make shape and line really small.
1363 LegendItem merged = new LegendItem(
1364 ", " + litem.getLabel(), litem.getDescription(), litem.getToolTipText(),
1365 litem.getURLText(), false, SPACE,
1366 false, litem.getFillPaint(), false,
1367 litem.getOutlinePaint(), litem.getOutlineStroke(), false,
1368 SPACE, litem.getLineStroke(), litem.getLinePaint());
1369 newLegend.add(merged);
1370 }
1371 else {
1372 newLegend.add(itemList.get(i));
1373 }
1374 }
1375 }
1376 else {
1377 // Do not merge entries.
1378 for (LegendItem li: itemList) {
1379 newLegend.add(li);
1380 }
1381 }
1382 }
1383
1384 plot.setFixedLegendItems (newLegend);
1385 } 1315 }
1386 1316
1387 1317
1388 /** Two Ranges that span a rectangular area. */ 1318 /** Two Ranges that span a rectangular area. */
1389 public static class Area { 1319 public static class Area {

http://dive4elements.wald.intevation.org