[Swift-commit] r4184 - usertools/plotter/src/plotter

noreply at svn.ci.uchicago.edu noreply at svn.ci.uchicago.edu
Mon Mar 14 22:36:36 CDT 2011


Author: wozniak
Date: 2011-03-14 22:36:36 -0500 (Mon, 14 Mar 2011)
New Revision: 4184

Added:
   usertools/plotter/src/plotter/Dual.java
   usertools/plotter/src/plotter/Plots.java
Modified:
   usertools/plotter/src/plotter/Lines.java
   usertools/plotter/src/plotter/Util.java
Log:
Essentially copy Lines to Dual


Added: usertools/plotter/src/plotter/Dual.java
===================================================================
--- usertools/plotter/src/plotter/Dual.java	                        (rev 0)
+++ usertools/plotter/src/plotter/Dual.java	2011-03-15 03:36:36 UTC (rev 4184)
@@ -0,0 +1,351 @@
+package plotter;
+
+import gnu.getopt.Getopt;
+
+import java.awt.Color;
+import java.awt.geom.Rectangle2D;
+import java.io.*;
+import java.util.*;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D;
+import org.jfree.chart.ChartFactory;
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.axis.NumberAxis;
+import org.jfree.chart.plot.PlotOrientation;
+import org.jfree.chart.plot.XYPlot;
+import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
+import org.jfree.data.Range;
+import org.jfree.data.general.Series;
+import org.jfree.data.xy.XYSeriesCollection;
+
+/**
+ * Plots two data lines on two axes
+ * See main() for command-line arguments
+ * */
+public class Dual
+{
+  static Properties properties;
+
+  public static boolean bw = false;
+
+  static int width = 400;
+  static int height = 400;
+
+  // null indicates the value was not set by the user
+  static Double xmin = null;
+  static Double xmax = null;
+  static Double ymin = null;
+  static Double ymax = null;
+
+  /**
+     Generate simple plot
+     @param collection The x,y data
+     @param title Plot title
+     @param xlabel X label text
+     @param ylabel1 Y label text
+     @param ylabel2 Y label text
+     @param output EPS filename
+  */
+  public static boolean plot(XYSeriesCollection collection,
+                             String title, String xlabel,
+                             String ylabel1, String ylabel2,
+                             String output)
+  {
+    EPSDocumentGraphics2D g2d = null;
+    Rectangle2D.Double rectangle = null;
+    OutputStream out = null;
+
+    try
+    {
+      out = new FileOutputStream(output);
+      out = new BufferedOutputStream(out);
+
+      g2d = new EPSDocumentGraphics2D(false);
+      g2d.setGraphicContext
+        (new org.apache.xmlgraphics.java2d.GraphicContext());
+
+      rectangle = new Rectangle2D.Double(0, 0, width, height);
+
+      g2d.setGraphicContext
+        (new org.apache.xmlgraphics.java2d.GraphicContext());
+      g2d.setupDocument(out, width, height);
+    }
+    catch (IOException e)
+    {
+      System.out.println("Problem with file: " + output);
+      return false;
+    }
+
+    final boolean withLegend = true;
+
+    JFreeChart chart =
+      ChartFactory.createXYLineChart
+      (title, xlabel, ylabel1, collection,
+       PlotOrientation.VERTICAL, withLegend, false, false);
+
+    setupPlot(chart, collection);
+    chart.draw(g2d, rectangle);
+
+    try
+    {
+      g2d.finish();
+    }
+    catch (Exception e)
+    {
+      System.out.println("Err!");
+    }
+
+    IOUtils.closeQuietly(out);
+    System.out.println("PLOTTED: " + output);
+
+    return true;
+  }
+
+  private static void setupPlot(JFreeChart chart,
+                                XYSeriesCollection collection)
+  {
+    XYPlot plot = chart.getXYPlot();
+    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
+    int count = plot.getSeriesCount();
+    if (count != 2)
+      Util.fatal("dual plot must have two data sets!");
+    if (bw)
+      for (int i = 0; i < count; i++)
+        renderer.setSeriesPaint(i, Color.BLACK);
+    Series series1 = collection.getSeries(0);
+    if (! showShape(series1.getDescription()))
+      renderer.setSeriesShapesVisible(0, false);
+    Series series2 = collection.getSeries(1);
+    if (! showShape(series2.getDescription()))
+      renderer.setSeriesShapesVisible(1, false);
+
+    Plots.setupLegend(chart, properties);
+    setAxes(plot);
+    plot.setRenderer(renderer);
+    plot.setBackgroundPaint(Color.WHITE);
+  }
+
+  static void setAxes(XYPlot plot)
+  {
+    // Actual values: modify if necessary
+    double axmin, axmax, aymin, aymax;
+
+    if (xmin != null || xmax != null)
+    {
+      NumberAxis axis = (NumberAxis) plot.getDomainAxis();
+      Range range = axis.getRange();
+      axmin = range.getLowerBound();
+      axmax = range.getUpperBound();
+      if (xmin != null) axmin = xmin;
+      if (xmax != null) axmax = xmax;
+      axis.setRange(axmin, axmax);
+    }
+
+    if (ymin != null || ymax != null)
+    {
+      NumberAxis axis = (NumberAxis) plot.getRangeAxis();
+      Range range = axis.getRange();
+      aymin = range.getLowerBound();
+      aymax = range.getUpperBound();
+      if (ymin != null) aymin = ymin;
+      if (ymax != null) aymax = ymax;
+      axis.setRange(aymin, aymax);
+    }
+  }
+
+  /**
+     Args: Lines <properties> <output file> <data file>*
+     Reads settings from properties: see scanProperties()
+     Produces EPS output file
+     Data files are two-columns of numbers each -
+        see LineReader.read() and LineReader.array()
+  */
+  public static void main(String[] args)
+  {
+    // Settings:
+    boolean verbose = false;
+
+    Getopt g = new Getopt("testprog", args, "v");
+    int c = -1;
+    while ((c = g.getopt()) != -1)
+    {
+      switch (c)
+      {
+        case 'v':
+          verbose = true;
+      }
+    }
+
+    if (args.length < 3)
+    {
+      System.out.println
+      ("usage: [<options>] <properties> <output> <data>*");
+      System.exit(2);
+    }
+
+    Bits.init();
+    Util.verbose(verbose);
+
+    String propFile = args[0];
+    String output = args[1];
+    List<String> names = new ArrayList<String>();
+    for (int i = 2; i < args.length; i++)
+      names.add(args[i]);
+
+    String title = null;
+    String xlabel = null;
+    String ylabel = null;
+    List<double[][]> data = new ArrayList<double[][]>();
+    List<String> labels = new ArrayList<String>();
+
+    properties = new Properties();
+    load(propFile);
+    title = properties.getProperty("title");
+    xlabel = properties.getProperty("xlabel");
+    ylabel = properties.getProperty("ylabel");
+
+    scanProperties();
+
+    for (String name : names)
+    {
+      File file = new File(name);
+      Util.verbose("open: " + file);
+      List<String> lines = null;
+      try
+      {
+        lines = LineReader.read(file);
+      }
+      catch (FileNotFoundException e)
+      {
+        System.out.println("not found: " + file);
+        System.exit(1);
+      }
+      double[][] array = LineReader.array(lines);
+      data.add(array);
+      addLabel(name, labels);
+      Util.verbose("array:\n" + toString(array));
+    }
+
+    XYSeriesCollection collection = Util.collection(data, labels,
+                                                    names);
+
+    plot(collection, title, xlabel, ylabel, null, output);
+  }
+
+  static void load(String propFile)
+  {
+    try
+    {
+      if (propFile.equals("-"))
+        properties.load(System.in);
+      else
+        properties.load(new FileInputStream(propFile));
+    }
+    catch (FileNotFoundException e)
+    {
+      System.out.println(e);
+      System.exit(1);
+    }
+    catch (IOException e)
+    {
+      e.printStackTrace();
+      System.exit(1);
+    }
+  }
+
+  /**
+     Various plot properties.  All are currently optional
+
+     Example.
+     Assume you want to plot a two-column table in file.data.
+     The first column is the x values and the second column
+     is the y values.  See LineReader for details.
+
+     Your properties may include:
+     title = Plot
+     xlabel = size
+     ylabel = speed
+     label.file.data = legend text
+     width (output image width)
+     height (output image height)
+     xmin, xmax, ymin, ymax (auto-selected if not given)
+     bw (Black and white, true/false, default false)
+  */
+  static void scanProperties()
+  {
+    String tmp;
+    tmp = properties.getProperty("width");
+    if (tmp != null)
+      width = Integer.parseInt(tmp.trim());
+    tmp = properties.getProperty("height");
+    if (tmp != null)
+      height = Integer.parseInt(tmp.trim());
+    tmp = properties.getProperty("xmin");
+    if (tmp != null)
+      xmin = Double.parseDouble(tmp);
+    tmp = properties.getProperty("xmax");
+    if (tmp != null)
+      xmax = Double.parseDouble(tmp);
+    tmp = properties.getProperty("ymin");
+    if (tmp != null)
+      ymin = Double.parseDouble(tmp);
+    tmp = properties.getProperty("ymax");
+    if (tmp != null)
+      ymax = Double.parseDouble(tmp);
+    tmp = properties.getProperty("bw");
+    if (tmp != null)
+      bw = Boolean.parseBoolean(tmp);
+  }
+
+  /**
+     Arrays.copyOfRange is a Java 1.6 feature.
+     This has the same signature.
+  */
+  /*
+  static String[] select(String[] s, int p, int q)
+  {
+    String[] result = new String[q-p];
+    int j = 0;
+    for (int i = p; i < q; i++)
+      result[j++] = s[i];
+    return result;
+  }
+  */
+
+  static void addLabel(String name,
+                       List<String> labels)
+  {
+    String label = properties.getProperty("label."+name);
+    if (label == null)
+      label = "";
+    labels.add(label);
+  }
+
+  static boolean showShape(String name)
+  {
+    // System.out.println(name);
+    String mode = properties.getProperty("shape."+name);
+    // System.out.println(mode);
+    if ("none".equals(mode))
+      return false;
+    return true;
+  }
+
+  static String toString(double[][] array)
+  {
+    StringBuilder sb = new StringBuilder();
+    for (int i = 0; i < array.length; i++)
+    {
+      double[] row = array[i];
+      for (int j = 0; j < row.length; j++)
+      {
+        sb.append(array[i][j]);
+        if (j < row.length-1)
+          sb.append(" ");
+      }
+      sb.append("\n");
+    }
+    return sb.toString();
+  }
+}


