[Index] [Introduction] [Installation] [Cookbook] [Package] [Component] [Chart] [Axis] [Utility] [Servlet] [Appndx A] [Appndx B] [API]

FRAMES  NO FRAMES   NO FRAMES INDEX


Applets

KavaChart includes a range of sample applets that implement a majority of the chart package's functionality in applet form. Along with basic charting functions, these applets also provide data reading utilities, URL data polling functions, and image management functions.

Overview

KavaChart applets are organized into the javachart.applet package. Individual applets provide the majority of KavaChart's chart package functionality in applet form for use on Internet or Intranet web pages. By convention, these applets are generally named javachart.applet.chartTypeApp.class. Unlike other charting packages, KavaChart applets typically implement a single chart type to reduce the applet "footprint" or download size. KavaChart applets can be well under 50k in size.

In addition to basic charting, the applet collection includes several "active" applets that implement scrolling, zooming, drill-down, and other frequently requested features.

KavaChart applets share a parameter parsing mechanism with KavaChart servlets to make it easy to translate your servlets to applets, or vice-versa.

Installation

Install the applets by placing the necessary "jar" files in an accessible location on your server. These jar files share names with the applets they contain: lineApp.jar contains javachart.applet.lineApp.class and all the class files necessary for lineApp to run. You can use the applet CODEBASE tag to point to the location of your jar files, or you can name it directly with your applet ARCHIVE tag. Here's an example of an applet definiton:
<applet code=javachart.applet.lineApp archive=http://myserver/jars/lineApp.jar
        width=500 height=300>
</applet>
Or, if you want to use the CODEBASE tag to point to your jar files:
<applet code=javachart.applet.lineApp archive=lineApp.jar codebase=../../jars
        width=500 height=300>
</applet>
Although you'll see a chart with this definition, it isn't very interesting. For starters, it contains internally generated data, and a default style. To improve the look of your chart, redefine some applet parameters. Applets definitions can include a lengthy list of parameters with this form:
<param name=parameterName value=parameterValue>
Each applet understands a set of general parameters that are shared by all applets and servlets, and a set of parameters that are specific to the chart type you're working with. For example, all applets and servlets let you change the backgroundImage beneath the chart, but only speedoApp lets you change the type of gauge from full to 1/4. In addition, there are a few parameters shared by all the applets, but not relevant to servlets, such as "networkInterval", which describes how frequently the applet should check for new data.

Applet Parameters

All applets share a common set of applet parameters for managing features that are useful to applets (and not useful for servlets). These include the following:
Parameter value type effect
networkInterval Integer Applets that use URL datasets will check for new data every networkInterval seconds.
dwellLabelsOn true/false Tells the applet whether to use dwell labels.
dwellUseLabelString true/false Tells the applet whether to use each datapoint's label as a part of the popup dwell labels.
dwellUseXLabel true/false Tells the applet whether to use each datapoint's X value as a part of the popup dwell labels.
dwellUseYLabel true/false Tells the applet whether to use each datapoint's Y value as a part of the popup dwell labels.
dwellXString String A text string containing the character "#" to add descriptive text to the dwell label X value. Example: "Category #"
dwellYString String A text string containing the character "#" to add descriptive text to the dwell label Y value. Example: "Unit Sales: $#"
dwellLabelPrecision Integer Number of digits of precision for dwell label values. For example, if precision is "2", labels will look like this: 123.45 or 123,45.

Shared Parameters

Other parameters are shared by all applets and servlets. Most applets use any of the parameters listed in KavaChart's ParameterParser document. If a parameter isn't relevant to a particular applet, it will be ignored.

Unique Applet Parameters

