KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > GraphDemo


1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 import JSci.awt.*;
5 import JSci.swing.*;
6
7 /**
8 * Sample program demonstrating use of the Swing/AWT graph components.
9 * @author Mark Hale
10 * @version 1.1
11 */

12 public class GraphDemo extends Frame {
13         private DefaultCategoryGraph2DModel categoryModel;
14         private DefaultGraph2DModel valueModel;
15
16         public static void main(String JavaDoc arg[]) {
17                 new GraphDemo();
18         }
19         public GraphDemo() {
20                 super("JSci Graph Demo");
21                 addWindowListener(new WindowAdapter() {
22                         public void windowClosing(WindowEvent evt) {
23                                 dispose();
24                                 System.exit(0);
25                         }
26                 });
27                 setSize(700,600);
28                 final Font titleFont=new Font("Default",Font.BOLD,14);
29                 Label title;
30         // category graphs
31
categoryModel = createCategoryData();
32                 // bar graph
33
JBarGraph barGraph=new JBarGraph(categoryModel);
34                 final Panel barGraphPanel=new Panel(new JGraphLayout());
35                 title=new Label("Bar graph",Label.CENTER);
36                 title.setFont(titleFont);
37                 barGraphPanel.add(title, JGraphLayout.TITLE);
38                 barGraphPanel.add(barGraph, JGraphLayout.GRAPH);
39                 barGraphPanel.add(new Label("y-axis",Label.RIGHT), JGraphLayout.Y_AXIS);
40                 barGraphPanel.add(new Label("Category axis",Label.CENTER), JGraphLayout.X_AXIS);
41                 // pie chart
42
JPieChart pieChart=new JPieChart(categoryModel);
43                 final Panel pieChartPanel=new Panel(new GraphLayout());
44                 title=new Label("Pie chart",Label.CENTER);
45                 title.setFont(titleFont);
46                 pieChartPanel.add(title, GraphLayout.TITLE);
47                 pieChartPanel.add(pieChart, GraphLayout.GRAPH);
48         // value graphs
49
valueModel=createValueData();
50                 // line graph
51
JLineGraph lineGraph=new JLineGraph(valueModel);
52                 lineGraph.setGridLines(true);
53                 lineGraph.setMarker(new Graph2D.DataMarker.Circle(5));
54                 final Panel lineGraphPanel=new Panel(new JGraphLayout());
55                 title=new Label("Line graph",Label.CENTER);
56                 title.setFont(titleFont);
57                 lineGraphPanel.add(title, JGraphLayout.TITLE);
58                 lineGraphPanel.add(lineGraph, JGraphLayout.GRAPH);
59                 Choice choice=new Choice();
60                 choice.add("Temp.");
61                 choice.add("Rain fall");
62                 choice.addItemListener(new ItemListener() {
63                         public void itemStateChanged(ItemEvent evt) {
64                                 if(evt.getStateChange()==ItemEvent.SELECTED) {
65                                         if(evt.getItem().toString().equals("Temp.")) {
66                                                 valueModel.setSeriesVisible(0,true);
67                                                 valueModel.setSeriesVisible(1,false);
68                                         } else if(evt.getItem().toString().equals("Rain fall")) {
69                                                 valueModel.setSeriesVisible(0,false);
70                                                 valueModel.setSeriesVisible(1,true);
71                                         }
72                                 }
73                         }
74                 });
75                 lineGraphPanel.add(choice, JGraphLayout.Y_AXIS);
76                 lineGraphPanel.add(new Label("x-axis",Label.CENTER), JGraphLayout.X_AXIS);
77
78                 // data series tables
79
final Box tablePanel = new Box(BoxLayout.Y_AXIS);
80                 tablePanel.add(new JLabel("Data series 0", JLabel.CENTER));
81                 tablePanel.add(new JTable(valueModel.getSeries(0)));
82                 tablePanel.add(new JLabel("Data series 1", JLabel.CENTER));
83                 tablePanel.add(new JTable(valueModel.getSeries(1)));
84         // layout
85
GridBagLayout gb=new GridBagLayout();
86                 GridBagConstraints gbc=new GridBagConstraints();
87                 setLayout(gb);
88                 gbc.weightx=0.5;gbc.weighty=0.5;
89                 gbc.fill=GridBagConstraints.BOTH;
90
91                 gbc.gridx=0;gbc.gridy=0;
92                 gb.setConstraints(barGraphPanel, gbc);
93                 add(barGraphPanel);
94                 gbc.gridx=1;gbc.gridy=0;
95                 gb.setConstraints(pieChartPanel, gbc);
96                 add(pieChartPanel);
97
98                 gbc.fill=GridBagConstraints.HORIZONTAL;
99                 gbc.gridx=0;gbc.gridy=1;
100                 gbc.gridwidth=GridBagConstraints.REMAINDER;
101                 Button updateButton=new Button("Update");
102                 updateButton.addActionListener(new ActionListener() {
103                         public void actionPerformed(ActionEvent evt) {
104                                 float newData[]={3.4f,5.6f,6.2f,3.9f,1.8f};
105                                 categoryModel.changeSeries(0,newData);
106                         }
107                 });
108                 gb.setConstraints(updateButton, gbc);
109                 add(updateButton);
110
111                 gbc.fill=GridBagConstraints.BOTH;
112                 gbc.gridx=0;gbc.gridy=2;
113                 gbc.gridwidth=1;
114                 gb.setConstraints(lineGraphPanel, gbc);
115                 add(lineGraphPanel);
116                 gbc.gridx=1;gbc.gridy=2;
117                 gbc.gridwidth=GridBagConstraints.REMAINDER;
118                 gb.setConstraints(tablePanel, gbc);
119                 add(tablePanel);
120                 setVisible(true);
121         }
122         private static DefaultCategoryGraph2DModel createCategoryData() {
123                 String JavaDoc labels[]={"Alpha1","Beta2","Gamma3","Delta4","Epsilon5"};
124                 float values1[]={2.4f,7.3f,3.2f,0.5f,2.2f};
125                 float values2[]={0.9f,3.4f,2.1f,6.5f,8.2f};
126                 DefaultCategoryGraph2DModel model=new DefaultCategoryGraph2DModel();
127                 model.setCategories(labels);
128                 model.addSeries(values1);
129                 model.addSeries(values2);
130                 return model;
131         }
132         private static DefaultGraph2DModel createValueData() {
133                 float values1[]={3.0f,2.8f,3.5f,3.6f,3.1f,2.6f};
134                 float values2[]={7.8f,4.1f,0.9f,0.2f,1.3f,2.5f};
135                 DefaultGraph2DModel model=new DefaultGraph2DModel();
136                 model.setXAxis(0.0f,5.0f,values1.length);
137                 model.addSeries(values1);
138                 model.addSeries(values2);
139                 model.setSeriesVisible(1,false);
140                 return model;
141         }
142 }
143
144
Popular Tags