prefuse学习(二)显示一张图

 

1.  把数据以点连线的方式在画面中显示

2.  数据按照数据的性别属性使用不同的颜色

3.  鼠标左键可以把图在画面中拖动

4.  鼠标右键可以把图放大或者缩小

5.  鼠标单击某个数据上,该数据点中心化显示(可以点击下试试就知道中心化显示)

6.  鼠标可以选中某个数据点进行任意位置的拖动,而在该点与其它点的关系保持不变

7.  在右下框输入a,则图中数据点中包含a的数据加亮显示

先把代码附上,注释中有讲解

 

package wjl;



import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.event.MouseEvent;

import java.util.Iterator;



import javax.swing.BorderFactory;

import javax.swing.Box;

import javax.swing.BoxLayout;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.SwingConstants;



import prefuse.Constants;

import prefuse.Display;

import prefuse.Visualization;

import prefuse.action.ActionList;

import prefuse.action.GroupAction;

import prefuse.action.ItemAction;

import prefuse.action.RepaintAction;

import prefuse.action.animate.ColorAnimator;

import prefuse.action.animate.PolarLocationAnimator;

import prefuse.action.animate.QualityControlAnimator;

import prefuse.action.animate.VisibilityAnimator;

import prefuse.action.assignment.ColorAction;

import prefuse.action.assignment.DataColorAction;

import prefuse.action.assignment.FontAction;

import prefuse.action.layout.CollapsedSubtreeLayout;

import prefuse.action.layout.graph.RadialTreeLayout;

import prefuse.activity.SlowInSlowOutPacer;

import prefuse.controls.ControlAdapter;

import prefuse.controls.DragControl;

import prefuse.controls.FocusControl;

import prefuse.controls.HoverActionControl;

import prefuse.controls.PanControl;

import prefuse.controls.ZoomControl;

import prefuse.controls.ZoomToFitControl;

import prefuse.data.Graph;

import prefuse.data.Node;

import prefuse.data.Table;

import prefuse.data.Tuple;

import prefuse.data.event.TupleSetListener;

import prefuse.data.io.DataIOException;

import prefuse.data.io.DelimitedTextTableReader;

import prefuse.data.io.GraphMLReader;

import prefuse.data.query.SearchQueryBinding;

import prefuse.data.search.PrefixSearchTupleSet;

import prefuse.data.search.SearchTupleSet;

import prefuse.data.tuple.DefaultTupleSet;

import prefuse.data.tuple.TupleSet;

import prefuse.render.AbstractShapeRenderer;

import prefuse.render.DefaultRendererFactory;

import prefuse.render.EdgeRenderer;

import prefuse.render.LabelRenderer;

import prefuse.util.ColorLib;

import prefuse.util.FontLib;

import prefuse.util.ui.JFastLabel;

import prefuse.util.ui.JSearchPanel;

import prefuse.util.ui.UILib;

import prefuse.visual.VisualItem;

import prefuse.visual.expression.InGroupPredicate;

import prefuse.visual.sort.TreeDepthItemSorter;

import wjl.util.PreTable;





public class RadialGraphView extends Display {



    

    private static final String tree = "tree";

    private static final String treeNodes = "tree.nodes";

    private static final String treeEdges = "tree.edges";

    private static final String linear = "linear";

    

    private LabelRenderer m_nodeRenderer;

    private EdgeRenderer m_edgeRenderer;

    

    private String m_label = "label";

    

