Using the Plot Widget in Your Own Applications

Release 2.0 Alpha 2 - Updated 14 January, 2004

Introduction

The jas.hist package contains the basic plot widget. This package is part of the Java Analysis Studio distribution, but is designed to be usable by itself in other applications. The package has the following features:

  1. Supports display of 1-D histograms, 2-D histograms, scatterplots
  2. Supports overlaying several histograms or scatterplots on one plot
  3. Supports interactive fitting of functions to 1-D histograms
  4. Supports numeric or time axes, plus axes with named bins
  5. Supports many display styles which can be set interactively or programmatically
  6. Supports display of slices and projections of 2-D data
  7. Supports direct user interaction, by clicking and dragging.
  8. Supports data which is constantly changing, including very efficient redrawing of data to support rapidly changing data.
  9. Supports printing using both Java 1 and Java 2 printing models.
  10. Supports saving  plots as GIF image and (in future) as an XML file.

The jas.hist is designed using the model-view-controller pattern, so that data to be displayed need only implement a simple java interface and need have no other dependence on the plot package. This makes interfacing arbitrary data to the plot widget very straightforward. This is discussed further below.

The current release of the jas.hist package supports both JDK 1.1 and JDK 1.2 and is based on Swing 1.1.1.

The jas.hist package is still a work in progress, and we plan to add more features in future releases. Every effort will be made to maintain backward compatibility with the current release.

Very Simple Example

The following code shows a very simple example of how to use the jas.hist package to display a simple 1D plot.

The example consists of a single class that creates a JASHist object (the basic plot object) and displays it within a JFrame (a swing top level window). The data source attached to the plot is a MemoryDataSource which creates a plot of Java memory usage (MemoryDataSource is one of the test data sources provided in the jas.hist.test package). After adding the data source to the plot the example goes on to customize the plot by adding a title, axis labels, and a border. The call to setAllowUserInteraction prevents the end user from interacting with (and changing) the plot.

To compile and run this example yourself you will need to have jas.jar (part of the Java Analysis Studio distribution) in your classpath, and if you are using JDK 1.1 you will also need swingall.jar (from the swing 1.1.1 fcs distribution).

More About Data Sources

The examples above all use a 1-dimensional data source. In fact there are several interfaces that can be passed to the JASHist.addData() method, resulting in a different type of plot. The table below summarizes the different types of DataSource.

Interface passed to JASHist.addData() Use
Rebinnable1DHistogramData 1 dimensional binned histograms
Rebinnable2DHistogramData 2 dimensional binned histograms
ScatterPlotSource Scatter Plot Data (unbinned)

Despite the name, the Rebinnable*HistogramData can be used for data which has fixed binning as well as for data which can be rebinned at user request.

The fact that the data source just needs to implement one of these fairly simple interfaces means that it is possible to display almost any data in the plot, either by extending the class that provides the data to  implement one of these interfaces or, more commonly, by writing a simple adaptor class which maps the existing data format to one of these interfaces.   

In addition to these basic interfaces there is a set of auxiliary interfaces which data sources can implement to gain extra functionality. These are summarized in the following table:

Interface Used With Use
HasStatistics All Indicates that the data source has a set of statistcis associated with it.
HasStyle All Indicates that the data source has an associated display style
HasScatterPlotData Rebinnable2DHistogramData Indicates that the 2D data source can provide scatter plot points (in addition to binned data)
HasSlices Rebinnable2DHistogramData Indicates that the 2D data has or can have slices/projections associated with it.

The following code shows a simple example of a custom data source, in this case the real source of the data is simply an array of binned data.


Overlaying Multiple Data Sets

To overlay multiple histograms on a plot it is only necessary to add multple DataSource's to the same JASHist.

Data Sources that Represent Changing Data

Many data sources represent data that is changing in real time, and the JASHist widget supports displaying such data sources and updating the display as the data changes. To indicate that it represents changing data, a data source should extend jas.util.Observable, in addition to implementing a suitable set of the interfaces described above. The plot will update itself whenever the data source indicates it has changed by calling its NotifyObservers method, passing a HistogramUpdate object as the argument. The HistogramUpdate object indicates what type of change has occured to the data represented by the data source.

The following example shows a simple  data source which is constantly updated by a separate thread of execution.

Threading Issues

In common with other Swing components, the jas.hist package is not designed to be thread safe. For a detailed discussion of threads and swing see the "Threads and Swing" article on the Swing Connection. The jas.plot package does however allow notifications of changes to the data source to be delivered asynchronously (via the NotifyObservers method), from any thread. This greatly simplifies the display of constantly updating data.

Data Sources for String and Date Axes

The JASHist component is able to display data sources with numeric or time/date information  on the x-axis, as well as with explicitly named bins. The following example illustrates each of these cases.

Note: In the case of the string axis the labels should appear on the plot in the order they appear in the array returned from getAxisLabels, the fact that they currently do not is a bug.

Printing

Included in the jas.plot package is a class called PrintHelper. This class takes care of some nitty gritty details of printing with Java, and in particular allows plots to be printed using either the new (JDK 1.2) printing mechanism, or the old (JDK 1.1) printing system. The mechanism to use is choosen automatically by the PrintHelper class based on which version of Java you are currently running. Although both mechanisms will work, the new JDK 1.2 printing system gives much better results.

The following example illustrates the use of the PrintHeler class:

Fitting

Documentation to be supplied.

2D Histograms

Scatter Plots

Slices and Projections

Documentation to be supplied.

Using jas.hist in an Applet

See the Applet How To.

Using jas.hist in a Servlet

See the Servlet How To.

Embedding JASHist in More Complex Applications

Documentation to be supplied.

Known Bugs

Wish List