Property changes on: usertools/plotter/src/plotter/Dual.java
___________________________________________________________________
Name: svn:executable
   + *

Modified: usertools/plotter/src/plotter/Lines.java
===================================================================
--- usertools/plotter/src/plotter/Lines.java	2011-03-15 03:21:07 UTC (rev 4183)
+++ usertools/plotter/src/plotter/Lines.java	2011-03-15 03:36:36 UTC (rev 4184)
@@ -3,7 +3,6 @@
 import gnu.getopt.Getopt;
 
 import java.awt.Color;
-import java.awt.Font;
 import java.awt.geom.Rectangle2D;
 import java.io.*;
 import java.util.*;
@@ -16,7 +15,6 @@
 import org.jfree.chart.plot.PlotOrientation;
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
-import org.jfree.chart.title.LegendTitle;
 import org.jfree.data.Range;
 import org.jfree.data.general.Series;
 import org.jfree.data.xy.XYSeriesCollection;
@@ -116,20 +114,12 @@
         renderer.setSeriesShapesVisible(i, false);
     }
 
-    setupLegend(chart);
+    Plots.setupLegend(chart, properties);
     setAxes(plot);
     plot.setRenderer(renderer);
     plot.setBackgroundPaint(Color.WHITE);
   }
 
-  private static void setupLegend(JFreeChart chart)
-  {
-    LegendTitle legend = chart.getLegend();
-    Font font = getLegendFont();
-    if (font != null)
-      legend.setItemFont(font);
-  }
-
   static void setAxes(XYPlot plot)
   {
     // Actual values: modify if necessary
@@ -336,17 +326,6 @@
     return true;
   }
 
