在信息化的社会,人们每天涉猎大量信息,如何高效的处理信息挑战着人们的大脑。可视化技术能够从海量信息中提取数据,并将信息以交互式方式展现在人们面前,能够有效减轻人们对信息的处理过程。可视化技术用途广泛,涉及到的领域包括网络、交通实施(如地铁地图)等方面。下面介绍一种可视化工具Prefuse及其简单应用。
Prefuse是一个可扩展的软件框架,用于帮助软件开发人员创建交互式的信息可视化应用。Prefuse可以用来开发单独的应用,应用中的可视化构件和Web applets。Prefuse具有下列特征:
2. Prefuse信息可视化参考模型
3. 如何使用Prefuse构建应用
4. 构建一个简单的社会网络可视化
1 import javax.swing.JFrame; 2 import prefuse.Constants; 3 import prefuse.Display; 4 import prefuse.Visualization; 5 import prefuse.action.ActionList; 6 import prefuse.action.RepaintAction; 7 import prefuse.action.assignment.ColorAction; 8 import prefuse.action.assignment.DataColorAction; 9 import prefuse.action.layout.graph.ForceDirectedLayout; 10 import prefuse.activity.Activity; 11 import prefuse.controls.DragControl; 12 import prefuse.controls.PanControl; 13 import prefuse.controls.ZoomControl; 14 import prefuse.data.Graph; 15 import prefuse.data.io.DataIOException; 16 import prefuse.data.io.GraphMLReader; 17 import prefuse.render.DefaultRendererFactory; 18 import prefuse.render.LabelRenderer; 19 import prefuse.util.ColorLib; 20 import prefuse.visual.VisualItem; 21 22 public class Example { 23 24 public static void main(String[] argv) { 25 26 // -- 1. load the data ------------------------------------------------ 27 28 // load the socialnet.xml file. it is assumed that the file can be 29 // found at the root of the java classpath 30 Graph graph = null; 31 try { 32 // graph = new GraphMLReader().readGraph("/socialnet.xml"); 33 graph = new GraphMLReader().readGraph("C:/Documents and Settings/kimmy/桌面/Prefuse/data/socialnet.xml"); 34 35 } catch ( DataIOException e ) { 36 e.printStackTrace(); 37 System.err.println("Error loading graph. Exiting..."); 38 System.exit(1); 39 } 40 41 42 // -- 2. the visualization -------------------------------------------- 43 44 // add the graph to the visualization as the data group "graph" 45 // nodes and edges are accessible as "graph.nodes" and "graph.edges" 46 Visualization vis = new Visualization(); 47 vis.add("graph", graph); 48 vis.setInteractive("graph.edges", null, false); 49 50 // -- 3. the renderers and renderer factory --------------------------- 51 52 // draw the "name" label for NodeItems 53 LabelRenderer r = new LabelRenderer("name"); 54 r.setRoundedCorner(8, 8); // round the corners 55 56 // create a new default renderer factory 57 // return our name label renderer as the default for all non-EdgeItems 58 // includes straight line edges for EdgeItems by default 59 vis.setRendererFactory(new DefaultRendererFactory(r)); 60 61 62 // -- 4. the processing actions --------------------------------------- 63 64 // create our nominal color palette 65 // pink for females, baby blue for males 66 int[] palette = new int[] { 67 ColorLib.rgb(255,180,180), ColorLib.rgb(190,190,255) 68 }; 69 // map nominal data values to colors using our provided palette 70 DataColorAction fill = new DataColorAction("graph.nodes", "gender", 71 Constants.NOMINAL, VisualItem.FILLCOLOR, palette); 72 // use black for node text 73 ColorAction text = new ColorAction("graph.nodes", 74 VisualItem.TEXTCOLOR, ColorLib.gray(0)); 75 // use light grey for edges 76 ColorAction edges = new ColorAction("graph.edges", 77 VisualItem.STROKECOLOR, ColorLib.gray(200)); 78 79 // create an action list containing all color assignments 80 ActionList color = new ActionList(); 81 color.add(fill); 82 color.add(text); 83 color.add(edges); 84 85 // create an action list with an animated layout 86 ActionList layout = new ActionList(Activity.INFINITY); 87 layout.add(new ForceDirectedLayout("graph")); 88 layout.add(new RepaintAction()); 89 90 // add the actions to the visualization 91 vis.putAction("color", color); 92 vis.putAction("layout", layout); 93 94 95 // -- 5. the display and interactive controls ------------------------- 96 97 Display d = new Display(vis); 98 d.setSize(720, 500); // set display size 99 // drag individual items around 100 d.addControlListener(new DragControl()); 101 // pan with left-click drag on background 102 d.addControlListener(new PanControl()); 103 // zoom with right-click drag 104 d.addControlListener(new ZoomControl()); 105 106 // -- 6. launch the visualization ------------------------------------- 107 108 // create a new window to hold the visualization 109 JFrame frame = new JFrame("prefuse example"); 110 // ensure application exits when window is closed 111 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 112 frame.add(d); 113 frame.pack(); // layout components in window 114 frame.setVisible(true); // show the window 115 116 // assign the colors 117 vis.run("color"); 118 // start up the animated layout 119 vis.run("layout"); 120 } 121 122 }
5. 生成结果
References:
[1] http://prefuse.org/doc/manual/