简体   繁体   English

如何在图形上显示边标签?

[英]How do I show edge labels on graphs?

I am trying to load a gexf file with sigInst.parseGexf('data/test.gexf').我正在尝试使用 sigInst.parseGexf('data/test.gexf') 加载 gexf 文件。

To create an edge with label I have this line in gexf file:要创建带标签的边,我在 gexf 文件中有这一行:

<edge label="test" id="7" source="14" target="18" type="directed"/>

but seems that sigma.js ignore this label field.但似乎 sigma.js 忽略了这个标签字段。

How can I show edge labels in graphs?如何在图形中显示边标签?

It's now possible to do this with a new plugin in the sigma repository: https://github.com/jacomyal/sigma.js/tree/master/plugins/sigma.renderers.edgeLabels现在可以使用 sigma 存储库中的新插件执行此操作: https : //github.com/jacomyal/sigma.js/tree/master/plugins/sigma.renderers.edgeLabels

Just follow the instructions to build the Sigma project and you'll find this file in the /build/plugins folder: sigma.renderers.edgeLabels.min.js只需按照说明构建 Sigma 项目,您就会在 /build/plugins 文件夹中找到此文件:sigma.renderers.edgeLabels.min.js

Include that in your html file:将其包含在您的 html 文件中:

<script src="sigma.min.js"></script>
<script src="sigma.renderers.edgeLabels.min.js"></script>

Make sure your edges have a 'label' key defined确保您的边缘定义了“标签”键

var data = {
    // specify 'nodes' as well
    edges: [                       
        {                          
            id: "e1",              
            source: "user", 
            target: "b1",        
            label: "This is the label", // <----- define 'label' key for your edges          
        }, 
    ]       
}

And then specify your renderer in the Sigma initialization.然后在 Sigma 初始化中指定您的渲染器。

var s = new sigma({                                                    
    graph: data,                                                       
    renderer: {                                                        
        container: "container",                                        
        type: "canvas"                                                 
    },                                                                 
    settings: {
    }                                                                  
});                                                                                                                                                  

It's not yet possible with the main sigma.js library.主要的 sigma.js 库尚无法实现。 However, I just forked the repository and added the capability on github.com here: https://github.com/sam2themax/sigma.js但是,我只是将存储库分叉并在 github.com 上添加了该功能: https : //github.com/sam2themax/sigma.js

To enable this new feature, set edgeLabels to true in your drawingProperties.要启用这一新功能,设置edgeLabelstrue在你drawingProperties。

All credit goes to @sam2themax - who forked it out.所有功劳都归功于@sam2themax - 谁把它分出来了。 I am just adding the relevant portions to make life easier.我只是添加相关部分以使生活更轻松。

On sigma.js main library - go to function drawEdge(edge) Add the below lines :在 sigma.js 主库上 - 转到函数 drawEdge(edge) 添加以下几行:

  var p1 = {};
  p1.x = sourceCoordinates[0];
  p1.y = sourceCoordinates[1];
  var p2 = {};
  p2.x = targetCoordinates[0];
  p2.y = targetCoordinates[1];
  drawEdgeLabel(ctx,edge['label'],p1,p2, color);

Create the new method :创建新方法:

  function drawEdgeLabel(ctx, text, p1, p2, color) {
console.log(text);
var alignment = 'center';
var padding = 10;

var dx = p2.x - p1.x;
var dy = p2.y - p1.y;
var len = Math.sqrt(dx * dx + dy * dy);
var avail = len - 2 * padding;

// Keep text upright
var angle = Math.atan2(dy, dx);
if (angle < -Math.PI / 2 || angle > Math.PI / 2) {
  var p = p1;
  p1 = p2;
  p2 = p;
  dx *= -1;
  dy *= -1;
  angle -= Math.PI;
}

var p = p1;
var pad = 1 / 2;

ctx.save();
ctx.textAlign = alignment;
ctx.translate(p.x + dx * pad, p.y + dy * pad);
ctx.rotate(angle);
var fontSize = self.p.defaultLabelSize;
ctx.font = self.p.fontStyle + fontSize + 'px ' + self.p.font;
ctx.fillStyle = color;
ctx.fillText(text, 0, -5);
ctx.restore();
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM