Add axis labels to charts
[sw/motorsim] / gui / com / billkuker / rocketry / motorsim / visual / Chart.java
index 08f60000663ba5820ed8946da9d0db99a53d4bb8..945f5470ffecc1336df2ed2e0144f53c67a37045 100644 (file)
@@ -118,12 +118,15 @@ public class Chart<X extends Quantity, Y extends Quantity> extends JPanel implem
        Unit<X> xUnit;\r
        Unit<Y> yUnit;\r
 \r
+       String xLabel;\r
+       String yLabel;\r
+       \r
        Object source;\r
        Method f;\r
        \r
        Iterable<Amount<X>> domain;\r
-\r
-       public Chart(Unit<X> xUnit, Unit<Y> yUnit, Object source, String method)\r
+       \r
+       public Chart(Unit<X> xUnit, Unit<Y> yUnit, Object source, String method, String xLabel, String yLabel)\r
                        throws NoSuchMethodException {\r
                super(new BorderLayout());\r
                f = source.getClass().getMethod(method, Amount.class);\r
@@ -132,12 +135,22 @@ public class Chart<X extends Quantity, Y extends Quantity> extends JPanel implem
                \r
                this.xUnit = xUnit;\r
                this.yUnit = yUnit;\r
+               \r
+               this.xLabel = xLabel;\r
+               this.yLabel = yLabel;\r
 \r
                RocketScience.addUnitPreferenceListener(this);\r
                \r
                setup();\r
        }\r
        \r
+       private static String toTitle(Method f) {\r
+               String ret = f.getName().substring(0, 1).toUpperCase()\r
+                               + f.getName().substring(1);\r
+               ret = ret.replaceAll("(\\p{Ll})(\\p{Lu})", "$1 $2");\r
+               return ret;\r
+       }\r
+       \r
        private void setup(){\r
                removeAll();\r
                this.xUnit = RocketScience.UnitPreference.getUnitPreference()\r
@@ -145,11 +158,10 @@ public class Chart<X extends Quantity, Y extends Quantity> extends JPanel implem
                this.yUnit = RocketScience.UnitPreference.getUnitPreference()\r
                                .getPreferredUnit(yUnit);\r
 \r
-               chart = ChartFactory.createXYLineChart(f.getName().substring(0, 1)\r
-                               .toUpperCase()\r
-                               + f.getName().substring(1), // Title\r
-                               this.xUnit.toString(), // x-axis Label\r
-                               this.yUnit.toString(), // y-axis Label\r
+               chart = ChartFactory.createXYLineChart(\r
+                               toTitle(f), // Title\r
+                               xLabel + " (" + xUnit.toString() + ")", // x-axis Label\r
+                               yLabel + " (" + yUnit.toString() + ")", // y-axis Label\r
                                dataset, PlotOrientation.VERTICAL, // Plot Orientation\r
                                false, // Show Legend\r
                                true, // Use tool tips\r
@@ -167,9 +179,9 @@ public class Chart<X extends Quantity, Y extends Quantity> extends JPanel implem
                                                sb.append(f.getName().substring(0, 1).toUpperCase()\r
                                                                + f.getName().substring(1));\r
                                                sb.append("\n");\r
-                                               sb.append(Chart.this.xUnit.toString());\r
+                                               sb.append(Chart.this.chart.getXYPlot().getDomainAxis().getLabel());\r
                                                sb.append(",");\r
-                                               sb.append(Chart.this.yUnit.toString());\r
+                                               sb.append(Chart.this.chart.getXYPlot().getRangeAxis().getLabel());\r
                                                sb.append("\n");\r
                                                for (int i = 0; i < s.getItemCount(); i++) {\r
                                                        sb.append(s.getX(i));\r
@@ -395,7 +407,7 @@ public class Chart<X extends Quantity, Y extends Quantity> extends JPanel implem
                g.setID(Amount.valueOf(10, SI.MILLIMETER));\r
 \r
                Chart<Length, Area> c = new Chart<Length, Area>(SI.MILLIMETER,\r
-                               SI.MILLIMETER.pow(2).asType(Area.class), g, "surfaceArea");\r
+                               SI.MILLIMETER.pow(2).asType(Area.class), g, "surfaceArea", "Regression", "Area");\r
 \r
                c.setDomain(c.new IntervalDomain(Amount.valueOf(0, SI.CENTIMETER), g\r
                                .webThickness()));\r