2017-05-23 2 views
1

Ich möchte ein einfaches Diagramm mit Knoten-IDs in Knoten mit Prefuse anzeigen, aber das scheint komplizierter als es klingt.Anzeige Prefuse Knoten Feld

Graph g = new Graph(); 
for (int i = 0; i < 3; ++i) { 
    Node n1 = g.addNode(); 
    n1.setInt("label", 1); // I am trying to add a field in a node 
    Node n2 = g.addNode(); 
    Node n3 = g.addNode(); 
    g.addEdge(n1, n2); 
    g.addEdge(n1, n3); 
    g.addEdge(n2, n3); 
} 
g.addEdge(0, 3); 
g.addEdge(3, 6); 
g.addEdge(6, 0); 

// add visual data groups 
VisualGraph vg = m_vis.addGraph(GRAPH, g); 
m_vis.setInteractive(EDGES, null, false); 
m_vis.setValue(NODES, null, VisualItem.SHAPE, new Integer(Constants.SHAPE_STAR)); 

Allerdings scheint es, dass dieses Feld nicht existiert, macht es Sinn, da ich nicht dieses Feld hinzugefügt haben, aber es gibt keine Option ein Feld hinzufügen keines von beiden. Ich erhalte diese Ausnahme unter Bezugnahme auf die n1.setInt("DEFAULT_NODE_KEY", 1) Linie:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 
    at java.util.ArrayList.elementData(Unknown Source) 
    at java.util.ArrayList.get(Unknown Source) 
    at prefuse.data.Table.getColumn(Table.java:457) 
    at prefuse.data.Table.setInt(Table.java:1032) 
    at prefuse.data.tuple.TableTuple.setInt(TableTuple.java:215) 
    at prefuse.demos.AggregateDemo.initDataGroups(AggregateDemo.java:141) 
    at prefuse.demos.AggregateDemo.<init>(AggregateDemo.java:72) 
    at prefuse.demos.AggregateDemo.demo(AggregateDemo.java:182) 
    at prefuse.demos.AggregateDemo.main(AggregateDemo.java:176) 

Ich bin nicht sicher, wie Felder in Knoten zu verwenden. Ich habe versucht, die library's help zu lesen, aber ich schaffe es nicht, das herauszufinden.

Antwort

1

Sie können nach einem LabelRenderer suchen. Im example unten wird ein LabelRenderer so aufgebaut ist, wie Knoten zu machen, das Etikett mit GraphLib.LABEL, definiert als "label":

LabelRenderer r = new LabelRenderer(GraphLib.LABEL); 

Die Knoten, die den Graph gegeben Text über setString() mit dem gleichen Schlüssel, GraphLib.LABEL. Beispielsweise wird dem Stamm Node, der zu dem Graph hinzugefügt wird, der von GraphLib.getDiamondTree() zurückgegeben wird, der Schlüssel GraphLib.LABEL und der Wert "0,0" zugewiesen.

Node r = t.addRoot(); 
r.setString(LABEL, "0,0"); 

Später, als die Visualization läuft, die Renderer Text aus dem GraphLib.LABEL Feld zu verwenden versucht.

image

import java.awt.Dimension; 
import javax.swing.JFrame; 
import prefuse.Display; 
import prefuse.Visualization; 
import prefuse.action.ActionList; 
import prefuse.action.RepaintAction; 
import prefuse.action.assignment.ColorAction; 
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.Graph; 
import prefuse.render.DefaultRendererFactory; 
import prefuse.render.LabelRenderer; 
import prefuse.util.ColorLib; 
import prefuse.util.GraphLib; 
import prefuse.visual.VisualItem; 

/** @see https://stackoverflow.com/a/44274886/230513 */ 
public class Example { 

    private static final int W = 640; 
    private static final int H = 480; 

    public static void main(String[] argv) { 

     // -- 1. create the data ------------------------------------------------ 
     Graph graph = GraphLib.getDiamondTree(3, 2, 1); 

     // -- 2. the visualization -------------------------------------------- 
     // add the graph to the visualization as the data group "graph" 
     // nodes and edges are accessible as "graph.nodes" and "graph.edges" 
     Visualization vis = new Visualization(); 
     vis.add("graph", graph); 
     vis.setInteractive("graph.edges", null, false); 

     // -- 3. the renderers and renderer factory --------------------------- 
     LabelRenderer r = new LabelRenderer(GraphLib.LABEL); 
     r.setRoundedCorner(8, 8); // round the corners 

     // create a new default renderer factory 
     // return our name label renderer as the default for all non-EdgeItems 
     // includes straight line edges for EdgeItems by default 
     vis.setRendererFactory(new DefaultRendererFactory(r)); 

     // -- 4. the processing actions --------------------------------------- 
     ColorAction fill = new ColorAction("graph.nodes", 
      VisualItem.FILLCOLOR, ColorLib.rgb(200, 200, 255)); 
     // use black for node text 
     ColorAction text = new ColorAction("graph.nodes", 
      VisualItem.TEXTCOLOR, ColorLib.gray(0)); 
     // use light grey for edges 
     ColorAction edges = new ColorAction("graph.edges", 
      VisualItem.STROKECOLOR, ColorLib.gray(200)); 

     // create an action list containing all color assignments 
     ActionList color = new ActionList(); 
     color.add(fill); 
     color.add(text); 
     color.add(edges); 

     // create an action list with an animated layout 
     ActionList layout = new ActionList(Activity.INFINITY); 
     layout.add(new ForceDirectedLayout("graph")); 
     layout.add(new RepaintAction()); 

     // add the actions to the visualization 
     vis.putAction("color", color); 
     vis.putAction("layout", layout); 

     // -- 5. the display and interactive controls ------------------------- 
     Display d = new Display(vis) { 

      @Override 
      public Dimension getPreferredSize() { 
       return new Dimension(W, H); 
      } 
     }; 
     d.setSize(W, H); // set display size 
     d.pan(W/2, H/2); // pan to center 
     d.addControlListener(new DragControl()); 
     d.addControlListener(new PanControl()); 
     d.addControlListener(new ZoomControl()); 

     // -- 6. launch the visualization ------------------------------------- 
     JFrame frame = new JFrame("prefuse label example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(d); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); // show the window 

     vis.run("color"); 
     vis.run("layout"); 
    } 
} 
+0

Die Antwort ist ein bisschen spät, ich es selbst herausgefunden, aber immer noch, danke. –

Verwandte Themen