diff options
Diffstat (limited to 'altosuilib')
-rw-r--r-- | altosuilib/AltosUIAxis.java | 92 | ||||
-rw-r--r-- | altosuilib/AltosUIDataMissing.java | 25 | ||||
-rw-r--r-- | altosuilib/AltosUIDataPoint.java | 8 | ||||
-rw-r--r-- | altosuilib/AltosUIEnable.java | 28 | ||||
-rw-r--r-- | altosuilib/AltosUIGraph.java | 38 | ||||
-rw-r--r-- | altosuilib/AltosUIMarker.java | 31 | ||||
-rw-r--r-- | altosuilib/AltosUISeries.java | 18 | ||||
-rw-r--r-- | altosuilib/Makefile.am | 1 |
8 files changed, 199 insertions, 42 deletions
diff --git a/altosuilib/AltosUIAxis.java b/altosuilib/AltosUIAxis.java new file mode 100644 index 00000000..70084a00 --- /dev/null +++ b/altosuilib/AltosUIAxis.java @@ -0,0 +1,92 @@ +/* + * Copyright © 2013 Keith Packard <keithp@keithp.com> + * + * 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. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.altosuilib_1; + +import java.io.*; +import java.util.ArrayList; + +import java.awt.*; +import javax.swing.*; +import org.altusmetrum.altoslib_1.*; + +import org.jfree.ui.*; +import org.jfree.chart.*; +import org.jfree.chart.plot.*; +import org.jfree.chart.axis.*; +import org.jfree.chart.renderer.*; +import org.jfree.chart.renderer.xy.*; +import org.jfree.chart.labels.*; +import org.jfree.data.xy.*; +import org.jfree.data.*; + +public class AltosUIAxis extends NumberAxis { + String label; + AltosUnits units; + Color color; + int ref; + int visible; + int index; + + public final static int axis_integer = 1; + public final static int axis_include_zero = 2; + + public final static int axis_default = axis_include_zero; + + public void set_units() { + setLabel(String.format("%s (%s)", label, units.show_units())); + } + + public void set_enable(boolean enable) { + if (enable) { + visible++; + if (visible > ref) + System.out.printf("too many visible\n"); + } else { + visible--; + if (visible < 0) + System.out.printf("too few visible\n"); + } + setVisible(visible > 0); + } + + public void ref(boolean enable) { + ++ref; + if (enable) { + ++visible; + setVisible(visible > 0); + } + } + + public AltosUIAxis(String label, AltosUnits units, Color color, int index, int flags) { + this.label = label; + this.units = units; + this.index = index; + this.visible = 0; + this.ref = 0; + setLabelPaint(color); + setTickLabelPaint(color); + setVisible(false); + if ((flags & axis_integer) != 0) + setStandardTickUnits(NumberAxis.createIntegerTickUnits()); + setAutoRangeIncludesZero((flags & axis_include_zero) != 0); + } + + public AltosUIAxis(String label, AltosUnits units, Color color, int index) { + this(label, units, color, index, axis_default); + } +} diff --git a/altosuilib/AltosUIDataMissing.java b/altosuilib/AltosUIDataMissing.java new file mode 100644 index 00000000..c7b01859 --- /dev/null +++ b/altosuilib/AltosUIDataMissing.java @@ -0,0 +1,25 @@ +/* + * Copyright © 2013 Keith Packard <keithp@keithp.com> + * + * 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. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ + +package org.altusmetrum.altosuilib_1; + +public class AltosUIDataMissing extends Exception { + public int id; + public AltosUIDataMissing(int id) { + this.id = id; + } +}
\ No newline at end of file diff --git a/altosuilib/AltosUIDataPoint.java b/altosuilib/AltosUIDataPoint.java index a98f7cdb..d3020410 100644 --- a/altosuilib/AltosUIDataPoint.java +++ b/altosuilib/AltosUIDataPoint.java @@ -18,8 +18,8 @@ package org.altusmetrum.altosuilib_1; public interface AltosUIDataPoint { - public abstract double x(); - public abstract double y(int index); - public abstract int id(int index); - public abstract String id_name(int index); + public abstract double x() throws AltosUIDataMissing; + public abstract double y(int index) throws AltosUIDataMissing; + public abstract int id(int index) throws AltosUIDataMissing; + public abstract String id_name(int index) throws AltosUIDataMissing; } diff --git a/altosuilib/AltosUIEnable.java b/altosuilib/AltosUIEnable.java index 297cf320..55486dea 100644 --- a/altosuilib/AltosUIEnable.java +++ b/altosuilib/AltosUIEnable.java @@ -84,10 +84,38 @@ public class AltosUIEnable extends Container { y++; } + public void add_units() { + /* Imperial units setting */ + /* Add label */ + GridBagConstraints c = new GridBagConstraints(); + c.gridx = 0; c.gridy = 1000; + c.fill = GridBagConstraints.NONE; + c.anchor = GridBagConstraints.LINE_START; + c.insets = il; + add(new JLabel("Imperial Units"), c); + + JRadioButton imperial_units = new JRadioButton("Enable", AltosUIPreferences.imperial_units()); + imperial_units.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + JRadioButton item = (JRadioButton) e.getSource(); + boolean enabled = item.isSelected(); + AltosUIPreferences.set_imperial_units(enabled); + } + }); + imperial_units.setToolTipText("Use Imperial units instead of metric"); + c = new GridBagConstraints(); + c.gridx = 1; c.gridy = 1000; + c.fill = GridBagConstraints.NONE; + c.anchor = GridBagConstraints.LINE_START; + c.insets = il; + add(imperial_units, c); + } + public AltosUIEnable() { il = new Insets(4,4,4,4); ir = new Insets(4,4,4,4); y = 0; setLayout(new GridBagLayout()); + add_units(); } } diff --git a/altosuilib/AltosUIGraph.java b/altosuilib/AltosUIGraph.java index e212093a..5c589c02 100644 --- a/altosuilib/AltosUIGraph.java +++ b/altosuilib/AltosUIGraph.java @@ -43,7 +43,8 @@ public class AltosUIGraph implements AltosUnitsListener { AltosUIEnable enable; ArrayList<AltosUIGrapher> graphers; AltosUIDataSet dataSet; - int index; + int axis_index; + int series_index; static final private Color gridline_color = new Color(0, 0, 0); static final private Color border_color = new Color(255, 255, 255); @@ -53,19 +54,33 @@ public class AltosUIGraph implements AltosUnitsListener { return panel; } - public void addSeries(String label, int fetch, AltosUnits units, Color color) { - AltosUISeries series = new AltosUISeries(label, fetch, units, color); + public AltosUIAxis newAxis(String label, AltosUnits units, Color color, int flags) { + AltosUIAxis axis = new AltosUIAxis(label, units, color, 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 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); series.renderer.setPlot(plot); - plot.setRangeAxis(index, series.axis); - plot.setDataset(index, dataset); - plot.setRenderer(index, series.renderer); - plot.mapDatasetToRangeAxis(index, index); + plot.setDataset(series_index, dataset); + plot.setRenderer(series_index, series.renderer); + plot.mapDatasetToRangeAxis(series_index, axis.index); if (enable != null) - enable.add(label, series, true); + enable.add(label, series, enabled); this.graphers.add(series); - index++; + 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(String label, int fetch, Color color) { @@ -97,16 +112,17 @@ public class AltosUIGraph implements AltosUnitsListener { public void setDataSet (AltosUIDataSet dataSet) { this.dataSet = dataSet; + resetData(); if (dataSet != null) setName(dataSet.name()); - resetData(); } public AltosUIGraph(AltosUIEnable enable) { this.enable = enable; this.graphers = new ArrayList<AltosUIGrapher>(); - this.index = 0; + this.series_index = 0; + this.axis_index = 0; xAxis = new NumberAxis("Time (s)"); diff --git a/altosuilib/AltosUIMarker.java b/altosuilib/AltosUIMarker.java index 560153f2..e2eb9028 100644 --- a/altosuilib/AltosUIMarker.java +++ b/altosuilib/AltosUIMarker.java @@ -72,20 +72,23 @@ public class AltosUIMarker implements AltosUIGrapher { } public void add(AltosUIDataPoint dataPoint) { - int id = dataPoint.id(fetch); - if (id < 0) - return; - if (id == last_id) - return; - ValueMarker marker = new ValueMarker(dataPoint.x()); - marker.setLabel(dataPoint.id_name(fetch)); - marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT); - marker.setLabelTextAnchor(TextAnchor.TOP_LEFT); - marker.setPaint(color); - if (enabled) - plot.addDomainMarker(marker); - markers.add(marker); - last_id = id; + try { + int id = dataPoint.id(fetch); + if (id < 0) + return; + if (id == last_id) + return; + ValueMarker marker = new ValueMarker(dataPoint.x()); + marker.setLabel(dataPoint.id_name(fetch)); + marker.setLabelAnchor(RectangleAnchor.TOP_RIGHT); + marker.setLabelTextAnchor(TextAnchor.TOP_LEFT); + marker.setPaint(color); + if (enabled) + plot.addDomainMarker(marker); + markers.add(marker); + last_id = id; + } catch (AltosUIDataMissing m) { + } } public AltosUIMarker (int fetch, Color color, XYPlot plot, boolean enable) { diff --git a/altosuilib/AltosUISeries.java b/altosuilib/AltosUISeries.java index 26679471..ac09a3cc 100644 --- a/altosuilib/AltosUISeries.java +++ b/altosuilib/AltosUISeries.java @@ -65,7 +65,10 @@ public class AltosUISeries extends XYSeries implements AltosUIGrapher { } public void add(AltosUIDataPoint dataPoint) { - super.add(dataPoint.x(), dataPoint.y(fetch)); + try { + super.add(dataPoint.x(), units.value(dataPoint.y(fetch))); + } catch (AltosUIDataMissing dm) { + } } public AltosUISeries (String label, int fetch, AltosUnits units, Color color, @@ -82,18 +85,7 @@ public class AltosUISeries extends XYSeries implements AltosUIGrapher { renderer = new XYLineAndShapeRenderer(true, false); renderer.setSeriesPaint(0, color); + renderer.setSeriesVisible(0, enable); set_units(); } - - public AltosUISeries (String label, int fetch, AltosUnits units, Color color, boolean enable) { - this(label, fetch, units, color, - enable, - new AltosUIAxis(label, units, color)); - } - - public AltosUISeries (String label, int fetch, AltosUnits units, Color color) { - this(label, fetch, units, color, - true, - new AltosUIAxis(label, units, color)); - } } diff --git a/altosuilib/Makefile.am b/altosuilib/Makefile.am index faf7921f..0cd2aaea 100644 --- a/altosuilib/Makefile.am +++ b/altosuilib/Makefile.am @@ -15,6 +15,7 @@ altosuilib_JAVA = \ AltosPositionListener.java \ AltosUIConfigure.java \ AltosUIAxis.java \ + AltosUIDataMissing.java \ AltosUIDataPoint.java \ AltosUIDataSet.java \ AltosUIGraph.java \ |