1 package prefuse.demos; 2 3 import java.awt.BorderLayout ; 4 import java.awt.event.ActionEvent ; 5 import java.awt.event.ActionListener ; 6 7 import javax.swing.BorderFactory ; 8 import javax.swing.Box ; 9 import javax.swing.BoxLayout ; 10 import javax.swing.JComboBox ; 11 import javax.swing.JFrame ; 12 import javax.swing.JLabel ; 13 import javax.swing.JToolBar ; 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 38 public class ScatterPlot extends Display { 39 40 private static final String group = "data"; 41 42 private ShapeRenderer m_shapeR = new ShapeRenderer(2); 43 44 public ScatterPlot(Table t, String xfield, String yfield) { 45 this(t, xfield, yfield, null); 46 } 47 48 public ScatterPlot(Table t, String xfield, String yfield, String sfield) { 49 super(new Visualization()); 50 51 54 m_vis.addTable(group, t); 55 56 DefaultRendererFactory rf = new DefaultRendererFactory(m_shapeR); 57 m_vis.setRendererFactory(rf); 58 59 62 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 90 setBorder(BorderFactory.createEmptyBorder(10,10,10,10)); 91 setSize(700,450); 92 setHighQuality(true); 93 94 ToolTipControl ttc = new ToolTipControl(new String [] {xfield,yfield}); 95 addControlListener(ttc); 96 97 98 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 116 public static void main(String [] argv) { 117 String data = "/fisher.iris.txt"; 118 String xfield = "SepalLength"; 119 String yfield = "PetalLength"; 120 String 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 toolbar = getEncodingToolbar(sp, xfield, yfield, sfield); 130 131 132 133 JFrame frame = new JFrame ("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 data, String xfield, String yfield) { 142 return demo(data, xfield, yfield, null); 143 } 144 145 public static ScatterPlot demo(String data, String xfield, 146 String yfield, String sfield) 147 { 148 Table table = null; 149 try { 150 table = new DelimitedTextTableReader().readTable(data); 151 } catch ( Exception 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 getEncodingToolbar(final ScatterPlot sp, 161 final String xfield, final String yfield, final String sfield) 162 { 163 int spacing = 10; 164 165 Table t = (Table)sp.getVisualization().getSourceData(group); 167 String [] colnames = new String [t.getColumnCount()]; 168 for ( int i=0; i<colnames.length; ++i ) 169 colnames[i] = t.getColumnName(i); 170 171 JToolBar toolbar = new JToolBar (); 173 toolbar.setLayout(new BoxLayout (toolbar, BoxLayout.X_AXIS)); 174 toolbar.add(Box.createHorizontalStrut(spacing)); 175 176 final JComboBox xcb = new JComboBox (colnames); 177 xcb.setSelectedItem(xfield); 178 xcb.addActionListener(new ActionListener () { 179 public void actionPerformed(ActionEvent e) { 180 Visualization vis = sp.getVisualization(); 181 AxisLayout xaxis = (AxisLayout)vis.getAction("x"); 182 xaxis.setDataField((String )xcb.getSelectedItem()); 183 vis.run("draw"); 184 } 185 }); 186 toolbar.add(new JLabel ("X: ")); 187 toolbar.add(xcb); 188 toolbar.add(Box.createHorizontalStrut(2*spacing)); 189 190 final JComboBox ycb = new JComboBox (colnames); 191 ycb.setSelectedItem(yfield); 192 ycb.addActionListener(new ActionListener () { 193 public void actionPerformed(ActionEvent e) { 194 Visualization vis = sp.getVisualization(); 195 AxisLayout yaxis = (AxisLayout)vis.getAction("y"); 196 yaxis.setDataField((String )ycb.getSelectedItem()); 197 vis.run("draw"); 198 } 199 }); 200 toolbar.add(new JLabel ("Y: ")); 201 toolbar.add(ycb); 202 toolbar.add(Box.createHorizontalStrut(2*spacing)); 203 204 final JComboBox scb = new JComboBox (colnames); 205 scb.setSelectedItem(sfield); 206 scb.addActionListener(new ActionListener () { 207 public void actionPerformed(ActionEvent e) { 208 Visualization vis = sp.getVisualization(); 209 DataShapeAction s = (DataShapeAction)vis.getAction("shape"); 210 s.setDataField((String )scb.getSelectedItem()); 211 vis.run("draw"); 212 } 213 }); 214 toolbar.add(new JLabel ("Shape: ")); 215 toolbar.add(scb); 216 toolbar.add(Box.createHorizontalStrut(spacing)); 217 toolbar.add(Box.createHorizontalGlue()); 218 219 return toolbar; 220 } 221 222 } | Popular Tags |