Back to index

wims  3.65+svn20090927
PlotApplet.java
Go to the documentation of this file.
00001 /* Appletable Plotter
00002 
00003  @Author: Edward A. Lee and Christopher Hylands
00004 
00005  @Version: $Id: PlotApplet.java,v 1.76 2005/07/30 05:30:56 cxh Exp $
00006 
00007  @Copyright (c) 1997-2005 The Regents of the University of California.
00008  All rights reserved.
00009 
00010  Permission is hereby granted, without written agreement and without
00011  license or royalty fees, to use, copy, modify, and distribute this
00012  software and its documentation for any purpose, provided that the
00013  above copyright notice and the following two paragraphs appear in all
00014  copies of this software.
00015 
00016  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
00017  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
00018  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
00019  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
00020  SUCH DAMAGE.
00021 
00022  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00023  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00024  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
00025  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
00026  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
00027  ENHANCEMENTS, OR MODIFICATIONS.
00028 
00029  PT_COPYRIGHT_VERSION_2
00030  COPYRIGHTENDKEY
00031  */
00032 package ptolemy.plot;
00033 
00034 import java.awt.BorderLayout;
00035 import java.awt.Color;
00036 import java.io.FileNotFoundException;
00037 import java.io.IOException;
00038 import java.io.InputStream;
00039 import java.net.MalformedURLException;
00040 import java.net.URL;
00041 
00042 import javax.swing.JApplet;
00043 import javax.swing.SwingUtilities;
00044 
00045 import java.applet.*;                                                                                                                      
00046 import java.awt.*;                                                                                                                         
00047 import java.util.*;                                                                                                                        
00048 import java.awt.event.*;                                                                                                                   
00049 import java.lang.Math;
00050 
00051 
00052 import org.javia.arity.*;
00053 
00056 
00072 public class PlotApplet extends JApplet {
00073     //     /** Construct a Plot applet */
00074     //     public PlotApplet() {
00075     // FIXME: having the mutex causes applets to hang.
00076     // The mutex was introduced to Work around problem in Java
00077     // 1.3.1_08 where if we create 6 instances of a Plot applet then
00078     // navigate forward then back - IE and Navigator hang.
00079     // However, since we are now operating inside the Swing Event
00080     // thread, I think this code is unnecessary.
00081     //         if (_mutex == null) {
00082     //             _mutex = new Object();
00083     //         }
00084     //     }
00085     public String ReadApplet(){
00086                                                                                                                 
00087         String reply="";
00088        reply=plot().ReadPlotApplet();                                                                                                                 
00089     return reply;                                                                                                                          
00090     }
00091 
00095     public String getAppletInfo() {
00096         return "PlotApplet " + PlotBox.PTPLOT_RELEASE + ": A data plotter.\n"
00097                 + "By: Edward A. Lee and\n " + "Christopher Hylands\n"
00098                 + "($Id: PlotApplet.java,v 1.76 2005/07/30 05:30:56 cxh Exp $)";
00099     }
00100 
00105     public String[][] getParameterInfo() {
00106         String[][] pinfo = {
00107                 { "background", "hexcolor value", "background color" },
00108                 { "foreground", "hexcolor value", "foreground color" },
00109                 { "dataurl", "url", "the URL of the data to plot" },
00110                 { "height", "integer", "100" },
00111               { "width", "integer", "100" }, 
00112               { "function", "String", "function1,function2,function3,function4 ..." }, 
00113               { "smooth", "integer", "0 or 1 : use with care. default=0" }, 
00114               { "names", "String", "names function1,name function2 ... or left blank " }, 
00115               { "xmin", "integer", "minimum x-value or left blank for auto detection" }, 
00116               { "xmax", "integer", "maximum x-value or left blank for auto detection" }, 
00117               { "ymin", "integer", "minimum y-value or left blank for auto detection" }, 
00118               { "ymax", "integer", "maximum y-value or left blank for auto detection" }, 
00119               { "xlog", "integer", "x-axis is log-scale: 0 or 1: default 0" }, 
00120               { "ylog", "integer", "y-axis is log-scale: 0 or 1: default 0" }, 
00121               { "color", "integer", " 0 or 1 :default 1" }, 
00122               { "xtics", "String", " zero 0, one 1, two 2, pi 3.1428, four 4, five 5 " }, 
00123               { "ytics", "String", " thing1 0, thing2 1, thing3 2, three 3, four 4, five 5 " }, 
00124               { "xlabel", "String", "label of the x-axis: default x-axis" }, 
00125               { "ylabel", "String", "label of the y-axis: default y-axis" }, 
00126               { "title", "String", "title of the plot" }, 
00127               };
00128         return pinfo;
00129     }
00130 
00142      // jm.evers                                                                                                                           
00143     public synchronized void read_from_param(String data){
00144        StringTokenizer f = new StringTokenizer(data, ";");
00145        int max=f.countTokens();String line="";
00146        for(int s=0;s<max;s++){
00147            line=f.nextToken();
00148             plot().read(line);
00149            //System.out.println("regel "+s+" = "+line);
00150         }
00151         showStatus("Done");
00152        repaint(); 
00153     }                                                                                     
00154 
00155     public synchronized void read_function_from_param(String function,String names,String totaal,int xmin,int xmax,int plotsteps,int smooth){
00156        String fun;String line="";String fff;
00157        Symbols symbols=new Symbols();
00158        double stap=(double) (xmax-xmin)/plotsteps;double org=stap;double y=0.0;double x=0.0;double dy=0.0;
00159        float diff;
00160        int s;int max;int tot=0;int n;String legenda="";
00161        if(names.length()!=0){ n=1; } else { n=0; }
00162        StringTokenizer ff = new StringTokenizer(function, ",");max=ff.countTokens();
00163        StringTokenizer nn = new StringTokenizer(names, ",");
00164        for(s=0;s<max;s++){
00165            if(n==0){ totaal=totaal+";dataset:"+s; } else {legenda=nn.nextToken(); totaal=totaal+";dataset:"+legenda; } 
00166            fff=ff.nextToken();
00167            System.out.println("functie "+s + "="+fff);
00168            for(x=xmin; x<xmax; x=x+stap){
00169               if(smooth==1){
00170                   diff=(float) Math.abs(y-dy);
00171                   if(diff>10*stap && stap>0.001){stap=0.5*stap;}else{stap=org;}
00172               }
00173               fun=fff.replaceAll("x","("+Double.toString(x)+")");
00174               try { y=symbols.eval(fun);} catch (SyntaxException e) {System.out.println("error "+e); }
00175               totaal=totaal+";"+x+","+y;
00176               tot++;
00177            }
00178        }
00179        System.out.println("totaal aantal punten is "+tot);
00180        StringTokenizer f = new StringTokenizer(totaal, ";");max=f.countTokens();
00181        for(s=0;s<max;s++){
00182            plot().read(f.nextToken());
00183        }
00184        showStatus("Done");
00185        repaint(); 
00186     }                                                                                     
00187 
00188 
00189     public void init() {
00190         super.init();
00191 
00192         // FIXME: having the mutex causes applets to hang.
00193         //synchronized (_mutex) {
00194         Runnable doActions = new Runnable() {
00195             public void run() {
00196                 if (_plot == null) {
00197                     _plot = newPlot();
00198                 }
00199 
00200                 getContentPane().add(plot(), BorderLayout.NORTH);
00201 
00202                 // Process the width and height applet parameters
00203                 int width;
00204 
00205                 // Process the width and height applet parameters
00206                 int height;
00207                 String widthspec = getParameter("width");
00208 
00209                 if (widthspec != null) {
00210                     width = Integer.parseInt(widthspec);
00211                 } else {
00212                     width = 400;
00213                 }
00214 
00215                 String heightspec = getParameter("height");
00216 
00217                 if (heightspec != null) {
00218                     height = Integer.parseInt(heightspec);
00219                 } else {
00220                     height = 400;
00221                 }
00222 
00223                 _setPlotSize(width, height);
00224                 plot().setButtons(true);
00225 
00226                 // Process the background parameter.
00227                 Color background = Color.white;
00228                 String colorspec = getParameter("background");
00229 
00230                 if (colorspec != null) {
00231                     background = PlotBox.getColorByName(colorspec);
00232                 }
00233 
00234                 setBackground(background);
00235                 plot().setBackground(background);
00236                 getContentPane().setBackground(background);
00237 
00238                 // Process the foreground parameter.
00239                 Color foreground = Color.black;
00240                 colorspec = getParameter("foreground");
00241 
00242                 if (colorspec != null) {
00243                     foreground = PlotBox.getColorByName(colorspec);
00244                 }
00245 
00246                 setForeground(foreground);
00247                 plot().setForeground(foreground);
00248                 plot().setVisible(true);
00249               
00250               //jm.evers
00251               int finput=0;String data;
00252               // reading the function param and all other params needed for plotting.
00253               data=getParameter("function"); 
00254               if(data!=null && data.length()!=0){
00255                   finput=1;int xmin;int xmax;int plotsteps=500;double ymin;double ymax;
00256                   String param;String xlabel;String ylabel;String grid;String title;String kleur="on";String xtics="";String ytics="";
00257                   String names="";int smooth=0;String xlog="off";String ylog="off";
00258                   String bargraph="";String binwidth="";String binoffset="";
00259                   
00260                   param=getParameter("bargraph");
00261                   if(param!=null && param.length()!=0){ bargraph=param; }
00262                   param=getParameter("binwidth");
00263                   if(param!=null && param.length()!=0){ binwidth=param; }
00264                   param=getParameter("binoffset");
00265                   if(param!=null && param.length()!=0){ binoffset=param; }
00266                   
00267                   
00268                   param=getParameter("xmax");
00269                   if(param!=null && param.length()!=0){ xmax=(int) Integer.parseInt(param, 10); } else { xmax=10; }
00270                   param=getParameter("xmin");
00271                   if(param!=null && param.length()!=0){ xmin=(int) Integer.parseInt(param, 10); } else { xmin=-1*xmax; }
00272 
00273                   param=getParameter("ymax");
00274                   if(param!=null && param.length()!=0){ 
00275                      ymax=(double) Integer.parseInt(param, 10);
00276                      param=getParameter("ymin");
00277                      ymin=(double) Integer.parseInt(param, 10);
00278                      plot().setYRange(ymin,ymax);
00279                   }
00280                   param=getParameter("ylog");
00281                   if(param!=null && param.length()!=0){ if(param.equals("1")){ylog="on";}}
00282                   param=getParameter("xlog");
00283                   if(param!=null && param.length()!=0){ if(param.equals("1")){xlog="on";}}
00284                   param=getParameter("smooth");
00285                   if(param!=null && param.length()!=0){ if(param.equals("1")){smooth=1;}}
00286                   param=getParameter("names");
00287                   if(param!=null && param.length()!=0){ names=param; } else { names=""; }
00288                   param=getParameter("xlabel");
00289                   if(param!=null && param.length()!=0){ xlabel=param; } else { xlabel="x-axis"; }
00290                   param=getParameter("ylabel");
00291                   if(param!=null && param.length()!=0){ ylabel=param; } else { ylabel="y-axis"; }
00292                   param=getParameter("grid");
00293                   if(param.equalsIgnoreCase("off")){ grid="off"; } else { grid="on"; }
00294                   param=getParameter("title");
00295                   if(param!=null && param.length()!=0){ title=param; } else { title="WIMS data plotter"; }
00296                   param=getParameter("color");
00297                   if(param.equalsIgnoreCase("off")){ kleur="off"; }
00298                   param=getParameter("xtics");
00299                   if(param!=null && param.length()!=0){ xtics=param; }
00300                   param=getParameter("ytics");
00301                   if(param!=null && param.length()!=0){ ytics=param; }
00302                   param=getParameter("plotsteps");
00303                   if(param!=null && param.length()!=0){ plotsteps=(int) Integer.parseInt(param, 10); }
00304 
00305                   String totaal="TitleText: "+title+";xlabel: "+xlabel+";ylabel: "+ylabel+";xlog: "+xlog+";ylog: "+ylog+";grid: "+grid+";color: "+kleur+";xticks:"+xtics+";yticks:"+ytics+";bargraph: "+bargraph+";binwidth: "+binwidth+";binoffset: "+binoffset;
00306                   read_function_from_param(data , names , totaal, xmin , xmax , plotsteps , smooth);
00307               }
00308               
00309               // reading a string of data in the "plt" format. typical: the string is provided by a wims variable
00310               data=getParameter("data"); 
00311               if(data!=null){
00312                   finput=1;
00313                   read_from_param(data);
00314               }
00315 
00316                 // Process the dataurl parameter.
00317               // the file is written by wims and stored in the session-dir/getfile/
00318                 String dataurlspec = getParameter("dataurl");
00319 
00320                 if (dataurlspec != null && finput==0) {
00321                     try {
00322                         showStatus("Reading data");
00323 
00324                         URL dataurl = new URL(getDocumentBase(), dataurlspec);
00325                         InputStream in = dataurl.openStream();
00326                         _read(in);
00327                         showStatus("Done");
00328                     } catch (MalformedURLException e) {
00329                         System.err.println(e.toString());
00330                     } catch (FileNotFoundException e) {
00331                         System.err.println("PlotApplet: file not found: " + e);
00332                     } catch (IOException e) {
00333                         System.err
00334                                 .println("PlotApplet: error reading input file: "
00335                                         + e);
00336                     }
00337                 }
00338             }
00339         };
00340 
00341         try {
00342             // NOTE: Using invokeAndWait() here risks causing
00343             // deadlock.  However, the Sun Tutorial recommends calling
00344             // invokeAndWait so that the work finishes before returning.
00345             // if we call invokeLater() then demo/PlotFourierSeries.java
00346             // has problems.
00347             SwingUtilities.invokeAndWait(doActions);
00348         } catch (Exception ex) {
00349             // Ignore InterruptedException.
00350             // Other exceptions should not occur.
00351         }
00352 
00353         //}
00354     }
00355 
00360     public PlotBox newPlot() {
00361         return new Plot();
00362     }
00363 
00367     public PlotBox plot() {
00368         return _plot;
00369     }
00370 
00374     public void setPlot(PlotBox plot) {
00375         // FIXME: this method is necessary for PxgraphApplet to work
00376         // properly.  We could modify newPlot() to set _plot, but
00377         // that would change the newPlot() contract, so we add another method.
00378         _plot = plot;
00379     }
00380 
00383 
00389     protected void _read(InputStream in) throws IOException {
00390         plot().read(in);
00391     }
00392 
00399     protected void _setPlotSize(int appletWidth, int appletHeight) {
00400         plot().setSize(appletWidth, appletHeight);
00401     }
00402 
00405     // Work around problem in Java 1.3.1_08 where if we create
00406     // 6 instances of a Plot applet then navigate forward then
00407     // back - IE and Navigator hang. (Roger Robins)
00408     // FIXME: having the mutex causes applets to hang.
00409     //private static Object _mutex = null;
00410     // The Plot component we are running.
00411     private transient PlotBox _plot;
00412 
00413 }