2011-08-29 5 views
4

Prefuse를 처음 사용합니다. 데모 예제는 모두 파일/데이터베이스의로드 된 데이터입니다. 파일에서로드하는 대신 동적으로 데이터를 만드는 방법이 있습니까? 예를 들어, 트리 데이터 구조를 만들고이를 시각화하려고합니다. 모든 간단한 작업 예제는 나에게 정말 도움이 될 것입니다.Prefuse에서 데이터 작성

답변

6

방금이 문제를 공격했습니다. 여기에 Example.java의 비틀어 진 버전이 있습니다.

대신에 데이터를로드의 socialnet.xml에서

, 나는 ( Aggregate.java에 있지만 집계 물건없이 기준) 프로그래밍 방식을 생성하고 각 NodeEdge 하나의 필드를 추가하고 있습니다. Node 필드는 렌더링되는 색상을 제어하는 ​​간단한 부울 플래그입니다. Edge 필드는 레이블이지만 이 아니며이 아직 렌더링되지 않았습니다. 나는 아직도 그 일을하고있다! :-)

질문 별 코드는 makeGraph()입니다. 기본 접근법은 Table (노드와 에지에 각각 하나씩)을 만들고 데이터의 모양을 정의하는 테이블에 열을 추가하는 것입니다. 노드와 모서리를 추가하면 해당 데이터를 추가 할 수 있습니다. 하나의 문제는 가장자리의 경우 에 가장자리의 소스/대상 노드에 대한 열을 포함해야합니다. 자세한 내용은 GraphTable 문서를 참조하십시오. 이를 수행하는 다른 방법이 있습니다 (예 : Schema 포함). 그러나 나는 아직 그들을 슬프게하지 않았습니다.

희망이 도움이됩니다.

import javax.swing.JFrame; 

import prefuse.Constants; 
import prefuse.Display; 
import prefuse.Visualization; 
import prefuse.action.ActionList; 
import prefuse.action.RepaintAction; 
import prefuse.action.assignment.ColorAction; 
import prefuse.action.assignment.DataColorAction; 
import prefuse.action.layout.graph.ForceDirectedLayout; 
import prefuse.activity.Activity; 
import prefuse.controls.DragControl; 
import prefuse.controls.PanControl; 
import prefuse.controls.ZoomControl; 
import prefuse.data.Edge; 
import prefuse.data.Graph; 
import prefuse.data.Node; 
import prefuse.data.Table; 
import prefuse.render.DefaultRendererFactory; 
import prefuse.render.EdgeRenderer; 
import prefuse.render.Renderer; 
import prefuse.render.ShapeRenderer; 
import prefuse.util.ColorLib; 
import prefuse.visual.VisualItem; 

public class Example extends Display { 

    private Graph makeGraph() { 

     // Create tables for node and edge data, and configure their columns. 
     Table nodeData = new Table(); 
     Table edgeData = new Table(0,1); 
     nodeData.addColumn("flag", boolean.class); 
     edgeData.addColumn(Graph.DEFAULT_SOURCE_KEY, int.class); 
     edgeData.addColumn(Graph.DEFAULT_TARGET_KEY, int.class); 
     edgeData.addColumn("label", String.class); 
     // Need more data in your nodes or edges? Just add more 
     // columns. 

     // Create Graph backed by those tables. Note that I'm 
     // creating a directed graph here also. 
     Graph g = new Graph(nodeData, edgeData, true); 

     // Create some nodes and edges, each carrying some data. 
     // There are surely prettier ways to do this, but for the 
     // example it gets the job done. 
     for (int i=0; i<3; ++i) { 
      Node n1 = g.addNode(); 
      Node n2 = g.addNode(); 
      Node n3 = g.addNode(); 
      n1.setBoolean("flag", false); 
      n2.setBoolean("flag", true); 
      n3.setBoolean("flag", true); 
      Edge e1 = g.addEdge(n1, n2); 
      Edge e2 = g.addEdge(n1, n3); 
      Edge e3 = g.addEdge(n2, n3); 
      e1.setString("label", "a"); 
      e2.setString("label", "a"); 
      e3.setString("label", "a"); 
     } 
     Edge e4 = g.getEdge(g.addEdge(0, 3)); 
     Edge e5 = g.getEdge(g.addEdge(3, 6)); 
     Edge e6 = g.getEdge(g.addEdge(6, 0)); 
     e4.setString("label", "b"); 
     e5.setString("label", "b"); 
     e6.setString("label", "b"); 
     return g; 
    } 

    public Example() { 

     super(new Visualization()); 

     Graph graph = makeGraph(); 

     m_vis.addGraph("graph", graph); 
     m_vis.setInteractive("graph.edges", null, false); 
     m_vis.setValue("graph.nodes", null, VisualItem.SHAPE, 
       new Integer(Constants.SHAPE_ELLIPSE)); 

     Renderer nodeR = new ShapeRenderer(20); 
     EdgeRenderer edgeR = new EdgeRenderer(prefuse.Constants.EDGE_TYPE_CURVE, prefuse.Constants.EDGE_ARROW_FORWARD); 

     DefaultRendererFactory drf = new DefaultRendererFactory(); 
     drf.setDefaultRenderer(nodeR); 
     drf.setDefaultEdgeRenderer(edgeR); 
     m_vis.setRendererFactory(drf); 

     int[] palette = new int[] { 
      ColorLib.rgb(255,180,180), ColorLib.rgb(190,190,255) 
     }; 
     ColorAction nStroke = new ColorAction("graph.nodes", VisualItem.STROKECOLOR); 
     nStroke.setDefaultColor(ColorLib.gray(100)); 

     DataColorAction nFill = new DataColorAction("graph.nodes", "flag", 
      Constants.NOMINAL, VisualItem.FILLCOLOR, palette); 
     ColorAction edges = new ColorAction("graph.edges", 
      VisualItem.STROKECOLOR, ColorLib.gray(200)); 
     ColorAction arrow = new ColorAction("graph.edges", 
      VisualItem.FILLCOLOR, ColorLib.gray(200)); 
     ActionList color = new ActionList(); 
     color.add(nStroke); 
     color.add(nFill); 
     color.add(edges); 
     color.add(arrow); 

     ActionList layout = new ActionList(Activity.INFINITY); 
     layout.add(new ForceDirectedLayout("graph")); 
     layout.add(new RepaintAction()); 

     m_vis.putAction("color", color); 
     m_vis.putAction("layout", layout); 

     setSize(720, 500); // set display size 
     pan(360, 250); 
     setHighQuality(true); 
     addControlListener(new DragControl()); 
     addControlListener(new PanControl()); 
     addControlListener(new ZoomControl()); 

     m_vis.run("color"); 
     m_vis.run("layout"); 

    } 

    public static void main(String[] argv) { 
     Example ex = new Example(); 
     JFrame frame = new JFrame("prefuse example"); 
     frame.getContentPane().add(ex); 
     frame.pack();   // layout components in window 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setVisible(true); // show the window 
    } 

}