Skip to content
This repository has been archived by the owner on Oct 8, 2021. It is now read-only.

Commit

Permalink
Ensure plot labels are properly displayed in IJ 1.49.
Browse files Browse the repository at this point in the history
A really nice side-effect is that labels will scale with plot, when plot 
window is resized, or when zooming in/out on plot data. Closes #11
  • Loading branch information
tferr committed Jun 15, 2015
1 parent 7e4f685 commit 10704a4
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/main/java/Sholl_Analysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,6 @@ public static void markPlotPoint(final Plot plot, final double[] coordinates,
/** Draws a label at the less "crowded" corner of a plot canvas */
public static void makePlotLabel(final Plot plot, final String label, final Color color) {

final int margin = 7; // Axes internal margin, 1+Plot.TICK_LENGTH
final ImageProcessor ip = plot.getProcessor();

int maxLength = 0; String maxLine = "";
Expand All @@ -2362,31 +2361,41 @@ public static void makePlotLabel(final Plot plot, final String label, final Colo
{ maxLength = length; maxLine = lines[i]; }
}

final Font font = new Font("Helvetica", Font.PLAIN, PlotWindow.fontSize);
ip.setFont(font);
plot.setFont(font);
final FontMetrics metrics = ip.getFontMetrics();
final int textWidth = metrics.stringWidth(maxLine+" ");
final int lineHeight = metrics.getHeight();
final int textHeight = lineHeight * lines.length;
final int textWidth = metrics.stringWidth(maxLine);
final int textHeight = metrics.getHeight() * lines.length;

final int yTop = Plot.TOP_MARGIN + margin + lineHeight;
final int yBottom = Plot.TOP_MARGIN + PlotWindow.plotHeight - textHeight;
final int xLeft = Plot.LEFT_MARGIN + margin;
final int xRight = Plot.LEFT_MARGIN + PlotWindow.plotWidth - margin - textWidth;
final Rectangle r = plot.getDrawingFrame();
final int padding = 4; // space between label and axes
final int yTop = r.y + 1 + padding;
final int yBottom = r.y + r.height - textHeight - padding;
final int xLeft = r.x + 1 + padding;
final int xRight = r.x + r.width - textWidth - padding;

final double northEast = meanRoiValue(ip, xLeft, yTop, textWidth, textHeight);
final double northWest = meanRoiValue(ip, xRight, yTop, textWidth, textHeight);
final double southEast = meanRoiValue(ip, xLeft, yBottom, textWidth, textHeight);
final double southWest = meanRoiValue(ip, xRight, yBottom, textWidth, textHeight);
final double pos = Math.max(Math.max(northEast, northWest), Math.max(southEast,southWest));

ip.setColor(color);
ip.setColor(0);
plot.setColor(color);
// We'll draw the text so that multiple labels can be added without overlap
if (pos==northEast) {
ip.drawString(label, xLeft, yTop);
plot.addText(label, plot.descaleX(xLeft), plot.descaleY(yTop));
} else if (pos==northWest) {
ip.drawString(label, xRight, yTop);
plot.addText(label, plot.descaleX(xRight), plot.descaleY(yTop));
} else if (pos==southEast) {
ip.drawString(label, xLeft, yBottom);
plot.addText(label, plot.descaleX(xLeft), plot.descaleY(yBottom));
} else {
ip.drawString(label, xRight, yBottom);
plot.addText(label, plot.descaleX(xRight), plot.descaleY(yBottom));
}

}
Expand Down

0 comments on commit 10704a4

Please sign in to comment.