我目前正在开发一种工具,用于使用图和Java JUNG图可视化库来可视化宏基因组学数据。

当显示大约1000个节点时,我会遇到一个延迟,这是通过移动摄像机或拖动某些节点来实现的。

有没有可以用来改善这种情况的技巧?
我读了一些关于将窗口分为几部分的内容,并且只适用于正在显示的面板的一部分,但是我不明白这一点。

谢谢。

最佳答案

该问题可能被认为过于笼统,因为优化的自由度太多了。并且存在至少相关的问题(Improve the rendering of a JUNG graph JUNG cannot display large graphs?或其他),如果不是重复的话。

但是,我会在这里尝试回答:

通常,使用JUNG,您可以轻松地用几行代码创建一个漂亮的图形,并具有令人印象深刻的默认功能(交互)和许多功能。在这方面,JUNG并非主要针对绘制具有1000个顶点的图形。相反,它的目的是很好地绘制具有数十个(或几百个)顶点和边的图形。

(实际上,从理论上以信息可视化的角度来看,绘制具有> 1000个顶点的图形几乎毫无意义。至少,如果没有过度的缩放和平移,您将无法从图形中直观地提取任何信息。 )

当您要绘制具有许多顶点和许多边的图形时,可以使用一些选项来提高性能。 (您没有说出边的数量。在很多情况下,这是最昂贵的东西!)。

根据我的经验,提高渲染性能的最重要的事情就是...

禁用抗锯齿!

严重的是,这确实很昂贵。在荣格,这可以做到

visualizationViewer.getRenderingHints().remove(
    RenderingHints.KEY_ANTIALIASING)


除此之外,还有许多提高性能的选项,但是,当然,它们全都取决于您要牺牲哪种视觉功能。下面的示例显示了一个具有2500个顶点和5000个边的图形。默认情况下,它非常慢。 improvePerformance方法包含有关如何使可视化更快的几个选项。即使仅禁用抗锯齿功能,在我的机器(速度较慢)上,性能还是可以接受的。

根据评论进行编辑/扩展:

import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.apache.commons.collections15.Predicate;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.Context;
import edu.uci.ics.jung.graph.util.Pair;
import edu.uci.ics.jung.visualization.Layer;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.EdgeShape;
import edu.uci.ics.jung.visualization.renderers.BasicEdgeRenderer;
import edu.uci.ics.jung.visualization.transform.shape.GraphicsDecorator;

public class JungPerformance
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Graph<String, String> g = createGraph();

        Dimension size = new Dimension(800,800);
        VisualizationViewer<String, String> vv =
            new VisualizationViewer<String, String>(
                new FRLayout<String, String>(g, size));
        DefaultModalGraphMouse<String, Double> graphMouse =
            new DefaultModalGraphMouse<String, Double>();
        vv.setGraphMouse(graphMouse);

        improvePerformance(vv);

        f.getContentPane().add(vv);
        f.setSize(size);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    // This method summarizes several options for improving the painting
    // performance. Enable or disable them depending on which visual features
    // you want to sacrifice for the higher performance.
    private static <V, E> void improvePerformance(
        VisualizationViewer<V, E> vv)
    {
        // Probably the most important step for the pure rendering performance:
        // Disable anti-aliasing
        vv.getRenderingHints().remove(RenderingHints.KEY_ANTIALIASING);

        // Skip vertices that are not inside the visible area.
        doNotPaintInvisibleVertices(vv);

        // May be helpful for performance in general, but not appropriate
        // when there are multiple edges between a pair of nodes: Draw
        // the edges not as curves, but as straight lines:
        vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line<V,E>());

        // May be helpful for painting performance: Omit the arrow heads
        // of directed edges
        Predicate<Context<Graph<V, E>, E>> edgeArrowPredicate =
            new Predicate<Context<Graph<V,E>,E>>()
        {
            @Override
            public boolean evaluate(Context<Graph<V, E>, E> arg0)
            {
                return false;
            }
        };
        vv.getRenderContext().setEdgeArrowPredicate(edgeArrowPredicate);

    }

    // Skip all vertices that are not in the visible area.
    // NOTE: See notes at the end of this method!
    private static <V, E> void doNotPaintInvisibleVertices(
        VisualizationViewer<V, E> vv)
    {
        Predicate<Context<Graph<V, E>, V>> vertexIncludePredicate =
            new Predicate<Context<Graph<V,E>,V>>()
        {
            Dimension size = new Dimension();

            @Override
            public boolean evaluate(Context<Graph<V, E>, V> c)
            {
                vv.getSize(size);
                Point2D point = vv.getGraphLayout().transform(c.element);
                Point2D transformed =
                    vv.getRenderContext().getMultiLayerTransformer()
                        .transform(point);
                if (transformed.getX() < 0 || transformed.getX() > size.width)
                {
                    return false;
                }
                if (transformed.getY() < 0 || transformed.getY() > size.height)
                {
                    return false;
                }
                return true;
            }
        };
        vv.getRenderContext().setVertexIncludePredicate(vertexIncludePredicate);

        // NOTE: By default, edges will NOT be included in the visualization
        // when ONE of their vertices is NOT included in the visualization.
        // This may look a bit odd when zooming and panning over the graph.
        // Calling the following method will cause the edges to be skipped
        // ONLY when BOTH their vertices are NOT included in the visualization,
        // which may look nicer and more intuitive
        doPaintEdgesAtLeastOneVertexIsVisible(vv);
    }

    // See note at end of "doNotPaintInvisibleVertices"
    private static <V, E> void doPaintEdgesAtLeastOneVertexIsVisible(
        VisualizationViewer<V, E> vv)
    {
        vv.getRenderer().setEdgeRenderer(new BasicEdgeRenderer<V, E>()
        {
            @Override
            public void paintEdge(RenderContext<V,E> rc, Layout<V, E> layout, E e)
            {
                GraphicsDecorator g2d = rc.getGraphicsContext();
                Graph<V,E> graph = layout.getGraph();
                if (!rc.getEdgeIncludePredicate().evaluate(
                        Context.<Graph<V,E>,E>getInstance(graph,e)))
                    return;

                Pair<V> endpoints = graph.getEndpoints(e);
                V v1 = endpoints.getFirst();
                V v2 = endpoints.getSecond();
                if (!rc.getVertexIncludePredicate().evaluate(
                        Context.<Graph<V,E>,V>getInstance(graph,v1)) &&
                    !rc.getVertexIncludePredicate().evaluate(
                        Context.<Graph<V,E>,V>getInstance(graph,v2)))
                    return;

                Stroke new_stroke = rc.getEdgeStrokeTransformer().transform(e);
                Stroke old_stroke = g2d.getStroke();
                if (new_stroke != null)
                    g2d.setStroke(new_stroke);

                drawSimpleEdge(rc, layout, e);

                // restore paint and stroke
                if (new_stroke != null)
                    g2d.setStroke(old_stroke);
            }
        });
    }


    public static Graph<String, String> createGraph()
    {
        Random random = new Random(0);
        int numVertices = 2500;
        int numEdges = 5000;
        Graph<String, String> g = new DirectedSparseGraph<String, String>();
        for (int i=0; i<numVertices; i++)
        {
            g.addVertex("v"+i);
        }
        for (int i=0; i<numEdges; i++)
        {
            int v0 = random.nextInt(numVertices);
            int v1 = random.nextInt(numVertices);
            g.addEdge("e"+i, "v"+v0, "v"+v1);
        }
        return g;
    }
}

关于java - JUNG-大图可视化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37740057/

10-12 23:23