KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > demos > ScatterPlot


1 package prefuse.demos;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.event.ActionEvent JavaDoc;
5 import java.awt.event.ActionListener JavaDoc;
6
7 import javax.swing.BorderFactory JavaDoc;
8 import javax.swing.Box JavaDoc;
9 import javax.swing.BoxLayout JavaDoc;
10 import javax.swing.JComboBox JavaDoc;
11 import javax.swing.JFrame JavaDoc;
12 import javax.swing.JLabel JavaDoc;
13 import javax.swing.JToolBar JavaDoc;
14
15 import prefuse.Constants;
16 import prefuse.Display;
17 import prefuse.Visualization;
18 import prefuse.action.ActionList;
19 import prefuse.action.RepaintAction;
20 import prefuse.action.assignment.ColorAction;
21 import prefuse.action.assignment.DataShapeAction;
22 import prefuse.action.layout.AxisLayout;
23 import prefuse.controls.ToolTipControl;
24 import prefuse.data.Table;
25 import prefuse.data.io.DelimitedTextTableReader;
26 import prefuse.render.DefaultRendererFactory;
27 import prefuse.render.ShapeRenderer;
28 import prefuse.util.ColorLib;
29 import prefuse.visual.VisualItem;
30 import prefuse.visual.expression.VisiblePredicate;
31
32 /**
33  * A simple scatter plot visualization that allows visual encodings to
34  * be changed at runtime.
35  *
36  * @author <a HREF="http://jheer.org">jeffrey heer</a>
37  */