    public RadialGraphView(Graph g, String label) {

        super(new Visualization());

        m_label = label;



        // -- 设置可视化 --

        m_vis.add(tree, g);

        m_vis.setInteractive(treeEdges, null, false);

        

        // -- 设置渲染 --

        m_nodeRenderer = new LabelRenderer(m_label);

        m_nodeRenderer.setRenderType(AbstractShapeRenderer.RENDER_TYPE_FILL);

        m_nodeRenderer.setHorizontalAlignment(Constants.CENTER);

        m_nodeRenderer.setRoundedCorner(8,8);

        m_edgeRenderer = new EdgeRenderer();

        

        DefaultRendererFactory rf = new DefaultRendererFactory(m_nodeRenderer);

        rf.add(new InGroupPredicate(treeEdges), m_edgeRenderer);

        m_vis.setRendererFactory(rf);

               

        // -- 处理行动 --

        

        // colors

        ItemAction nodeColor = new NodeColorAction(treeNodes);

        ItemAction textColor = new TextColorAction(treeNodes);

        m_vis.putAction("textColor", textColor);

        

        ItemAction edgeColor = new ColorAction(treeEdges,

                VisualItem.STROKECOLOR, ColorLib.rgb(200,200,200));

        

        

        

        FontAction fonts = new FontAction(treeNodes, 

                FontLib.getFont("Tahoma", 10));

        fonts.add("ingroup('_focus_')", FontLib.getFont("Tahoma", 11));

        

        // recolor

        ActionList recolor = new ActionList();

//        recolor.add(nodeColor);

        recolor.add(textColor);

        m_vis.putAction("recolor", recolor);

        

        // repaint 个人感觉repaint 没有用处 但是不知道为什么demo里面会有

        ActionList repaint = new ActionList();

        repaint.add(recolor);

        repaint.add(new RepaintAction());

        m_vis.putAction("repaint", repaint);

        

        // 动画油漆变化

        ActionList animatePaint = new ActionList(400);

        animatePaint.add(new ColorAnimator(treeNodes));

        animatePaint.add(new RepaintAction());

        m_vis.putAction("animatePaint", animatePaint);

        

        // 创建布局

        RadialTreeLayout treeLayout = new RadialTreeLayout(tree);

        //下面这条语句可以将一个圆改成一个扇形

//        treeLayout.setAngularBounds(-Math.PI/2, Math.PI);

        m_vis.putAction("treeLayout", treeLayout);

        

        CollapsedSubtreeLayout subLayout = new CollapsedSubtreeLayout(tree);

        m_vis.putAction("subLayout", subLayout);

        

        // 创建过滤和布局

        ActionList filter = new ActionList();

        filter.add(new TreeRootAction(tree));

        filter.add(fonts);

        filter.add(treeLayout);

        filter.add(subLayout);

        filter.add(textColor);

//        filter.add(nodeColor);

        filter.add(edgeColor);

        m_vis.putAction("filter", filter);

        

        // 动画过渡

        ActionList animate = new ActionList(1250);

        animate.setPacingFunction(new SlowInSlowOutPacer());

        animate.add(new QualityControlAnimator());

        animate.add(new VisibilityAnimator(tree));

        animate.add(new PolarLocationAnimator(treeNodes, linear));

        animate.add(new ColorAnimator(treeNodes));

        animate.add(new RepaintAction());

        m_vis.putAction("animate", animate);

        m_vis.alwaysRunAfter("filter", "animate");

        

        // ------------------------------------------------

        

        // 初始化

        setSize(600,600);

        setItemSorter(new TreeDepthItemSorter());

        addControlListener(new DragControl());

        addControlListener(new ZoomToFitControl());

        addControlListener(new ZoomControl());

        addControlListener(new PanControl());

        addControlListener(new FocusControl(1, "filter"));

        addControlListener(new HoverActionControl("repaint"));

        

        // ------------------------------------------------

        

        // 过滤图和进行布局

        m_vis.run("filter");

        

        //我也不清楚这个是做什么的demo里面有

        m_vis.addFocusGroup(linear, new DefaultTupleSet());

        m_vis.getGroup(Visualization.FOCUS_ITEMS).addTupleSetListener(

            new TupleSetListener() {

                public void tupleSetChanged(TupleSet t, Tuple[] add, Tuple[] rem) {

                    TupleSet linearInterp = m_vis.getGroup(linear);

                    if ( add.length < 1 ) return; linearInterp.clear();

                    for ( Node n = (Node)add[0]; n!=null; n=n.getParent() )

                        linearInterp.addTuple(n);

                }

            }

        );

        

      //给数据改变颜色

        int[] palette = new int[] {      

                ColorLib.rgb(255,180,180), ColorLib.rgb(190,190,255), ColorLib.rgb(190,190,180)

        };

        

        DataColorAction fill = new DataColorAction(treeNodes, "gender",      

                Constants.NOMINAL, VisualItem.FILLCOLOR, palette);

        m_vis.putAction("shujucolor", fill);

        m_vis.run("shujucolor");

        //end

        

        SearchTupleSet search = new PrefixSearchTupleSet();

        m_vis.addFocusGroup(Visualization.SEARCH_ITEMS, search);

        search.addTupleSetListener(new TupleSetListener() {

            public void tupleSetChanged(TupleSet t, Tuple[] add, Tuple[] rem) {

                m_vis.cancel("animatePaint");

                m_vis.run("recolor");

                m_vis.run("animatePaint");

            }

        });

    }

    

    // ------------------------------------------------------------------------

    

