X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=android%2Fsrc%2Fnet%2Fsf%2Fopenrocket%2Fandroid%2Fsimulation%2FSimulationChart.java;h=601bf859c92f9074e8cd501195ebe980445b9411;hb=2f32575d96905bfad9d3d94098995fb3057730e9;hp=20bab823c2708a6f7fe8ce514aa35260548d4b69;hpb=7901d5fdebc50a0c1d180ebfc27fff725c232443;p=debian%2Fopenrocket diff --git a/android/src/net/sf/openrocket/android/simulation/SimulationChart.java b/android/src/net/sf/openrocket/android/simulation/SimulationChart.java index 20bab823..601bf859 100644 --- a/android/src/net/sf/openrocket/android/simulation/SimulationChart.java +++ b/android/src/net/sf/openrocket/android/simulation/SimulationChart.java @@ -15,13 +15,18 @@ */ package net.sf.openrocket.android.simulation; +import java.io.Serializable; +import java.util.ArrayList; import java.util.List; +import net.sf.openrocket.android.util.AndroidLogWrapper; +import net.sf.openrocket.document.OpenRocketDocument; +import net.sf.openrocket.document.Simulation; import net.sf.openrocket.simulation.FlightDataBranch; import net.sf.openrocket.simulation.FlightDataType; import net.sf.openrocket.simulation.FlightEvent; +import net.sf.openrocket.unit.Unit; -import org.achartengine.ChartFactory; import org.achartengine.chart.LineChart; import org.achartengine.chart.PointStyle; import org.achartengine.chart.XYChart; @@ -30,70 +35,97 @@ import org.achartengine.model.XYSeries; import org.achartengine.renderer.XYMultipleSeriesRenderer; import org.achartengine.renderer.XYSeriesRenderer; -import android.content.Context; -import android.content.Intent; import android.graphics.Color; import android.graphics.Paint.Align; -import android.util.Log; /** - * Multiple temperature demo chart. + * This is really a flyweight object so we can serialize the + * values behind a simulation chart. Since OpenRocketDocument, FlightDataBranch, + * FlightDataType, Unit and all the other underlying types are not serializable, + * we have to resort to persisting just the bare minimum of information. + * + * This also means without further changes to FlightDataType, we cannot actually + * restore the displayed series. + * + * TODO make FlightDataBranch serializable or at least reconstructable from + * from some the name. + * */ -public class SimulationChart { +public class SimulationChart implements Serializable { + + private final int simulationIndex; + private transient FlightDataType series1; + private transient FlightDataType series2; + private transient List events; - private final static String TAG = "SimulationChart"; - - private FlightDataBranch flightDataBranch; - private FlightDataType series1; - private FlightDataType series2; - private final FlightDataType time = FlightDataType.TYPE_TIME; - private List flightEvents; - // Define 4 different colors and point styles to use for the series. // For now only 2 series are supported though. private final static int[] colors = new int[] { Color.BLUE, Color.YELLOW, Color.GREEN, Color.RED }; private final static PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND, PointStyle.TRIANGLE, PointStyle.SQUARE }; - /** - * @param flightDataBranch the flightDataBranch to set - */ - public void setFlightDataBranch(FlightDataBranch flightDataBranch) { - this.flightDataBranch = flightDataBranch; + public SimulationChart(int simulationIndex) { + super(); + this.simulationIndex = simulationIndex; + } + + private static String formatFlightDataTypeAxisLabel( FlightDataType fdt ) { + return fdt.getName() + " (" + fdt.getUnitGroup().getDefaultUnit().toString() + ")"; } - /** - * @param series1 the series1 to set - */ public void setSeries1(FlightDataType series1) { this.series1 = series1; } - /** - * @param series2 the series2 to set - */ + public FlightDataType getSeries1() { + return series1; + } + public void setSeries2(FlightDataType series2) { this.series2 = series2; } - /** - * @param flightEvents the flightEvents to set - */ - public void setFlightEvents(List flightEvents) { - this.flightEvents = flightEvents; + public FlightDataType getSeries2() { + return series2; } - private static String formatFlightDataTypeAxisLabel( FlightDataType fdt ) { - return fdt.getName() + " (" + fdt.getUnitGroup().getDefaultUnit().toString() + ")"; + public void setEvents( List events ) { + this.events = events; + } + + public List getEvents() { + return events; } + public FlightDataBranch getFlightDataBranch( OpenRocketDocument rocketDocument ) { + Simulation sim = rocketDocument.getSimulation(simulationIndex); + FlightDataBranch flightDataBranch = sim.getSimulatedData().getBranch(0); + return flightDataBranch; + } /** * Executes the chart demo. * * @param context the context * @return the built intent */ - public Intent execute(Context context) { + public XYChart buildChart(OpenRocketDocument rocketDocument) { + + Simulation sim = rocketDocument.getSimulation(simulationIndex); + FlightDataBranch flightDataBranch = sim.getSimulatedData().getBranch(0); + FlightDataType time = FlightDataType.TYPE_TIME; + if (series1== null) { + series1 = flightDataBranch.getTypes()[1]; + } + if (series2== null) { + series2 = flightDataBranch.getTypes()[2]; + } + + if ( events == null ) { + events = new ArrayList(); + for ( FlightEvent event : flightDataBranch.getEvents() ) { + events.add(event); + } + } /* * TODO - @@ -116,7 +148,13 @@ public class SimulationChart { renderer.setYLabels(10); renderer.setShowGrid(true); renderer.setZoomButtonsVisible(true); - renderer.setChartTitle("Simulation"); + renderer.setChartTitle(sim.getName()); + renderer.setShowCustomTextGrid(true); + renderer.setXLabelsAlign(Align.RIGHT); + renderer.setXLabelsAngle(90); // rotate right + for( FlightEvent event : events ) { + renderer.addXTextLabel(event.getTime(), event.getType().toString()); + } renderer.setMargins(new int[] { 50, 30, 0, 20 }); { @@ -149,7 +187,13 @@ public class SimulationChart { XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); List timevalues = flightDataBranch.get(time); - List series1values = flightDataBranch.get(series1); + List series1values = new ArrayList( flightDataBranch.get(series1).size() ); + { + Unit u = series1.getUnitGroup().getDefaultUnit(); + for( Double d: flightDataBranch.get(series1) ) { + series1values.add( u.toUnit(d)); + } + } // compute the axis limits using timevalues and series1values. double xmin = 0; @@ -160,7 +204,7 @@ public class SimulationChart { double ymax = computeMaxValueWithPadding( series1values ); double xmax = Math.ceil( timevalues.get( timevalues.size()-1)); - Log.d(TAG,"ymax = " + ymax); + AndroidLogWrapper.d(SimulationChart.class,"ymax = " + ymax); renderer.setXAxisMax(xmax); renderer.setYAxisMax(ymax); @@ -173,10 +217,19 @@ public class SimulationChart { if ( seriesCount > 1 ) { // Add second series - addXYSeries(dataset, series2.getName(), timevalues, flightDataBranch.get(series2), 1); + List series2values = new ArrayList( flightDataBranch.get(series2).size() ); + { + Unit u = series2.getUnitGroup().getDefaultUnit(); + for( Double d: flightDataBranch.get(series2) ) { + series2values.add( u.toUnit(d)); + } + } + + addXYSeries(dataset, series2.getName(), timevalues, series2values, 1); } - Intent intent = getLineChartIntent(context, dataset, renderer,"Simulation"); - return intent; + XYChart chart = new LineChart(dataset, renderer); + + return chart; } private static void addXYSeries(XYMultipleSeriesDataset dataset, String titles, List xValues, List yValues, int scale) { @@ -189,16 +242,6 @@ public class SimulationChart { } - private static Intent getLineChartIntent(Context context, XYMultipleSeriesDataset dataset, - XYMultipleSeriesRenderer renderer, String activityTitle) { - // checkParameters(dataset, renderer); - Intent intent = new Intent(context, GraphicalActivity.class); - XYChart chart = new LineChart(dataset, renderer); - intent.putExtra(ChartFactory.CHART, chart); - intent.putExtra(ChartFactory.TITLE, activityTitle); - return intent; - } - private static double computeMaxValueWithPadding( List list ) { double max = list.get(0); for( double v : list ) {