38 public class ScatterPlot extends Display {
39
40     private static final String JavaDoc group = "data";
41     
42     private ShapeRenderer m_shapeR = new ShapeRenderer(2);
43     
44     public ScatterPlot(Table t, String JavaDoc xfield, String JavaDoc yfield) {
45         this(t, xfield, yfield, null);
46     }
47     
48     public ScatterPlot(Table t, String JavaDoc xfield, String JavaDoc yfield, String JavaDoc sfield) {
49         super(new Visualization());
50         
51         // --------------------------------------------------------------------
52
// STEP 1: setup the visualized data
53

54         m_vis.addTable(group, t);
55         
56         DefaultRendererFactory rf = new DefaultRendererFactory(m_shapeR);
57         m_vis.setRendererFactory(rf);
58         
59         // --------------------------------------------------------------------
60
// STEP 2: create actions to process the visual data
61

62         // set up the actions
63
AxisLayout x_axis = new AxisLayout(group, xfield,
64                 Constants.X_AXIS, VisiblePredicate.TRUE);
65         m_vis.putAction("x", x_axis);
66         
67         AxisLayout y_axis = new AxisLayout(group, yfield,
68                 Constants.Y_AXIS, VisiblePredicate.TRUE);
69         m_vis.putAction("y", y_axis);
70
71         ColorAction color = new ColorAction(group,
72                 VisualItem.STROKECOLOR, ColorLib.rgb(100,100,255));
73         m_vis.putAction("color", color);
74         
75         DataShapeAction shape = new DataShapeAction(group, sfield);
76         m_vis.putAction("shape", shape);
77         
78         ActionList draw = new ActionList();
79         draw.add(x_axis);
80         draw.add(y_axis);
81         if ( sfield != null )
82             draw.add(shape);
83         draw.add(color);
84         draw.add(new RepaintAction());
85         m_vis.putAction("draw", draw);
86         
87         // --------------------------------------------------------------------
88
// STEP 3: set up a display and ui components to show the visualization
89

90         setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
91         setSize(700,450);
92         setHighQuality(true);
93         
94         ToolTipControl ttc = new ToolTipControl(new String JavaDoc[] {xfield,yfield});
95         addControlListener(ttc);
96         
97         
98         // --------------------------------------------------------------------
99
// STEP 4: launching the visualization
100

101         m_vis.run("draw");
102
103     }
104     
105     public int getPointSize() {
106         return m_shapeR.getBaseSize();
107     }
108     
109     public void setPointSize(int size) {
110         m_shapeR.setBaseSize(size);
111         repaint();
112     }
113     
114     // ------------------------------------------------------------------------
115

116     public static void main(String JavaDoc[] argv) {
117         String JavaDoc data = "/fisher.iris.txt";
118         String JavaDoc xfield = "SepalLength";
119         String JavaDoc yfield = "PetalLength";
120         String JavaDoc sfield = "Species";
121         if ( argv.length >= 3 ) {
122             data = argv[0];
123             xfield = argv[1];
124             yfield = argv[2];
125             sfield = ( argv.length > 3 ? argv[3] : null );
126         }
127         
128         final ScatterPlot sp = demo(data, xfield, yfield, sfield);
129         JToolBar JavaDoc toolbar = getEncodingToolbar(sp, xfield, yfield, sfield);
130         
131         
132         
133         JFrame JavaDoc frame = new JFrame JavaDoc("p r e f u s e | s c a t t e r");
134         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
135         frame.getContentPane().add(toolbar, BorderLayout.NORTH);
136         frame.getContentPane().add(sp, BorderLayout.CENTER);
137         frame.pack();
138         frame.setVisible(true);
139     }
140     
141     public static ScatterPlot demo(String JavaDoc data, String JavaDoc xfield, String JavaDoc yfield) {
142         return demo(data, xfield, yfield, null);
143     }
144     
145     public static ScatterPlot demo(String JavaDoc data, String JavaDoc xfield,
146                                    String JavaDoc yfield, String JavaDoc sfield)
147     {
148         Table table = null;
149         try {
150             table = new DelimitedTextTableReader().readTable(data);
151         } catch ( Exception JavaDoc e ) {
152             e.printStackTrace();
153             return null;
154         }
155         ScatterPlot scatter = new ScatterPlot(table, xfield, yfield, sfield);
156         scatter.setPointSize(10);
157         return scatter;
158     }
159     
160     private static JToolBar JavaDoc getEncodingToolbar(final ScatterPlot sp,
161             final String JavaDoc xfield, final String JavaDoc yfield, final String JavaDoc sfield)
162     {
163         int spacing = 10;
164         
165         // create list of column names
166
Table t = (Table)sp.getVisualization().getSourceData(group);
167         String JavaDoc[] colnames = new String JavaDoc[t.getColumnCount()];
168         for ( int i=0; i<colnames.length; ++i )
169             colnames[i] = t.getColumnName(i);
170         
171         // create toolbar that allows visual mappings to be changed
172
JToolBar JavaDoc toolbar = new JToolBar JavaDoc();
173         toolbar.setLayout(new BoxLayout JavaDoc(toolbar, BoxLayout.X_AXIS));
174         toolbar.add(Box.createHorizontalStrut(spacing));
175         
176         final JComboBox JavaDoc xcb = new JComboBox JavaDoc(colnames);
177         xcb.setSelectedItem(xfield);
178         xcb.addActionListener(new ActionListener JavaDoc() {
179             public void actionPerformed(ActionEvent JavaDoc e) {
180                 Visualization vis = sp.getVisualization();
181                 AxisLayout xaxis = (AxisLayout)vis.getAction("x");
182                 xaxis.setDataField((String JavaDoc)xcb.getSelectedItem());
183                 vis.run("draw");
184             }
185         });
186         toolbar.add(new JLabel JavaDoc("X: "));
187         toolbar.add(xcb);
188         toolbar.add(Box.createHorizontalStrut(2*spacing));
189         
190         final JComboBox JavaDoc ycb = new JComboBox JavaDoc(colnames);
191         ycb.setSelectedItem(yfield);
192         ycb.addActionListener(new ActionListener JavaDoc() {
193             public void actionPerformed(ActionEvent JavaDoc e) {
194                 Visualization vis = sp.getVisualization();
195                 AxisLayout yaxis = (AxisLayout)vis.getAction("y");
196                 yaxis.setDataField((String JavaDoc)ycb.getSelectedItem());
197                 vis.run("draw");
198             }
199         });
200         toolbar.add(new JLabel JavaDoc("Y: "));
201         toolbar.add(ycb);
202         toolbar.add(Box.createHorizontalStrut(2*spacing));
203         
204         final JComboBox JavaDoc scb = new JComboBox JavaDoc(colnames);
205         scb.setSelectedItem(sfield);
206         scb.addActionListener(new ActionListener JavaDoc() {
207             public void actionPerformed(ActionEvent JavaDoc e) {
208                 Visualization vis = sp.getVisualization();
209                 DataShapeAction s = (DataShapeAction)vis.getAction("shape");
210                 s.setDataField((String JavaDoc)scb.getSelectedItem());
211                 vis.run("draw");
212             }
213         });
214         toolbar.add(new JLabel JavaDoc("Shape: "));
215         toolbar.add(scb);
216         toolbar.add(Box.createHorizontalStrut(spacing));
217         toolbar.add(Box.createHorizontalGlue());
218         
219         return toolbar;
220     }
221     
222 } // end of class ScatterPlot
223
Popular Tags