micropeak: Change graph tooltip units on the fly
authorKeith Packard <keithp@keithp.com>
Thu, 3 Jan 2013 00:30:44 +0000 (16:30 -0800)
committerKeith Packard <keithp@keithp.com>
Thu, 3 Jan 2013 00:30:44 +0000 (16:30 -0800)
Make sure the tooltips show the right units when they change

Signed-off-by: Keith Packard <keithp@keithp.com>
micropeak/MicroGraph.java

index d2e86f9fe50f151e8f1ac32f98ea1a6cda760038..84320be3503fbee52709945fed765c15cbca3207 100644 (file)
@@ -39,15 +39,19 @@ class MicroSeries extends XYSeries {
        String          label;
        String          units;
        Color           color;
+       XYItemRenderer  renderer;
        
-       String label() {
-               return String.format("%s (%s)", label, units);
-       }
-
        void set_units(String units) {
                this.units = units;
 
-               axis.setLabel(label());
+               axis.setLabel(String.format("%s (%s)", label, units));
+
+               StandardXYToolTipGenerator      ttg;
+
+               ttg = new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})", units),
+                                                    new java.text.DecimalFormat("0.00"),
+                                                    new java.text.DecimalFormat("0.00"));
+               renderer.setBaseToolTipGenerator(ttg);
        }
 
        public MicroSeries (String label, String units, Color color) {
@@ -56,9 +60,13 @@ class MicroSeries extends XYSeries {
                this.units = units;
                this.color = color;
 
-               axis = new NumberAxis(label());
+               axis = new NumberAxis();
                axis.setLabelPaint(color);
                axis.setTickLabelPaint(color);
+
+               renderer = new XYLineAndShapeRenderer(true, false);
+               renderer.setSeriesPaint(0, color);
+               set_units(units);
        }
 }
 
@@ -88,16 +96,11 @@ public class MicroGraph implements AltosUnitsListener {
        private MicroSeries addSeries(int index, String label, String units, Color color) {
                MicroSeries             series = new MicroSeries(label, units, color);
                XYSeriesCollection      dataset = new XYSeriesCollection(series);
-               XYItemRenderer          renderer = new XYLineAndShapeRenderer(true, false);
 
-               renderer.setSeriesPaint(0, color);
-               renderer.setPlot(plot);
-               renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(String.format("{1}s: {2}%s ({0})", units),
-                                                                               new java.text.DecimalFormat("0.00"),
-                                                                               new java.text.DecimalFormat("0.00")));
+               series.renderer.setPlot(plot);
                plot.setRangeAxis(index, series.axis);
                plot.setDataset(index, dataset);
-               plot.setRenderer(index, renderer);
+               plot.setRenderer(index, series.renderer);
                plot.mapDatasetToRangeAxis(index, index);
                return series;
        }