    public static void main(String argv[]) throws DataIOException {

        String label = "name";

        

        

        

        Table nodes = new DelimitedTextTableReader().readTable("src/nodes");

		Table edges = new DelimitedTextTableReader().readTable("src/edges");

		Graph graph = new Graph(nodes, edges, false, "id", "sid", "tid");

        

        UILib.setPlatformLookAndFeel();

        

        JFrame frame = new JFrame("wjl 图显示");

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setContentPane(demo(graph, label));

        frame.pack();

        frame.setVisible(true);

    }

    

    

    public static JPanel demo(String datafile, final String label) {

        Graph g = null;

        try {

            g = new GraphMLReader().readGraph(datafile);

        } catch ( Exception e ) {

            e.printStackTrace();

            System.exit(1);

        }

        return demo(g, label);

    }

    

    public static JPanel demo(Graph g, final String label) {        

        // 创建一个视图

        final RadialGraphView gview = new RadialGraphView(g, label);

        Visualization vis = gview.getVisualization();

        

        // 创建一个搜索 panel可以查询元素

        // 这段代码在自带的demo中有

        SearchQueryBinding sq = new SearchQueryBinding(

             (Table)vis.getGroup(treeNodes), label,

             (SearchTupleSet)vis.getGroup(Visualization.SEARCH_ITEMS));

        JSearchPanel search = sq.createSearchPanel();

        search.setShowResultCount(true);

        search.setBorder(BorderFactory.createEmptyBorder(5,5,4,0));

        search.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 11));

        

        final JFastLabel title = new JFastLabel("                 ");

        title.setPreferredSize(new Dimension(350, 20));

        title.setVerticalAlignment(SwingConstants.BOTTOM);

        title.setBorder(BorderFactory.createEmptyBorder(3,0,0,0));

        title.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 16));

        

        gview.addControlListener(new ControlAdapter() {

            public void itemEntered(VisualItem item, MouseEvent e) {

                if ( item.canGetString(label) )

                    title.setText(item.getString(label));

            }

            public void itemExited(VisualItem item, MouseEvent e) {

                title.setText(null);

            }

        });

        

        Box box = new Box(BoxLayout.X_AXIS);

        box.add(Box.createHorizontalStrut(10));

        box.add(title);

        box.add(Box.createHorizontalGlue());

        box.add(search);

        box.add(Box.createHorizontalStrut(3));

        

        JPanel panel = new JPanel(new BorderLayout());

        panel.add(gview, BorderLayout.CENTER);

        panel.add(box, BorderLayout.SOUTH);

        

        Color BACKGROUND = Color.WHITE;

        Color FOREGROUND = Color.DARK_GRAY;

        UILib.setColor(panel, BACKGROUND, FOREGROUND);

        

        return panel;

    }

    

    // ------------------------------------------------------------------------

    

    /**

     * 创建一个action 可以更换一个图的新中心

     */

    public static class TreeRootAction extends GroupAction {

        public TreeRootAction(String graphGroup) {

            super(graphGroup);

        }

        public void run(double frac) {

            TupleSet focus = m_vis.getGroup(Visualization.FOCUS_ITEMS);

            if ( focus==null || focus.getTupleCount() == 0 ) return;

            

            Graph g = (Graph)m_vis.getGroup(m_group);

            Node f = null;

            Iterator tuples = focus.tuples();

            while (tuples.hasNext() && !g.containsTuple(f=(Node)tuples.next()))

            {

                f = null;

            }

            if ( f == null ) return;

            g.getSpanningTree(f);

        }

    }

    

    /**

     * 设置node的形态

     */

    public static class NodeColorAction extends ColorAction {

        public NodeColorAction(String group) {

            super(group, VisualItem.FILLCOLOR, ColorLib.rgba(255,255,255,0));

            add("_hover", ColorLib.gray(220,230));

            add("ingroup('_search_')", ColorLib.rgb(255,190,190));

            add("ingroup('_focus_')", ColorLib.rgb(198,229,229));

        }

                

    }

    

    /**

     * 设置文字的形态

     */

    public static class TextColorAction extends ColorAction {

        public TextColorAction(String group) {

            super(group, VisualItem.TEXTCOLOR, ColorLib.gray(0));

            add("_hover", ColorLib.rgb(255,0,0));

        }

    }

    

    

}

 

显示的结果: 

 

两个文件的内容

nodes

id name gender
0 aasda boy
1 bqweq nokonw
2 weas girl
3 asdad boy
4 ezsuy girl
5 fghyt boy

edges

id sid tid
0 0 1
1 0 2
2 1 3
3 4 5
4 0 4

 

你可能感兴趣的:(pre)