altos: get ao_max6691 driver working
[fw/altos] / altosuilib / AltosUIGraph.java
index ef0cc6772daa8e80e8f06d216ddfdabed136c5fe..098e9a52f17ebe28add7f8f3829f10f86279f278 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altosuilib_1;
+package org.altusmetrum.altosuilib_13;
 
 import java.io.*;
+import java.util.*;
 import java.util.ArrayList;
 
 import java.awt.*;
 import javax.swing.*;
-import org.altusmetrum.altoslib_2.*;
+import org.altusmetrum.altoslib_13.*;
 
 import org.jfree.ui.*;
 import org.jfree.chart.*;
@@ -34,17 +36,17 @@ import org.jfree.chart.labels.*;
 import org.jfree.data.xy.*;
 import org.jfree.data.*;
 
-public class AltosUIGraph implements AltosUnitsListener {
+public class AltosUIGraph implements AltosUnitsListener, AltosShapeListener {
 
        XYPlot                          plot;
        JFreeChart                      chart;
        public ChartPanel               panel;
        NumberAxis                      xAxis;
        AltosUIEnable                   enable;
-       ArrayList<AltosUIGrapher>       graphers;
-       AltosUIDataSet                  dataSet;
+       AltosUITimeSeries[]             series;
        int                             axis_index;
        int                             series_index;
+       Hashtable<Integer,Boolean>      axes_added;
 
        static final private Color gridline_color = new Color(0, 0, 0);
        static final private Color border_color = new Color(255, 255, 255);
@@ -54,84 +56,93 @@ public class AltosUIGraph implements AltosUnitsListener {
                return panel;
        }
 
-       public AltosUIAxis newAxis(String label, AltosUnits units, Color color, int flags) {
-               AltosUIAxis axis = new AltosUIAxis(label, units, color, axis_index++, flags);
+       public AltosUIAxis newAxis(String label, AltosUnits units, AltosUILineStyle line_style, int flags) {
+               AltosUIAxis axis = new AltosUIAxis(label, units, line_style, axis_index++, flags);
                plot.setRangeAxis(axis.index, axis);
                return axis;
        }
 
-       public AltosUIAxis newAxis(String label, AltosUnits units, Color color) {
-               return newAxis(label, units, color, AltosUIAxis.axis_default);
+       public AltosUIAxis newAxis(String label, AltosUnits units, AltosUILineStyle line_style) {
+               return newAxis(label, units, line_style, AltosUIAxis.axis_default);
        }
 
-       public void addSeries(String label, int fetch, AltosUnits units, Color color,
-                             boolean enabled, AltosUIAxis axis) {
-               AltosUISeries           series = new AltosUISeries(label, fetch, units, color, enabled, axis);
-               XYSeriesCollection      dataset = new XYSeriesCollection(series);
+       void addAxis(AltosUIAxis axis) {
+               if (!axes_added.containsKey(axis.index)) {
+                       axes_added.put(axis.index, true);
+                       plot.setRangeAxis(axis.index, axis);
+               }
+       }
+
+       public void addSeries(AltosUITimeSeries series) {
+               XYSeriesCollection      dataset = new XYSeriesCollection(series.xy_series());
+
+               addAxis(series.axis);
 
                series.renderer.setPlot(plot);
                plot.setDataset(series_index, dataset);
                plot.setRenderer(series_index, series.renderer);
-               plot.mapDatasetToRangeAxis(series_index, axis.index);
+               plot.mapDatasetToRangeAxis(series_index, series.axis.index);
                if (enable != null)
-                       enable.add(label, series, enabled);
-               this.graphers.add(series);
+                       enable.add(series.label, series, series.enable);
                series_index++;
        }
 
-       public void addSeries(String label, int fetch, AltosUnits units, Color color) {
-               addSeries(label, fetch, units, color, true, newAxis(label, units, color));
+       public void addMarker(AltosUITimeSeries series) {
        }
-       
-       public void addMarker(String label, int fetch, Color color) {
-               AltosUIMarker           marker = new AltosUIMarker(fetch, color, plot);
-               if (enable != null)
-                       enable.add(label, marker, true);
-               this.graphers.add(marker);
+
+       public void units_changed(boolean imperial_units) {
+               for (AltosUITimeSeries s : series)
+                       s.set_units();
        }
 
-       public void resetData() {
-               for (AltosUIGrapher g : graphers) {
-                       g.clear();
-                       g.setNotify(false);
-               }
-               if (dataSet != null) {
-                       for (AltosUIDataPoint dataPoint : dataSet.dataPoints())
-                               for (AltosUIGrapher g : graphers)
-                                       g.add(dataPoint);
-               }
-               for (AltosUIGrapher g : graphers) {
-                       g.setNotify(true);
-                       g.fireSeriesChanged();
-               }
+       public void filter_changed() {
+               units_changed(false);
        }
 
-       public void units_changed(boolean imperial_units) {
-               for (AltosUIGrapher g : graphers)
-                       g.set_units();
-               resetData();
+       public void set_shapes_visible(boolean visible) {
+               for (AltosUITimeSeries s : series)
+                       s.set_shapes_visible(visible);
+       }
+
+       public void set_line_width(float width) {
+               for (AltosUITimeSeries s : series)
+                       s.set_line_width(width);
        }
 
        public void setName (String name) {
                chart.setTitle(name);
        }
 
-       public void setDataSet (AltosUIDataSet dataSet) {
-               this.dataSet = dataSet;
-               resetData();
-               if (dataSet != null)
-                       setName(dataSet.name());
+       public void set_series(AltosUITimeSeries[] series) {
+               this.series = series;
+               boolean any_enabled = false;
+
+               for (AltosUITimeSeries s : series)
+                       if (s.enable)
+                               any_enabled = true;
+
+               if (!any_enabled)
+                       for (AltosUITimeSeries s : series)
+                               s.set_enable(true);
+
+               for (AltosUITimeSeries s : series)
+                       addSeries(s);
+
+               units_changed(false);
        }
 
-       public AltosUIGraph(AltosUIEnable enable) {
+       public AltosUIGraph(AltosUIEnable enable, String title) {
 
                this.enable = enable;
-               this.graphers = new ArrayList<AltosUIGrapher>();
-               this.series_index = 0;
+               this.series = null;
                this.axis_index = 0;
 
+               enable.register_shape_listener(this);
+
+               axes_added = new Hashtable<Integer,Boolean>();
+
                xAxis = new NumberAxis("Time (s)");
-               
+
                xAxis.setAutoRangeIncludesZero(true);
 
                plot = new XYPlot();
@@ -140,7 +151,7 @@ public class AltosUIGraph implements AltosUnitsListener {
                plot.setDomainPannable(true);
                plot.setRangePannable(true);
 
-               chart = new JFreeChart("Flight", JFreeChart.DEFAULT_TITLE_FONT,
+               chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT,
                                       plot, true);
 
                ChartUtilities.applyCurrentTheme(chart);
@@ -157,5 +168,6 @@ public class AltosUIGraph implements AltosUnitsListener {
                panel.setPreferredSize(new java.awt.Dimension(800, 500));
 
                AltosPreferences.register_units_listener(this);
+
        }
-}
\ No newline at end of file
+}