KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > demos > Congress


1 package prefuse.demos;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Dimension JavaDoc;
6 import java.awt.Insets JavaDoc;
7 import java.awt.event.ComponentAdapter JavaDoc;
8 import java.awt.event.ComponentEvent JavaDoc;
9 import java.awt.event.MouseAdapter JavaDoc;
10 import java.awt.event.MouseEvent JavaDoc;
11 import java.awt.geom.Rectangle2D JavaDoc;
12 import java.text.NumberFormat JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 import javax.swing.BorderFactory JavaDoc;
16 import javax.swing.Box JavaDoc;
17 import javax.swing.BoxLayout JavaDoc;
18 import javax.swing.JFrame JavaDoc;
19 import javax.swing.JPanel JavaDoc;
20 import javax.swing.SwingConstants JavaDoc;
21
22 import prefuse.Constants;
23 import prefuse.Display;
24 import prefuse.Visualization;
25 import prefuse.action.ActionList;
26 import prefuse.action.GroupAction;
27 import prefuse.action.RepaintAction;
28 import prefuse.action.assignment.ColorAction;
29 import prefuse.action.assignment.DataColorAction;
30 import prefuse.action.assignment.DataShapeAction;
31 import prefuse.action.filter.VisibilityFilter;
32 import prefuse.action.layout.AxisLabelLayout;
33 import prefuse.action.layout.AxisLayout;
34 import prefuse.controls.Control;
35 import prefuse.controls.ControlAdapter;
36 import prefuse.controls.ToolTipControl;
37 import prefuse.data.Table;
38 import prefuse.data.expression.AndPredicate;
39 import prefuse.data.expression.Predicate;
40 import prefuse.data.expression.parser.ExpressionParser;
41 import prefuse.data.io.DelimitedTextTableReader;
42 import prefuse.data.query.ListQueryBinding;
43 import prefuse.data.query.RangeQueryBinding;
44 import prefuse.data.query.SearchQueryBinding;
45 import prefuse.render.AxisRenderer;
46 import prefuse.render.Renderer;
47 import prefuse.render.RendererFactory;
48 import prefuse.render.ShapeRenderer;
49 import prefuse.render.AbstractShapeRenderer;
50 import prefuse.util.ColorLib;
51 import prefuse.util.FontLib;
52 import prefuse.util.UpdateListener;
53 import prefuse.util.ui.JFastLabel;
54 import prefuse.util.ui.JRangeSlider;
55 import prefuse.util.ui.JSearchPanel;
56 import prefuse.util.ui.UILib;
57 import prefuse.visual.VisualItem;
58 import prefuse.visual.VisualTable;
59 import prefuse.visual.expression.VisiblePredicate;
60 import prefuse.visual.sort.ItemSorter;
61
62 /**
63  * @author <a HREF="http://jheer.org">jeffrey heer</a>
64  */