-  static Font getLegendFont()
-  {
-    Font result = null;
-    String name = properties.getProperty("legend.font");
-    if (name == null)
-      return null;
-    System.out.println(name);
-    result = new Font(name, Font.PLAIN, 12);
-    return result;
-  }
-
   static String toString(double[][] array)
   {
     StringBuilder sb = new StringBuilder();

Added: usertools/plotter/src/plotter/Plots.java
===================================================================
--- usertools/plotter/src/plotter/Plots.java	                        (rev 0)
+++ usertools/plotter/src/plotter/Plots.java	2011-03-15 03:36:36 UTC (rev 4184)
@@ -0,0 +1,34 @@
+package plotter;
+
+import java.awt.Font;
+import java.util.Properties;
+
+import org.jfree.chart.JFreeChart;
+import org.jfree.chart.title.LegendTitle;
+
+/**
+ * Reusable JFreeChart-specific functions
+ * @author wozniak
+ *
+ */
+public class Plots
+{
+  static void setupLegend(JFreeChart chart,
+                          Properties properties)
+  {
+    LegendTitle legend = chart.getLegend();
+    Font font = getLegendFont(properties);
+    if (font != null)
+      legend.setItemFont(font);
+  }
+
+  static Font getLegendFont(Properties properties)
+  {
+    Font result = null;
+    String name = properties.getProperty("legend.font");
+    if (name == null)
+      return null;
+    result = new Font(name, Font.PLAIN, 12);
+    return result;
+  }
+}


Property changes on: usertools/plotter/src/plotter/Plots.java
___________________________________________________________________
Name: svn:executable
   + *

Modified: usertools/plotter/src/plotter/Util.java
===================================================================
--- usertools/plotter/src/plotter/Util.java	2011-03-15 03:21:07 UTC (rev 4183)
+++ usertools/plotter/src/plotter/Util.java	2011-03-15 03:36:36 UTC (rev 4184)
@@ -45,6 +45,12 @@
     return collection;
   }
 
+  public static void fatal(String s)
+  {
+    System.out.println(s);
+    System.exit(2);
+  }
+
   public static void verbose(String s)
   {
     if (v)




More information about the Swift-commit mailing list