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.*;To compile this program with Javasoft's JDK, you would execute the following command:
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 change this chart from a LineChart to a BarChart or PieChart, you could simply change the following line:
//f.chart = new BarChart("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.
f.chart = new LineChart("Test Chart");
import java.applet.*;Compile this applet as you would any other Java class, and reference it within your HTML code like this:
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);
}
}
<applet code=simpleApp width=400 height=400>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.
</applet>