Each applet has its own set of parameters in addition to the shared parameter set. You can find links to individual applet parameters here:
  • Area Charts
  • Bar and Column Charts
  • Line Charts
  • Pie Charts
  • Speedo and Polar Charts
  • Combination Charts
  • Finance Charts 
  • Extending KavaChart Applets

    All KavaChart applets (except the purposely trivial simpleApp.java) are extensions of ChartAppShell. This superclass contains functionality common to all Chart classes. ChartAppShell implements a simple interface, GetParam, which requires implementations of simple parameter parsing and file I/O. It also uses a ParameterParser class to provide general functions for reading URLs, parsing parameter strings, etc. ChartAppShell also reads its own parameters to manage dwell labels and performs simple thread management to re-read URL data and ensure that necessary chart images are available before drawing.

    KavaChart applets are provided in source code form for easy extension. In general, applet definitions and servlet definitions are identical, except that applets extend javachart.applet.ChartAppShell, and servlets extend javachart.servlet.ServletShell. Both servlets and applets use ParameterParser or one of its subclasses to interpret incoming parameters and apply them to the chart.

    ChartAppShell extensions work by overriding the following methods:

    ChartAppShell extensions:
    Interactive Applets

    KavaChart's standard applet set also includes a number of  applets that manage user interactions.  These are subclasses of the applet classes listed above, but override event handling to add interactive behavior.


    Creating New KavaChart Applets

    You can easily build custom applets using either the KavaChart chart package or the KavaChart beans classes. By building your own applets, you may be able to shrink the footprint by eliminating parameter and data parsing you don't require for your situation. You can also put charts into a customized context with other information or add user interactivity as required. All charting applets install a chart, and override the applet paint method to draw a chart. Here's a quick sample:
    import java.applet.*;
    import java.awt.Graphics;
    import javachart.chart.*;
    public class simpleApp extends Applet {
    
    ChartInterface chart;
    
            public void init () { 
                    double yvals[] = new double[5];
                    for(int i=0;i<5;i++)
                            yvals[i] = Math.random();
                    chart = new BarChart();
                    chart.addDataset("my data", yvals);
            }
    
            public void paint(Graphics g) {
                    chart.resize(this.getSize().width, this.getSize().height);
                    chart.paint(this, g);
            }
    }
    Our simple applet installs a BarChart in init() and then draws the chart in paint. Other than that, we're just generating some random numbers to draw. You'll presumably want real data.

    Notice that in the chart paint method, we're passing the applet itself as an argument. This lets the chart take care of issues like rotating text as required, drawing flicker-free images, and so on.


    Since all our charts are subclasses of the abstract Chart superclass, we can make a quick modification to our applet to make it a bit more flexible:

    public class flexibleApp extends Applet {
    Chart chart;
            public void init(){
                    double yvals[] = new double[5];
                    for(int i=0;i<5;i++)
                            yvals[i] = Math.random();
                    try{
                    Class chartClass = Class.forName("javachart.chart." + getParameter("chartType"));
                    chart = (Chart)(chartClass.newInstance());
                    chart.addDataset("my data", yvals);
                    }
                    catch(Exception e){ /*  do nothing... */ };
            }
    
            public void paint(Graphics g) {
                    chart.resize(this.getSize().width, this.getSize().height);
                    chart.paint(this, g);
            }
    }
    Now we can load any sort of chart we want by passing a "chartType" parameter to our applet, like this:
    <applet code=flexibleApp width=500 height=300>
    <param name=chartType value=LineChart>
    </applet>


    Adding a Chart Bean to Your Applet

    Adding a chart bean to your applet is even easier. Since chart beans are graphical components themselves, you can just add them directly to your applet like this:

    public class beanApplet extends Applet {
            public void init() {
                    setLayout(new java.awt.BorderLayout());
                    add(new BarChart(), "Center");
            }
    }


    Adding a Data Feed to Your Applet

    Of course, you'd probably want a real data feed as well, but that's a simple matter of adding any of the existing beans in javachart.beans.data, or creating your own extension to SimpleDataFeed. For our purposes, we'll just add a DataFeedSimulator to see what this applet might look like:

    public class beanApplet extends Applet {
            public void init() {
                    setLayout(new java.awt.BorderLayout());
                    ChartBean chart = new BarChart();
                    SimpleDataFeed dataFeed = new DataFeedSimulator();
                    dataFeed.addDataFeedListener(chart);
                    dataFeed.update();
                    add(chart, "Center");
            }
    }
    The chart automatically paints, data automatically updates on a specified thread, and the entire applet took only a few lines of code to create. Similar to the example above, we could also select the chart type at runtime, since all the chart beans are subclasses of ChartBean. We could also use javachart.beans.chart.ChartWriter to create a serialized version of our chart, making all the modifications interactively through the customizer GUI. This serialized data could be used to re-create our chart instance. Our chart instance can then be attached to whatever data source we define in the applet. See the Cookbook Chapter for more information on reading serialized chart data.

    [Index] [Introduction] [Installation] [Cookbook] [Package] [Component] [Chart] [Axis] [Utility] [Servlet] [Appndx A] [Appndx B] [API]