add code to support outputting gnuplot compatible graphs
[IRC.git] / Robust / TransSim / Plot.java
1 import java.io.*;
2 import java.util.*;
3
4 public class Plot {
5   PrintWriter out;
6   PrintWriter command;
7   String filename;
8   int count=0;
9   String cmdstr="plot ";
10   Hashtable series;
11   boolean first=true;
12   public Plot(String filename) {
13     try {
14       command=new PrintWriter(new FileOutputStream(filename+".cmd"), true);
15     } catch (IOException e) {
16       e.printStackTrace();
17       System.exit(-1);
18     }
19     this.filename=filename;
20     series=new Hashtable();
21   }
22
23   public Series getSeries(String name) {
24     if (series.containsKey(name))
25       return (Series)series.get(name);
26     Series s=createSeries(name);
27     series.put(name, s);
28     return s;
29   }
30
31   private Series createSeries(String name) {
32     Series s=null;
33     try {
34       s=new Series(new PrintWriter(new FileOutputStream(filename+"."+count),true));
35     } catch (IOException e) {
36       e.printStackTrace();
37       System.exit(-1);
38     }
39     if (!first) cmdstr+=",";
40     first=false;
41     cmdstr+="\""+filename+"."+count+"\" title \""+name+"\"";
42     count++;
43     return s;
44   }
45   
46   public void close() {
47     for(Iterator it=series.values().iterator();it.hasNext();) {
48       Series s=(Series)it.next();
49       s.close();
50     }
51     command.println(cmdstr);
52     command.close();
53   }
54 }