Demographics

Summary

This demo illustrates how the JMSL Library can be used to easily create a Java™ application that extends the basic charting features. Historical demographic data are plotted in a horizontal double-sided bar chart. Fifty years of data and a seven year forecast are displayed in a JDialog when a particular age group and sex are specified and the forecast option is selected.

Usage

Use the slider to select which year's data to display. Click on a bar and then choose "Forecast" to generate a new chart with the forecasted population for that demographic. Click on the "Animate" button to have the slider move automatically, animating the chart.

JMSL Library Math/Stat Classes

com.imsl.stat.ARMA - Various ARMA methods are used to create a 7 year forecast (double[][] forecast = arma.forecast(numYears);) from the set of observations at a 50% confidence: arma.setConfidence(0.50);

JMSL Library Charting Classes

A variety of charting classes are used in this application. The main chart uses several methods from two bar objects (bar1.setLabels(xlabels, bar1.BAR_TYPE_HORIZONTAL); bar2.getBarSet(0,0).setFillColor(Color.magenta);) to generate the bar chart nodes in the chart object. A detailed discussion of the JMSL PickListener interface is provided in the Java code section in order to illustrate its substantial contribution to chart interactivity.

The x axis position in the chart is explicitly set to provide the two bar data sets in one chart (axis.getAxisX().setWindow(-12,12);). The multiplicative inverse of the male data is actually displayed and the axis labels are set to obscure this fact: axis.getAxisX().getAxisLabel().setLabels({"12", "8", "4", "0", "4", "8", "12"});

Java Code

This application is most instructive in its approach to JMSL chart interaction. The addPickListener() method, inherited by the bar objects from the ChartNode class, provides any easy way to "reduce" data by simple point and click interaction.

The first step is to add a PickListener to the bar data nodes and a MouseListener to the JPanelChart in the JFrameChart. The pick event is then triggered within the mouseClicked() method of the implemented MouseListener interface. This identifies the appropriate node of the bar chart.

bar1.addPickListener(this);
...
// Add the mouse listener
getPanel().addMouseListener(new java.awt.event.MouseAdapter() {
   public void mouseClicked(java.awt.event.MouseEvent event) {
      getChart().pick(event);
   }
});
...
The pickPerformed() method deselects any previous bar designation and activates the current chart node and data.
public void pickPerformed(PickEvent event) {
   // repaint old node before selecting new node
   if (currentBar > 0) {
      Color color = ((currentBar < 200) ? Color.blue : Color.magenta);
      node.setFillColor(color);
   }

   // set node to selected bar and color it green
   node = event.getNode();
   node.setFillColor(Color.green);

   // get code for current bar
   currentBar = node.getIntegerAttribute("barNumber", 0);
   repaint();
}

Links to Source Code

Population.java This is the main class for this demo. It extends JFrameChart with two JButtons and a JSlider with associated listeners. It also adds the JMSL PickListener to the bar chart node for significant chart interaction.
ForecastPopulation.java This class extends JDialog. The addition of a JPanelChart and data objects results in a typical chart with some interesting rendering effects. A result of two data fill attributes is the visual designation of a confidence interval.

Running This Demo

Two alternatives are available to run this demo:

1) Use the source code in your development environment as any other Java code. More information is available in the How To.

2) An executable jar file containing all of the demos referenced in this guide is included in the jmsl/lib directory. On Windows, you may double-click the file to run it if files with a ".jar" extension are properly registered with javaw.exe. Alternatively, for both Windows and UNIX environments, the jar file may be executed from the command line using java -jar gallery.jar.

As list of buttons, one for each demo, is created. Demos can be subsetted as they relate to specific areas (Math, Stat, Finance, Charting) by choosing the appropriate selection on the JComboBox. To run the Additional Demos, select Quick Start in the JComboBox.