From 809113740bc954583db087e1b6da80a6df4e89df Mon Sep 17 00:00:00 2001 From: kruland2607 Date: Fri, 9 Nov 2012 13:04:17 -0600 Subject: [PATCH] Modified event display logic so only events for the selected stage are displayed. --- .../openrocket/gui/plot/SimulationPlot.java | 164 ++++++++++++++---- 1 file changed, 133 insertions(+), 31 deletions(-) diff --git a/core/src/net/sf/openrocket/gui/plot/SimulationPlot.java b/core/src/net/sf/openrocket/gui/plot/SimulationPlot.java index 6f18b9e6e..332406660 100644 --- a/core/src/net/sf/openrocket/gui/plot/SimulationPlot.java +++ b/core/src/net/sf/openrocket/gui/plot/SimulationPlot.java @@ -14,6 +14,8 @@ import java.awt.geom.Line2D; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HashSet; import java.util.List; @@ -50,8 +52,13 @@ public class SimulationPlot { private static final float PLOT_STROKE_WIDTH = 1.5f; - private JFreeChart chart; + private final JFreeChart chart; + private final PlotConfiguration config; + private final Simulation simulation; + private final PlotConfiguration filled; + + private final List eventList; private final List renderers = new ArrayList(); int branchCount; @@ -73,9 +80,13 @@ public class SimulationPlot { r.setSeriesVisible(j, show); } } + drawDomainMarkers(branch); } SimulationPlot( Simulation simulation, PlotConfiguration config, boolean initialShowPoints ) { + this.simulation = simulation; + this.config = config; + this.chart = ChartFactory.createXYLineChart( //// Simulated flight simulation.getName(), @@ -90,11 +101,11 @@ public class SimulationPlot { chart.addSubtitle(new TextTitle(config.getName())); - branchCount = simulation.getSimulatedData().getBranchCount(); + this.branchCount = simulation.getSimulatedData().getBranchCount(); // Fill the auto-selections based on first branch selected. FlightDataBranch mainBranch = simulation.getSimulatedData().getBranch( 0 ); - PlotConfiguration filled = config.fillAutoAxes(mainBranch); + this.filled = config.fillAutoAxes(mainBranch); List axes = filled.getAllAxes(); // Create the data series for both axes @@ -181,17 +192,94 @@ public class SimulationPlot { // Create list of events to show (combine event too close to each other) - List eventList = buildEventInfo(simulation, config); + this.eventList = buildEventInfo(); // Create the event markers + drawDomainMarkers(-1); + } + + JFreeChart getJFreeChart() { + return chart; + } + + private String getLabel(FlightDataType type, Unit unit) { + String name = type.getName(); + if (unit != null && !UnitGroup.UNITS_NONE.contains(unit) && + !UnitGroup.UNITS_COEFFICIENT.contains(unit) && unit.getUnit().length() > 0) + name += " (" + unit.getUnit() + ")"; + return name; + } + + private void drawDomainMarkers( int stage ) { + XYPlot plot = chart.getXYPlot(); + FlightDataBranch mainBranch = simulation.getSimulatedData().getBranch( 0 ); + + // Clear existing domain markers + plot.clearDomainMarkers(); + + // Construct domain marker lists collapsing based on time. + + List eventTimes = new ArrayList(); + List eventLabels = new ArrayList(); + List eventColors = new ArrayList(); + List eventImages = new ArrayList(); + { + HashSet typeSet = new HashSet(); + double prevTime = -100; + String text = null; + Color color = null; + Image image = null; + for( EventDisplayInfo info : eventList ) { + if ( stage >=0 && stage != info.stage ) { + continue; + } + + double t = info.time; + FlightEvent.Type type = info.event.getType(); + + if (Math.abs(t - prevTime) <= 0.05) { + + if (!typeSet.contains(type)) { + text = text + ", " + type.toString(); + color = EventGraphics.getEventColor(type); + image = EventGraphics.getEventImage(type); + typeSet.add(type); + } + + } else { + + if (text != null) { + eventTimes.add(prevTime); + eventLabels.add(text); + eventColors.add(color); + eventImages.add(image); + } + prevTime = t; + text = type.toString(); + color = EventGraphics.getEventColor(type); + image = EventGraphics.getEventImage(type); + typeSet.clear(); + typeSet.add(type); + } + + } + if (text != null) { + eventTimes.add(prevTime); + eventLabels.add(text); + eventColors.add(color); + eventImages.add(image); + } + } + + // Plot the markers if (config.getDomainAxisType() == FlightDataType.TYPE_TIME) { // Domain time is plotted as vertical markers - for ( EventDisplayInfo info : eventList ) { - double t = info.time; - String event = info.event; - Color color = info.color; + for ( int i=0; i 0) - name += " (" + unit.getUnit() + ")"; - return name; - } - - private List buildEventInfo(Simulation simulation, PlotConfiguration config) { + + private List buildEventInfo() { ArrayList eventList = new ArrayList(); - HashSet typeSet = new HashSet(); + for( int branch=0; branch events = simulation.getSimulatedData().getBranch(branch).getEvents(); + for( FlightEvent event : events ) { + FlightEvent.Type type = event.getType(); + if ( type != FlightEvent.Type.ALTITUDE && config.isEventActive(type)) { + EventDisplayInfo info = new EventDisplayInfo(); + info.stage = branch; + info.time = event.getTime(); + info.event = event; + eventList.add(info); + } + } + } + + Collections.sort(eventList, new Comparator() { + + @Override + public int compare(EventDisplayInfo o1, EventDisplayInfo o2) { + if ( o1.time< o2.time) return -1; + if ( o1.time == o2.time)return 0; + return 1; + } + + }); + + return eventList; + + /* double prevTime = -100; String text = null; Color color = null; @@ -312,6 +414,7 @@ public class SimulationPlot { eventList.add(info); } return eventList; + */ } /** @@ -459,10 +562,9 @@ public class SimulationPlot { } private static class EventDisplayInfo { + int stage; double time; - String event; - Color color; - Image image; + FlightEvent event; } }