001: import java.awt.*;
002: import java.awt.event.*;
003: import javax.swing.*;
004: import JSci.awt.*;
005: import JSci.swing.*;
006:
007: /**
008: * Sample program demonstrating use of the Swing/AWT graph components.
009: * @author Mark Hale
010: * @version 1.1
011: */
012: public class GraphDemo extends Frame {
013: private DefaultCategoryGraph2DModel categoryModel;
014: private DefaultGraph2DModel valueModel;
015:
016: public static void main(String arg[]) {
017: new GraphDemo();
018: }
019:
020: public GraphDemo() {
021: super ("JSci Graph Demo");
022: addWindowListener(new WindowAdapter() {
023: public void windowClosing(WindowEvent evt) {
024: dispose();
025: System.exit(0);
026: }
027: });
028: setSize(700, 600);
029: final Font titleFont = new Font("Default", Font.BOLD, 14);
030: Label title;
031: // category graphs
032: categoryModel = createCategoryData();
033: // bar graph
034: JBarGraph barGraph = new JBarGraph(categoryModel);
035: final Panel barGraphPanel = new Panel(new JGraphLayout());
036: title = new Label("Bar graph", Label.CENTER);
037: title.setFont(titleFont);
038: barGraphPanel.add(title, JGraphLayout.TITLE);
039: barGraphPanel.add(barGraph, JGraphLayout.GRAPH);
040: barGraphPanel.add(new Label("y-axis", Label.RIGHT),
041: JGraphLayout.Y_AXIS);
042: barGraphPanel.add(new Label("Category axis", Label.CENTER),
043: JGraphLayout.X_AXIS);
044: // pie chart
045: JPieChart pieChart = new JPieChart(categoryModel);
046: final Panel pieChartPanel = new Panel(new GraphLayout());
047: title = new Label("Pie chart", Label.CENTER);
048: title.setFont(titleFont);
049: pieChartPanel.add(title, GraphLayout.TITLE);
050: pieChartPanel.add(pieChart, GraphLayout.GRAPH);
051: // value graphs
052: valueModel = createValueData();
053: // line graph
054: JLineGraph lineGraph = new JLineGraph(valueModel);
055: lineGraph.setGridLines(true);
056: lineGraph.setMarker(new Graph2D.DataMarker.Circle(5));
057: final Panel lineGraphPanel = new Panel(new JGraphLayout());
058: title = new Label("Line graph", Label.CENTER);
059: title.setFont(titleFont);
060: lineGraphPanel.add(title, JGraphLayout.TITLE);
061: lineGraphPanel.add(lineGraph, JGraphLayout.GRAPH);
062: Choice choice = new Choice();
063: choice.add("Temp.");
064: choice.add("Rain fall");
065: choice.addItemListener(new ItemListener() {
066: public void itemStateChanged(ItemEvent evt) {
067: if (evt.getStateChange() == ItemEvent.SELECTED) {
068: if (evt.getItem().toString().equals("Temp.")) {
069: valueModel.setSeriesVisible(0, true);
070: valueModel.setSeriesVisible(1, false);
071: } else if (evt.getItem().toString().equals(
072: "Rain fall")) {
073: valueModel.setSeriesVisible(0, false);
074: valueModel.setSeriesVisible(1, true);
075: }
076: }
077: }
078: });
079: lineGraphPanel.add(choice, JGraphLayout.Y_AXIS);
080: lineGraphPanel.add(new Label("x-axis", Label.CENTER),
081: JGraphLayout.X_AXIS);
082:
083: // data series tables
084: final Box tablePanel = new Box(BoxLayout.Y_AXIS);
085: tablePanel.add(new JLabel("Data series 0", JLabel.CENTER));
086: tablePanel.add(new JTable(valueModel.getSeries(0)));
087: tablePanel.add(new JLabel("Data series 1", JLabel.CENTER));
088: tablePanel.add(new JTable(valueModel.getSeries(1)));
089: // layout
090: GridBagLayout gb = new GridBagLayout();
091: GridBagConstraints gbc = new GridBagConstraints();
092: setLayout(gb);
093: gbc.weightx = 0.5;
094: gbc.weighty = 0.5;
095: gbc.fill = GridBagConstraints.BOTH;
096:
097: gbc.gridx = 0;
098: gbc.gridy = 0;
099: gb.setConstraints(barGraphPanel, gbc);
100: add(barGraphPanel);
101: gbc.gridx = 1;
102: gbc.gridy = 0;
103: gb.setConstraints(pieChartPanel, gbc);
104: add(pieChartPanel);
105:
106: gbc.fill = GridBagConstraints.HORIZONTAL;
107: gbc.gridx = 0;
108: gbc.gridy = 1;
109: gbc.gridwidth = GridBagConstraints.REMAINDER;
110: Button updateButton = new Button("Update");
111: updateButton.addActionListener(new ActionListener() {
112: public void actionPerformed(ActionEvent evt) {
113: float newData[] = { 3.4f, 5.6f, 6.2f, 3.9f, 1.8f };
114: categoryModel.changeSeries(0, newData);
115: }
116: });
117: gb.setConstraints(updateButton, gbc);
118: add(updateButton);
119:
120: gbc.fill = GridBagConstraints.BOTH;
121: gbc.gridx = 0;
122: gbc.gridy = 2;
123: gbc.gridwidth = 1;
124: gb.setConstraints(lineGraphPanel, gbc);
125: add(lineGraphPanel);
126: gbc.gridx = 1;
127: gbc.gridy = 2;
128: gbc.gridwidth = GridBagConstraints.REMAINDER;
129: gb.setConstraints(tablePanel, gbc);
130: add(tablePanel);
131: setVisible(true);
132: }
133:
134: private static DefaultCategoryGraph2DModel createCategoryData() {
135: String labels[] = { "Alpha1", "Beta2", "Gamma3", "Delta4",
136: "Epsilon5" };
137: float values1[] = { 2.4f, 7.3f, 3.2f, 0.5f, 2.2f };
138: float values2[] = { 0.9f, 3.4f, 2.1f, 6.5f, 8.2f };
139: DefaultCategoryGraph2DModel model = new DefaultCategoryGraph2DModel();
140: model.setCategories(labels);
141: model.addSeries(values1);
142: model.addSeries(values2);
143: return model;
144: }
145:
146: private static DefaultGraph2DModel createValueData() {
147: float values1[] = { 3.0f, 2.8f, 3.5f, 3.6f, 3.1f, 2.6f };
148: float values2[] = { 7.8f, 4.1f, 0.9f, 0.2f, 1.3f, 2.5f };
149: DefaultGraph2DModel model = new DefaultGraph2DModel();
150: model.setXAxis(0.0f, 5.0f, values1.length);
151: model.addSeries(values1);
152: model.addSeries(values2);
153: model.setSeriesVisible(1, false);
154: return model;
155: }
156: }
|