[#1128] Fix common zero problem between left and right axis

This commit is contained in:
SiboVG 2022-03-11 13:29:26 +01:00
parent 74debb78a5
commit 8022e9fedc
3 changed files with 46 additions and 52 deletions

View File

@ -138,6 +138,10 @@ public class FlightData {
return branches.get(n); return branches.get(n);
} }
public List<FlightDataBranch> getBranches() {
return branches;
}
public double getMaxAltitude() { public double getMaxAltitude() {

View File

@ -1,5 +1,6 @@
package net.sf.openrocket.gui.plot; package net.sf.openrocket.gui.plot;
import java.util.Collections;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -426,8 +427,9 @@ public class PlotConfiguration implements Cloneable {
} }
protected void fitAxes(FlightDataBranch data) {
this.fitAxes(Collections.singletonList(data));
}
/** /**
* Fit the axes to hold the provided data. All of the plotDataAxis elements must * Fit the axes to hold the provided data. All of the plotDataAxis elements must
@ -435,8 +437,7 @@ public class PlotConfiguration implements Cloneable {
* <p> * <p>
* NOTE: This method assumes that only two axes are used. * NOTE: This method assumes that only two axes are used.
*/ */
protected void fitAxes(FlightDataBranch data) { public void fitAxes(List<FlightDataBranch> data) {
// Reset axes // Reset axes
for (Axis a : allAxes) { for (Axis a : allAxes) {
a.reset(); a.reset();
@ -453,8 +454,13 @@ public class PlotConfiguration implements Cloneable {
} }
Axis axis = allAxes.get(index); Axis axis = allAxes.get(index);
double min = unit.toUnit(data.getMinimum(type)); double min = unit.toUnit(data.get(0).getMinimum(type));
double max = unit.toUnit(data.getMaximum(type)); double max = unit.toUnit(data.get(0).getMaximum(type));
for (int j = 1; j < data.size(); j++) {
min = Math.min(min, unit.toUnit(data.get(j).getMinimum(type)));
max = Math.max(max, unit.toUnit(data.get(j).getMaximum(type)));
}
axis.addBound(min); axis.addBound(min);
axis.addBound(max); axis.addBound(max);
@ -491,41 +497,28 @@ public class PlotConfiguration implements Cloneable {
//// Compute common zero //// Compute common zero
// TODO: MEDIUM: This algorithm may require tweaking
double min1 = left.getMinValue(); double min1 = left.getMinValue();
double max1 = left.getMaxValue(); double max1 = left.getMaxValue();
double min2 = right.getMinValue(); double min2 = right.getMinValue();
double max2 = right.getMaxValue(); double max2 = right.getMaxValue();
// Calculate and round scaling factor // Get the zero locations, scaled down to a value from 0 to 1 (0 = bottom of y axis, 1 = top)
double scale = Math.max(left.getRangeLength(), right.getRangeLength()) / double zeroLoc1 = -min1 / left.getRangeLength();
Math.min(left.getRangeLength(), right.getRangeLength()); double zeroLoc2 = -min2 / right.getRangeLength();
//System.out.println("Scale: " + scale); // Scale the right axis
if (zeroLoc1 > zeroLoc2) {
scale = roundScale(scale); min2 = -max2 * (zeroLoc1 / (1 - zeroLoc1));
if (right.getRangeLength() > left.getRangeLength()) { } else {
scale = 1 / scale; max2 = min2 * (-1 / zeroLoc1 + 1);
} }
//System.out.println("Rounded scale: " + scale);
// Scale right axis, enlarge axes if necessary and scale back
min2 *= scale;
max2 *= scale;
min1 = Math.min(min1, min2);
min2 = min1;
max1 = Math.max(max1, max2);
max2 = max1;
min2 /= scale;
max2 /= scale;
// Apply scale // Apply scale
left.addBound(min1); left.addBound(min1);
left.addBound(max1); left.addBound(max1);
right.addBound(min2); right.addBound(min2);
right.addBound(max2); right.addBound(max2);
} }

View File

@ -134,7 +134,11 @@ public class SimulationPlot {
// Fill the auto-selections based on first branch selected. // Fill the auto-selections based on first branch selected.
FlightDataBranch mainBranch = simulation.getSimulatedData().getBranch(0); FlightDataBranch mainBranch = simulation.getSimulatedData().getBranch(0);
this.filled = config.fillAutoAxes(mainBranch); this.filled = config.fillAutoAxes(mainBranch);
List<Axis> axes = filled.getAllAxes();
// Compute the axes based on the min and max value of all branches
PlotConfiguration plotConfig = filled.clone();
plotConfig.fitAxes(simulation.getSimulatedData().getBranches());
List<Axis> minMaxAxes = plotConfig.getAllAxes();
// Create the data series for both axes // Create the data series for both axes
XYSeriesCollection[] data = new XYSeriesCollection[2]; XYSeriesCollection[] data = new XYSeriesCollection[2];
@ -250,15 +254,8 @@ public class SimulationPlot {
// Check whether axis has any data // Check whether axis has any data
if (data[axisno].getSeriesCount() > 0) { if (data[axisno].getSeriesCount() > 0) {
// Create and set axis // Create and set axis
double min = axes.get(axisno).getMinValue(); double min = minMaxAxes.get(axisno).getMinValue();
double max = axes.get(axisno).getMaxValue(); double max = minMaxAxes.get(axisno).getMaxValue();
for (Object series : data[axisno].getSeries()) {
if (series instanceof XYSeries) {
min = Math.min(min, ((XYSeries) series).getMinY());
max = Math.max(max, ((XYSeries) series).getMaxY());
}
}
NumberAxis axis = new PresetNumberAxis(min, max); NumberAxis axis = new PresetNumberAxis(min, max);
axis.setLabel(axisLabel[axisno]); axis.setLabel(axisLabel[axisno]);