Move Chart creation inside Plot

This commit is contained in:
SiboVG 2024-08-20 18:17:41 +02:00
parent baaca0c64d
commit 627abcd87e
2 changed files with 22 additions and 28 deletions

View File

@ -8,6 +8,7 @@ import info.openrocket.core.unit.Unit;
import info.openrocket.core.unit.UnitGroup;
import info.openrocket.swing.gui.util.SwingPreferences;
import info.openrocket.swing.utils.DecimalFormatter;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
@ -53,6 +54,11 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*
* TODO: It should be possible to simplify this code quite a bit by using a single Renderer instance for
* both datasets and the legend. But for now, the renderers are queried for the line color information
* and this is held in the Legend.
*/
public abstract class Plot<T extends DataType, B extends DataBranch<T>, C extends PlotConfiguration<T, B>> {
protected static final Translator trans = Application.getTranslator();
protected static final SwingPreferences preferences = (SwingPreferences) Application.getPreferences();
@ -64,15 +70,23 @@ public abstract class Plot<T extends DataType, B extends DataBranch<T>, C extend
protected final LegendItems legendItems;
protected final XYSeriesCollection[] data;
protected final C filledConfig; // Configuration after using 'fillAutoAxes'
private final List<B> allBranches;
protected final JFreeChart chart;
protected Plot(JFreeChart chart, B mainBranch, C config, List<B> allBranches, boolean initialShowPoints) {
this.chart = chart;
this.allBranches = allBranches;
protected Plot(String plotName, B mainBranch, C config, List<B> allBranches, boolean initialShowPoints) {
this.branchCount = allBranches.size();
this.chart = ChartFactory.createXYLineChart(
//// Simulated flight
/*title*/plotName,
/*xAxisLabel*/null,
/*yAxisLabel*/null,
/*dataset*/null,
/*orientation*/PlotOrientation.VERTICAL,
/*legend*/false,
/*tooltips*/true,
/*urls*/false
);
this.chart.addSubtitle(new TextTitle(config.getName()));
this.chart.getTitle().setFont(new Font("Dialog", Font.BOLD, 23));
this.chart.setBackgroundPaint(new Color(240, 240, 240));

View File

@ -19,12 +19,9 @@ import info.openrocket.core.simulation.FlightEvent;
import info.openrocket.core.preferences.ApplicationPreferences;
import info.openrocket.core.util.LinearInterpolator;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYImageAnnotation;
import org.jfree.chart.annotations.XYTitleAnnotation;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
@ -36,11 +33,6 @@ import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.chart.ui.RectangleInsets;
/*
* TODO: It should be possible to simplify this code quite a bit by using a single Renderer instance for
* both datasets and the legend. But for now, the renderers are queried for the line color information
* and this is held in the Legend.
*/
@SuppressWarnings("serial")
public class SimulationPlot extends Plot<FlightDataType, FlightDataBranch, SimulationPlotConfiguration> {
private final SimulationPlotConfiguration config;
@ -50,8 +42,8 @@ public class SimulationPlot extends Plot<FlightDataType, FlightDataBranch, Simul
private ErrorAnnotationSet errorAnnotations = null;
SimulationPlot(Simulation simulation, SimulationPlotConfiguration config, boolean initialShowPoints,
JFreeChart chart, FlightDataBranch mainBranch, List<FlightDataBranch> allBranches) {
super(chart, mainBranch, config, allBranches, initialShowPoints);
FlightDataBranch mainBranch, List<FlightDataBranch> allBranches) {
super(simulation.getName(), mainBranch, config, allBranches, initialShowPoints);
this.simulation = simulation;
this.config = config;
@ -67,21 +59,9 @@ public class SimulationPlot extends Plot<FlightDataType, FlightDataBranch, Simul
}
public static SimulationPlot create(Simulation simulation, SimulationPlotConfiguration config, boolean initialShowPoints) {
JFreeChart chart = ChartFactory.createXYLineChart(
//// Simulated flight
/*title*/simulation.getName(),
/*xAxisLabel*/null,
/*yAxisLabel*/null,
/*dataset*/null,
/*orientation*/PlotOrientation.VERTICAL,
/*legend*/false,
/*tooltips*/true,
/*urls*/false
);
FlightDataBranch mainBranch = simulation.getSimulatedData().getBranch(0);
return new SimulationPlot(simulation, config, initialShowPoints, chart, mainBranch,
return new SimulationPlot(simulation, config, initialShowPoints, mainBranch,
simulation.getSimulatedData().getBranches());
}
@ -101,7 +81,7 @@ public class SimulationPlot extends Plot<FlightDataType, FlightDataBranch, Simul
XYPlot plot = (XYPlot) chart.getPlot();
int datasetcount = plot.getDatasetCount();
for (int i = 0; i < datasetcount; i++) {
int seriescount = ((XYSeriesCollection) plot.getDataset(i)).getSeriesCount();
int seriescount = plot.getDataset(i).getSeriesCount();
XYItemRenderer r = ((XYPlot) chart.getPlot()).getRenderer(i);
for (int j = 0; j < seriescount; j++) {
boolean show = (branch < 0) || (j % branchCount == branch);