Skip to content

Commit

Permalink
Viewer2D: Allow fixed range in all axes
Browse files Browse the repository at this point in the history
  • Loading branch information
tferr committed Dec 1, 2024
1 parent 8b7d542 commit 23ae70e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private String getNormalizedMeasurementInternal(final String measurement) {
final String normMeasurement = tryReallyHardToGuessMetric(measurement);
if (!measurement.equals(normMeasurement)) {
SNTUtils.log("\"" + normMeasurement + "\" assumed");
if ("unknonwn".equals(normMeasurement)) {
if ("unknown".equals(normMeasurement)) {
throw new IllegalArgumentException("Unrecognizable measurement! "
+ "Maybe you meant one of the following?: " + Arrays.toString(MULTI_TREE_FLAGS));
}
Expand Down
87 changes: 79 additions & 8 deletions src/main/java/sc/fiji/snt/viewer/MultiViewer2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ public void setOutlineVisible(final boolean visible) {
outlineVisible = visible;
}

/**
* Sets a manual range for the viewers' X-axis. Calling {@code setXrange(-1, -1)} enables auto-range (the default).
* Must be called before Viewer is fully assembled.
*
* @param xMin the lower-limit for the X-axis
* @param xMax the upper-limit for the X-axis
*/
public void setXrange(final double xMin, final double xMax) {
viewers.forEach( v -> v.setXrange(xMin, xMax));
}

/**
* Sets a manual range for the viewers' Y-axis. Calling {@code setYrange(-1, -1)} enables auto-range (the default).
* Must be called before Viewer is fully assembled.
*
* @param yMin the lower-limit for the Y-axis
* @param yMax the upper-limit for the Y-axis
*/
public void setYrange(final double yMin, final double yMax) {
viewers.forEach( v -> v.setYrange(yMin, yMax));
}

public <T extends sc.fiji.snt.analysis.ColorMapper> void setColorBarLegend(final T colorMapper) {
final double[] minMax = colorMapper.getMinMax();
setColorBarLegend(colorMapper.getColorTable(), minMax[0], minMax[1], (colorMapper.isIntegerScale()) ? 0 : 2);
Expand Down Expand Up @@ -239,8 +261,32 @@ private String getTitlesAsString(final List<Viewer2D> viewers) {
return null;
}

private SNTChart getMergedChart(final List<Viewer2D> viewers, final String style) {
private double[] getFixedXRange(final List<Viewer2D> viewers) {
final double min = viewers.get(0).getPlot().xAxis().getMin();
final double max = viewers.get(0).getPlot().xAxis().getMax();
if (min == max) return null;
for (int i = 1; i < viewers.size(); i++) {
if (min != viewers.get(i).getPlot().xAxis().getMin() && max != viewers.get(i).getPlot().xAxis().getMax())
return null;
}
return new double[]{min, max};
}

private double[] getFixedYRange(final List<Viewer2D> viewers) {
final double min = viewers.get(0).getPlot().yAxis().getMin();
final double max = viewers.get(0).getPlot().yAxis().getMax();
if (min == max) return null;
for (int i = 1; i < viewers.size(); i++) {
if (min != viewers.get(i).getPlot().yAxis().getMin() && max != viewers.get(i).getPlot().yAxis().getMax())
return null;
}
return new double[]{min, max};
}

private SNTChart getMergedChart(final List<Viewer2D> viewers, final String style) {
JFreeChart result;
final double[] fixedXrange = getFixedXRange(viewers);
final double[] fixedYrange = getFixedYRange(viewers);
if (style != null && style.toLowerCase().startsWith("c")) { // column
final CombinedRangeXYPlot mergedPlot = new CombinedRangeXYPlot();
for (final Viewer2D viewer : viewers) {
Expand All @@ -250,18 +296,43 @@ private SNTChart getMergedChart(final List<Viewer2D> viewers, final String style
plot.setDomainGridlinesVisible(gridVisible);
plot.setRangeGridlinesVisible(gridVisible);
plot.setOutlineVisible(outlineVisible);
if (fixedXrange != null) {
plot.getDomainAxis().setRange(fixedXrange[0], fixedXrange[1]);
plot.getDomainAxis().setAutoRange(false);
}
if (fixedYrange != null) {
plot.getRangeAxis().setRange(fixedYrange[0], fixedYrange[1]);
plot.getRangeAxis().setAutoRange(false);
}
mergedPlot.add(plot, 1);
if (fixedYrange != null) {
mergedPlot.getRangeAxis().setRange(fixedYrange[0], fixedYrange[1]);
mergedPlot.getRangeAxis().setAutoRange(false);
}
}
result = new JFreeChart(null, mergedPlot);
} else {
final CombinedDomainXYPlot mergedPlot = new CombinedDomainXYPlot();
for (final Viewer2D viewer : viewers) {
mergedPlot.add(viewer.getChart().getChart().getXYPlot(), 1);
final XYPlot plot = viewer.getJFreeChart().getXYPlot();
if (fixedXrange != null) {
plot.getDomainAxis().setRange(fixedXrange[0], fixedXrange[1]);
plot.getDomainAxis().setAutoRange(false);
}
if (fixedYrange != null) {
plot.getRangeAxis().setRange(fixedYrange[0], fixedYrange[1]);
plot.getRangeAxis().setAutoRange(false);
}
mergedPlot.add(plot, 1);
if (fixedXrange != null) {
mergedPlot.getDomainAxis().setRange(fixedXrange[0], fixedXrange[1]);
mergedPlot.getDomainAxis().setAutoRange(false);
}
}
result = new JFreeChart(null, mergedPlot);
}
if (legend != null && viewers.contains(colorLegendViewer)) {
if (gridCols >= this.viewers.size()) {
if (gridCols >= viewers.size()) {
legend.setPosition(RectangleEdge.RIGHT);
legend.setMargin(50, 5, 50, 5);
} else {
Expand Down Expand Up @@ -290,7 +361,7 @@ public static void main(final String... args) {

// Color code each cell and assign a hue ramp to the group
final MultiTreeColorMapper mapper = new MultiTreeColorMapper(trees);
mapper.map("tips", ColorTables.ICE);
mapper.map("no. of tips", ColorTables.ICE);

// Assemble a multi-panel Viewer2D from the color mapper
final MultiViewer2D viewer1 = mapper.getMultiViewer();
Expand All @@ -300,8 +371,7 @@ public static void main(final String... args) {
viewer1.setAxesVisible(false);
viewer1.show();

// Sholl mapping //TODO: The API for this feels clunky
// and everything is really slow. Optimization needed!
// Sholl mapping
final List<Viewer2D> viewers = new ArrayList<>();
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
Expand All @@ -325,9 +395,10 @@ public static void main(final String... args) {
viewer2.setLayoutColumns(0);
viewer2.setGridlinesVisible(false);
viewer2.setOutlineVisible(false);
viewer2.setAxesVisible(false);
viewer2.setAxesVisible(true);
viewer2.setXrange(-100, 100);
viewer2.setYrange(-200, 200);
viewer2.show();

}

}
28 changes: 28 additions & 0 deletions src/main/java/sc/fiji/snt/viewer/Viewer2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,34 @@ public String getTitle() {
return title;
}

/**
* Sets a manual range for the viewers' X-axis. Calling {@code setXrange(-1, -1)} enables auto-range (the default).
* Must be called before Viewer is fully assembled.
*
* @param xMin the lower-limit for the X-axis
* @param xMax the upper-limit for the X-axis
*/
public void setXrange(final double xMin, final double xMax) {
if (xMin == xMax && xMin == -1)
plot.xAxis().setAutoRange();
else
plot.xAxis().setManualRange(xMin, xMax);
}

/**
* Sets a manual range for the viewers' Y-axis. Calling {@code setYrange(-1, -1)} enables auto-range (the default).
* Must be called before Viewer is fully assembled.
*
* @param yMin the lower-limit for the Y-axis
* @param yMax the upper-limit for the Y-axis
*/
public void setYrange(final double yMin, final double yMax) {
if (yMin == yMax && yMin == -1)
plot.yAxis().setAutoRange();
else
plot.yAxis().setManualRange(yMin, yMax);
}

/**
* @deprecated Use {@link #show()} instead.
*/
Expand Down

1 comment on commit 23ae70e

@imagesc-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/snt-problem-with-inconsistent-scale-on-x-axis-in-multiple-reconstruction-plotter/105520/2

Please sign in to comment.