KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PieGraphExample


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 PieGraphExample
8 {
9     public static void main (String JavaDoc args[]) throws IOException JavaDoc
10     {
11         // Set up slice colors
12
Color JavaDoc chocolateFactory = new Color JavaDoc(72,201,47);
13         Color JavaDoc weddingCrashers = new Color JavaDoc(251,163,49);
14         Color JavaDoc fantasticFour = new Color JavaDoc(250,123,213);
15         Color JavaDoc island = Color.CYAN;
16         Color JavaDoc badNewBears = Color.YELLOW;
17         Color JavaDoc warWorlds = new Color JavaDoc(188,129,226);
18         Color JavaDoc hustleFlow = new Color JavaDoc(252,47,110);
19         Color JavaDoc devilsRejects = new Color JavaDoc(250,206,73);
20         Color JavaDoc batmanBegins = new Color JavaDoc(124,124,255);
21         Color JavaDoc marchPenguins = Color.LIGHT_GRAY;
22         
23         // Create the pie graph and add the data
24
//
25
PieGraph graph = new PieGraph();
26         graph.set("Charlie and the Chocolate Factory", 28.3);
27         graph.set("Wedding Crashers", 26.2);
28         graph.set("Fantastic Four", 12.3);
29         graph.set("The Island", 12.1);
30         graph.set("The Bad News Bears", 11.5);
31         graph.set("War of the Worlds", 8.8);
32         graph.set("Hustle and Flow", 8.1);
33         graph.set("The Devil's Rejects", 7);
34         graph.set("Batman Begins", 4.7);
35         graph.set("March of the Penguins", 4.3);
36         
37         // Set labels on each slice of the pie
38
//
39
TextStyle labelstyle = new TextStyle("Default",10,Color.BLACK);
40         graph.setLabel ("Charlie and the Chocolate Factory", new Text("$28.5M", labelstyle), 0.9);
41         graph.setLabel ("Wedding Crashers", new Text("$26.2M", labelstyle), 0.9);
42         graph.setLabel ("Fantastic Four", new Text("$12.3M", labelstyle), 0.9);
43         graph.setLabel ("The Island", new Text("$12.1M", labelstyle), 0.9);
44         graph.setLabel ("The Bad News Bears", new Text("$11.5M", labelstyle), 0.9);
45         graph.setLabel ("War of the Worlds", new Text("$8.8M", labelstyle), 0.9);
46         graph.setLabel ("Hustle and Flow", new Text("$8.1M", labelstyle), 0.9);
47         graph.setLabel ("The Devil's Rejects", new Text("$7M", labelstyle), 0.9);
48         graph.setLabel ("Batman Begins", new Text("$4.7M", labelstyle), 0.9);
49         graph.setLabel ("March of the Penguins", new Text("$4.3M", labelstyle), 0.9);
50
51         // Set some other options
52
//
53
graph.setDefaultColors(new Paint JavaDoc[] {chocolateFactory,weddingCrashers,fantasticFour,island,badNewBears,warWorlds,hustleFlow,devilsRejects,batmanBegins,marchPenguins});
54         graph.setYRotation(-30);
55         graph.setZRotation(-10);
56         graph.setDepth(30);
57
58         // Add a title
59
//
60
graph.addText("Weekend Box Office Figures 22-24 July, 2005", new TextStyle("Default", 22, Color.red, Align.CENTER));
61         graph.addText("North American Theatres", new TextStyle("Default", 18, Color.black, Align.CENTER));
62         
63         // Add a key
64
//
65
Key key = new Key(new Style(Color.WHITE));
66         TextStyle ts = new TextStyle("Arial", 12, Color.BLACK);
67         Marker m1 = new Marker("square",12);
68         m1.setStyle (new Style(chocolateFactory));
69         key.addCustom(m1, "Charlie and the Chocolate Factory", ts);
70         Marker m2 = new Marker("square",12);
71         m2.setStyle (new Style(weddingCrashers));
72         key.addCustom(m2, "Wedding Crashers", ts);
73         Marker m3 = new Marker("square",12);
74         m3.setStyle (new Style(fantasticFour));
75         key.addCustom(m3, "Fantastic Four", ts);
76         Marker m4 = new Marker("square",12);
77         m4.setStyle (new Style(island));
78         key.addCustom(m4, "The Island", ts);
79         Marker m5 = new Marker("square",12);
80         m5.setStyle (new Style(badNewBears));
81         key.addCustom(m5, "The Bad News Bears", ts);
82         Marker m6 = new Marker("square",12);
83         m6.setStyle (new Style(warWorlds));
84         key.addCustom(m6, "War of the Worlds", ts);
85         Marker m7 = new Marker("square",12);
86         m7.setStyle (new Style(hustleFlow));
87         key.addCustom(m7, "Hustle and Flow", ts);
88         Marker m8 = new Marker("square",12);
89         m8.setStyle (new Style(devilsRejects));
90         key.addCustom(m8, "The Devil's Rejects", ts);
91         Marker m9 = new Marker("square",12);
92         m9.setStyle (new Style(batmanBegins));
93         key.addCustom(m9, "Batman Begins", ts);
94         Marker m10 = new Marker("square",12);
95         m10.setStyle (new Style(marchPenguins));
96         key.addCustom(m10, "March of the Penguins", ts);
97         key.setMaxWidth(500);
98         graph.addKey(key, Align.BOTTOM);
99         
100         // Write the file
101
//
102
ImageOutput image = new ImageOutput(500, 500);
103     graph.draw(image);
104     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc("PieGraphExample.png");
105     image.writePNG(out, 0);
106         out.close();
107     }
108 }
Popular Tags