KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > LineBarExample


1 import org.faceless.graph2.*;
2 import java.awt.Color JavaDoc;
3 import java.io.FileOutputStream JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 public class LineBarExample
7 {
8     public static void main (String JavaDoc args[]) throws IOException JavaDoc
9     {
10         // Create a Bar Series
11
//
12
BarSeries barseries1 = new BarSeries("bars");
13     barseries1.set("apples", 10);
14     barseries1.set("bananas", 30);
15     barseries1.set("cranberries", 25);
16     barseries1.set("dates", 15);
17     barseries1.set("eggs", 10);
18     barseries1.set("figs", 50);
19     barseries1.set("guava", 6);
20
21         // Create a Line Series
22
//
23
LineSeries lineseries1 = new LineSeries("lines");
24     lineseries1.set(0,10);
25     lineseries1.set(1,32);
26     lineseries1.set(2,18);
27     lineseries1.set(3,24);
28     lineseries1.set(4,27);
29     lineseries1.set(5,32);
30     lineseries1.set(6,22);
31
32         // Set the lineseries to use dashed lines
33
//
34
Style style = new Style(Color.yellow);
35     style.setLineDash(new double[] { 5, 5 });
36     lineseries1.setStyle(style);
37
38         // Add the two series to the graph, and set a couple of options
39
//
40
AxesGraph graph = new AxesGraph();
41         graph.addSeries(lineseries1);
42         graph.addSeries(barseries1);
43         graph.setXRotation(30);
44         graph.setYRotation(20);
45
46         // Set some labels on the graph
47
//
48
graph.getAxis(Axis.LEFT).setLabel("Y Axis Label", new TextStyle("Default", 11, Color.black));
49     graph.getAxis(Axis.BOTTOM).setLabel("X Axis Label", new TextStyle("Default", 11, Color.black));
50
51         // Create a key using two different fonts, and set on the right
52
//
53
Key key = new Key(new Style(Color.ORANGE));
54     key.addSeries(lineseries1, new TextStyle("Courier", 12, Color.BLACK));
55     key.addSeries(barseries1, new TextStyle("Arial", 12, Color.BLUE));
56     graph.addKey(key, Align.RIGHT);
57
58         // And write the file out
59
//
60
ImageOutput image = new ImageOutput(500,500);
61         graph.draw(image);
62     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc("LineBarExample.png");
63     image.writePNG(out, 0);
64         out.close();
65     }
66 }
67
Popular Tags