package ij.gui;
import java.awt.*;
import java.util.*;
import ij.*;
import ij.process.*;
import ij.util.*;
import ij.plugin.Colors;
import ij.plugin.filter.Analyzer;
import ij.macro.Interpreter;
import ij.measure.Calibration;
import ij.measure.Measurements;
import ij.measure.ResultsTable;
public class Plot implements Cloneable {
public static final int LEFT=ImageProcessor.LEFT_JUSTIFY, CENTER=ImageProcessor.CENTER_JUSTIFY, RIGHT=ImageProcessor.RIGHT_JUSTIFY;
public static final int TOP_LEFT=0x90, TOP_RIGHT=0xA0, BOTTOM_LEFT=0xB0, BOTTOM_RIGHT=0xC0, AUTO_POSITION=0x80;
private static final int LEGEND_POSITION_MASK = 0xf0;
public static final int LEGEND_BOTTOM_UP = 0x100;
public static final int LEGEND_TRANSPARENT = 0x200;
public static final int CIRCLE = 0;
public static final int X = 1;
public static final int LINE = 2;
public static final int BOX = 3;
public static final int TRIANGLE = 4;
public static final int CROSS = 5;
public static final int DOT = 6;
public static final int CONNECTED_CIRCLES = 7;
final static String[] SHAPE_NAMES = new String[] {
"Circle", "X", "Line", "Box", "Triangle", "+", "Dot", "Connected Circles"};
final static String[] SORTED_SHAPES = new String[] {
SHAPE_NAMES[LINE], SHAPE_NAMES[CONNECTED_CIRCLES], SHAPE_NAMES[CIRCLE], SHAPE_NAMES[BOX], SHAPE_NAMES[TRIANGLE],
SHAPE_NAMES[CROSS], SHAPE_NAMES[X], SHAPE_NAMES[DOT] };
public static final int X_NUMBERS = 0x1;
public static final int Y_NUMBERS = 0x2;
public static final int X_TICKS = 0x4;
public static final int Y_TICKS = 0x8;
public static final int X_GRID = 0x10;
public static final int Y_GRID = 0x20;
public static final int X_FORCE2GRID = 0x40;
public static final int Y_FORCE2GRID = 0x80;
public static final int X_MINOR_TICKS = 0x100;
public static final int Y_MINOR_TICKS = 0x200;
public static final int X_LOG_NUMBERS = 0x400;
public static final int Y_LOG_NUMBERS = 0x800;
public static final int X_LOG_TICKS = 0x1000;
public static final int Y_LOG_TICKS = 0x2000;
public static final int DEFAULT_FLAGS = X_NUMBERS + Y_NUMBERS +
X_GRID + Y_GRID + X_LOG_TICKS + Y_LOG_TICKS;
public static final int X_RANGE = 0x1;
public static final int Y_RANGE = 0x2;
public static final int COPY_SIZE = 0x10;
public static final int COPY_LABELS = 0x20;
public static final int COPY_LEGEND = 0x40;
public static final int LEFT_MARGIN = 60;
public static final int RIGHT_MARGIN = 20;
public static final int TOP_MARGIN = 13;
public static final int BOTTOM_MARGIN = 42;
public static final int MIN_FRAMEWIDTH = 160;
public static final int MIN_FRAMEHEIGHT = 90;
public static final String PROPERTY_KEY = "thePlot";
private static final int MIN_X_GRIDSPACING = 45; private static final int MIN_Y_GRIDSPACING = 30; private final double MIN_LOG_RATIO = 3; private static final int LEGEND_PADDING = 4; private static final int LEGEND_LINELENGTH = 20; private static final int USUALLY_ENLARGE = 1, ALWAYS_ENLARGE = 2; private static final double RELATIVE_ARROWHEAD_SIZE = 0.2; private static final int MIN_ARROWHEAD_LENGTH = 3;
private static final int MAX_ARROWHEAD_LENGTH = 20;
static final int ZOOM_AS_PREVIOUS = -20202020; Rectangle frame = null; float scale = 1.0f; int leftMargin = LEFT_MARGIN, rightMargin = RIGHT_MARGIN, topMargin = TOP_MARGIN, bottomMargin = BOTTOM_MARGIN;
int frameWidth; int frameHeight;
double xMin = Double.NaN, xMax, yMin, yMax; double[] currentMinMax = new double[]{Double.NaN, 0, Double.NaN, 0}; double[] defaultMinMax = new double[]{Double.NaN, 0, Double.NaN, 0}; double[] savedMinMax = new double[]{Double.NaN, 0, Double.NaN, 0}; int[] enlargeRange; boolean logXAxis, logYAxis;
Font defaultFont = new Font("Helvetica", Font.PLAIN, PlotWindow.fontSize); Font currentFont; Font xLabelFont; Font yLabelFont;
String xLabel; String yLabel;
int templateFlags = COPY_SIZE | COPY_LABELS | COPY_LEGEND; private double xScale, yScale; private int xBasePxl, yBasePxl; private double previousXZoom = Double.NaN;
private double previousYZoom = Double.NaN;
private int maxIntervals = 12; private int tickLength = 7; private int minorTickLength = 3; private Color gridColor = new Color(0xc0c0c0); private Color frameColor = Color.black;
private int flags; private ImageProcessor ip;
private ImagePlus imp; private boolean frozen; private String title;
private boolean invertedLut; private boolean isColor; private boolean plotDrawn;
private boolean antialiasedText = true;
int plotWidth = PlotWindow.plotWidth;
int plotHeight = PlotWindow.plotHeight;
PlotMaker plotMaker;
Vector<PlotObject> allPlotObjects = new Vector<PlotObject>(); PlotObject legend; private Color currentColor; private Color currentColor2; float currentLineWidth;
float frameLineWidth;
private int currentJustification = LEFT;
private boolean ignoreForce2Grid; private Color backgroundColor;
public Plot(String title, String xLabel, String yLabel, float[] xValues, float[] yValues) {
this(title, xLabel, yLabel, xValues, yValues, getDefaultFlags());
}
public Plot(String title, String xLabel, String yLabel, double[] xValues, double[] yValues) {
this(title, xLabel, yLabel, xValues!=null?Tools.toFloat(xValues):null, yValues!=null?Tools.toFloat(yValues):null, getDefaultFlags());
}
public Plot(String dummy, String title, String xLabel, String yLabel, float[] xValues, float[] yValues) {
this(title, xLabel, yLabel, xValues, yValues, getDefaultFlags());
}
public Plot(String title, String xLabel, String yLabel) {
this(title, xLabel, yLabel, (float[])null, (float[])null, getDefaultFlags());
}
public Plot(String title, String xLabel, String yLabel, int flags) {
this(title, xLabel, yLabel, (float[])null, (float[])null, flags);
}
public Plot(String title, String xLabel, String yLabel, float[] xValues, float[] yValues, int flags) {
this.title = title;
this.flags = flags;
setXYLabels(xLabel, yLabel);
if (yValues != null && yValues.length>0) {
addPoints(xValues, yValues, null, LINE, null);
allPlotObjects.get(0).flags = PlotObject.CONSTRUCTOR_DATA;
}
}
public Plot(String title, String xLabel, String yLabel, double[] xValues, double[] yValues, int flags) {
this(title, xLabel, yLabel, xValues!=null?Tools.toFloat(xValues):null, yValues!=null?Tools.toFloat(yValues):null, flags);
}
public String getTitle() {
return imp == null ? title : imp.getTitle();
}
public void setLimits(double xMin, double xMax, double yMin, double yMax) {
boolean containsNaN = (Double.isNaN(xMin + xMax + yMin + yMax));
if (containsNaN && allPlotObjects.isEmpty()) return;
boolean[] auto = new boolean[4];
double[] range = {xMin, xMax, yMin, yMax};
if (containsNaN) {
double[] extrema = getMinAndMax(true, 0xff);
for (int jj = 0; jj < 4; jj++)
if (Double.isNaN(range[jj])) {
range[jj] = extrema[jj];
auto[jj] = true;
}
double left = range[0];
double right = range[1];
double bottom = range[2];
double top = range[3];
if ((auto[0] || auto[1]) && (left >= right)) {
left = extrema[0];
right = extrema[1];
auto[0] = true;
auto[1] = true;
}
if ((auto[2] || auto[3]) && (bottom >= top)) {
bottom = extrema[2];
top = extrema[3];
auto[2] = true;
auto[3] = true;
}
double extraXLin = (right - left) * 0.03;
double extraYLin = (top - bottom) * 0.03;
double extraXLog = (Math.log(right) - Math.log(left)) * 0.03;
double extraYLog = (Math.log(top) - Math.log(bottom)) * 0.03;
boolean isLogX = hasFlag(X_LOG_NUMBERS);
boolean isLogY = hasFlag(Y_LOG_NUMBERS);
if (auto[0] && !isLogX)
range[0] = left - extraXLin; if (auto[1] && !isLogX)
range[1] = right + extraXLin;
if (auto[2] && !isLogY)
range[2] = bottom - extraYLin;
if (auto[3] && !isLogY)
range[3] = top + extraYLin;
if (auto[0] && isLogX)
range[0] = Math.exp(Math.log(left) - extraXLog); if (auto[1] && isLogX)
range[1] = Math.exp(Math.log(right) + extraXLog);
if (auto[2] && isLogY)
range[2] = Math.exp(Math.log(bottom) - extraYLog);
if (auto[3] && isLogY)
range[3] = Math.exp(Math.log(top) + extraYLog);
}
defaultMinMax = range; enlargeRange = null;
ignoreForce2Grid = true;
if (plotDrawn)
setLimitsToDefaults(true);
}
public double[] getLimits() {
return new double[] {xMin, xMax, yMin, yMax};
}
public void setSize(int width, int height) {
if (ip != null && width == ip.getWidth() && height == ip.getHeight()) return;
Dimension minSize = getMinimumSize();
plotWidth = Math.max(width, minSize.width) - (leftMargin+rightMargin);
plotHeight = Math.max(height, minSize.height) - (topMargin+bottomMargin);
scale = 1.0f;
ip = null;
if (plotDrawn) updateImage();
}
public Dimension getSize() {
if (ip == null)
getBlankProcessor();
return new Dimension(ip.getWidth(), ip.getHeight());
}
public void setFrameSize(int width, int height) {
plotWidth = width;
plotHeight = height;
ip = null;
if (plotDrawn) updateImage();
}
public Dimension getMinimumSize() {
return new Dimension(MIN_FRAMEWIDTH + leftMargin + rightMargin,
MIN_FRAMEHEIGHT + topMargin + bottomMargin);
}
public void useTemplate(Plot plot) {
useTemplate(plot, templateFlags);
}
public void useTemplate(Plot plot, int templateFlags) {
if (plot == null) return;
this.flags = plot.flags;
this.defaultFont = plot.defaultFont;
this.currentFont = plot.currentFont;
this.xLabelFont = plot.xLabelFont;
this.yLabelFont = plot.yLabelFont;
this.currentLineWidth = plot.currentLineWidth;
this.frameLineWidth = plot.frameLineWidth;
this.currentColor = plot.currentColor;
this.frameColor = plot.frameColor;
if ((templateFlags & COPY_LABELS) != 0) {
this.xLabel = plot.xLabel;
this.yLabel = plot.yLabel;
}
if ((templateFlags & COPY_LEGEND) != 0) {
}
for (int i=0; i<currentMinMax.length; i++)
if ((templateFlags>>(i/2)&0x1) != 0) {
currentMinMax[i] = plot.currentMinMax[i];
if (!plotDrawn) defaultMinMax[i] = plot.currentMinMax[i];
}
if ((templateFlags & COPY_LEGEND) != 0) {
if (plot.legend != null)
this.legend = plot.legend.clone();
int plotPObjectIndex = 0;
int plotPObjectNumber = plot.allPlotObjects.size();
for (PlotObject plotObject : allPlotObjects) {
if (plotObject.type == PlotObject.XY_DATA) {
while(plotPObjectIndex<plotPObjectNumber && plot.allPlotObjects.get(plotPObjectIndex).type != PlotObject.XY_DATA)
plotPObjectIndex++; if (plotPObjectIndex>=plotPObjectNumber) break;
plotObject.label = plot.allPlotObjects.get(plotPObjectIndex).label;
}
}
}
setFrameSize(plot.plotWidth, plot.plotHeight);
this.templateFlags = templateFlags;
}
public void setScale(float scale) {
this.scale = scale;
if (scale > 20f) scale = 20f;
if (scale < 0.7f) scale = 0.7f;
plotDrawn = false;
}
public void setXYLabels(String xLabel, String yLabel) {
this.xLabel = xLabel!=null && xLabel.length()>0 ? xLabel : "";
this.yLabel = yLabel!=null && yLabel.length()>0 ? yLabel : "";
}
public void setMaxIntervals(int intervals) {
maxIntervals = intervals;
}
public void setTickLength(int tickLength) {
tickLength = tickLength;
}
public void setMinorTickLength(int minorTickLength) {
minorTickLength = minorTickLength;
}
public void setFormatFlags(int flags) {
int unchangedFlags = X_LOG_NUMBERS | Y_LOG_NUMBERS | X_FORCE2GRID | Y_FORCE2GRID;
flags = flags & (~unchangedFlags); this.flags = (this.flags & unchangedFlags) | flags;
}
public int getFlags() {
return flags;
}
public void setAxisXLog(boolean axisXLog) {
flags = axisXLog ? flags | X_LOG_NUMBERS : flags & (~X_LOG_NUMBERS);
}
public void setAxisYLog(boolean axisYLog) {
flags = axisYLog ? flags | Y_LOG_NUMBERS : flags & (~Y_LOG_NUMBERS);
}
public void setXTicks(boolean xTicks) {
flags = xTicks ? flags | X_TICKS : flags & (~X_TICKS);
}
public void setYTicks(boolean yTicks) {
flags = yTicks ? flags | Y_TICKS : flags & (~Y_TICKS);
}
public void setXMinorTicks(boolean xMinorTicks) {
flags = xMinorTicks ? flags | X_MINOR_TICKS : flags & (~X_MINOR_TICKS);
if (xMinorTicks && !hasFlag(X_GRID))
flags |= X_TICKS;
}
public void setYMinorTicks(boolean yMinorTicks) {
flags = yMinorTicks ? flags | Y_MINOR_TICKS : flags & (~Y_MINOR_TICKS);
if (yMinorTicks && !hasFlag(Y_GRID))
flags |= Y_TICKS;
}
public void setAxes(boolean xLog, boolean yLog, boolean xTicks, boolean yTicks, boolean xMinorTicks, boolean yMinorTicks,
int tickLenght, int minorTickLenght) {
setAxisXLog (xLog);
setAxisYLog (yLog);
setXMinorTicks (xMinorTicks);
setYMinorTicks (yMinorTicks);
setXTicks (xTicks);
setYTicks (yTicks);
setTickLength (tickLenght);
setMinorTickLength(minorTickLenght);
}
public void setLogScaleX() {
setAxisXLog(true);
}
public void setLogScaleY() {
setAxisYLog(true);
}
public static int getDefaultFlags() {
int defaultFlags = 0;
if (!PlotWindow.noGridLines) defaultFlags |= X_GRID | Y_GRID | X_NUMBERS | Y_NUMBERS | X_LOG_TICKS | Y_LOG_TICKS;
if (!PlotWindow.noTicks)
defaultFlags |= X_TICKS | Y_TICKS | X_MINOR_TICKS | Y_MINOR_TICKS | X_NUMBERS | Y_NUMBERS | X_LOG_TICKS | Y_LOG_TICKS;
return defaultFlags;
}
public void addPoints(float[] xValues, float[] yValues, float[] yErrorBars, int shape, String label) {
if (xValues==null || xValues.length==0) {
xValues = new float[yValues.length];
for (int i=0; i<yValues.length; i++)
xValues[i] = i;
}
allPlotObjects.add(new PlotObject(xValues, yValues, yErrorBars, shape, currentLineWidth, currentColor, currentColor2, label));
if (plotDrawn) updateImage();
}
public void addPoints(float[] x, float[] y, int shape) {
addPoints(x, y, null, shape, null);
}
public void addPoints(double[] x, double[] y, int shape) {
addPoints(Tools.toFloat(x), Tools.toFloat(y), shape);
}
public void addPoints(String dummy, float[] x, float[] y, int shape) {
addPoints(x, y, shape);
}
public void add(String shape, double[] x, double[] y) {
addPoints(Tools.toFloat(x), Tools.toFloat(y), null, toShape(shape), null);
}
public static int toShape(String str) {
str = str.toLowerCase(Locale.US);
int shape = Plot.CIRCLE;
if (str.contains("curve") || str.contains("line"))
shape = Plot.LINE;
if (str.contains("connected"))
shape = Plot.CONNECTED_CIRCLES;
else if (str.contains("box"))
shape = Plot.BOX;
else if (str.contains("triangle"))
shape = Plot.TRIANGLE;
else if (str.contains("cross") || str.contains("+"))
shape = Plot.CROSS;
else if (str.contains("dot"))
shape = Plot.DOT;
else if (str.contains("xerror"))
shape = -2;
else if (str.contains("error"))
shape = -1;
else if (str.contains("x"))
shape = Plot.X;
return shape;
}
public void addPoints(ArrayList x, ArrayList y, int shape) {
addPoints(getDoubleFromArrayList(x), getDoubleFromArrayList(y), shape);
}
public void addPoints(double[] x, double[] y, double[] errorBars, int shape) {
addPoints(Tools.toFloat(x), Tools.toFloat(y), Tools.toFloat(errorBars), shape, null);
}
public void addPoints(ArrayList x, ArrayList y, ArrayList z, int shape) {
addPoints(getDoubleFromArrayList(x), getDoubleFromArrayList(y), getDoubleFromArrayList(z), shape);
}
public double[] getDoubleFromArrayList(ArrayList list) {
double[] targ = new double[list.size()];
for (int i = 0; i < list.size(); i++)
targ[i] = ((Double) list.get(i)).doubleValue();
return targ;
}
public void drawVectors(double[] x1, double[] y1, double[] x2, double[] y2) {
allPlotObjects.add(new PlotObject(Tools.toFloat(x1), Tools.toFloat(y1),
Tools.toFloat(x2), Tools.toFloat(y2), currentLineWidth, currentColor));
}
public static double calculateDistance(int x1, int y1, int x2, int y2) {
return java.lang.Math.sqrt((x2 - x1)*(double)(x2 - x1) + (y2 - y1)*(double)(y2 - y1));
}
public void drawVectors(ArrayList x1, ArrayList y1, ArrayList x2, ArrayList y2) {
drawVectors(getDoubleFromArrayList(x1), getDoubleFromArrayList(y1), getDoubleFromArrayList(x2), getDoubleFromArrayList(y2));
}
public void addErrorBars(float[] errorBars) {
PlotObject mainObject = getLastCurveObject();
if (mainObject != null)
mainObject.yEValues = errorBars;
else throw new RuntimeException("Plot can't add y error bars without data");
}
public void addErrorBars(double[] errorBars) {
addErrorBars(Tools.toFloat(errorBars));
}
public void addErrorBars(String dummy, float[] errorBars) {
addErrorBars(errorBars);
}
public void addHorizontalErrorBars(double[] xErrorBars) {
PlotObject mainObject = getLastCurveObject();
if (mainObject != null)
mainObject.xEValues = Tools.toFloat(xErrorBars);
else throw new RuntimeException("Plot can't add x error bars without data");
}
public void addLabel(double x, double y, String label) {
allPlotObjects.add(new PlotObject(label, x, y, currentJustification, currentFont, currentColor, PlotObject.NORMALIZED_LABEL));
}
public void addText(String label, double x, double y) {
allPlotObjects.add(new PlotObject(label, x, y, currentJustification, currentFont, currentColor, PlotObject.LABEL));
}
public void addLegend(String labels) {
addLegend(labels, null);
}
public void addLegend(String labels, String options) {
int flags = Plot.AUTO_POSITION;
if (options!=null) {
options = options.toLowerCase();
if (options.contains("top-left"))
flags |= Plot.TOP_LEFT;
else if (options.contains("top-right"))
flags |= Plot.TOP_RIGHT;
else if (options.contains("bottom-left"))
flags |= Plot.BOTTOM_LEFT;
else if (options.contains("bottom-right"))
flags |= Plot.BOTTOM_RIGHT;
if (options.contains("bottom-to-top"))
flags |= Plot.LEGEND_BOTTOM_UP;
if (options.contains("transparent"))
flags |= Plot.LEGEND_TRANSPARENT;
}
setLegend(labels, flags);
}
public void setLegend(String labels, int flags) {
legend = new PlotObject(labels, currentLineWidth == 0 ? 1 : currentLineWidth,
currentFont == null ? defaultFont : currentFont, currentColor == null ? Color.black : currentColor, flags);
if (plotDrawn) updateImage();
}
public void setJustification(int justification) {
currentJustification = justification;
}
public void setColor(Color c) {
currentColor = c;
currentColor2 = null;
checkForColor(c);
}
public void setColor(String color) {
setColor(Colors.getColor(color, Color.black));
}
public void setColor(Color c, Color c2) {
currentColor = c;
currentColor2 = c2;
checkForColor(c);
checkForColor(c2);
}
public void setColor(String c1, String c2) {
setColor(Colors.getColor(c1,Color.black), Colors.getColor(c2,Color.black));
}
public void setBackgroundColor(Color c) {
backgroundColor = c;
checkForColor(c);
}
public void setBackgroundColor(String c) {
setBackgroundColor(Colors.getColor(c,Color.white));
}
public void setLineWidth(int lineWidth) {
currentLineWidth = lineWidth;
}
public void setLineWidth(float lineWidth) {
currentLineWidth = lineWidth;
}
public void drawLine(double x1, double y1, double x2, double y2) {
allPlotObjects.add(new PlotObject(x1, y1, x2, y2, currentLineWidth, 0, currentColor, PlotObject.LINE));
}
public void drawNormalizedLine(double x1, double y1, double x2, double y2) {
allPlotObjects.add(new PlotObject(x1, y1, x2, y2, currentLineWidth, 0, currentColor, PlotObject.NORMALIZED_LINE));
}
public void drawDottedLine(double x1, double y1, double x2, double y2, int step) {
allPlotObjects.add(new PlotObject(x1, y1, x2, y2, currentLineWidth, step, currentColor, PlotObject.DOTTED_LINE));
}
public void setFont(Font font) {
currentFont = font;
}
public void setFont(int style, float size) {
if (size < 9) size = 9f;
if (size > 24) size = 24f;
Font previousFont = currentFont == null ? defaultFont : currentFont;
if (style < 0) style = previousFont.getStyle();
setFont(previousFont.deriveFont(size));
}
public void setAxisLabelFont(int style, float size) {
if (size < 9) size = 9f;
if (size > 33) size = 33f;
Font usualFont = currentFont == null ? defaultFont : currentFont;
if (xLabelFont == null) xLabelFont = usualFont;
if (yLabelFont == null) yLabelFont = usualFont;
setXLabelFont(xLabelFont.deriveFont(style < 0 ? xLabelFont.getStyle() : style, size));
setYLabelFont(xLabelFont.deriveFont(style < 0 ? yLabelFont.getStyle() : style, size));
}
public void setXLabelFont(Font font) {
xLabelFont = font;
}
public void setYLabelFont(Font font) {
yLabelFont = font;
}
public void setAntialiasedText(boolean antialiasedText) {
this.antialiasedText = antialiasedText;
}
public void changeFont(Font font) {
setFont(font);
}
public float[] getXValues() {
PlotObject p = getMainCurveObject();
return p==null ? null : p.xValues;
}
public float[] getYValues() {
PlotObject p = getMainCurveObject();
return p==null ? null : p.yValues;
}
public String[] getPlotObjectDesignations() {
int nObjects = allPlotObjects.size();
String[] names = new String[nObjects];
if (names.length == 0) return names;
String[] labels = null;
if (legend != null && legend.label != null)
labels = legend.label.split("[\t\n]");
int iData = 1, iArrow = 1, iLine = 1, iText = 1; int firstObject = allPlotObjects.get(0).hasFlag(PlotObject.CONSTRUCTOR_DATA) ? 1 : 0; for (int i=0, p=firstObject; i<nObjects; i++, p++) {
if (p >= allPlotObjects.size()) p = 0; PlotObject plotObject = allPlotObjects.get(p);
int type = plotObject.type;
String label = plotObject.label;
switch (type) {
case PlotObject.XY_DATA:
names[i] = "Data Set "+iData+": "+(labels!=null && labels.length>=iData ?
labels[iData-1] : "(" + plotObject.yValues.length + " data points)");
iData++;
break;
case PlotObject.ARROWS:
names[i] = "Arrow Set "+iArrow+" ("+ plotObject.xValues.length + ")";
iArrow++;
break;
case PlotObject.LINE: case PlotObject.NORMALIZED_LINE: case PlotObject.DOTTED_LINE:
String detail = "";
if (type == PlotObject.DOTTED_LINE) detail = "dotted ";
if (plotObject.x ==plotObject.xEnd) detail += "vertical";
else if (plotObject.y ==plotObject.yEnd) detail += "horizontal";
if (detail.length()>0) detail = " ("+detail.trim()+")";
names[i] = "Straight Line "+iLine+detail;
iLine++;
break;
case PlotObject.LABEL: case PlotObject.NORMALIZED_LABEL:
String text = plotObject.label.replaceAll("\n"," ");
if (text.length()>45) text = text.substring(0, 40)+"...";
names[i] = "Text "+iText+": \""+text+'"';
iText++;
break;
}
}
return names;
}
public String getPlotObjectStyles(int i) {
if (allPlotObjects.get(0).hasFlag(PlotObject.CONSTRUCTOR_DATA)) i++;
if (i == allPlotObjects.size()) i = 0; PlotObject plotObject = allPlotObjects.get(i);
String styleString = Colors.colorToString(plotObject.color) + "," +
Colors.colorToString(plotObject.color2) + "," +
plotObject.lineWidth;
if (plotObject.type == PlotObject.XY_DATA)
styleString += ","+SHAPE_NAMES[plotObject.shape];
return styleString;
}
public void setPlotObjectStyles(int i, String styleString) {
if (allPlotObjects.get(0).hasFlag(PlotObject.CONSTRUCTOR_DATA)) i++;
if (i == allPlotObjects.size()) i = 0; PlotObject plotObject = allPlotObjects.get(i);
String[] items = styleString.split(",");
plotObject.color = Colors.decode(items[0].trim(), plotObject.color);
plotObject.color2 = Colors.decode(items[1].trim(), null);
checkForColor(plotObject.color);
checkForColor(plotObject.color2);
float lineWidth = plotObject.lineWidth;
if (items.length >= 3) try {
plotObject.lineWidth = Float.parseFloat(items[2].trim());
} catch (NumberFormatException e) {};
if (items.length >= 4)
plotObject.shape = toShape(items[3].trim());
updateImage();
return;
}
public void setLimitsToDefaults(boolean updateImg) {
saveMinMax();
System.arraycopy(defaultMinMax, 0, currentMinMax, 0, defaultMinMax.length);
if (plotDrawn && updateImg) updateImage();
}
public void setLimitsToFit(boolean updateImg) {
saveMinMax();
currentMinMax = getMinAndMax(true, 0xff);
if (Double.isNaN(defaultMinMax[0]))
System.arraycopy(currentMinMax, 0, defaultMinMax, 0, currentMinMax.length);
if (plotDrawn && updateImg) updateImage();
}
public void setPreviousMinMax() {
if (Double.isNaN(savedMinMax[0])) return; double[] swap = new double[currentMinMax.length];
System.arraycopy(currentMinMax, 0, swap, 0, currentMinMax.length);
System.arraycopy(savedMinMax, 0, currentMinMax, 0, currentMinMax.length);
System.arraycopy(swap, 0, savedMinMax, 0, currentMinMax.length);
updateImage();
}
public ImageProcessor getProcessor() {
draw();
return ip;
}
public ImagePlus getImagePlus() {
if (plotDrawn)
updateImage();
else
draw();
if (imp != null) {
if (imp.getProcessor() != ip) imp.setProcessor(ip);
return imp;
} else {
ImagePlus imp = new ImagePlus(title, ip);
setImagePlus(imp);
return imp;
}
}
public void setImagePlus(ImagePlus imp) {
if (this.imp != null)
this.imp.setProperty(PROPERTY_KEY, null);
this.imp = imp;
if (imp != null) {
imp.setIgnoreGlobalCalibration(true);
adjustCalibration(imp.getCalibration());
imp.setProperty(PROPERTY_KEY, this);
}
}
public void adjustCalibration(Calibration cal) {
if (xMin == xMax) xScale = 1e6;
if (yMin == yMax)
yScale = 1e6;
cal.xOrigin = xBasePxl-xMin*xScale;
cal.pixelWidth = 1.0/Math.abs(xScale); cal.yOrigin = yBasePxl+yMin*yScale;
cal.pixelHeight = 1.0/Math.abs(yScale);
cal.setInvertY(yScale >= 0);
if (xMin == xMax)
xScale = Double.POSITIVE_INFINITY;
if (yMin == yMax)
yScale = Double.POSITIVE_INFINITY;
}
public PlotWindow show() {
if ((IJ.macroRunning() && IJ.getInstance()==null) || Interpreter.isBatchMode()) {
imp = getImagePlus();
WindowManager.setTempCurrentImage(imp);
if (getMainCurveObject() != null) {
imp.setProperty("XValues", getXValues()); imp.setProperty("YValues", getYValues()); }
Interpreter.addBatchModeImage(imp);
return null;
}
if (imp != null) {
Window win = imp.getWindow();
if (win instanceof PlotWindow && win.isVisible()) {
updateImage(); return (PlotWindow)win;
} else
setImagePlus(null);
}
PlotWindow pw = new PlotWindow(this); if (IJ.isMacro() && imp!=null) IJ.selectWindow(imp.getID());
return pw;
}
public void draw() {
if (plotDrawn) return;
getInitialMinAndMax();
getBlankProcessor();
drawContents(ip);
}
public void setFrozen(boolean frozen) {
this.frozen = frozen;
if (!frozen) { if (imp != null && ip != null) {
ImageCanvas ic = imp.getCanvas();
if (ic instanceof PlotCanvas)
((PlotCanvas)ic).resetMagnification();
}
updateImage();
ImageWindow win = imp.getWindow();
if (win != null) win.updateImage(imp); }
}
public boolean isFrozen() {
return frozen;
}
public void updateImage() {
if (!plotDrawn || frozen) return;
getBlankProcessor();
drawContents(ip);
if (imp == null) return;
adjustCalibration(imp.getCalibration());
imp.updateAndDraw();
}
public Rectangle getDrawingFrame() {
if (frame == null)
getBlankProcessor(); return new Rectangle(frame.x, frame.y, frameWidth, frameHeight);
}
public ImagePlus makeHighResolution(String title, float scale, boolean antialiasedText, boolean showIt) {
Plot hiresPlot = null;
try {
hiresPlot = (Plot)clone(); } catch (Exception e) {return null;}
hiresPlot.ip = null;
hiresPlot.imp = null;
if (!plotDrawn) hiresPlot.getInitialMinAndMax();
hiresPlot.setScale(scale);
hiresPlot.setAntialiasedText(antialiasedText);
hiresPlot.defaultMinMax = currentMinMax.clone();
ImageProcessor hiresIp = hiresPlot.getProcessor();
if (title == null || title.length() == 0)
title = getTitle()+"_HiRes";
title = WindowManager.makeUniqueName(title);
ImagePlus hiresImp = new ImagePlus(title, hiresIp);
Calibration cal = hiresImp.getCalibration();
hiresPlot.adjustCalibration(cal);
if (showIt) {
hiresImp.setIgnoreGlobalCalibration(true);
hiresImp.show();
}
hiresPlot.dispose(); return hiresImp;
}
public void dispose() {
if (imp != null)
imp.setProperty(PROPERTY_KEY, null);
imp = null;
ip = null;
}
public double descaleX(int x) {
if (xMin == xMax) return xMin;
double xv = (x-xBasePxl)/xScale + xMin;
if (logXAxis) xv = Math.pow(10, xv);
return xv;
}
public double descaleY(int y) {
if (yMin == yMax) return yMin;
double yv = (yBasePxl-y)/yScale +yMin;
if (logYAxis) yv = Math.pow(10, yv);
return yv;
}
public double scaleXtoPxl(double x) {
if (xMin == xMax) {
if (x==xMin) return xBasePxl;
else return x>xMin ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
if (logXAxis)
return xBasePxl+(Math.log10(x)-xMin)*xScale;
else
return xBasePxl+(x-xMin)*xScale;
}
public double scaleYtoPxl(double y) {
if (yMin == yMax) {
if (y==xMin) return yBasePxl;
else return y>yMin ? Double.POSITIVE_INFINITY : Double.NEGATIVE_INFINITY;
}
if (logYAxis)
return yBasePxl-(Math.log10(y)-yMin)*yScale;
else
return yBasePxl-(y-yMin)*yScale;
}
private int scaleX(double x) {
if (xMin == xMax) {
if (x==xMin) return xBasePxl;
else return x>xMin ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
if (logXAxis)
return xBasePxl+(int)Math.round((Math.log10(x)-xMin)*xScale);
else
return xBasePxl+(int)Math.round((x-xMin)*xScale);
}
private int scaleY(double y) {
if (yMin == yMax) {
if (y==yMin) return yBasePxl;
else return y>yMin ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
if (logYAxis)
return yBasePxl-(int)Math.round((Math.log10(y)-yMin)*yScale);
else
return yBasePxl-(int)Math.round((y-yMin)*yScale);
}
private int scaleXWithOverflow(double x) {
if (!logXAxis || x>0)
return scaleX(x);
else
return xScale > 0 ? -1 : ip.getWidth();
}
private int scaleYWithOverflow(double y) {
if (!logYAxis || y>0)
return scaleY(y);
else
return yScale > 0 ? ip.getHeight() : -1;
}
int sc(float length) {
int pixels = (int)(length*scale + 0.5);
if (pixels < 1) pixels = 1;
return pixels;
}
Font scFont(Font font) {
float size = font.getSize2D();
return scale==1 ? font : font.deriveFont(size*scale);
}
void checkForColor(Color c) {
if (c == null) return;
if (c.getRed() != c.getGreen() || c.getGreen() != c.getBlue())
isColor = true;
}
void drawContents(ImageProcessor ip) {
makeRangeGetSteps();
ip.setColor(Color.black);
ip.setLineWidth(sc(1));
float lineWidth = 1;
Color color = Color.black;
Font font = defaultFont;
for (PlotObject plotObject : allPlotObjects)
if (!plotObject.hasFlag(PlotObject.CONSTRUCTOR_DATA)) {
if (plotObject.lineWidth > 0)
lineWidth = plotObject.lineWidth;
else
plotObject.lineWidth = lineWidth;
if (plotObject.color != null)
color = plotObject.color;
else
plotObject.color = color;
if (plotObject.font != null)
font = plotObject.font;
else
plotObject.font = font;
drawPlotObject(plotObject, ip);
}
if (allPlotObjects.size()>0 && allPlotObjects.get(0).hasFlag(PlotObject.CONSTRUCTOR_DATA)) {
PlotObject mainPlotObject = allPlotObjects.get(0);
if (mainPlotObject.lineWidth == 0)
mainPlotObject.lineWidth = currentLineWidth == 0 ? 1 : currentLineWidth;
lineWidth = mainPlotObject.lineWidth;
if (mainPlotObject.color == null)
mainPlotObject.color = currentColor == null ? Color.black : currentColor;
drawPlotObject(mainPlotObject, ip);
} else {
if (currentLineWidth > 0) lineWidth = currentLineWidth; }
frameLineWidth = lineWidth;
if (frameLineWidth == 0) frameLineWidth = 1;
if (frameLineWidth > 3) frameLineWidth = 3;
ip.setLineWidth(sc(frameLineWidth));
ip.setColor(frameColor);
int x2 = frame.x + frame.width - 1;
int y2 = frame.y + frame.height - 1;
ip.moveTo(frame.x, frame.y); ip.lineTo(x2, frame.y);
ip.lineTo(x2, y2);
ip.lineTo(frame.x, y2);
ip.lineTo(frame.x, frame.y);
if (legend != null && (legend.flags & LEGEND_POSITION_MASK) != 0)
drawPlotObject(legend, ip);
plotDrawn = true;
}
ImageProcessor getBlankProcessor() {
float marginScale = 0.2f + 0.8f*(currentFont == null ? defaultFont : currentFont).getSize2D()/12f;
if (marginScale < 0.7f) marginScale = 0.7f;
if (marginScale > 2f) marginScale = 2f;
leftMargin = sc(LEFT_MARGIN*marginScale);
rightMargin = sc(RIGHT_MARGIN*marginScale);
topMargin = sc(TOP_MARGIN*marginScale);
bottomMargin = sc(BOTTOM_MARGIN*marginScale);
frameWidth = sc(plotWidth);
frameHeight = sc(plotHeight);
int width = frameWidth + leftMargin + rightMargin;
int height = frameHeight + topMargin + bottomMargin;
if (ip == null || width != ip.getWidth() || height != ip.getHeight() || (isColor && (ip instanceof ByteProcessor))) {
if (isColor) {
ip = new ColorProcessor(width, height);
} else {
ip = new ByteProcessor(width, height);
invertedLut = Prefs.useInvertingLut && !Interpreter.isBatchMode() && IJ.getInstance()!=null;
if (invertedLut) ip.invertLut();
}
if (imp != null) imp.setProcessor(ip);
}
if (ip instanceof ColorProcessor)
Arrays.fill((int[])(ip.getPixels()), 0xffffff);
else
Arrays.fill((byte[])(ip.getPixels()), invertedLut ? (byte)0 : (byte)0xff);
ip.setFont(scFont(defaultFont));
ip.setLineWidth(sc(1));
ip.setAntialiasedText(antialiasedText);
frame = new Rectangle(leftMargin, topMargin, frameWidth+1, frameHeight+1);
if (backgroundColor!=null) {
ip.setColor(backgroundColor);
ip.setRoi(frame);
ip.fill();
ip.resetRoi();
}
ip.setColor(Color.black);
return ip;
}
double[] makeRangeGetSteps() {
double[] steps = new double[2];
logXAxis = hasFlag(X_LOG_NUMBERS);
logYAxis = hasFlag(Y_LOG_NUMBERS);
for (int i=0; i<currentMinMax.length; i+=2) { boolean logAxis = hasFlag(i==0 ? X_LOG_NUMBERS : Y_LOG_NUMBERS);
double range = currentMinMax[i+1]-currentMinMax[i];
double mid = 0.5*(currentMinMax[i+1]+currentMinMax[i]);
double relativeRange = Math.abs(range/mid);
if (!logAxis)
relativeRange = Math.min(relativeRange, Math.abs(range/(defaultMinMax[i+1]-defaultMinMax[i])));
if (range != 0 && relativeRange<1e-4) {
currentMinMax[i+1] = mid + 0.5*range*1e-4/relativeRange;
currentMinMax[i] = mid - 0.5*range*1e-4/relativeRange;
}
if (logAxis) {
double rangeRatio = currentMinMax[i+1]/currentMinMax[i];
if (!(rangeRatio > MIN_LOG_RATIO || 1./rangeRatio > MIN_LOG_RATIO) ||
!(currentMinMax[i] > 10*Float.MIN_VALUE) || !(currentMinMax[i+1] > 10*Float.MIN_VALUE))
logAxis = false;
}
if (logAxis) {
currentMinMax[i] = Math.log10(currentMinMax[i]);
currentMinMax[i+1] = Math.log10(currentMinMax[i+1]);
}
if ((i==0 && !simpleXAxis()) || (i==2 && !simpleYAxis())) {
int minGridspacing = i==0 ? MIN_X_GRIDSPACING : MIN_Y_GRIDSPACING;
int frameSize = i==0 ? frameWidth : frameHeight;
double step = Math.abs((currentMinMax[i+1] - currentMinMax[i]) *
Math.max(1.0/maxIntervals, (float)sc(minGridspacing)/frameSize+0.06)); step = niceNumber(step);
if (logAxis && step < 1)
step = 1;
steps[i/2] = step;
boolean force2grid = hasFlag(i==0 ? X_FORCE2GRID : Y_FORCE2GRID) && !ignoreForce2Grid;
if (force2grid) {
int i1 = (int)Math.floor(Math.min(currentMinMax[i],currentMinMax[i+1])/step+1.e-10);
int i2 = (int)Math.ceil (Math.max(currentMinMax[i],currentMinMax[i+1])/step-1.e-10);
if (currentMinMax[i+1] > currentMinMax[i]) { currentMinMax[i] = i1 * step;
currentMinMax[i+1] = i2 * step;
} else {
currentMinMax[i] = i2 * step;
currentMinMax[i+1] = i1 * step;
}
} else if (enlargeRange != null) {
range = currentMinMax[i+1]-currentMinMax[i];
double tmpMin = currentMinMax[i] - 0.015*range;
if (enlargeRange[i] == USUALLY_ENLARGE) currentMinMax[i] = (tmpMin*currentMinMax[i] <= 0) ? 0 : tmpMin;
else if (enlargeRange[i] == ALWAYS_ENLARGE) currentMinMax[i] = tmpMin;
double tmpMax = currentMinMax[i+1] + 0.015*range;
if (enlargeRange[i+1] == USUALLY_ENLARGE)
currentMinMax[i+1] = (tmpMax*currentMinMax[i+1] <= 0) ? 0 : tmpMax;
else if (enlargeRange[i+1] == ALWAYS_ENLARGE)
currentMinMax[i+1] = tmpMax;
}
}
if (i==0) {
xMin = currentMinMax[i];
xMax = currentMinMax[i+1];
logXAxis = logAxis;
} else {
yMin = currentMinMax[i];
yMax = currentMinMax[i+1];
logYAxis = logAxis;
}
if (logAxis) {
currentMinMax[i] = Math.pow(10, currentMinMax[i]);
currentMinMax[i+1] = Math.pow(10, currentMinMax[i+1]);
}
}
enlargeRange = null;
ignoreForce2Grid = false;
xBasePxl = leftMargin;
yBasePxl = topMargin + frameHeight;
xScale = frameWidth/(xMax-xMin);
if (!(xMax-xMin!=0.0)) xBasePxl += sc(10);
yScale = frameHeight/(yMax-yMin);
if (!(yMax-yMin!=0.0))
yBasePxl -= sc(10);
drawAxesTicksGridNumbers(steps);
return steps;
}
void getInitialMinAndMax() {
int axisFlags = 0;
if (Double.isNaN(defaultMinMax[0])) axisFlags |= X_RANGE;
if (Double.isNaN(defaultMinMax[2])) axisFlags |= Y_RANGE;
if (axisFlags != 0)
defaultMinMax = getMinAndMax(false, axisFlags);
setLimitsToDefaults(false); }
double[] getMinAndMax(boolean allObjects, int axisFlags) {
double[] allMinMax = new double[]{Double.MAX_VALUE, -Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE};
for (int i=0; i<allMinMax.length; i++)
if (((axisFlags>>i/2) & 1)==0) allMinMax[i] = defaultMinMax[i];
enlargeRange = new int[allMinMax.length];
for (PlotObject plotObject : allPlotObjects) {
if (plotObject.type == PlotObject.XY_DATA || plotObject.type == PlotObject.ARROWS) {
getMinAndMax(allMinMax, enlargeRange, plotObject, axisFlags);
if (!allObjects) break;
}
}
return allMinMax;
}
void getMinAndMax(double[] allMinAndMax, int[] enlargeRange, PlotObject plotObject, int axisFlags) {
if (plotObject.type == PlotObject.XY_DATA) {
if ((axisFlags & X_RANGE) != 0) {
int suggestedEnlarge = 0;
if (plotObject.shape==DOT || plotObject.yEValues != null) suggestedEnlarge = ALWAYS_ENLARGE;
else if (plotObject.shape != LINE)
suggestedEnlarge = USUALLY_ENLARGE;
getMinAndMax(allMinAndMax, enlargeRange, suggestedEnlarge, 0, plotObject.xValues, plotObject.xEValues);
}
if ((axisFlags & Y_RANGE) != 0) {
int suggestedEnlarge = 0;
if (plotObject.shape==DOT || plotObject.xEValues != null) suggestedEnlarge = ALWAYS_ENLARGE;
else if (plotObject.shape != LINE)
suggestedEnlarge = USUALLY_ENLARGE;
getMinAndMax(allMinAndMax, enlargeRange, suggestedEnlarge, 2, plotObject.yValues, plotObject.yEValues);
}
} else if (plotObject.type == PlotObject.ARROWS) {
if ((axisFlags & X_RANGE) != 0) {
getMinAndMax(allMinAndMax, enlargeRange, ALWAYS_ENLARGE, 0, plotObject.xValues, null);
getMinAndMax(allMinAndMax, enlargeRange, ALWAYS_ENLARGE, 0, plotObject.xEValues, null);
}
if ((axisFlags & Y_RANGE) != 0) {
getMinAndMax(allMinAndMax, enlargeRange, ALWAYS_ENLARGE, 2, plotObject.yValues, null);
getMinAndMax(allMinAndMax, enlargeRange, ALWAYS_ENLARGE, 2, plotObject.yEValues, null);
}
}
}
void getMinAndMax(double[] allMinAndMax, int[] enlargeRange, int suggestedEnlarge,
int axisIndex, float[] data, float[] errorBars) {
int nMinEqual = 0, nMaxEqual = 0;
for (int i=0; i<data.length; i++) {
double v1 = data[i];
double v2 = data[i];
if (errorBars != null && i<errorBars.length) {
v1 -= errorBars[i];
v2 += errorBars[i];
}
if (v1 < allMinAndMax[axisIndex]) {
allMinAndMax[axisIndex] = v1;
nMinEqual = 1;
enlargeRange[axisIndex] = suggestedEnlarge;
if (suggestedEnlarge == 0 && i>0 && i<data.length-1) enlargeRange[axisIndex] = USUALLY_ENLARGE;
} else if (v1 == allMinAndMax[axisIndex])
nMinEqual++;
if (v2 > allMinAndMax[axisIndex+1]) {
allMinAndMax[axisIndex+1] = v2;
nMaxEqual = 1;
enlargeRange[axisIndex+1] = suggestedEnlarge;
if (suggestedEnlarge == 0 && i>0 && i<data.length-1) enlargeRange[axisIndex+1] = USUALLY_ENLARGE;
} else if (v2 == allMinAndMax[axisIndex+1])
nMaxEqual++;
}
if (enlargeRange[axisIndex] == 0 && nMinEqual > 2 && nMinEqual*10 > data.length)
enlargeRange[axisIndex] = USUALLY_ENLARGE;
if (enlargeRange[axisIndex+1] == 0 && nMaxEqual > 2 && nMaxEqual*10 > data.length)
enlargeRange[axisIndex+1] = USUALLY_ENLARGE;
if (nMinEqual == data.length)
enlargeRange[axisIndex] = ALWAYS_ENLARGE;
if (nMaxEqual == data.length)
enlargeRange[axisIndex+1] = ALWAYS_ENLARGE;
if (nMinEqual>0 && enlargeRange[axisIndex]<suggestedEnlarge)
enlargeRange[axisIndex] = suggestedEnlarge;
if (nMaxEqual>0 && enlargeRange[axisIndex+1]<suggestedEnlarge)
enlargeRange[axisIndex+1] = suggestedEnlarge;
}
void saveMinMax() {
System.arraycopy(currentMinMax, 0, savedMinMax, 0, currentMinMax.length);
}
void zoomToRect(Rectangle r) {
saveMinMax();
currentMinMax[0] = descaleX(r.x);
currentMinMax[1] = descaleX(r.x + r.width);
currentMinMax[2] = descaleY(r.y + r.height);
currentMinMax[3] = descaleY(r.y);
updateImage();
}
void zoomOnRangeArrow(int arrowIndex) {
if (arrowIndex < 8) { int axisIndex = (arrowIndex / 4) * 2; double min = axisIndex == 0 ? xMin : yMin;
double max = axisIndex == 0 ? xMax : yMax;
double range = max - min;
boolean isMin = (arrowIndex % 4) < 2;
boolean shrinkRange = arrowIndex % 4 == 1 || arrowIndex % 4 == 2;
double factor = Math.sqrt(2);
if (shrinkRange)
factor = 1.0 / factor;
if (isMin)
min = max - range * factor;
else
max = min + range * factor;
boolean logAxis = axisIndex == 0 ? logXAxis : logYAxis;
if (logAxis) {
min = Math.pow(10, min);
max = Math.pow(10, max);
}
currentMinMax[axisIndex] = min;
currentMinMax[axisIndex + 1] = max;
}
if (arrowIndex == 8)
setLimitsToDefaults(false);
updateImage();
}
void zoom(int x, int y, double zoomFactor) {
boolean zoomIn = zoomFactor > 1.0;
boolean zoomAsPrevious = x==ZOOM_AS_PREVIOUS && (!Double.isNaN(previousXZoom) || !Double.isNaN(previousYZoom));
if (!zoomAsPrevious) {
previousXZoom = Double.NaN;
previousYZoom = Double.NaN;
saveMinMax();
}
boolean cursorLeft = x >= 0 && x<leftMargin-1;
boolean cursorBottom = y>topMargin+frameHeight+1;
boolean zoomX = (!cursorLeft && !zoomAsPrevious) || (!Double.isNaN(previousXZoom) && zoomAsPrevious);
boolean zoomY = cursorLeft || !cursorBottom || (!Double.isNaN(previousYZoom) && zoomAsPrevious);
if (cursorLeft && cursorBottom) x = -1;
for (int axisIndex = 0; axisIndex<currentMinMax.length; axisIndex+=2) {
if (axisIndex==0 && !zoomX) continue;
if (axisIndex==2 && !zoomY) continue;
boolean logAxis = axisIndex==0 ? logXAxis : logYAxis;
double min = axisIndex==0 ? xMin : yMin;
double max = axisIndex==0 ? xMax : yMax;
double mid = 0.5 * (min + max);
if (zoomAsPrevious) {
mid = axisIndex==0 ? previousXZoom : previousYZoom;
if (logAxis) mid = Math.log10(mid);
}
double span = max - min;
if (x >= 0) { mid = axisIndex==0 ? descaleX(x) : descaleY(y);
if (logAxis) mid = Math.log10(mid);
}
if (axisIndex==0)
previousXZoom = logAxis ? Math.pow(10, mid) : mid;
else
previousYZoom = logAxis ? Math.pow(10, mid) : mid;
double newHalfSpan = 0.5 * span / zoomFactor;
currentMinMax[axisIndex] = mid - newHalfSpan;
currentMinMax[axisIndex+1] = mid + newHalfSpan;
if (logAxis) {
currentMinMax[axisIndex] = Math.pow(10, currentMinMax[axisIndex]);
currentMinMax[axisIndex+1] = Math.pow(10, currentMinMax[axisIndex+1]);
}
}
updateImage();
}
void scroll(int dx, int dy) {
if (logXAxis) {
currentMinMax[0] /= Math.pow(10, dx/xScale);
currentMinMax[1] /= Math.pow(10, dx/xScale);
} else {
currentMinMax[0] -= dx/xScale;
currentMinMax[1] -= dx/xScale;
}
if (logYAxis) {
currentMinMax[2] *= Math.pow(10, dy/yScale);
currentMinMax[3] *= Math.pow(10, dy/yScale);
} else {
currentMinMax[2] += dy/yScale;
currentMinMax[3] += dy/yScale;
}
updateImage();
}
private boolean simpleXAxis() {
return !hasFlag(X_TICKS | X_MINOR_TICKS | X_LOG_TICKS | X_GRID | X_NUMBERS);
}
private boolean simpleYAxis() {
return !hasFlag(Y_TICKS | Y_MINOR_TICKS | Y_LOG_TICKS | Y_GRID | Y_NUMBERS);
}
void drawAxesTicksGridNumbers(double[] steps) {
Font scFont = scFont(currentFont==null ? defaultFont : currentFont);
Font scFontMedium = scFont.deriveFont(scFont.getSize2D()*10f/12f); Font scFontSmall = scFont.deriveFont(scFont.getSize2D()*9f/12f); int extraWidth = scFont.getSize()/3; ip.setFont(scFont);
FontMetrics fm = ip.getFontMetrics();
int fontAscent = fm.getAscent();
ip.setJustification(LEFT);
int yOfXAxisNumbers = topMargin + frameHeight + fm.getHeight()*5/4 + sc(3);
if (hasFlag(X_NUMBERS | (logXAxis ? (X_TICKS | X_MINOR_TICKS) : X_LOG_TICKS) + X_GRID)) {
Font baseFont = scFont;
boolean majorTicks = logXAxis ? hasFlag(X_LOG_TICKS) : hasFlag(X_TICKS);
boolean minorTicks = hasFlag(X_MINOR_TICKS);
double step = steps[0];
int i1 = (int)Math.ceil (Math.min(xMin, xMax)/step-1.e-10);
int i2 = (int)Math.floor(Math.max(xMin, xMax)/step+1.e-10);
int digits = getDigits(xMin, xMax, step, 7);
int y1 = topMargin;
int y2 = topMargin + frameHeight;
if (xMin==xMax) {
if (hasFlag(X_NUMBERS)) {
String s = IJ.d2s(xMin,getDigits(xMin, 0.001*xMin, 5));
int y = yBasePxl;
ip.drawString(s, xBasePxl-ip.getStringWidth(s)/2, yOfXAxisNumbers);
}
} else {
if (hasFlag(X_NUMBERS)) {
int w1 = ip.getStringWidth(IJ.d2s(currentMinMax[0], logXAxis ? -1 : digits));
int w2 = ip.getStringWidth(IJ.d2s(currentMinMax[1], logXAxis ? -1 : digits));
int wMax = Math.max(w1,w2);
if (wMax > Math.abs(step*xScale)-sc(8)) {
baseFont = scFontMedium; ip.setFont(baseFont);
}
}
for (int i=0; i<=(i2-i1); i++) {
double v = (i+i1)*step;
int x = (int)Math.round((v - xMin)*xScale) + leftMargin;
if (hasFlag(X_GRID)) {
ip.setColor(gridColor);
ip.drawLine(x, y1, x, y2);
ip.setColor(Color.black);
}
if (majorTicks) {
ip.drawLine(x, y1, x, y1+sc(tickLength));
ip.drawLine(x, y2, x, y2-sc(tickLength));
}
if (hasFlag(X_NUMBERS)) {
if (logXAxis || digits<0) {
drawExpString(logXAxis ? Math.pow(10,v) : v, logXAxis ? -1 : -digits,
x, yOfXAxisNumbers-fontAscent/2, CENTER, fontAscent, baseFont, scFontSmall);
} else {
String s = IJ.d2s(v,digits);
ip.drawString(s, x-ip.getStringWidth(s)/2, yOfXAxisNumbers);
}
}
}
boolean haveMinorLogNumbers = i2-i1 < 2; if (minorTicks && (!logXAxis || step > 1.1)) { step = niceNumber(step*0.19); if (logXAxis && step < 1) step = 1;
i1 = (int)Math.ceil (Math.min(xMin,xMax)/step-1.e-10);
i2 = (int)Math.floor(Math.max(xMin,xMax)/step+1.e-10);
for (int i=i1; i<=i2; i++) {
double v = i*step;
int x = (int)Math.round((v - xMin)*xScale) + leftMargin;
ip.drawLine(x, y1, x, y1+sc(minorTickLength));
ip.drawLine(x, y2, x, y2-sc(minorTickLength));
}
} else if (logXAxis && majorTicks && Math.abs(xScale)>sc(MIN_X_GRIDSPACING)) { int minorNumberLimit = haveMinorLogNumbers ? (int)(0.12*Math.abs(xScale)/(fm.charWidth('0')+sc(2))) : 0; i1 = (int)Math.floor(Math.min(xMin,xMax)-1.e-10);
i2 = (int)Math.ceil (Math.max(xMin,xMax)+1.e-10);
for (int i=i1; i<=i2; i++) {
for (int m=2; m<10; m++) {
double v = i+Math.log10(m);
if (v > Math.min(xMin,xMax) && v < Math.max(xMin,xMax)) {
int x = (int)Math.round((v - xMin)*xScale) + leftMargin;
ip.drawLine(x, y1, x, y1+sc(minorTickLength));
ip.drawLine(x, y2, x, y2-sc(minorTickLength));
if (m<=minorNumberLimit)
drawExpString(Math.pow(10,v), 0, x, yOfXAxisNumbers-fontAscent/2, CENTER, fontAscent, baseFont, scFontSmall);
}
}
}
}
}
}
int maxNumWidth = 0;
if (hasFlag(Y_NUMBERS | (logYAxis ? (Y_TICKS | Y_MINOR_TICKS) : Y_LOG_TICKS) + Y_GRID)) {
ip.setFont(scFont);
ip.setJustification(RIGHT);
Font baseFont = scFont;
boolean majorTicks = logYAxis ? hasFlag(Y_LOG_TICKS) : hasFlag(Y_TICKS);
boolean minorTicks = logYAxis ? hasFlag(Y_LOG_TICKS) : hasFlag(Y_MINOR_TICKS);
double step = steps[1];
int i1 = (int)Math.ceil (Math.min(yMin, yMax)/step-1.e-10);
int i2 = (int)Math.floor(Math.max(yMin, yMax)/step+1.e-10);
int digits = getDigits(yMin, yMax, step, 5);
int x1 = leftMargin;
int x2 = leftMargin + frameWidth;
if (yMin==yMax) {
if (hasFlag(Y_NUMBERS)) {
String s = IJ.d2s(yMin,getDigits(yMin, 0.001*yMin, 5));
maxNumWidth = ip.getStringWidth(s);
int y = yBasePxl;
ip.drawString(s, leftMargin-1, y+fontAscent/2+sc(1));
}
} else {
int w1 = ip.getStringWidth(IJ.d2s(currentMinMax[2], logYAxis ? -1 : digits));
int w2 = ip.getStringWidth(IJ.d2s(currentMinMax[3], logYAxis ? -1 : digits));
int wMax = Math.max(w1,w2);
if (hasFlag(Y_NUMBERS)) {
if (wMax > leftMargin-sc(2)-extraWidth - (yLabel.length()>0 ? fm.getHeight() : 0)) {
baseFont = scFontMedium; ip.setFont(baseFont);
}
}
for (int i=i1; i<=i2; i++) {
double v = step==0 ? yMin : i*step;
int y = topMargin + frameHeight - (int)Math.round((v - yMin)*yScale);
if (hasFlag(Y_GRID)) {
ip.setColor(gridColor);
ip.drawLine(x1, y, x2, y);
ip.setColor(Color.black);
}
if (majorTicks) {
ip.drawLine(x1, y, x1+sc(tickLength), y);
ip.drawLine(x2, y, x2-sc(tickLength), y);
}
if (hasFlag(Y_NUMBERS)) {
int w = 0;
if (logYAxis || digits<0) {
w = drawExpString(logYAxis ? Math.pow(10,v) : v, logYAxis ? -1 : -digits,
leftMargin, y, RIGHT, fontAscent, baseFont, scFontSmall);
} else {
String s = IJ.d2s(v,digits);
w = ip.getStringWidth(s);
ip.drawString(s, leftMargin-1, y+fontAscent*2/3+1);
}
if (w > maxNumWidth) maxNumWidth = w;
}
}
}
boolean haveMinorLogNumbers = i2-i1 < 2; if (minorTicks && (!logYAxis || step > 1.1)) { step = niceNumber(step*0.19); if (logYAxis && step < 1) step = 1;
i1 = (int)Math.ceil (Math.min(yMin,yMax)/step-1.e-10);
i2 = (int)Math.floor(Math.max(yMin,yMax)/step+1.e-10);
for (int i=i1; i<=i2; i++) {
double v = i*step;
int y = topMargin + frameHeight - (int)Math.round((v - yMin)*yScale);
ip.drawLine(x1, y, x1+sc(minorTickLength), y);
ip.drawLine(x2, y, x2-sc(minorTickLength), y);
}
}
if (logYAxis && majorTicks && Math.abs(yScale)>sc(MIN_X_GRIDSPACING)) { int minorNumberLimit = haveMinorLogNumbers ? (int)(0.4*Math.abs(yScale)/fm.getHeight()) : 0; i1 = (int)Math.floor(Math.min(yMin,yMax)-1.e-10);
i2 = (int)Math.ceil(Math.max(yMin,yMax)+1.e-10);
for (int i=i1; i<=i2; i++) {
for (int m=2; m<10; m++) {
double v = i+Math.log10(m);
if (v > Math.min(yMin,yMax) && v < Math.max(yMin,yMax)) {
int y = topMargin + frameHeight - (int)Math.round((v - yMin)*yScale);
ip.drawLine(x1, y, x1+sc(minorTickLength), y);
ip.drawLine(x2, y, x2-sc(minorTickLength), y);
if (m<=minorNumberLimit) {
int w = drawExpString(Math.pow(10,v), 0, leftMargin-sc(1), y, RIGHT, fontAscent, baseFont, scFontSmall);
if (w > maxNumWidth) maxNumWidth = w;
}
}
}
}
}
}
ip.setFont(scFont);
ip.setJustification(LEFT);
String xLabelToDraw = xLabel;
String yLabelToDraw = yLabel;
if (simpleYAxis()) { int digits = getDigits(yMin, yMax, 0.001*(yMax-yMin), 6);
String s = IJ.d2s(yMax, digits);
int sw = ip.getStringWidth(s);
if ((sw+sc(4)) > leftMargin)
ip.drawString(s, sc(4), topMargin-sc(4));
else
ip.drawString(s, leftMargin-ip.getStringWidth(s)-sc(4), topMargin+10);
s = IJ.d2s(yMin, digits);
sw = ip.getStringWidth(s);
if ((sw+4)>leftMargin)
ip.drawString(s, sc(4), topMargin+frame.height);
else
ip.drawString(s, leftMargin-ip.getStringWidth(s)-sc(4), topMargin+frame.height);
if (logYAxis) yLabelToDraw += " (LOG)";
}
int y = yOfXAxisNumbers;
if (simpleXAxis()) { int digits = getDigits(xMin, xMax, 0.001*(xMax-xMin), 7);
ip.drawString(IJ.d2s(xMin,digits), leftMargin, y);
String s = IJ.d2s(xMax,digits);
ip.drawString(s, leftMargin + frame.width-ip.getStringWidth(s)+6, y);
y -= fm.getHeight();
if (logXAxis) xLabelToDraw += " (LOG)";
} else
y += sc(1);
ip.setFont(xLabelFont == null ? scFont : scFont(xLabelFont));
ip.drawString(xLabelToDraw, leftMargin+(frame.width-ip.getStringWidth(xLabel))/2, y+ip.getFontMetrics().getHeight());
if (yLabel.length() > 0) {
int xOfYLabel = leftMargin-maxNumWidth-extraWidth-sc(4);
if (xOfYLabel < 0) xOfYLabel = 0;
drawYLabel(yLabelToDraw, xOfYLabel, topMargin, frame.height, yLabelFont == null ? scFont : scFont(yLabelFont));
}
}
double niceNumber(double v) {
double base = Math.pow(10,Math.floor(Math.log10(v)-1.e-6));
if (v > 5.0000001*base) return 10*base;
else if (v > 2.0000001*base) return 5*base;
else return 2*base;
}
int drawExpString(double value, int digits, int x, int y, int justification, int fontAscent, Font baseFont, Font smallFont) {
String base = "10";
String exponent = null;
String s = IJ.d2s(value, digits<=0 ? -1 : -digits);
if (Tools.parseDouble(s) == 0) s = "0"; int ePos = s.indexOf('E');
if (ePos < 0)
base = s; else {
if (digits>=0) {
base = s.substring(0,ePos);
if (digits == 0)
base = Integer.toString((int)Math.round(Tools.parseDouble(base)));
base += "\u00B710"; }
exponent = s.substring(ePos+1);
}
ip.setJustification(RIGHT);
int width = ip.getStringWidth(base);
if (exponent != null) {
ip.setFont(smallFont);
int wExponent = ip.getStringWidth(exponent);
width += wExponent;
if (justification == CENTER) x += width/2;
ip.drawString(exponent, x, y+fontAscent*3/10);
x -= wExponent;
ip.setFont(baseFont);
}
ip.drawString(base, x, y+fontAscent*7/10);
return width;
}
static int getDigits(double n, double resolution, int maxDigits) {
if (n==Math.round(n) && Math.abs(n) < Math.pow(10,maxDigits-1)-1) return 0;
else
return getDigits2(n, resolution, maxDigits);
}
static int getDigits(double n1, double n2, double resolution, int maxDigits) {
if (n1==0 && n2==0) return 0;
return getDigits2(Math.max(Math.abs(n1),Math.abs(n2)), resolution, maxDigits);
}
static int getDigits2(double n, double resolution, int maxDigits) {
int log10ofN = (int)Math.floor(Math.log10(Math.abs(n))+1e-7);
int digits = resolution != 0 ?
-(int)Math.floor(Math.log10(Math.abs(resolution))+1e-7) :
Math.max(0, -log10ofN+maxDigits-2);
int sciDigits = -Math.max((log10ofN+digits),1);
if (digits < -2 && log10ofN >= maxDigits)
digits = sciDigits; else if (digits < 0)
digits = 0;
else if (digits > maxDigits-1 && log10ofN < -2)
digits = sciDigits; return digits;
}
static boolean isInteger(double n) {
return n==Math.round(n);
}
private void drawPlotObject(PlotObject plotObject, ImageProcessor ip) {
ip.setColor(plotObject.color);
ip.setLineWidth(sc(plotObject.lineWidth));
int type = plotObject.type;
switch (type) {
case PlotObject.XY_DATA:
ip.setClipRect(frame);
if (plotObject.yEValues != null)
drawVerticalErrorBars(plotObject.xValues, plotObject.yValues, plotObject.yEValues);
if (plotObject.xEValues != null)
drawHorizontalErrorBars(plotObject.xValues, plotObject.yValues, plotObject.xEValues);
boolean drawMarker = plotObject.hasMarker();
boolean drawLine = plotObject.hasCurve();
if (plotObject.shape == CONNECTED_CIRCLES)
ip.setColor(plotObject.color2 == null ? Color.black : plotObject.color2);
if (drawLine)
drawFloatPolyline(ip, plotObject.xValues, plotObject.yValues,
Math.min(plotObject.xValues.length, plotObject.yValues.length));
if (drawMarker) {
int markSize = plotObject.getMarkerSize();
if (plotObject.hasFilledMarker()) {
ip.setColor(plotObject.color2);
ip.setLineWidth(1);
for (int i=0; i<Math.min(plotObject.xValues.length, plotObject.yValues.length); i++)
if ((!logXAxis || plotObject.xValues[i]>0) && (!logYAxis || plotObject.yValues[i]>0))
fillShape(plotObject.shape, scaleX(plotObject.xValues[i]), scaleY(plotObject.yValues[i]), markSize);
ip.setLineWidth(sc(plotObject.lineWidth));
}
ip.setColor(plotObject.color);
for (int i=0; i<Math.min(plotObject.xValues.length, plotObject.yValues.length); i++)
if ((!logXAxis || plotObject.xValues[i]>0) && (!logYAxis || plotObject.yValues[i]>0))
drawShape(plotObject.shape, scaleX(plotObject.xValues[i]), scaleY(plotObject.yValues[i]), markSize);
}
ip.setClipRect(null);
break;
case PlotObject.ARROWS:
ip.setClipRect(frame);
for (int i=0; i<plotObject.xValues.length; i++) {
int xt1 = scaleX(plotObject.xValues[i]);
int yt1 = scaleY(plotObject.yValues[i]);
int xt2 = scaleX(plotObject.xEValues[i]);
int yt2 = scaleY(plotObject.yEValues[i]);
double dist = calculateDistance(xt1, yt1, xt2, yt2);
if (xt1==xt2 && yt1==yt2)
ip.drawDot(xt1, yt1);
else if (dist < sc(1.5f*MIN_ARROWHEAD_LENGTH))
ip.drawLine(xt1, yt1, xt2, yt2);
else {
int arrowHeadLength = (int)(dist*RELATIVE_ARROWHEAD_SIZE+0.5);
if (arrowHeadLength > sc(MAX_ARROWHEAD_LENGTH)) arrowHeadLength = sc(MAX_ARROWHEAD_LENGTH);
if (arrowHeadLength < sc(MIN_ARROWHEAD_LENGTH)) arrowHeadLength = sc(MIN_ARROWHEAD_LENGTH);
drawArrow(xt1, yt1, xt2, yt2, arrowHeadLength);
}
}
ip.setClipRect(null);
break;
case PlotObject.LINE:
ip.setClipRect(frame);
ip.drawLine(scaleX(plotObject.x), scaleY(plotObject.y), scaleX(plotObject.xEnd), scaleY(plotObject.yEnd));
ip.setClipRect(null);
break;
case PlotObject.NORMALIZED_LINE:
ip.setClipRect(frame);
int ix1 = leftMargin + (int)(plotObject.x*frameWidth);
int iy1 = topMargin + (int)(plotObject.y*frameHeight);
int ix2 = leftMargin + (int)(plotObject.xEnd*frameWidth);
int iy2 = topMargin + (int)(plotObject.yEnd*frameHeight);
ip.drawLine(ix1, iy1, ix2, iy2);
ip.setClipRect(null);
break;
case PlotObject.DOTTED_LINE:
ip.setClipRect(frame);
ix1 = scaleX(plotObject.x);
iy1 = scaleY(plotObject.y);
ix2 = scaleX(plotObject.xEnd);
iy2 = scaleY(plotObject.yEnd);
double length = calculateDistance(ix1, ix2, iy1, iy2) + 0.1;
int n = (int)(length/plotObject.step);
for (int i = 0; i<=n; i++)
ip.drawDot(ix1 + (int)Math.round((ix2-ix1)*(double)i/n), iy1 + (int)Math.round((iy2-iy1)*(double)i/n));
ip.setClipRect(null);
break;
case PlotObject.LABEL:
case PlotObject.NORMALIZED_LABEL:
ip.setJustification(plotObject.justification);
if (plotObject.font != null)
ip.setFont(scFont(plotObject.font));
int xt = type==PlotObject.LABEL ? scaleX(plotObject.x) : leftMargin + (int)(plotObject.x*frameWidth);
int yt = type==PlotObject.LABEL ? scaleY(plotObject.y) : topMargin + (int)(plotObject.y*frameHeight);
ip.drawString(plotObject.label, xt, yt);
break;
case PlotObject.LEGEND:
drawLegend(plotObject, ip);
break;
}
}
void drawShape(int shape, int x, int y, int size) {
int xbase = x-sc(size/2);
int ybase = y-sc(size/2);
int xend = x+sc(size/2);
int yend = y+sc(size/2);
switch(shape) {
case X:
ip.drawLine(xbase,ybase,xend,yend);
ip.drawLine(xend,ybase,xbase,yend);
break;
case BOX:
ip.drawLine(xbase,ybase,xend,ybase);
ip.drawLine(xend,ybase,xend,yend);
ip.drawLine(xend,yend,xbase,yend);
ip.drawLine(xbase,yend,xbase,ybase);
break;
case TRIANGLE:
ip.drawLine(x,ybase-sc(1),xend+sc(1),yend); ip.drawLine(x,ybase-sc(1),xbase-sc(1),yend);
ip.drawLine(xend+sc(1),yend,xbase-sc(1),yend);
break;
case CROSS:
ip.drawLine(xbase,y,xend,y);
ip.drawLine(x,ybase,x,yend);
break;
case DOT:
ip.drawDot(x, y); break;
default: if (sc(size) < 5.01) {
ip.drawLine(x-1, y-2, x+1, y-2);
ip.drawLine(x-1, y+2, x+1, y+2);
ip.drawLine(x+2, y+1, x+2, y-1);
ip.drawLine(x-2, y+1, x-2, y-1);
} else {
int r = sc(0.5f*size-0.5f);
ip.drawOval(x-r, y-r, 2*r, 2*r);
}
break;
}
}
void fillShape(int shape, int x0, int y0, int size) {
int r = sc(size/2)-1;
switch(shape) {
case BOX:
int widthOrHeight = 2*sc(size/2);
for (int dy=-r; dy<=r; dy++)
for (int dx=-r; dx<=r; dx++)
ip.drawDot(x0+dx, y0+dy);
break;
case TRIANGLE:
int ybase = y0 - r - sc(1);
int yend = y0 + r;
double halfWidth = sc(size/2)+sc(1)-1;
double hwStep = halfWidth/(yend-ybase+1);
for (int y=yend; y>=ybase; y--, halfWidth -= hwStep) {
int dx = (int)(Math.round(halfWidth));
for (int x=x0-dx; x<=x0+dx; x++)
ip.drawDot(x,y);
}
break;
case CIRCLE:
int rsquare = (r+1)*(r+1);
for (int dy=-r; dy<=r; dy++)
for (int dx=-r; dx<=r; dx++)
if (dx*dx + dy*dy <= rsquare)
ip.drawDot(x0+dx, y0+dy);
break;
}
}
public void drawArrow(int x1, int y1, int x2, int y2, double size) {
double dx = x2 - x1;
double dy = y2 - y1;
double ra = Math.sqrt(dx * dx + dy * dy);
dx /= ra;
dy /= ra;
int x3 = (int) Math.round(x2 - dx * size); int y3 = (int) Math.round(y2 - dy * size);
double r = 0.3 * size;
int x4 = (int) Math.round(x3 + dy * r);
int y4 = (int) Math.round(y3 - dx * r);
int x5 = (int) Math.round(x3 - dy * r);
int y5 = (int) Math.round(y3 + dx * r);
ip.moveTo(x1, y1); ip.lineTo(x2, y2);
ip.moveTo(x4, y4); ip.lineTo(x2, y2); ip.lineTo(x5, y5);
}
private void drawVerticalErrorBars(float[] x, float[] y, float[] e) {
int nPoints = Math.min(Math.min(x.length, y.length), e.length);
for (int i=0; i<nPoints; i++) {
if (Float.isNaN(x[i]) || Float.isNaN(y[i]) || (logXAxis && !(x[i] >0))) continue;
int x0 = scaleX(x[i]);
int yPlus = scaleYWithOverflow(y[i] + e[i]);
int yMinus = scaleYWithOverflow(y[i] - e[i]);
ip.moveTo(x0,yMinus);
ip.lineTo(x0, yPlus);
}
}
private void drawHorizontalErrorBars(float[] x, float[] y, float[] e) {
int nPoints = Math.min(Math.min(x.length, y.length), e.length);
float[] xpoints = new float[2];
float[] ypoints = new float[2];
for (int i=0; i<nPoints; i++) {
if (Float.isNaN(x[i]) || Float.isNaN(y[i]) || (logXAxis && !(y[i] >0))) continue;
int y0 = scaleY(y[i]);
int xPlus = scaleXWithOverflow(x[i] + e[i]);
int xMinus = scaleXWithOverflow(x[i] - e[i]);
ip.moveTo(xMinus,y0);
ip.lineTo(xPlus, y0);
}
}
void drawFloatPolyline(ImageProcessor ip, float[] x, float[] y, int n) {
if (x==null || x.length==0) return;
int x1, y1;
boolean isNaN1;
int x2 = scaleX(x[0]);
int y2 = scaleY(y[0]);
boolean isNaN2 = Float.isNaN(x[0]) || Float.isNaN(y[0]) || (logXAxis && x[0]<=0) || (logYAxis && y[0]<=0);
for (int i=1; i<n; i++) {
x1 = x2;
y1 = y2;
isNaN1 = isNaN2;
x2 = scaleX(x[i]);
y2 = scaleY(y[i]);
isNaN2 = Float.isNaN(x[i]) || Float.isNaN(y[i]) || (logXAxis && x[i]<=0) || (logYAxis && y[i]<=0);
if (!isNaN1 && !isNaN2)
ip.drawLine(x1, y1, x2, y2);
}
}
void drawYLabel(String yLabel, int xRight, int yFrameTop, int frameHeight, Font scaledFont) {
if (yLabel.equals(""))
return;
ip.setFont(scaledFont);
FontMetrics fm = ip.getFontMetrics();
int w = ip.getStringWidth(yLabel) + sc(5);
int h = fm.getHeight()+sc(1);
ImageProcessor label = new ByteProcessor(w, h);
label.setAntialiasedText(antialiasedText);
if (invertedLut)
label.invertLut();
label.setColor(Color.white);
label.fill();
label.setColor(Color.black);
label.setFont(scaledFont);
label.drawString(yLabel, 0, h);
label = label.rotateLeft();
int y2 = yFrameTop + (frameHeight-ip.getStringWidth(yLabel))/2;
if (y2 < yFrameTop) y2 = yFrameTop;
int x2 = Math.max(xRight-h, 0);
ip.insert(label, x2, y2);
}
void drawLegend(PlotObject legendObject, ImageProcessor ip) {
ip.setFont (scFont(legendObject.font));
String[] labels = null;
if (legendObject.label != null)
labels = legendObject.label.split("[\t\n]");
int n = 0;
int nLabels = 0;
int maxStringWidth = 0;
float maxLineThickness = 0;
for (PlotObject plotObject : allPlotObjects)
if (plotObject.type == PlotObject.XY_DATA) {
if (labels != null && n < labels.length && labels[n].length()>0)
plotObject.label = labels[n]; if (plotObject.label != null) { nLabels++;
int w = ip.getStringWidth(plotObject.label);
if (w > maxStringWidth) maxStringWidth = w;
if (plotObject.lineWidth > maxLineThickness) maxLineThickness = plotObject.lineWidth;
}
n++;
}
if (nLabels == 0) return;
if (antialiasedText && scale > 1) maxStringWidth = (int)((1 + 0.004*scale) * maxStringWidth);
int frameThickness = sc(legendObject.lineWidth > 0 ? legendObject.lineWidth : 1);
FontMetrics fm = ip.getFontMetrics();
ip.setJustification(LEFT);
int lineHeight = fm.getHeight();
int height = nLabels*lineHeight + 2*sc(LEGEND_PADDING);
int width = maxStringWidth + sc(3*LEGEND_PADDING + LEGEND_LINELENGTH + maxLineThickness);
int positionCode = legendObject.flags & LEGEND_POSITION_MASK;
if (positionCode == AUTO_POSITION)
positionCode = autoLegendPosition(width, height, frameThickness);
Rectangle rect = legendRect(positionCode, width, height, frameThickness);
int x0 = rect.x;
int y0 = rect.y;
ip.setColor(Color.white);
ip.setLineWidth(1);
if (!legendObject.hasFlag(LEGEND_TRANSPARENT)) {
ip.setRoi(x0, y0, width, height);
ip.fill();
} else if (hasFlag(X_GRID | Y_GRID)) { int grid = ip instanceof ColorProcessor ? (gridColor.getRGB() & 0xffffff) : ip.getBestIndex(gridColor);
for (int y=y0; y<y0+height; y++)
for (int x=x0; x<x0+width; x++)
if ((ip.getPixel(x, y) & 0xffffff) == grid)
ip.drawDot(x, y);
}
ip.setLineWidth(frameThickness);
ip.setColor(legendObject.color);
ip.drawRect(x0-frameThickness/2, y0-frameThickness/2, width+frameThickness, height);
boolean bottomUp = legendObject.hasFlag(LEGEND_BOTTOM_UP);
int y = y0 + frameThickness/2 + sc(LEGEND_PADDING) + lineHeight/2;
if (bottomUp) y += (nLabels-1) * lineHeight;
int xText = x0 + frameThickness/2 + sc(2f*LEGEND_PADDING + LEGEND_LINELENGTH + maxLineThickness);
int xMarker = x0 + frameThickness/2 + sc(LEGEND_PADDING + 0.5f*(LEGEND_LINELENGTH + maxLineThickness));
for (PlotObject plotObject : allPlotObjects)
if (plotObject.type == PlotObject.XY_DATA && plotObject.label != null) {
if (plotObject.hasFilledMarker()) {
ip.setColor(plotObject.color2);
fillShape(plotObject.shape, xMarker, y, plotObject.getMarkerSize());
}
int lineWidth = sc(plotObject.lineWidth);
ip.setLineWidth(lineWidth);
if (plotObject.hasMarker()) {
ip.setColor(plotObject.color);
drawShape(plotObject.shape, xMarker, y, plotObject.getMarkerSize());
}
if (plotObject.hasCurve()) {
Color c = plotObject.shape == CONNECTED_CIRCLES ?
(plotObject.color2 == null ? Color.black : plotObject.color2) :
plotObject.color;
ip.setColor(c);
ip.drawLine(x0+frameThickness/2+sc(LEGEND_PADDING)+lineWidth, y, xText-sc(LEGEND_PADDING)-lineWidth, y);
}
ip.setColor(plotObject.color);
ip.setLineWidth(frameThickness);
ip.drawString(plotObject.label, xText, y+ lineHeight/2);
y += bottomUp ? -lineHeight : lineHeight;
}
}
Rectangle legendRect(int positionCode, int width, int height, int frameThickness) {
boolean leftPosition = positionCode == TOP_LEFT || positionCode == BOTTOM_LEFT;
boolean topPosition = positionCode == TOP_LEFT || positionCode == TOP_RIGHT;
int x0 = (leftPosition) ?
leftMargin + sc(2*LEGEND_PADDING) + frameThickness/2 :
leftMargin + frameWidth - width - sc(2*LEGEND_PADDING) - frameThickness/2;
int y0 = (topPosition) ?
topMargin + sc(LEGEND_PADDING) + frameThickness/2 :
topMargin + frameHeight - height - sc(LEGEND_PADDING) + frameThickness/2;
if (hasFlag(Y_TICKS))
x0 += (leftPosition ? 1 : -1) * sc(tickLength - LEGEND_PADDING);
if (hasFlag(X_TICKS))
y0 += (topPosition ? 1 : -1) * sc(tickLength - LEGEND_PADDING/2);
return new Rectangle(x0, y0, width, height);
}
int autoLegendPosition(int width, int height, int frameThickness) {
int background = ip instanceof ColorProcessor ? (0xffffff) : (ip.isInvertedLut() ? 0 : 0xff);
int grid = ip instanceof ColorProcessor ? (gridColor.getRGB() & 0xffffff) : ip.getBestIndex(gridColor);
int bestPosition = 0;
int minCoveredPixels = Integer.MAX_VALUE;
for (int positionCode : new int[]{TOP_LEFT, TOP_RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT}) {
Rectangle rect = legendRect(positionCode, width, height, frameThickness);
int coveredPixels = 0;
for (int y = rect.y - frameThickness/2; y <= rect.y + rect.height + frameThickness/2; y++)
for (int x = rect.x - frameThickness/2; x <= rect.x + rect.width + frameThickness/2; x++) {
int pixel = ip.getPixel(x, y) & 0xffffff;
if (pixel != background && pixel != grid)
coveredPixels ++;
}
if (coveredPixels < minCoveredPixels) {
minCoveredPixels = coveredPixels;
bestPosition = positionCode;
}
}
return bestPosition;
}
String getCoordinates(int x, int y) {
if (frame==null) return "";
String text = "";
if (!frame.contains(x, y))
return text;
double xv = descaleX(x); double yv = descaleY(y);
boolean yIsValue = false;
if (!hasMultiplePlots()) {
PlotObject p = getMainCurveObject(); if (p != null) {
double bestDx = Double.MAX_VALUE;
double xBest = 0, yBest = 0;
for (int i=0; i<Math.min(p.xValues.length, p.yValues.length); i++) {
double xp = p.xValues[i];
if (Math.abs(xp-xv) < bestDx) {
bestDx = Math.abs(xp-xv);
xBest = xp;
yBest = p.yValues[i];
}
}
if (xScale != 0 && bestDx*xScale < 50) { xv = xBest;
yv = yBest;
yIsValue = true;
} else
xv = Double.NaN;
}
}
if (!Double.isNaN(xv)) {
text = "X=" + IJ.d2s(xv, getDigits(xv, 0.001*(xMax-xMin), 6))+", Y";
if (yIsValue) text += "(X)";
text +="="+ IJ.d2s(yv, getDigits(yv, 0.001*(yMax-yMin), 6));
}
return text;
}
private PlotObject getMainCurveObject() {
for (PlotObject plotObject : allPlotObjects) {
if (plotObject.type == PlotObject.XY_DATA)
return plotObject;
}
return null;
}
private PlotObject getLastCurveObject() {
for (int i=allPlotObjects.size()-1; i>=0; i--) {
if (allPlotObjects.get(i).type == PlotObject.XY_DATA)
return allPlotObjects.get(i);
}
return null;
}
private boolean hasMultiplePlots() {
int nPlots = 0;
for (PlotObject plotObject : allPlotObjects) {
if (plotObject.type == PlotObject.ARROWS)
return true;
else if (plotObject.type == PlotObject.XY_DATA) {
nPlots ++;
if (nPlots > 1) return true;
}
}
return nPlots > 1;
}
public void setPlotMaker(PlotMaker plotMaker) {
this.plotMaker = plotMaker;
}
PlotMaker getPlotMaker() {
return plotMaker;
}
String getDataLabels() {
String labels = "";
boolean first = true;
for (PlotObject plotObject : allPlotObjects)
if (plotObject.type == PlotObject.XY_DATA) {
if (first)
first = false;
else
labels += '\n';
if (plotObject.label != null) labels += plotObject.label;
}
return labels;
}
public ResultsTable getResultsTable() {
return getResultsTable(true);
}
public ResultsTable getResultsTable(boolean writeFirstXColumn) {
ResultsTable rt = new ResultsTable();
rt.showRowNumbers(false);
int nDataSets = 0;
int tableLength = 0;
for (PlotObject plotObject : allPlotObjects)
if (plotObject.xValues != null) {
nDataSets++;
tableLength = Math.max(tableLength, plotObject.xValues.length);
}
if (nDataSets == 0) return null;
ArrayList<String> headings = new ArrayList<String>(2*nDataSets);
ArrayList<float[]> data = new ArrayList<float[]>(2*nDataSets);
int dataSetNumber = 0;
int arrowsNumber = 0;
PlotObject firstXYobject = null;
for (PlotObject plotObject : allPlotObjects) {
if (plotObject.type==PlotObject.XY_DATA) {
boolean sameX = firstXYobject != null && Arrays.equals(firstXYobject.xValues, plotObject.xValues);
boolean sameXY = sameX && Arrays.equals(firstXYobject.yValues, plotObject.yValues); boolean writeX = firstXYobject==null?writeFirstXColumn:!sameX;
addToLists(headings, data, plotObject, dataSetNumber, writeX, !sameXY, nDataSets>1);
if (firstXYobject == null) firstXYobject = plotObject;
dataSetNumber++;
} else if (plotObject.type==PlotObject.ARROWS) {
addToLists(headings, data, plotObject, arrowsNumber, true, true, nDataSets>1);
arrowsNumber++;
}
}
int nColumns = headings.size();
for (int line=0; line<tableLength; line++) {
for (int col=0; col<nColumns; col++) {
String heading = headings.get(col);
float[] values = data.get(col);
if (line<values.length)
rt.setValue(heading, line, values[line]);
else
rt.setValue(heading, line, "");
}
}
nColumns = rt.getLastColumn() + 1;
for (int i=0; i<nColumns; i++)
rt.setDecimalPlaces(i, getPrecision(rt.getColumn(i)));
return rt;
}
static final int MIN_SCIENTIFIC_DIGITS = 4;
static final double MIN_FLOAT_PRECISION = 1e-5;
void addToLists(ArrayList<String> headings, ArrayList<float[]>data, PlotObject plotObject,
int dataSetNumber, boolean writeX, boolean writeY, boolean multipleSets) {
if (writeX) {
String label = plotObject.type == PlotObject.ARROWS ? "XStart" : "X";
if (multipleSets) label += dataSetNumber;
headings.add(label);
data.add(plotObject.xValues);
}
if (writeY) {
String label = plotObject.type == PlotObject.ARROWS ? "YStart" : "Y";
if (multipleSets) label += dataSetNumber;
headings.add(label);
data.add(plotObject.yValues);
}
if (plotObject.xEValues != null) {
String label = plotObject.type == PlotObject.ARROWS ? "XEnd" : "XERR";
if (multipleSets) label += dataSetNumber;
headings.add(label);
data.add(plotObject.xEValues);
}
if (plotObject.yEValues != null) {
String label = plotObject.type == PlotObject.ARROWS ? "YEnd" : "ERR";
if (multipleSets) label += dataSetNumber;
headings.add(label);
data.add(plotObject.yEValues);
}
}
static int getPrecision(float[] values) {
int setDigits = Analyzer.getPrecision();
int measurements = Analyzer.getMeasurements();
boolean scientificNotation = (measurements&Measurements.SCIENTIFIC_NOTATION)!=0;
if (scientificNotation) {
if (setDigits<MIN_SCIENTIFIC_DIGITS)
setDigits = MIN_SCIENTIFIC_DIGITS;
return -setDigits;
}
boolean allInteger = true;
float min = Float.MAX_VALUE, max = -Float.MAX_VALUE;
for (int i=0; i<values.length; i++) {
if ((int)values[i]!=values[i] && !Float.isNaN(values[i])) {
allInteger = false;
if (values[i] < min) min = values[i];
if (values[i] > max) max = values[i];
}
}
if (allInteger)
return 0;
int digits = (max - min) > 0 ? getDigits(min, max, MIN_FLOAT_PRECISION*(max-min), 15) :
getDigits(max, MIN_FLOAT_PRECISION*Math.abs(max), 15);
if (setDigits>Math.abs(digits))
digits = setDigits * (digits < 0 ? -1 : 1); return digits;
}
boolean hasFlag(int what) {
return (flags&what) != 0;
}
}
class PlotObject implements Cloneable {
public final static int XY_DATA = 0, ARROWS = 1, LINE = 2, NORMALIZED_LINE = 3, DOTTED_LINE = 4,
LABEL = 5, NORMALIZED_LABEL = 6, LEGEND = 7;
public final static int CONSTRUCTOR_DATA = 1;
public int type = XY_DATA;
public int flags;
public float[] xValues, yValues, xEValues, yEValues;
public int shape;
public float lineWidth;
public Color color;
public Color color2;
public double x, y;
public double xEnd, yEnd;
public int step;
public String label;
public int justification;
public Font font;
PlotObject(float[] xValues, float[] yValues, float[] yErrorBars, int shape, float lineWidth, Color color, Color color2, String yLabel) {
this.type = XY_DATA;
this.xValues = xValues;
this.yValues = yValues;
this.yEValues = yErrorBars;
this.shape = shape;
this.lineWidth = lineWidth;
this.color = color;
this.color2 = color2;
this.label = yLabel;
}
PlotObject(float[] x1, float[] y1, float[] x2, float[] y2, float lineWidth, Color color) {
this.type = ARROWS;
this.xValues = x1;
this.yValues = y1;
this.xEValues = x2;
this.yEValues = y2;
this.lineWidth = lineWidth;
this.color = color;
}
PlotObject(double x, double y, double xEnd, double yEnd, float lineWidth, int step, Color color, int type) {
this.type = type;
this.x = x;
this.y = y;
this.xEnd = xEnd;
this.yEnd = yEnd;
this.lineWidth = lineWidth;
this.step = step;
this.color = color;
}
PlotObject(String label, double x, double y, int justification, Font font, Color color, int type) {
this.type = type;
this.label = label;
this.x = x;
this.y = y;
this.justification = justification;
this.font = font;
this.color = color;
}
PlotObject(String labels, float lineWidth, Font font, Color color, int flags) {
this.type = LEGEND;
this.label = labels;
this.lineWidth = lineWidth;
this.font = font;
this.color = color;
this.flags = flags;
}
boolean hasFlag(int what) {
return (flags&what) != 0;
}
boolean hasCurve() {
return type == XY_DATA && (shape == Plot.LINE || shape == Plot.CONNECTED_CIRCLES);
}
boolean hasMarker() {
return type == XY_DATA && (shape == Plot.CIRCLE || shape == Plot.X || shape == Plot.BOX || shape == Plot.TRIANGLE ||
shape == Plot.CROSS || shape == Plot.DOT || shape == Plot.CONNECTED_CIRCLES);
}
boolean hasFilledMarker() {
return type == XY_DATA && color2 != null && (shape == Plot.CIRCLE || shape == Plot.BOX || shape == Plot.TRIANGLE);
}
int getMarkerSize () {
return lineWidth<=1 ? 5 : 7;
}
public PlotObject clone() {
try {
return (PlotObject)(super.clone());
} catch (CloneNotSupportedException e) {
return null;
}
}
}