在 Prefuse 中创建数据

发布于 2024-12-01 23:50:00 字数 100 浏览 2 评论 0原文

我是 Prefuse 的新手。演示示例都是从文件/数据库加载的数据。有没有办法动态创建数据而不是从文件加载。例如,我想创建一个树数据结构并将其可视化。任何简单的工作示例都会对我很有帮助。

I am new to Prefuse. The demo examples are all loaded data from a file/database. Is there any way to create the data dynamically instead of loading from file. For example, I want to create a tree data structure and visualize this. Any simple working example would be really helpful for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏九 2024-12-08 23:50:00

我刚刚一直在解决这个问题。这是 Example.java 的调整版本,它展示了实现这一目标的一种方法。

我不是从 socialnet.xml 加载数据,而是以编程方式生成数据(基于 Aggregate.java 但没有聚合内容)并向每个 添加一个字段节点和每个Node 字段是一个简单的布尔标志,用于控制其渲染的颜色; Edge 字段是一个标签 - 但它还没有被渲染。我还在研究那个! :-)

特定于问题的代码位于 makeGraph() 中。基本方法是创建一个表(节点和边各一个),并向这些表中添加列来定义数据的形状;然后当您添加节点和边时,您可以添加相应的数据。一个问题是,对于边,您必须包含边的源/目标节点的列 - 有关更多详细信息,请参阅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
    }

}

I've just been attacking this problem. Here's a tweaked version of Example.java which shows one way to make it happen.

Instead of loading data from socialnet.xml, I'm generating it programatically (based on Aggregate.java but without the aggregation stuff) and adding one field to each Node and each Edge. The Node field is a simple boolean flag which controls the colour it's rendered; the Edge field is a label - but it's not being rendered yet. I'm still working on that one! :-)

The question-specific code is in makeGraph(). The basic approach is to create a Table (well, one each for nodes and edges), and add columns to those tables defining the shape of the data; then when you add your nodes and edges, you can add the corresponding data. One gotcha is that for edges you must include columns for the edge's source/target nodes - see the Graph and Table documentation for more details. There are other ways to do this (e.g. involving Schema) but I haven't sussed them out yet.

Hope this helps.

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
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文