Merge pull request #1010 from SiboVG/issue-1009

[fixes #1009] Added more advanced DecimalFormat for plot hover
This commit is contained in:
Joe Pfeiffer 2021-10-15 11:06:40 -06:00 committed by GitHub
commit f750a8c1cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -27,6 +27,7 @@ import net.sf.openrocket.unit.Unit;
import net.sf.openrocket.unit.UnitGroup;
import net.sf.openrocket.util.LinearInterpolator;
import net.sf.openrocket.utils.DecimalFormatter;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
@ -263,7 +264,6 @@ public class SimulationPlot {
// Custom tooltip generator
int finalAxisno = axisno;
StandardXYToolTipGenerator tooltipGenerator = new StandardXYToolTipGenerator() {
final DecimalFormat f = new DecimalFormat("0.00");
@Override
public String generateToolTip(XYDataset dataset, int series, int item) {
XYSeries ser = data[finalAxisno].getSeries(series);
@ -282,14 +282,18 @@ public class SimulationPlot {
ord_end = "nd";
else if (item % 10 == 3)
ord_end = "rd";
double data_y = dataset.getYValue(series, item);
double data_x = dataset.getXValue(series, item);
DecimalFormat df_y = DecimalFormatter.df(data_y, 2, false);
DecimalFormat df_x = DecimalFormatter.df(data_x, 2, false);
return String.format("<html>" +
"<b><i>%s</i></b><br>" +
"Y: %s %s<br>" +
"X: %s %s<br>" +
"%d<sup>%s</sup> sample" +
"</html>",
name, f.format(dataset.getYValue(series, item)), unit_y,
f.format(dataset.getXValue(series, item)), unit_x, item, ord_end);
name, df_y.format(data_y), unit_y,
df_x.format(data_x), unit_x, item, ord_end);
}
};

View File

@ -0,0 +1,36 @@
package net.sf.openrocket.utils;
import java.text.DecimalFormat;
/**
* This is a helper class for generating more elaborate DecimalFormats.
* @author SiboVanGool <sibo.vangool@hotmail.com>
*/
public abstract class DecimalFormatter {
/**
* Return a decimal formatter for displaying {value} with {decimals} decimals. If {value} is smaller than the second
* to last least significant decimal, then value will be formatted to show the 2 most significant decimals of {value}.
* E.g. value = 0.00345, decimals = 2 => smallest value = 0.01; value < 0.1 => format the 2 most significant decimals
* of value => value will be displayed as 0.0035.
* @param value number to be formatted
* @param decimals number of decimals to round off to
* @param display_zeros flag to check whether to display trailing zeros or not
* value true: display e.g. 8.00, value false, display e.g. 8
* @return decimal format
*/
public static DecimalFormat df (double value, int decimals, boolean display_zeros) {
if (decimals <= 0) {
return new DecimalFormat("#");
}
value = Math.abs(value);
String pattern = "#";
if (display_zeros) {
pattern = "0";
}
if (value < Math.pow(0.1, decimals-1) && value > 0) {
int decimals_value = (int) Math.floor(Math.log10(1/value));
decimals = decimals_value + decimals;
}
return new DecimalFormat("#." + new String(new char[decimals]).replace("\0", pattern));
}
}