KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > BarGraphExample


1 import org.faceless.graph2.*;
2 import java.awt.Color JavaDoc;
3 import java.awt.Paint JavaDoc;
4 import java.io.FileOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6
7 public class BarGraphExample
8 {
9     public static void main (String JavaDoc args[]) throws IOException JavaDoc
10     {
11         // Create 2 series
12
//
13
BarSeries series1 = new BarSeries("2001");
14         series1.setRoundBars(true);
15         series1.setBarWidth(0.8);
16     series1.set("apples",5);
17     series1.set("bananas",9);
18     series1.set("oranges",2);
19     series1.set("grapes",6);
20     series1.setBarLabels(new TextStyle("Helvetica", 10, Color.black, Align.BOTTOM), null);
21
22         BarSeries series2 = new BarSeries("2002");
23         series2.setBarWidth(0.8);
24         series2.setBarDepth(0.8);
25     series2.set("apples",9);
26     series2.set("bananas",10);
27     series2.set("oranges",5);
28     series2.set("grapes",3);
29         
30         // Create the graph and add the series
31
//
32
AxesGraph graph = new AxesGraph();
33     graph.addSeries(series1);
34     graph.addSeries(series2);
35
36         // Set some options, including coloring the walls of the graph with stripes
37
//
38
graph.getAxis(Axis.LEFT).setWallPaint(new Paint JavaDoc[]{new Color JavaDoc(238,238,238),Color.white}, new Color JavaDoc(204,204,204), null);
39         graph.getAxis(Axis.BOTTOM).setWallPaint(new Paint JavaDoc[]{new Color JavaDoc(238,238,238),Color.white}, new Color JavaDoc(204,204,204), null);
40         graph.setBackWallPaint(new Paint JavaDoc[]{new Color JavaDoc(238,238,238),Color.white}, new Color JavaDoc(204,204,204), Axis.LEFT, Axis.BOTTOM, null);
41         graph.setLightVector(1,0,-1);
42     graph.setXRotation(30);
43     graph.setYRotation(50);
44         graph.setAxis(Axis.ZAXIS, new ZAxis());
45
46         // Add a title
47
//
48
TextStyle style = new TextStyle("Default", 24, Color.black, Align.CENTER);
49     graph.addText("Fruit Consumption Graph", style);
50         
51         // Write the file
52
//
53
ImageOutput image = new ImageOutput(500, 500);
54     graph.draw(image);
55     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc("BarGraphExample.png");
56     image.writePNG(out, 0);
57         out.close();
58     }
59 }
60
Popular Tags