001/* 002 Copyright (c) 2011-2014 The Regents of the University of California. 003 All rights reserved. 004 Permission is hereby granted, without written agreement and without 005 license or royalty fees, to use, copy, modify, and distribute this 006 software and its documentation for any purpose, provided that the above 007 copyright notice and the following two paragraphs appear in all copies 008 of this software. 009 010 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 011 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 012 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 013 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 014 SUCH DAMAGE. 015 016 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 017 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 019 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 020 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 021 ENHANCEMENTS, OR MODIFICATIONS. 022 023 PT_COPYRIGHT_VERSION_2 024 COPYRIGHTENDKEY 025 */ 026 027package ptolemy.plot; 028 029import java.io.IOException; 030import java.io.InputStream; 031import java.io.OutputStream; 032import java.io.PrintWriter; 033import java.io.Writer; 034import java.net.URL; 035import java.util.Vector; 036 037/////////////////////////////////////////////////////////////////// 038//// PlotBoxInterface 039 040/** 041 * Definitions for an object that contains a plotter. 042 * @author ahuseyno 043 * @version $Id$ 044 * @since Ptolemy II 10.0 045 * @Pt.ProposedRating Red (cxh) 046 * @Pt.AcceptedRating Red (cxh) 047 */ 048public interface PlotBoxInterface { 049 050 /////////////////////////////////////////////////////////////////// 051 //// public methods //// 052 053 /** Add a line to the caption (displayed at below graph) . 054 * @param captionLine The string to be added. 055 * @see #getCaptions() 056 */ 057 public void addCaptionLine(String captionLine); 058 059 /** Add a legend (displayed at the upper right) for the specified 060 * data set with the specified string. Short strings generally 061 * fit better than long strings. If the string is empty, or the 062 * argument is null, then no legend is added. 063 * @param dataset The dataset index. 064 * @param legend The label for the dataset. 065 * @see #renameLegend(int, String) 066 */ 067 public void addLegend(int dataset, String legend); 068 069 /** Specify a tick mark for the X axis. The label given is placed 070 * on the axis at the position given by <i>position</i>. If this 071 * is called once or more, automatic generation of tick marks is 072 * disabled. The tick mark will appear only if it is within the X 073 * range. 074 * <p>Note that if {@link #setXLog(boolean)} has been called, then 075 * the position value should be in log units. 076 * So, addXTick("1K", 3) will display the string <pre>1K</pre> 077 * at the 1000 mark. 078 * @param label The label for the tick mark. 079 * @param position The position on the X axis. 080 */ 081 public void addXTick(String label, double position); 082 083 /** Specify a tick mark for the Y axis. The label given is placed 084 * on the axis at the position given by <i>position</i>. If this 085 * is called once or more, automatic generation of tick marks is 086 * disabled. The tick mark will appear only if it is within the Y 087 * range. 088 * <p>Note that if {@link #setYLog(boolean)} has been called, then 089 * the position value should be in log units. 090 * So, addYTick("1K", 3) will display the string <pre>1K</pre> 091 * at the 1000 mark. 092 * @param label The label for the tick mark. 093 * @param position The position on the Y axis. 094 */ 095 public void addYTick(String label, double position); 096 097 /** If the argument is true, clear the axes. I.e., set all parameters 098 * controlling the axes to their initial conditions. 099 * For the change to take effect, call repaint(). If the argument 100 * is false, do nothing. 101 * @param axes If true, clear the axes parameters. 102 */ 103 public void clear(boolean axes); 104 105 /** Clear all the captions. 106 * For the change to take effect, call repaint(). 107 * @see #setCaptions(Vector) 108 */ 109 public void clearCaptions(); 110 111 /** Clear all legends. This will show up on the next redraw. 112 */ 113 public void clearLegends(); 114 115 /** If this method is called in the event thread, then simply 116 * execute the specified action. Otherwise, 117 * if there are already deferred actions, then add the specified 118 * one to the list. Otherwise, create a list of deferred actions, 119 * if necessary, and request that the list be processed in the 120 * event dispatch thread. 121 * 122 * Note that it does not work nearly as well to simply schedule 123 * the action yourself on the event thread because if there are a 124 * large number of actions, then the event thread will not be able 125 * to keep up. By grouping these actions, we avoid this problem. 126 * 127 * This method is not , so the caller should be. 128 * @param action The Runnable object to execute. 129 */ 130 public void deferIfNecessary(Runnable action); 131 132 /** Destroy the plotter. This method is usually 133 * called by PlotApplet.destroy(). It does 134 * various cleanups to reduce memory usage. 135 */ 136 public void destroy(); 137 138 /** Rescale so that the data that is currently plotted just fits. 139 * This is done based on the protected variables _xBottom, _xTop, 140 * _yBottom, and _yTop. It is up to derived classes to ensure that 141 * variables are valid. 142 * This method calls repaint(), which eventually causes the display 143 * to be updated. 144 */ 145 public void fillPlot(); 146 147 /** Get the captions. 148 * @return the captions 149 * @see #addCaptionLine(String) 150 * @see #setCaptions(Vector) 151 */ 152 public Vector getCaptions(); 153 154 /** Return whether the plot uses color. 155 * @return True if the plot uses color. 156 * @see #setColor(boolean) 157 */ 158 public boolean getColor(); 159 160 /** Get the point colors. 161 * @return Array of colors 162 * @see #setColors(Object[]) 163 */ 164 public Object[] getColors(); 165 166 /** Get the file specification that was given by setDataurl(). 167 * @return the file specification 168 * @see #setDataurl(String) 169 * @deprecated Use read() instead. 170 */ 171 @Deprecated 172 public String getDataurl(); 173 174 /** Get the document base that was set by setDocumentBase(). 175 * @return the document base. 176 * @see #setDocumentBase(URL) 177 * @deprecated Use read() instead. 178 */ 179 @Deprecated 180 public URL getDocumentBase(); 181 182 /** Return whether the grid is drawn. 183 * @return True if a grid is drawn. 184 * @see #setGrid(boolean) 185 */ 186 public boolean getGrid(); 187 188 /** Get the legend for a dataset, or null if there is none. 189 * The legend would have been set by addLegend(). 190 * @param dataset The dataset index. 191 * @return The legend label, or null if there is none. 192 */ 193 public String getLegend(int dataset); 194 195 /** Given a legend string, return the corresponding dataset or -1 if no 196 * legend was added with that legend string 197 * The legend would have been set by addLegend(). 198 * @param legend The String naming the legend 199 * @return The legend dataset, or -1 if not found. 200 * @since Ptolemy II 10.0 201 */ 202 public int getLegendDataset(String legend); 203 204 /** If the size of the plot has been set by setSize(), 205 * then return that size. Otherwise, return what the superclass 206 * returns (which is undocumented, but apparently imposes no maximum size). 207 * Currently (JDK 1.3), only BoxLayout pays any attention to this. 208 * @return The maximum desired size. 209 */ 210 211 // public Dimension getMaximumSize() { 212 // if (_sizeHasBeenSet) { 213 // return new Dimension(_preferredWidth, _preferredHeight); 214 // } else { 215 // return super.getMaximumSize(); 216 // } 217 // } 218 /** Get the minimum size of this component. 219 * This is simply the dimensions specified by setSize(), 220 * if this has been called. Otherwise, return whatever the base 221 * class returns, which is undocumented. 222 * @return The minimum size. 223 */ 224 225 // public Dimension getMinimumSize() { 226 // if (_sizeHasBeenSet) { 227 // return new Dimension(_preferredWidth, _preferredHeight); 228 // } else { 229 // return super.getMinimumSize(); 230 // } 231 // } 232 233 /** Get the current plot rectangle. Note that in PlotBox, the 234 * Rectangle returned by this method is calculated from the 235 * values of _ulx _uly, _lrx and _lry. The value passed in by 236 * setPlotRectangle() is not directly used, thus calling 237 * getPlotRectangle() may not return the same rectangle that was 238 * passed in with setPlotRectangle(). 239 * @return Rectangle 240 * @see #setPlotRectangle(Object) 241 */ 242 public Object getPlotRectangle(); 243 244 /** Get the title of the graph, or an empty string if there is none. 245 * @return The title. 246 * @see #setTitle(String) 247 */ 248 public String getTitle(); 249 250 /** Get the range for X values of the data points registered so far. 251 * Usually, derived classes handle managing the range by checking 252 * each new point against the current range. 253 * @return An array of two doubles where the first element is the 254 * minimum and the second element is the maximum. 255 * @see #getXRange() 256 */ 257 public double[] getXAutoRange(); 258 259 /** Get the label for the X (horizontal) axis, or null if none has 260 * been set. 261 * @return The X label. 262 * @see #setXLabel(String) 263 */ 264 public String getXLabel(); 265 266 /** Return whether the X axis is drawn with a logarithmic scale. 267 * @return True if the X axis is logarithmic. 268 * @see #setXLog(boolean) 269 */ 270 public boolean getXLog(); 271 272 /** Get the X range. If {@link #setXRange(double, double)} has been 273 * called, then this method returns the values passed in as 274 * arguments to setXRange(double, double). If setXRange(double, 275 * double) has not been called, then this method returns the 276 * range of the data to be plotted, which might not be all of the 277 * data due to zooming. 278 * @return An array of two doubles where the first element is the 279 * minimum and the second element is the maximum. 280 * @see #getXAutoRange() 281 * @see #setXRange(double, double) 282 */ 283 public double[] getXRange(); 284 285 /** Get the X ticks that have been specified, or null if none. 286 * The return value is an array with two vectors, the first of 287 * which specifies the X tick locations (as instances of Double), 288 * and the second of which specifies the corresponding labels. 289 * @return The X ticks. 290 */ 291 public Vector[] getXTicks(); 292 293 /** Get the range for Y values of the data points registered so far. 294 * Usually, derived classes handle managing the range by checking 295 * each new point against the range. 296 * @return An array of two doubles where the first element is the 297 * minimum and the second element is the maximum. 298 * @see #getYRange() 299 */ 300 public double[] getYAutoRange(); 301 302 /** Get the label for the Y (vertical) axis, or null if none has 303 * been set. 304 * @return The Y label. 305 * @see #setYLabel(String) 306 */ 307 public String getYLabel(); 308 309 /** Return whether the Y axis is drawn with a logarithmic scale. 310 * @return True if the Y axis is logarithmic. 311 * @see #setYLog(boolean) 312 */ 313 public boolean getYLog(); 314 315 /** Get the Y range. If {@link #setYRange(double, double)} has been 316 * called, then this method returns the values passed in as 317 * arguments to setYRange(double, double). If setYRange(double, 318 * double) has not been called, then this method returns the 319 * range of the data to be plotted, which might not be all of the 320 * data due to zooming. 321 * @return An array of two doubles where the first element is the 322 * minimum and the second element is the maximum. 323 * @see #getYAutoRange() 324 * @see #setYRange(double, double) 325 */ 326 public double[] getYRange(); 327 328 /** Get the Y ticks that have been specified, or null if none. 329 * The return value is an array with two vectors, the first of 330 * which specifies the Y tick locations (as instances of Double), 331 * and the second of which specifies the corresponding labels. 332 * @return The Y ticks. 333 */ 334 public Vector[] getYTicks(); 335 336 /** Initialize the component, creating the fill button and parsing 337 * an input file, if one has been specified. This is deprecated. 338 * Call setButtons() and read() instead. 339 * @deprecated 340 */ 341 @Deprecated 342 public void init(); 343 344 /** Syntactic sugar for parseFile(filespec, documentBase). 345 * @param filespec The file to be read. 346 * @deprecated Use read() to read the old file 347 * format, or use one of the classes in the plotml package to 348 * read the XML-based file format. 349 */ 350 @Deprecated 351 public void parseFile(String filespec); 352 353 /** Open up the input file, which could be stdin, a URL, or a file. 354 * @param filespec The file to be read. 355 * @param documentBase The base of the URL 356 * @deprecated Use read() instead. 357 */ 358 @Deprecated 359 public void parseFile(String filespec, URL documentBase); 360 361 /** Read commands and/or plot data from an input stream in the old 362 * (non-XML) file syntax. 363 * To update the display, call repaint(), or make the plot visible with 364 * setVisible(true). 365 * <p> 366 * To read from standard input, use: 367 * <pre> 368 * read(System.in); 369 * </pre> 370 * To read from a url, use: 371 * <pre> 372 * read(url.openStream()); 373 * </pre> 374 * To read a URL from within an applet, use: 375 * <pre> 376 * URL url = new URL(getDocumentBase(), urlSpec); 377 * read(url.openStream()); 378 * </pre> 379 * Within an application, if you have an absolute URL, use: 380 * <pre> 381 * URL url = new URL(urlSpec); 382 * read(url.openStream()); 383 * </pre> 384 * To read from a file, use: 385 * <pre> 386 * read(new FileInputStream(filename)); 387 * </pre> 388 * @param in The input stream. 389 * @exception IOException If the stream cannot be read. 390 */ 391 public void read(InputStream in) throws IOException; 392 393 /** Read a single line command provided as a string. 394 * The commands can be any of those in the ASCII file format. 395 * @param command A command. 396 */ 397 public void read(String command); 398 399 /** Remove the legend (displayed at the upper right) for the specified 400 * data set. If the dataset is not found, nothing will occur. 401 * The PlotBox must be repainted in order for this to take effect. 402 * @param dataset The dataset index. 403 */ 404 public void removeLegend(int dataset); 405 406 /** Rename a legend. 407 * @param dataset The dataset of the legend to be renamed. 408 * If there is no dataset with this value, then nothing happens. 409 * @param newName The new name of legend. 410 * @see #addLegend(int, String) 411 */ 412 public void renameLegend(int dataset, String newName); 413 414 /** Repaint the object. */ 415 public void repaint(); 416 417 /** Reset the X and Y axes to the ranges that were first specified 418 * using setXRange() and setYRange(). If these methods have not been 419 * called, then reset to the default ranges. 420 * This method calls repaint(), which eventually causes the display 421 * to be updated. 422 */ 423 public void resetAxes(); 424 425 /** Do nothing in this base class. Derived classes might want to override 426 * this class to give an example of their use. 427 */ 428 public void samplePlot(); 429 430 /** 431 * Set automatic rescale. Automatic rescaling is enabled 432 * when automaticRescale equals true and disabled when 433 * automaticRescale equals false. 434 * @param automaticRescale The boolean that specifies whether 435 * plots should be automatic rescaled. 436 */ 437 public void setAutomaticRescale(boolean automaticRescale); 438 439 /** Set the background color. 440 * @param background The background color. 441 */ 442 public void setBackground(Object background); 443 444 /** If the argument is true, make a fill button visible at the upper 445 * right. This button auto-scales the plot. 446 * NOTE: The button may infringe on the title space, 447 * if the title is long. In an application, it is preferable to provide 448 * a menu with the fill command. This way, when printing the plot, 449 * the printed plot will not have a spurious button. Thus, this method 450 * should be used only by applets, which normally do not have menus. 451 * This method should only be called from within the event dispatch 452 * thread, since it interacts with swing. 453 * @param visible If true, make the fill button appear. 454 * @see #destroy() 455 */ 456 public void setButtons(boolean visible); 457 458 /** Set the strings of the caption. 459 * @param captionStrings A Vector where each element contains a String 460 * that is one line of the caption. 461 * @see #getCaptions() 462 * @see #clearCaptions() 463 */ 464 public void setCaptions(Vector captionStrings); 465 466 /** If the argument is false, draw the plot without using color 467 * (in black and white). Otherwise, draw it in color (the default). 468 * @param useColor False to draw in back and white. 469 * @see #getColor() 470 */ 471 public void setColor(boolean useColor); 472 473 /** Set the point colors. Note that the default colors have been 474 * carefully selected to maximize readability and that it is easy 475 * to use colors that result in a very ugly plot. 476 * @param colors Array of colors to use in succession for data sets. 477 * @see #getColors() 478 */ 479 public void setColors(Object[] colors); 480 481 /** Set the file to read when init() is called. 482 * @param filespec the file to be read 483 * @see #getDataurl() 484 * @deprecated Use read() instead. 485 */ 486 @Deprecated 487 public void setDataurl(String filespec); 488 489 /** Set the document base to used when init() is called to read a URL. 490 * @param documentBase The document base to be used. 491 * @see #getDocumentBase() 492 * @deprecated Use read() instead. 493 */ 494 @Deprecated 495 public void setDocumentBase(URL documentBase); 496 497 /** Set the foreground color. 498 * @param foreground The foreground color. 499 */ 500 public void setForeground(Object foreground); 501 502 /** Control whether the grid is drawn. 503 * @param grid If true, a grid is drawn. 504 * @see #getGrid() 505 */ 506 public void setGrid(boolean grid); 507 508 /** Set the label font, which is used for axis labels and legend labels. 509 * The font names understood are those understood by 510 * java.awt.Font.decode(). 511 * @param name A font name. 512 */ 513 public void setLabelFont(String name); 514 515 /** Set the plot rectangle inside the axes. This method 516 * can be used to create two plots that share the same axes. 517 * @param rectangle Rectangle space inside axes. 518 * @see #getPlotRectangle() 519 */ 520 public void setPlotRectangle(Object rectangle); 521 522 /** Set the size of the plot. This overrides the base class to make 523 * it work. In particular, it records the specified size so that 524 * getMinimumSize() and getPreferredSize() return the specified value. 525 * However, it only works if the plot is placed in its own JPanel. 526 * This is because the JPanel asks the contained component for 527 * its preferred size before determining the size of the panel. 528 * If the plot is placed directly in the content pane of a JApplet, 529 * then, mysteriously, this method has no effect. 530 * @param width The width, in pixels. 531 * @param height The height, in pixels. 532 */ 533 public void setSize(int width, int height); 534 535 /** 536 * Set repainting with a certain fixed refresh rate. This timed 537 * repainting is enabled when timedRepaint equals true and 538 * disabled when timedRepaint equals false. 539 * @param timedRepaint The boolean that specifies whether 540 * repainting should happen with a certain fixed refresh rate. 541 */ 542 public void setTimedRepaint(boolean timedRepaint); 543 544 /** Set the title of the graph. 545 * @param title The title. 546 * @see #getTitle() 547 */ 548 public void setTitle(String title); 549 550 /** Set the title font. 551 * The font names understood are those understood by 552 * java.awt.Font.decode(). 553 * @param name A font name. 554 */ 555 public void setTitleFont(String name); 556 557 /** Specify whether the X axis is wrapped. 558 * If it is, then X values that are out of range are remapped 559 * to be in range using modulo arithmetic. The X range is determined 560 * by the most recent call to setXRange() (or the most recent zoom). 561 * If the X range has not been set, then use the default X range, 562 * or if data has been plotted, then the current fill range. 563 * @param wrap If true, wrapping of the X axis is enabled. 564 */ 565 public void setWrap(boolean wrap); 566 567 /** Set the label for the X (horizontal) axis. 568 * @param label The label. 569 * @see #getXLabel() 570 */ 571 public void setXLabel(String label); 572 573 /** Specify whether the X axis is drawn with a logarithmic scale. 574 * If you would like to have the X axis drawn with a 575 * logarithmic axis, then setXLog(true) should be called before 576 * adding any data points. 577 * @param xlog If true, logarithmic axis is used. 578 * @see #getXLog() 579 */ 580 public void setXLog(boolean xlog); 581 582 /** Set the X (horizontal) range of the plot. If this is not done 583 * explicitly, then the range is computed automatically from data 584 * available when the plot is drawn. If min and max 585 * are identical, then the range is arbitrarily spread by 1. 586 * <p>Note that if {@link #setXLog(boolean)} has been called, then 587 * the min and max values should be in log units. 588 * @param min The left extent of the range. 589 * @param max The right extent of the range. 590 * @see #getXRange() 591 */ 592 public void setXRange(double min, double max); 593 594 /** Set the label for the Y (vertical) axis. 595 * @param label The label. 596 * @see #getYLabel() 597 */ 598 public void setYLabel(String label); 599 600 /** Specify whether the Y axis is drawn with a logarithmic scale. 601 * If you would like to have the Y axis drawn with a 602 * logarithmic axis, then setYLog(true) should be called before 603 * adding any data points. 604 * @param ylog If true, logarithmic axis is used. 605 * @see #getYLog() 606 */ 607 public void setYLog(boolean ylog); 608 609 /** Set the Y (vertical) range of the plot. If this is not done 610 * explicitly, then the range is computed automatically from data 611 * available when the plot is drawn. If min and max are identical, 612 * then the range is arbitrarily spread by 0.1. 613 * <p>Note that if {@link #setYLog(boolean)} has been called, then 614 * the min and max values should be in log units. 615 * @param min The bottom extent of the range. 616 * @param max The top extent of the range. 617 * @see #getYRange() 618 */ 619 public void setYRange(double min, double max); 620 621 /** Write the current data and plot configuration to the 622 * specified stream in PlotML syntax. PlotML is an XML 623 * extension for plot data. The written information is 624 * standalone, in that it includes the DTD (document type 625 * definition). This makes is somewhat verbose. To get 626 * smaller files, use the two argument version of write(). 627 * The output is buffered, and is flushed and 628 * closed before exiting. Derived classes should override 629 * writeFormat and writeData rather than this method. 630 * @param out An output stream. 631 */ 632 public void write(OutputStream out); 633 634 /** Write the current data and plot configuration to the 635 * specified stream in PlotML syntax. PlotML is an XML 636 * scheme for plot data. The URL (relative or absolute) for the DTD is 637 * given as the second argument. If that argument is null, 638 * then the PlotML PUBLIC DTD is referenced, resulting in a file 639 * that can be read by a PlotML parser without any external file 640 * references, as long as that parser has local access to the DTD. 641 * The output is buffered, and is flushed and 642 * closed before exiting. Derived classes should override 643 * writeFormat and writeData rather than this method. 644 * @param out An output stream. 645 * @param dtd The reference (URL) for the DTD, or null to use the 646 * PUBLIC DTD. 647 */ 648 public void write(OutputStream out, String dtd); 649 650 /** Write the current data and plot configuration to the 651 * specified stream in PlotML syntax. PlotML is an XML 652 * scheme for plot data. The URL (relative or absolute) for the DTD is 653 * given as the second argument. If that argument is null, 654 * then the PlotML PUBLIC DTD is referenced, resulting in a file 655 * that can be read by a PlotML parser without any external file 656 * references, as long as that parser has local access to the DTD. 657 * The output is buffered, and is flushed before exiting. 658 * @param out An output writer. 659 * @param dtd The reference (URL) for the DTD, or null to use the 660 * PUBLIC DTD. 661 */ 662 public void write(Writer out, String dtd); 663 664 /** Write plot data information to the specified output stream in PlotML. 665 * In this base class, there is no data to write, so this method 666 * returns without doing anything. 667 * @param output A buffered print writer. 668 */ 669 public void writeData(PrintWriter output); 670 671 /** Write plot format information to the specified output stream in PlotML. 672 * Derived classes should override this method to first call 673 * the parent class method, then add whatever additional format 674 * information they wish to add to the stream. 675 * @param output A buffered print writer. 676 */ 677 public void writeFormat(PrintWriter output); 678 679 /** Write the current data and plot configuration to the 680 * specified stream in the old PtPlot syntax. 681 * The output is buffered, and is flushed and 682 * closed before exiting. Derived classes should override 683 * _writeOldSyntax() rather than this method. 684 * @param out An output stream. 685 * @deprecated 686 */ 687 @Deprecated 688 public void writeOldSyntax(OutputStream out); 689 690 /** Zoom in or out to the specified rectangle. 691 * This method calls repaint(). 692 * @param lowx The low end of the new X range. 693 * @param lowy The low end of the new Y range. 694 * @param highx The high end of the new X range. 695 * @param highy The high end of the new Y range. 696 */ 697 public void zoom(double lowx, double lowy, double highx, double highy); 698}