Appendix B - Creating and compiling a KavaChart program

Compiling a KavaChart Program

To compile a KavaChart program, you must have a standard Sun JDK or another Java compiler, such as those found in Eclipse, NetBeans, etc. Your Java compiler will locate the KavaChart classes if you include their location (or zip file) in your CLASSPATH environment variable.

A number of sample KavaChart programs are included with this release in the com.ve.kavachart.demos directory. The programs in this directory serve as a useful set of templates for adding KavaChart graphics to any Java program.

KavaChart programs, whether applets or applications, always draw to a java.awt.Graphics class. They generally draw to a subclass of Java's java.awt.Component class, and require a Component to draw rotated labels, such as those found on axes and bars.

Here is a simple KavaChart program, with comments that describe each step.

import com.ve.kavachart.chart.*;
import java.awt.*;
import java.util.Vector;

public class SimpleChart extends java.awt.Frame {

        public Chart                    chart;

        public static void main (String[] args) {

                SimpleChart             f = new SimpleChart();
                double x[] = new double[5];
                double y[] = new double[5];

                //fill an array with random numbers
                for (int i = 0; i < 5; i++) {
                x[i] = (double) i;
                y[i] = Math.random();
                }

                //create a new BarChart instance
                f.chart = new BarChart("Test Chart");

                //add the random numbers as a dataset with the name "Test"
                f.chart.addDataSet("Test", x, y);

                //Define some axis titles
                f.chart.getXAxis().setTitleString("X axis");
                f.chart.getYAxis().setTitleString("Y axis");

                f.resize(500, 300);
                //resize the chart to be the same size as this Frame
                f.chart.resize(500, 300);
                f.show();
        } 

        public void paint(Graphics g){
                //draw the graph as part of the paint process
                chart.drawGraph(g);
        }
}
To compile this program with Javasoft's JDK, you would execute the following command: To run the program with Javasoft's JDK, you would run the program as follows: Assuming you have KavaChart installed on your system and you have set your CLASSPATH variable correctly, these commands will create and execute a simple KavaChart program, creating a chart that looks like this:

To change this chart from a LineChart to a BarChart or PieChart, you could simply change the following line:

                //f.chart = new BarChart("Test Chart");
                f.chart = new LineChart("Test Chart");

Note: if you're using the SimpleChart example in KavaChart's "standalone" directory, you'll have to run "com.ve.kavachart.standalone.SimpleChart", because these files have been placed into a Java package.



Creating an applet

KavaChart's applet package has a wide range of applets that take advantage of most of KavaChart's functionality. You can easily create a very functional applet with your own capabilities by extending one of the applet source code files. If you'd prefer to create a simpler applet, you can start with a skeleton that looks like this:
import java.applet.*;
import java.awt.Graphics;
import com.ve.kavachart.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("My Chart");
                chart.resize(this.size().width, this.size().height);
                chart.addDataSet("my data", yvals);
        }

        public void paint(Graphics g) {
                chart.drawGraph(g);
        }
}
Compile this applet as you would any other Java class, and reference it within your HTML code like this:
        <applet code=simpleApp width=400 height=400>
        </applet>
You can also use the "CODEBASE" parameter to point to the directory that contains your applet and "com.ve.kavachart. directory. This can also point to a zip file containing KavaChart classes.

Additional Resources

You can stay current with KavaChart developments by visiting Visual Engineering's web site at www.ve.com. If you have purchased a software maintenance subscription, you will also receive periodic e-mail messages to alert you to new software releases available by password.