Improve legend display

- Draw box around legend
- Prefix graph titles with colored line
- Allow toggling of grid in FunctionArea

refs #48
This commit is contained in:
Fabian Becker 2015-12-09 15:07:00 +01:00
parent fa1c0a052e
commit f0716a3576
6 changed files with 86 additions and 132 deletions

View File

@ -53,7 +53,7 @@ public class FunctionArea extends DArea implements Serializable {
/**
* Indicate whether graph legend entries should show their unique number.
*/
private boolean appendIndexInLegend = true;
private boolean appendIndexInLegend = false;
/**
*
@ -162,64 +162,36 @@ public class FunctionArea extends DArea implements Serializable {
xPos = e.getX();
yPos = e.getY();
addMenuItem(graphPopupMenu, "Rename graph", new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
renameGraph(getNearestGraphIndex(FunctionArea.this.xPos, FunctionArea.this.yPos));
}
});
addMenuItem(graphPopupMenu, "Rename graph", ee -> renameGraph(getNearestGraphIndex(FunctionArea.this.xPos, FunctionArea.this.yPos)));
// General entries
String togGTTName = (isShowGraphToolTips() ? "Deactivate"
: "Activate")
+ " graph tool tips";
addMenuItem(graphPopupMenu, togGTTName,
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
setShowGraphToolTips(!isShowGraphToolTips());
}
});
addMenuItem(graphPopupMenu, togGTTName, ee -> setShowGraphToolTips(!isShowGraphToolTips()));
String togLName = (isShowLegend() ? "Hide" : "Show")
+ " legend";
addMenuItem(graphPopupMenu, togLName, new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
toggleLegend();
}
});
String toggleLegend = (isShowLegend() ? "Hide" : "Show") + " legend";
addMenuItem(graphPopupMenu, toggleLegend, ee -> toggleLegend());
addMenuItem(graphPopupMenu, "Toggle scientific format", new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
toggleScientificY(true);
}
});
String toggleGrid = (isShowGrid() ? "Hide" : "Show") + " grid";
addMenuItem(graphPopupMenu, toggleGrid, ee -> toggleShowGrid());
addMenuItem(graphPopupMenu, "Toggle scientific format", ee -> toggleScientificY(true));
if (FunctionArea.this.pointSetContainer.size() > 0) {
addMenuItem(graphPopupMenu, "Recolor all graphs",
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
recolorAllGraphsByIndex();
}
});
addMenuItem(graphPopupMenu, "Recolor all graphs", ee -> recolorAllGraphsByIndex());
}
if (refPointListener != null) {
DPoint temp = getDMeasures().getDPoint(xPos, yPos);
addMenuItem(graphPopupMenu, "Select Reference Point:("
+ temp.x + "/" + temp.y + ")",
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
DPoint temp = getDMeasures().getDPoint(
xPos, yPos);
double[] point = new double[2];
point[0] = temp.x;
point[1] = temp.y;
refPointListener.refPointGiven(point);
}
ee -> {
DPoint temp1 = getDMeasures().getDPoint(
xPos, yPos);
double[] point = new double[2];
point[0] = temp1.x;
point[1] = temp1.y;
refPointListener.refPointGiven(point);
});
}
@ -229,20 +201,11 @@ public class FunctionArea extends DArea implements Serializable {
// the point info element
addMenuItem(graphPopupMenu, "Nearest point: ("
+ point.x + "/" + point.y + ")",
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
}
}, false);
ee -> {}, false);
addMenuItem(graphPopupMenu, " Remove point",
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
removePoint(FunctionArea.this.xPos,
FunctionArea.this.yPos);
}
});
ee -> removePoint(FunctionArea.this.xPos,
FunctionArea.this.yPos));
if (point.getIcon() instanceof InterfaceSelectablePointIcon) {
currentPointIcon = point.getIcon();
@ -251,27 +214,15 @@ public class FunctionArea extends DArea implements Serializable {
String selectTitle = indy.isMarked() ? " Deselect individual"
: " Select individual";
addMenuItem(graphPopupMenu, selectTitle,
new ActionListener() {
@Override
public void actionPerformed(
ActionEvent ee) {
((InterfaceSelectablePointIcon) currentPointIcon).getSelectionListener().individualSelected(
((InterfaceSelectablePointIcon) currentPointIcon).getEAIndividual());
}
});
ee -> ((InterfaceSelectablePointIcon) currentPointIcon).getSelectionListener().individualSelected(
((InterfaceSelectablePointIcon) currentPointIcon).getEAIndividual()));
}
}
if (point.getIcon() instanceof InterfaceDPointWithContent) {
currentPointIcon = point.getIcon();
addMenuItem(graphPopupMenu, " Show individual",
new ActionListener() {
@Override
public void actionPerformed(
ActionEvent ee) {
((InterfaceDPointWithContent) currentPointIcon).showIndividual();
}
});
ee -> ((InterfaceDPointWithContent) currentPointIcon).showIndividual());
}
}
@ -282,46 +233,31 @@ public class FunctionArea extends DArea implements Serializable {
// e.getY());
addMenuItem(graphPopupMenu, "Graph Info: "
+ getGraphInfo(e.getX(), e.getY()),
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
DPoint temp = FunctionArea.this.getDMeasures().getDPoint(
FunctionArea.this.xPos,
FunctionArea.this.yPos);
DPointIcon icon1 = new DPointIcon() {
@Override
public DBorder getDBorder() {
return new DBorder(4, 4, 4, 4);
}
ee -> {
DPoint temp = FunctionArea.this.getDMeasures().getDPoint(
FunctionArea.this.xPos,
FunctionArea.this.yPos);
DPointIcon icon1 = new DPointIcon() {
@Override
public DBorder getDBorder() {
return new DBorder(4, 4, 4, 4);
}
@Override
public void paint(Graphics g) {
g.drawLine(-2, 0, 2, 0);
g.drawLine(0, 0, 0, 4);
}
};
temp.setIcon(icon1);
FunctionArea.this.addDElement(temp);
}
@Override
public void paint(Graphics g) {
g.drawLine(-2, 0, 2, 0);
g.drawLine(0, 0, 0, 4);
}
};
temp.setIcon(icon1);
FunctionArea.this.addDElement(temp);
}, false);
addMenuItem(graphPopupMenu, " Remove graph",
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
clearGraph(FunctionArea.this.xPos,
FunctionArea.this.yPos);
}
});
ee -> clearGraph(FunctionArea.this.xPos, FunctionArea.this.yPos));
addMenuItem(graphPopupMenu, " Change graph color",
new ActionListener() {
@Override
public void actionPerformed(ActionEvent ee) {
changeColorGraph(FunctionArea.this.xPos,
FunctionArea.this.yPos);
}
});
ee -> changeColorGraph(FunctionArea.this.xPos, FunctionArea.this.yPos));
}
graphPopupMenu.show(FunctionArea.this, e.getX(), e.getY());
}
@ -1049,8 +985,7 @@ public class FunctionArea extends DArea implements Serializable {
if (!on) {
legendBox = null;
} else {
legendBox = new GraphPointSetLegend(pointSetContainer,
isAppendIndexInLegend());
legendBox = new GraphPointSetLegend(pointSetContainer, isAppendIndexInLegend());
}
repaint();
}
@ -1148,8 +1083,7 @@ public class FunctionArea extends DArea implements Serializable {
* Recreate the legend object with the current point sets.
*/
public void updateLegend() {
GraphPointSetLegend lb = new GraphPointSetLegend(pointSetContainer,
isAppendIndexInLegend());
GraphPointSetLegend lb = new GraphPointSetLegend(pointSetContainer, isAppendIndexInLegend());
setLegend(lb);
}

View File

@ -136,24 +136,12 @@ public class GraphPointSetLegend {
g.setColor(origCol);
}
// public void paintIn(Graphics g, Dimension dim) {
// paintIn(g, dim.width);
// }
//
// public void paintIn(Graphics g, Rectangle rect) {
// paintIn(g, rect.width);
// }
//
// public void paintIn(Graphics g, DRectangle rect) {
// paintIn(g, (int)rect.width);
// }
/**
*
* Called from FunctionArea
* @see FunctionArea#paint
*/
public void paintIn(Graphics g, SlimRect rect) {
paintIn(g, (int) rect.getX(), (int) rect.getY(), (int) rect.getX()
+ (int) rect.getWidth());
paintIn(g, (int) rect.getX(), (int) rect.getY(), (int) rect.getX() + (int) rect.getWidth());
}
/**
@ -164,21 +152,35 @@ public class GraphPointSetLegend {
*/
private void paintIn(Graphics g, int x, int y, int maxX) {
FontMetrics fm = g.getFontMetrics();
// System.out.println("In LegendBox.paintIn!");
double fontHeightSum = 0.0;
int yOffs = 5 + y + fm.getHeight();
int xOffs = x;
int xOffs;
int minX = Integer.MAX_VALUE;
int padding = 5;
Color origCol = g.getColor();
// Draw bounding box
for (Pair<String, Color> legendEntry : legendEntries) {
Rectangle2D stringBounds = fm.getStringBounds(legendEntry.head, g);
minX = Math.min(minX, (int) (maxX - stringBounds.getWidth() - 15));
fontHeightSum += stringBounds.getHeight();
}
int boxHeight = (int)(fontHeightSum + padding * legendEntries.size());
g.setColor(Color.WHITE);
g.fillRect(minX - 20, padding + y, maxX, boxHeight);
g.setColor(Color.BLACK);
g.drawRect(minX - 20, padding + y, maxX, boxHeight);
// avoid that an entry with identical label and color occurs multiple
// times.
for (Pair<String, Color> legendEntry : legendEntries) {
// System.out.println(legendEntries[i].toString() + "\tcontaines: "
// + set.contains(legendEntries[i]));
g.setColor(legendEntry.tail);
Rectangle2D stringBounds = fm.getStringBounds(legendEntry.head, g);
xOffs = (int) (maxX - stringBounds.getWidth() - 5);
xOffs = (int) (maxX - stringBounds.getWidth() - 10);
g.drawString(legendEntry.head, xOffs, yOffs);
// g.drawString(legendEntries[i].head, 80, 80);
yOffs += (5 + stringBounds.getHeight());
g.drawLine(xOffs - 20, yOffs - (int)(stringBounds.getHeight()/2) + 1, xOffs - padding, yOffs - (int)(stringBounds.getHeight()/2) + 1);
yOffs += (padding + stringBounds.getHeight());
}
g.setColor(origCol);
}

View File

@ -84,6 +84,7 @@ public class StatisticsWithGUI extends AbstractStatistics implements Serializabl
// unite the point sets for a multirun
for (int i = 0; i < fitnessGraph.length; i++) {
for (int j = 0; j < fitnessGraph[i].length; j++) {
// Shown in the legend of the plot window
statGraph[i][j].setInfoString(
(fitnessGraph[i][j].getInfo().length() > 0 ? (fitnessGraph[i][j].getInfo() + "_") : "")
+ "Mean_of_" + fullRuns + " ",

View File

@ -310,6 +310,13 @@ public class DArea extends JComponent implements DParent, Printable {
return grid.isVisible();
}
/**
* Toggle visibility of the grid
*/
public void toggleShowGrid() {
grid.toggleVisible();
}
/**
* paints the DArea by a Graphics object
*

View File

@ -157,4 +157,12 @@ public abstract class DComponent implements DElement {
return visible;
}
/**
* toggles visibility of the component
*/
@Override
public void toggleVisible() {
setVisible(!isVisible());
}
}

View File

@ -22,6 +22,8 @@ public interface DElement {
boolean isVisible();
void toggleVisible();
void setColor(Color color);
Color getColor();