Merge pull request #1010 from SiboVG/issue-1009
[fixes #1009] Added more advanced DecimalFormat for plot hover
This commit is contained in:
commit
f750a8c1cb
@ -27,6 +27,7 @@ import net.sf.openrocket.unit.Unit;
|
|||||||
import net.sf.openrocket.unit.UnitGroup;
|
import net.sf.openrocket.unit.UnitGroup;
|
||||||
import net.sf.openrocket.util.LinearInterpolator;
|
import net.sf.openrocket.util.LinearInterpolator;
|
||||||
|
|
||||||
|
import net.sf.openrocket.utils.DecimalFormatter;
|
||||||
import org.jfree.chart.ChartFactory;
|
import org.jfree.chart.ChartFactory;
|
||||||
import org.jfree.chart.JFreeChart;
|
import org.jfree.chart.JFreeChart;
|
||||||
import org.jfree.chart.LegendItem;
|
import org.jfree.chart.LegendItem;
|
||||||
@ -263,7 +264,6 @@ public class SimulationPlot {
|
|||||||
// Custom tooltip generator
|
// Custom tooltip generator
|
||||||
int finalAxisno = axisno;
|
int finalAxisno = axisno;
|
||||||
StandardXYToolTipGenerator tooltipGenerator = new StandardXYToolTipGenerator() {
|
StandardXYToolTipGenerator tooltipGenerator = new StandardXYToolTipGenerator() {
|
||||||
final DecimalFormat f = new DecimalFormat("0.00");
|
|
||||||
@Override
|
@Override
|
||||||
public String generateToolTip(XYDataset dataset, int series, int item) {
|
public String generateToolTip(XYDataset dataset, int series, int item) {
|
||||||
XYSeries ser = data[finalAxisno].getSeries(series);
|
XYSeries ser = data[finalAxisno].getSeries(series);
|
||||||
@ -282,14 +282,18 @@ public class SimulationPlot {
|
|||||||
ord_end = "nd";
|
ord_end = "nd";
|
||||||
else if (item % 10 == 3)
|
else if (item % 10 == 3)
|
||||||
ord_end = "rd";
|
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>" +
|
return String.format("<html>" +
|
||||||
"<b><i>%s</i></b><br>" +
|
"<b><i>%s</i></b><br>" +
|
||||||
"Y: %s %s<br>" +
|
"Y: %s %s<br>" +
|
||||||
"X: %s %s<br>" +
|
"X: %s %s<br>" +
|
||||||
"%d<sup>%s</sup> sample" +
|
"%d<sup>%s</sup> sample" +
|
||||||
"</html>",
|
"</html>",
|
||||||
name, f.format(dataset.getYValue(series, item)), unit_y,
|
name, df_y.format(data_y), unit_y,
|
||||||
f.format(dataset.getXValue(series, item)), unit_x, item, ord_end);
|
df_x.format(data_x), unit_x, item, ord_end);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
36
swing/src/net/sf/openrocket/utils/DecimalFormatter.java
Normal file
36
swing/src/net/sf/openrocket/utils/DecimalFormatter.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user