Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java @ 3175:4616436a84f4
Progressed LegendItemAggregation.
flys-artifacts/trunk@4789 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Mon, 25 Jun 2012 18:12:19 +0000 |
parents | b6da650b0718 |
children | 436ab826e82b |
comparison
equal
deleted
inserted
replaced
3174:6bde7c90dc93 | 3175:4616436a84f4 |
---|---|
3 import java.awt.BasicStroke; | 3 import java.awt.BasicStroke; |
4 import java.awt.Color; | 4 import java.awt.Color; |
5 import java.awt.Font; | 5 import java.awt.Font; |
6 import java.awt.Paint; | 6 import java.awt.Paint; |
7 import java.awt.Stroke; | 7 import java.awt.Stroke; |
8 import java.awt.geom.Line2D; | |
8 | 9 |
9 import java.text.NumberFormat; | 10 import java.text.NumberFormat; |
10 | 11 |
11 import java.util.ArrayList; | 12 import java.util.ArrayList; |
12 import java.util.HashMap; | 13 import java.util.HashMap; |
1296 return hash; | 1297 return hash; |
1297 } | 1298 } |
1298 | 1299 |
1299 | 1300 |
1300 /** | 1301 /** |
1301 * Create a label for multiple items. | 1302 * Create new legend entries, dependent on settings. |
1302 * | |
1303 * For example from "W(Q=100)" and "W(Q=220)" | |
1304 * "W(Q= {100, 22})" would result. | |
1305 * | |
1306 * @param items list of legenditems. | |
1307 * @return the merged label. | |
1308 */ | |
1309 public String mergeLegendNames(List<LegendItem> items) { | |
1310 if (items.size() == 0) { | |
1311 return null; | |
1312 } | |
1313 if (items.size() == 1) { | |
1314 return items.get(0).getLabel(); | |
1315 } | |
1316 | |
1317 // TODO consider using regionMathches for implementation. | |
1318 int lastMatchedChar = 0; | |
1319 boolean first = true; | |
1320 String startPattern = ""; | |
1321 String endPattern = ""; | |
1322 String name = ""; | |
1323 // First, figure out beginning and end patterns, then merge. | |
1324 for (LegendItem item : items) { | |
1325 if (first) { | |
1326 startPattern = item.getLabel(); | |
1327 endPattern = item.getLabel(); | |
1328 first = false; | |
1329 continue; | |
1330 } | |
1331 while (startPattern.length() > 0 && !item.getLabel().startsWith(startPattern)) { | |
1332 startPattern = startPattern.substring(0, startPattern.length() -1); | |
1333 } | |
1334 while (endPattern.length() > 0 && !item.getLabel().endsWith(endPattern)) { | |
1335 endPattern = endPattern.substring(1); | |
1336 } | |
1337 } | |
1338 | |
1339 // Then, merge. | |
1340 name = startPattern + " {"; | |
1341 first = true; | |
1342 for (LegendItem item : items) { | |
1343 if (!first) { | |
1344 name += ", "; | |
1345 } | |
1346 else { | |
1347 first = false; | |
1348 } | |
1349 // Possible case in differences | |
1350 if (startPattern.length() + endPattern.length() < item.getLabel().length()) { | |
1351 // Note that we do not really want this. | |
1352 logger.error("Legend aggregation does not cover case: " + | |
1353 startPattern + " -- " + item.getLabel() + " -- " + endPattern); | |
1354 name += item.getLabel(); | |
1355 } | |
1356 else { | |
1357 name += item.getLabel().substring(startPattern.length(), | |
1358 item.getLabel().length() - endPattern.length()); | |
1359 } | |
1360 } | |
1361 name += "} "; | |
1362 name += endPattern; | |
1363 | |
1364 return name; | |
1365 } | |
1366 | |
1367 | |
1368 /** | |
1369 * Create new legend entries, dependant on settings. | |
1370 * @param plot The plot for which to modify the legend. | 1303 * @param plot The plot for which to modify the legend. |
1371 */ | 1304 */ |
1372 public void aggregateLegendEntries(XYPlot plot) { | 1305 public void aggregateLegendEntries(XYPlot plot) { |
1373 LegendItemCollection old = plot.getLegendItems(); | 1306 LegendItemCollection old = plot.getLegendItems(); |
1374 // Find "similar" entries if aggregation is enabled. | 1307 // Find "similar" entries if aggregation is enabled. |
1415 if (itemList.size() >= AGGR_THRESHOLD) { | 1348 if (itemList.size() >= AGGR_THRESHOLD) { |
1416 // Now do merging. | 1349 // Now do merging. |
1417 LegendItem item = (LegendItem) itemList.get(0); | 1350 LegendItem item = (LegendItem) itemList.get(0); |
1418 // Unfortunately we cannot clone and just setDescription, as this | 1351 // Unfortunately we cannot clone and just setDescription, as this |
1419 // method was added in JFreeChart 1.0.14 (we are at .13). | 1352 // method was added in JFreeChart 1.0.14 (we are at .13). |
1420 LegendItem merged = new LegendItem( | 1353 |
1421 mergeLegendNames(itemList), item.getDescription(), item.getToolTipText(), | 1354 // Remove the shapes of all but the first items, |
1422 item.getURLText(), item.isShapeVisible(), item.getShape(), | 1355 // to prevent "overfill" of legenditemblock. |
1423 item.isShapeFilled(), item.getFillPaint(), item.isShapeOutlineVisible(), | 1356 for (int i = 0; i < itemList.size(); i++) { |
1424 item.getOutlinePaint(), item.getOutlineStroke(), item.isLineVisible(), | 1357 if (i != 0) { |
1425 item.getLine(), item.getLineStroke(), item.getLinePaint()); | 1358 LegendItem litem = itemList.get(i); |
1426 newLegend.add(merged); | 1359 |
1360 // TODO Like in D-Flys, define a static "SPACE" shape. | |
1361 // Make shape and line really small. | |
1362 LegendItem merged = new LegendItem( | |
1363 ", " + litem.getLabel(), litem.getDescription(), litem.getToolTipText(), | |
1364 litem.getURLText(), false, /*litem.getShape()*/ new Line2D.Double(0,0,0,0), | |
1365 false, litem.getFillPaint(), false, | |
1366 litem.getOutlinePaint(), litem.getOutlineStroke(), false, | |
1367 new Line2D.Double(0,0,0,0) | |
1368 /*litem.getLine()*/, litem.getLineStroke(), litem.getLinePaint()); | |
1369 newLegend.add(merged); | |
1370 } | |
1371 else { | |
1372 newLegend.add(itemList.get(i)); | |
1373 } | |
1374 } | |
1427 } | 1375 } |
1428 else { | 1376 else { |
1429 // Do not merge entries. | 1377 // Do not merge entries. |
1430 for (LegendItem li: itemList) { | 1378 for (LegendItem li: itemList) { |
1431 newLegend.add(li); | 1379 newLegend.add(li); |