Prefuse:向 GraphView 演示添加边权重

发布于 2024-12-13 20:21:49 字数 184 浏览 1 评论 0原文

我正在使用 prefuse 可视化工具包,工具包中的 GraphView 演示非常出色,提供了各种控件来可视化数据。

我能够为我的数据集生成 GraphML 并使用 GraphView 对其进行可视化,我希望拥有的另一件事是使用权重或颜色编码来标记边缘,以展示两个节点之间的强度。

任何有关相同的意见都非常感谢..谢谢..

I am using the prefuse visualization toolkit, The GraphView Demo in the toolkit is amazing providing a variety of controls to visualize the data.

I am able to generate GraphML for my dataset and visualize it using GraphView, One additional thing that i would love to have is to label the edges with weights or color coding to demonstrate the strength between two nodes.

Any input about the same are greatly appreciated..Thanks..

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

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

发布评论

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

评论(1

z祗昰~ 2024-12-20 20:21:49

免责声明:我没有使用过该 API,只是检查了文档:) 看来该 API 有一个 EdgeRenderer 接口,您应该实现该接口来实现所需的行为。

参考:http://prefuse.org/doc/manual/introduction/example/ , http://prefuse.org/doc/api/prefuse/render/DefaultRendererFactory.html

更新:首先更正:事实上 EdgeRenderer 不是一个 iterface,而是一个类。我做了一个简单的演示来说明如何实现自定义边缘渲染。

功能

将标签添加到包含节点标签首字母缩写的边缘

方法

我做了一个快速但肮脏的解决方案,即复制 LabelRenderer 并进行修改以处理边缘。

代码

我将该类命名为 MyEdgeRenderer