65 public class Congress extends JPanel JavaDoc {
66
67     public static void main(String JavaDoc[] args) {
68         UILib.setPlatformLookAndFeel();
69         
70         JFrame JavaDoc f = demo();
71         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
72         f.setVisible(true);
73     }
74     
75     public static JFrame JavaDoc demo() {
76         // load the data
77
Table t = null;
78         try {
79             t = new DelimitedTextTableReader().readTable("/fec.txt");
80         } catch ( Exception JavaDoc e ) {
81             e.printStackTrace();
82             System.exit(1);
83         }
84         
85         JFrame JavaDoc frame = new JFrame JavaDoc((char)0x00a2+" o n g r e s s");
86         frame.setContentPane(new Congress(t));
87         frame.pack();
88         return frame;
89     }
90     
91     // ------------------------------------------------------------------------
92

93     private static final String JavaDoc TOTAL_RECEIPTS = "Total Receipts";
94     private static final String JavaDoc RECEIPTS = "Total Receipts";
95     
96     private String JavaDoc m_title = (char)0x00a2+"ongress";
97     private String JavaDoc m_totalStr;
98     private double m_totalMoney = 1000000000;
99     private int m_totalPeople = 10000;
100     private JFastLabel m_total = new JFastLabel(m_totalPeople+" Candidates: "+m_totalMoney);
101     private JFastLabel m_details;
102     
103     private Visualization m_vis;
104     private Display m_display;
105     private Rectangle2D JavaDoc m_dataB = new Rectangle2D.Double JavaDoc();
106     private Rectangle2D JavaDoc m_xlabB = new Rectangle2D.Double JavaDoc();
107     private Rectangle2D JavaDoc m_ylabB = new Rectangle2D.Double JavaDoc();
108     
109     public Congress(Table t) {
110         super(new BorderLayout JavaDoc());
111         
112         // --------------------------------------------------------------------
113
// STEP 1: setup the visualized data
114

115         final Visualization vis = new Visualization();
116         m_vis = vis;
117         
118         final String JavaDoc group = "by_state";
119
120         // filter to show only candidates receiving more than $100,000
121
Predicate p = (Predicate)
122             ExpressionParser.parse("["+TOTAL_RECEIPTS+"] >= 100000");
123         VisualTable vt = vis.addTable(group, t, p);
124         
125         // add a new column containing a label string showing
126
// candidate name, party, state, year, and total receipts
127
vt.addColumn("label", "CONCAT(CAP(Candidate), ' (', "
128                 + "CAP([Party Designation]), '-', [State Code], "
129                 + "') ', Year, ': $', FORMAT([Total Receipts],2))");
130
131         // add calculation for senators
132
vt.addColumn("Senate", "District <= 0");
133                 
134         vis.setRendererFactory(new RendererFactory() {
135             AbstractShapeRenderer sr = new ShapeRenderer();
136             Renderer arY = new AxisRenderer(Constants.RIGHT, Constants.TOP);
137             Renderer arX = new AxisRenderer(Constants.CENTER, Constants.FAR_BOTTOM);
138             
139             public Renderer getRenderer(VisualItem item) {
140                 return item.isInGroup("ylab") ? arY :
141                        item.isInGroup("xlab") ? arX : sr;
142             }
143         });
144         
145         // --------------------------------------------------------------------
146
// STEP 2: create actions to process the visual data
147

148         // set up dynamic queries, search set
149
RangeQueryBinding receiptsQ = new RangeQueryBinding(vt, RECEIPTS);
150         ListQueryBinding yearsQ = new ListQueryBinding(vt, "Year");
151         SearchQueryBinding searchQ = new SearchQueryBinding(vt, "Candidate");
152         
153         // construct the filtering predicate
154
AndPredicate filter = new AndPredicate(searchQ.getPredicate());
155         filter.add(yearsQ.getPredicate());
156         filter.add(receiptsQ.getPredicate());
157         
158         // set up the actions
159
AxisLayout xaxis = new AxisLayout(group, "State Code",
160                 Constants.X_AXIS, VisiblePredicate.TRUE);
161         AxisLayout yaxis = new AxisLayout(group, RECEIPTS,
162                 Constants.Y_AXIS, VisiblePredicate.TRUE);
163         //yaxis.setScale(Constants.LOG_SCALE);
164
yaxis.setRangeModel(receiptsQ.getModel());
165         receiptsQ.getNumberModel().setValueRange(0,65000000,0,65000000);
166         
167         xaxis.setLayoutBounds(m_dataB);
168         yaxis.setLayoutBounds(m_dataB);
169         
170         AxisLabelLayout ylabels = new AxisLabelLayout("ylab", yaxis, m_ylabB);
171         NumberFormat JavaDoc nf = NumberFormat.getCurrencyInstance();
172         nf.setMaximumFractionDigits(0);
173         ylabels.setNumberFormat(nf);
174         
175         AxisLabelLayout xlabels = new AxisLabelLayout("xlab", xaxis, m_xlabB, 15);
176         vis.putAction("xlabels", xlabels);
177         
178         // dems = blue, reps = red, other = gray
179
int[] palette = new int[] {
180             ColorLib.rgb(150,150,255), ColorLib.rgb(255,150,150),
181             ColorLib.rgb(180,180,180)
182         };
183         DataColorAction color = new DataColorAction(group, "Party",
184                 Constants.ORDINAL, VisualItem.STROKECOLOR, palette);
185         
186         int[] shapes = new int[]
187             { Constants.SHAPE_RECTANGLE, Constants.SHAPE_DIAMOND };
188         DataShapeAction shape = new DataShapeAction(group, "Senate", shapes);
189         
190         Counter cntr = new Counter(group);
191         
192         ActionList draw = new ActionList();
193         draw.add(cntr);
194         draw.add(color);
195         draw.add(shape);
196         draw.add(xaxis);
197         draw.add(yaxis);
198         draw.add(ylabels);
199         draw.add(new ColorAction(group, VisualItem.FILLCOLOR, 0));
200         draw.add(new RepaintAction());
201         vis.putAction("draw", draw);
202
203         ActionList update = new ActionList();
204         update.add(new VisibilityFilter(group, filter));
205         update.add(cntr);
206         update.add(xaxis);
207         update.add(yaxis);
208         update.add(ylabels);
209         update.add(new RepaintAction());
210         vis.putAction("update", update);
211         
212         UpdateListener lstnr = new UpdateListener() {
213             public void update(Object JavaDoc src) {
214                 vis.run("update");
215             }
216         };
217         filter.addExpressionListener(lstnr);
218         
219         // --------------------------------------------------------------------
220
// STEP 4: set up a display and ui components to show the visualization
221

222         m_display = new Display(vis);
223         m_display.setItemSorter(new ItemSorter() {
224             public int score(VisualItem item) {
225                 int score = super.score(item);
226                 if ( item.isInGroup(group) )
227                     score += item.getInt(TOTAL_RECEIPTS);
228                 return score;
229             }
230         });
231         m_display.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
232         m_display.setSize(700,450);
233         m_display.setHighQuality(true);
234         m_display.addComponentListener(new ComponentAdapter JavaDoc() {
235             public void componentResized(ComponentEvent JavaDoc e) {
236                 displayLayout();
237             }
238         });
239         displayLayout();
240         
241         m_details = new JFastLabel(m_title);
242         m_details.setPreferredSize(new Dimension JavaDoc(75,20));
243         m_details.setVerticalAlignment(SwingConstants.BOTTOM);
244         
245         m_total.setPreferredSize(new Dimension JavaDoc(500,20));
246         m_total.setHorizontalAlignment(SwingConstants.RIGHT);
247         m_total.setVerticalAlignment(SwingConstants.BOTTOM);
248         
249         ToolTipControl ttc = new ToolTipControl("label");
250         Control hoverc = new ControlAdapter() {
251             public void itemEntered(VisualItem item, MouseEvent JavaDoc evt) {
252                 if ( item.isInGroup(group) ) {
253                   m_total.setText(item.getString("label"));
254                   item.setFillColor(item.getStrokeColor());
255                   item.setStrokeColor(ColorLib.rgb(0,0,0));
256                   item.getVisualization().repaint();
257                 }
258             }
259             public void itemExited(VisualItem item, MouseEvent JavaDoc evt) {
260                 if ( item.isInGroup(group) ) {
261                   m_total.setText(m_totalStr);
262                   item.setFillColor(item.getEndFillColor());
263                   item.setStrokeColor(item.getEndStrokeColor());
264                   item.getVisualization().repaint();
265                 }
266             }
267         };
268         m_display.addControlListener(ttc);
269         m_display.addControlListener(hoverc);
270         
271         
272         // --------------------------------------------------------------------
273
// STEP 5: launching the visualization
274

275         this.addComponentListener(lstnr);
276         
277         // details
278
Box JavaDoc infoBox = new Box JavaDoc(BoxLayout.X_AXIS);
279         infoBox.add(Box.createHorizontalStrut(5));
280         infoBox.add(m_details);
281         infoBox.add(Box.createHorizontalGlue());
282         infoBox.add(Box.createHorizontalStrut(5));
283         infoBox.add(m_total);
284         infoBox.add(Box.createHorizontalStrut(5));
285         
286         // set up search box
287
JSearchPanel searcher = searchQ.createSearchPanel();
288         searcher.setLabelText("Candidate: ");
289         searcher.setBorder(BorderFactory.createEmptyBorder(5,5,5,0));
290         
291         // create dynamic queries
292
Box JavaDoc radioBox = new Box JavaDoc(BoxLayout.X_AXIS);
293         radioBox.add(Box.createHorizontalStrut(5));
294         radioBox.add(searcher);
295         radioBox.add(Box.createHorizontalGlue());
296         radioBox.add(Box.createHorizontalStrut(5));
297         radioBox.add(yearsQ.createRadioGroup());
298         radioBox.add(Box.createHorizontalStrut(16));
299         
300         JRangeSlider slider = receiptsQ.createVerticalRangeSlider();
301         slider.setThumbColor(null);
302         slider.setMinExtent(150000);
303         slider.addMouseListener(new MouseAdapter JavaDoc() {
304             public void mousePressed(MouseEvent JavaDoc e) {
305                 m_display.setHighQuality(false);
306             }
307             public void mouseReleased(MouseEvent JavaDoc e) {
308                 m_display.setHighQuality(true);
309                 m_display.repaint();
310             }
311         });
312         
313         vis.run("draw");
314         vis.run("xlabels");
315         
316         add(infoBox, BorderLayout.NORTH);
317         add(m_display, BorderLayout.CENTER);
318         add(slider, BorderLayout.EAST);
319         add(radioBox, BorderLayout.SOUTH);
320         UILib.setColor(this, ColorLib.getColor(255,255,255), Color.GRAY);
321         slider.setForeground(Color.LIGHT_GRAY);
322         UILib.setFont(radioBox, FontLib.getFont("Tahoma", 15));
323         m_details.setFont(FontLib.getFont("Tahoma", 18));
324         m_total.setFont(FontLib.getFont("Tahoma", 16));
325     }
326     
327     public void displayLayout() {
328         Insets JavaDoc i = m_display.getInsets();
329         int w = m_display.getWidth();
330         int h = m_display.getHeight();
331         int iw = i.left+i.right;
332         int ih = i.top+i.bottom;
333         int aw = 85;
334         int ah = 15;
335         
336         m_dataB.setRect(i.left, i.top, w-iw-aw, h-ih-ah);
337         m_xlabB.setRect(i.left, h-ah-i.bottom, w-iw-aw, ah-10);
338         m_ylabB.setRect(i.left, i.top, w-iw, h-ih-ah);
339         
340         m_vis.run("update");
341         m_vis.run("xlabels");
342     }
343     
344     private class Counter extends GroupAction {
345         public Counter(String JavaDoc group) {
346             super(group);
347         }
348         public void run(double frac) {
349             double total = 0;
350             int count = 0;
351             VisualItem item = null;
352             Iterator JavaDoc items = m_vis.visibleItems(m_group);
353             while ( items.hasNext() ) {
354                 item = (VisualItem)items.next();
355                 total += item.getDouble("Total Receipts");
356                 ++count;
357             }
358             m_totalMoney = total;
359             m_totalPeople = count;
360             
361             if ( count == 1 ) {
362                 m_totalStr = item.getString("label");
363             } else {
364                 m_totalStr = count + " Candidates receiving " +
365                     NumberFormat.getCurrencyInstance().format(total);
366             }
367             m_total.setText(m_totalStr);
368         }
369     }
370     
371 } // end of class ScatterPlot
372
Popular Tags