001: package prefuse.demos;
002:
003: import java.awt.BorderLayout;
004: import java.awt.event.ActionEvent;
005: import java.awt.event.ActionListener;
006:
007: import javax.swing.BorderFactory;
008: import javax.swing.Box;
009: import javax.swing.BoxLayout;
010: import javax.swing.JComboBox;
011: import javax.swing.JFrame;
012: import javax.swing.JLabel;
013: import javax.swing.JToolBar;
014:
015: import prefuse.Constants;
016: import prefuse.Display;
017: import prefuse.Visualization;
018: import prefuse.action.ActionList;
019: import prefuse.action.RepaintAction;
020: import prefuse.action.assignment.ColorAction;
021: import prefuse.action.assignment.DataShapeAction;
022: import prefuse.action.layout.AxisLayout;
023: import prefuse.controls.ToolTipControl;
024: import prefuse.data.Table;
025: import prefuse.data.io.DelimitedTextTableReader;
026: import prefuse.render.DefaultRendererFactory;
027: import prefuse.render.ShapeRenderer;
028: import prefuse.util.ColorLib;
029: import prefuse.visual.VisualItem;
030: import prefuse.visual.expression.VisiblePredicate;
031:
032: /**
033: * A simple scatter plot visualization that allows visual encodings to
034: * be changed at runtime.
035: *
036: * @author <a href="http://jheer.org">jeffrey heer</a>
037: */
038: public class ScatterPlot extends Display {
039:
040: private static final String group = "data";
041:
042: private ShapeRenderer m_shapeR = new ShapeRenderer(2);
043:
044: public ScatterPlot(Table t, String xfield, String yfield) {
045: this (t, xfield, yfield, null);
046: }
047:
048: public ScatterPlot(Table t, String xfield, String yfield,
049: String sfield) {
050: super (new Visualization());
051:
052: // --------------------------------------------------------------------
053: // STEP 1: setup the visualized data
054:
055: m_vis.addTable(group, t);
056:
057: DefaultRendererFactory rf = new DefaultRendererFactory(m_shapeR);
058: m_vis.setRendererFactory(rf);
059:
060: // --------------------------------------------------------------------
061: // STEP 2: create actions to process the visual data
062:
063: // set up the actions
064: AxisLayout x_axis = new AxisLayout(group, xfield,
065: Constants.X_AXIS, VisiblePredicate.TRUE);
066: m_vis.putAction("x", x_axis);
067:
068: AxisLayout y_axis = new AxisLayout(group, yfield,
069: Constants.Y_AXIS, VisiblePredicate.TRUE);
070: m_vis.putAction("y", y_axis);
071:
072: ColorAction color = new ColorAction(group,
073: VisualItem.STROKECOLOR, ColorLib.rgb(100, 100, 255));
074: m_vis.putAction("color", color);
075:
076: DataShapeAction shape = new DataShapeAction(group, sfield);
077: m_vis.putAction("shape", shape);
078:
079: ActionList draw = new ActionList();
080: draw.add(x_axis);
081: draw.add(y_axis);
082: if (sfield != null)
083: draw.add(shape);
084: draw.add(color);
085: draw.add(new RepaintAction());
086: m_vis.putAction("draw", draw);
087:
088: // --------------------------------------------------------------------
089: // STEP 3: set up a display and ui components to show the visualization
090:
091: setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
092: setSize(700, 450);
093: setHighQuality(true);
094:
095: ToolTipControl ttc = new ToolTipControl(new String[] { xfield,
096: yfield });
097: addControlListener(ttc);
098:
099: // --------------------------------------------------------------------
100: // STEP 4: launching the visualization
101:
102: m_vis.run("draw");
103:
104: }
105:
106: public int getPointSize() {
107: return m_shapeR.getBaseSize();
108: }
109:
110: public void setPointSize(int size) {
111: m_shapeR.setBaseSize(size);
112: repaint();
113: }
114:
115: // ------------------------------------------------------------------------
116:
117: public static void main(String[] argv) {
118: String data = "/fisher.iris.txt";
119: String xfield = "SepalLength";
120: String yfield = "PetalLength";
121: String sfield = "Species";
122: if (argv.length >= 3) {
123: data = argv[0];
124: xfield = argv[1];
125: yfield = argv[2];
126: sfield = (argv.length > 3 ? argv[3] : null);
127: }
128:
129: final ScatterPlot sp = demo(data, xfield, yfield, sfield);
130: JToolBar toolbar = getEncodingToolbar(sp, xfield, yfield,
131: sfield);
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,
142: String yfield) {
143: return demo(data, xfield, yfield, null);
144: }
145:
146: public static ScatterPlot demo(String data, String xfield,
147: String yfield, String sfield) {
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,
156: sfield);
157: scatter.setPointSize(10);
158: return scatter;
159: }
160:
161: private static JToolBar getEncodingToolbar(final ScatterPlot sp,
162: final String xfield, final String yfield,
163: final String sfield) {
164: int spacing = 10;
165:
166: // create list of column names
167: Table t = (Table) sp.getVisualization().getSourceData(group);
168: String[] colnames = new String[t.getColumnCount()];
169: for (int i = 0; i < colnames.length; ++i)
170: colnames[i] = t.getColumnName(i);
171:
172: // create toolbar that allows visual mappings to be changed
173: JToolBar toolbar = new JToolBar();
174: toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS));
175: toolbar.add(Box.createHorizontalStrut(spacing));
176:
177: final JComboBox xcb = new JComboBox(colnames);
178: xcb.setSelectedItem(xfield);
179: xcb.addActionListener(new ActionListener() {
180: public void actionPerformed(ActionEvent e) {
181: Visualization vis = sp.getVisualization();
182: AxisLayout xaxis = (AxisLayout) vis.getAction("x");
183: xaxis.setDataField((String) xcb.getSelectedItem());
184: vis.run("draw");
185: }
186: });
187: toolbar.add(new JLabel("X: "));
188: toolbar.add(xcb);
189: toolbar.add(Box.createHorizontalStrut(2 * spacing));
190:
191: final JComboBox ycb = new JComboBox(colnames);
192: ycb.setSelectedItem(yfield);
193: ycb.addActionListener(new ActionListener() {
194: public void actionPerformed(ActionEvent e) {
195: Visualization vis = sp.getVisualization();
196: AxisLayout yaxis = (AxisLayout) vis.getAction("y");
197: yaxis.setDataField((String) ycb.getSelectedItem());
198: vis.run("draw");
199: }
200: });
201: toolbar.add(new JLabel("Y: "));
202: toolbar.add(ycb);
203: toolbar.add(Box.createHorizontalStrut(2 * spacing));
204:
205: final JComboBox scb = new JComboBox(colnames);
206: scb.setSelectedItem(sfield);
207: scb.addActionListener(new ActionListener() {
208: public void actionPerformed(ActionEvent e) {
209: Visualization vis = sp.getVisualization();
210: DataShapeAction s = (DataShapeAction) vis
211: .getAction("shape");
212: s.setDataField((String) scb.getSelectedItem());
213: vis.run("draw");
214: }
215: });
216: toolbar.add(new JLabel("Shape: "));
217: toolbar.add(scb);
218: toolbar.add(Box.createHorizontalStrut(spacing));
219: toolbar.add(Box.createHorizontalGlue());
220:
221: return toolbar;
222: }
223:
224: } // end of class ScatterPlot
|