public class MyEdgeRenderer extends AbstractShapeRenderer {

使用原始 EdgeRenderer 绘制边缘线(有关渲染器的实际操作,请参阅下面的 render()):

protected EdgeRenderer m_edgeRenderer = new EdgeRenderer();

修改 getText() 以从节点获取缩写:

protected String getText(VisualItem item) {
    EdgeItem edge = (EdgeItem)item;
    VisualItem item1 = edge.getSourceItem();
    VisualItem item2 = edge.getTargetItem();    

    String t1 = null, t2 = null;
    if ( item1.canGetString(m_labelName) ) {
        t1 = item1.getString(m_labelName).substring(0,1);            
    };
    if ( item2.canGetString(m_labelName) ) {
        t2 = item2.getString(m_labelName).substring(0,1);            
    };
    if (t1 != null && t2 != null)
        return t1 + "-" + t2;
    else
        return null;
}

修改 getAlignedPoint() 以将标签定位在边缘的一半位置:

protected void getAlignedPoint(Point2D p, VisualItem item, 
        double w, double h, int xAlign, int yAlign)
{
    double x=0, y=0;                

    EdgeItem edge = (EdgeItem)item;
    VisualItem item1 = edge.getSourceItem();
    VisualItem item2 = edge.getTargetItem();

    // label is positioned to the center of the edge
    x = (item1.getX()+item2.getX())/2;
    y = (item1.getY()+item2.getY())/2;      
    ...

修改 render() (I) 首先画线并(II) 使用黑色:

public void render(Graphics2D g, VisualItem item) {         
    m_edgeRenderer.render(g, item);
    ...

    // render text
    int textColor = ColorLib.color(Color.BLACK); // item.getTextColor() 
    if ( text != null && ColorLib.alpha(textColor) > 0 ) {
    ...

为了测试,我修改了 Prefuse 网站上找到的示例 (http://prefuse.org/doc/manual/introduction/example/Example.java):

    // -- 3. the renderers and renderer factory ---------------------------

    // draw the "name" label for NodeItems
    LabelRenderer ir = new LabelRenderer("name");
    ir.setRoundedCorner(8, 8); // round the corners

    // draw the "name" initials for EdgeItems
    MyEdgeRenderer er = new MyEdgeRenderer("name");
    er.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(ir, er));

这只是一个演示,用于说明自定义渲染。实际上,您可能会从图形模型中检索标签文本和颜色,即:EdgeItem.getString()、getTextColor()。我猜这两个属性都可能来自 GraphML 数据。示例代码还显示了如何为节点设置颜色,它也可能适用于边缘(尽管我还没有尝试过):

    // -- 4. the processing actions ---------------------------------------
    ...
    // use black for node text
    ColorAction text = new ColorAction("graph.nodes",
            VisualItem.TEXTCOLOR, ColorLib.gray(0));

Disclaimer: I haven't worked with the API just checked the documentation:) It seems that the API has an EdgeRenderer interface that you should implement to achieve the desired behaviour.

Ref: http://prefuse.org/doc/manual/introduction/example/, http://prefuse.org/doc/api/prefuse/render/DefaultRendererFactory.html

Update: First a correction: in fact EdgeRenderer is not an iterface but a class. I've made a simple demo to illustrate how to implement custom edge rendering.

Feature

Add label to edges containing the initials of the node labels

Method

I made a quick and dirty solution, that is copied LabelRenderer and modified in order to handle edges.

Code

I named the class as MyEdgeRenderer:

public class MyEdgeRenderer extends AbstractShapeRenderer {

use the original EdgeRenderer to draw edge lines (see render() below for the renderer in action):

protected EdgeRenderer m_edgeRenderer = new EdgeRenderer();

modify getText() to get the initials from nodes:

protected String getText(VisualItem item) {
    EdgeItem edge = (EdgeItem)item;
    VisualItem item1 = edge.getSourceItem();
    VisualItem item2 = edge.getTargetItem();    

    String t1 = null, t2 = null;
    if ( item1.canGetString(m_labelName) ) {
        t1 = item1.getString(m_labelName).substring(0,1);            
    };
    if ( item2.canGetString(m_labelName) ) {
        t2 = item2.getString(m_labelName).substring(0,1);            
    };
    if (t1 != null && t2 != null)
        return t1 + "-" + t2;
    else
        return null;
}

modified getAlignedPoint() to position the label half way on the edge:

protected void getAlignedPoint(Point2D p, VisualItem item, 
        double w, double h, int xAlign, int yAlign)
{
    double x=0, y=0;                

    EdgeItem edge = (EdgeItem)item;
    VisualItem item1 = edge.getSourceItem();
    VisualItem item2 = edge.getTargetItem();

    // label is positioned to the center of the edge
    x = (item1.getX()+item2.getX())/2;
    y = (item1.getY()+item2.getY())/2;      
    ...

modify render() to (I) first draw the line and (II) use black color:

public void render(Graphics2D g, VisualItem item) {         
    m_edgeRenderer.render(g, item);
    ...

    // render text
    int textColor = ColorLib.color(Color.BLACK); // item.getTextColor() 
    if ( text != null && ColorLib.alpha(textColor) > 0 ) {
    ...

For testing I modified the sample found on the Prefuse website (http://prefuse.org/doc/manual/introduction/example/Example.java):

    // -- 3. the renderers and renderer factory ---------------------------

    // draw the "name" label for NodeItems
    LabelRenderer ir = new LabelRenderer("name");
    ir.setRoundedCorner(8, 8); // round the corners

    // draw the "name" initials for EdgeItems
    MyEdgeRenderer er = new MyEdgeRenderer("name");
    er.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(ir, er));

This is just a demo to illustrate custom rendering. In real you would probably retrieve label text and color from the graph model, ie: EdgeItem.getString(), getTextColor(). I guess both attribute could come from the GraphML data. Also the example code shows how to set colors for nodes, it might be adapted for edges as well (though I haven't tried):

    // -- 4. the processing actions ---------------------------------------
    ...
    // use black for node text
    ColorAction text = new ColorAction("graph.nodes",
            VisualItem.TEXTCOLOR, ColorLib.gray(0));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文