GraphStream 中的 setAttribute

setAttribute in GraphStream

我在 GraphStream 中有一个项目,我需要在其中更改图表的节点坐标。

这些坐标存储在一个名为 "xy"

的变量中

完成这项工作的函数应该是 setAttribute()addAttribute(),但是当我使用它们时,没有任何反应,或者有时它会给我一个 NaN 消息。

这是一个完全没有改变的代码示例:

public class TestClass {

public static void main(String[] args) {
    Graph graph = new MultiGraph("Dorogovtsev mendes");
    Generator gen = new DMCoordGen();       // Coordinates Generator
    gen.addSink(graph);
    gen.begin();
    for (int j = 0; j < 5; j++) {           // Graph Nodes
        gen.nextEvents();
    }
    gen.end();
    System.out.println("-----------------");
    System.out.println("First Coordinates");
    System.out.println("-----------------");
    for(int i=0; i<graph.getNodeCount(); i++) {             // First Reading
        Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
        Double x = attributes[0];
        Double y = attributes[1];
        System.out.println(x + " , " + y);
    }
    System.out.println("---------------");
    System.out.println("New Coordinates");
    System.out.println("---------------");
    for(int i=0; i<graph.getNodeCount(); i++) {             // Modification
        Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
        Double x = attributes[0] * 100;
        Double y = attributes[1] * 100;
        graph.getNode(i).setAttribute("xy", (Object[]) attributes);
    }
    for(int i=0; i<graph.getNodeCount(); i++) {             // Second Reading
        Double[] attributes = (Double[]) graph.getNode(i).getAttribute("xy");
        Double x = attributes[0];
        Double y = attributes[1];
        System.out.println(x + " , " + y);
    }
}

}

这段代码返回的结果是:

-----------------
First Coordinates
-----------------
0.27463410536937105 , 0.908142618579691
0.5945324304252239 , 0.011861230502362319
0.7645069243611142 , 0.8994092027470882
0.23856199477010953 , 0.6174255664753833
0.9215549969974312 , 0.46748048612026005
0.5283548936726747 , 0.3995089175747245
0.14035732608566487 , 0.32181008971710223
0.8782155705197804 , 0.8271792979519879
---------------
New Coordinates
---------------
0.27463410536937105 , 0.908142618579691
0.5945324304252239 , 0.011861230502362319
0.7645069243611142 , 0.8994092027470882
0.23856199477010953 , 0.6174255664753833
0.9215549969974312 , 0.46748048612026005
0.5283548936726747 , 0.3995089175747245
0.14035732608566487 , 0.32181008971710223
0.8782155705197804 , 0.8271792979519879

如您所见,使用 setAttribute()addAttribute() 都没有修改任何内容。

请参考graph.getNode(i).setAttribute("xy", (Object[]) attributes);行。

我做错了什么?我该如何解决?

谢谢!

在您的示例中,您的意思是:

attributes[0] *= 100;

而不是:

Double x = attributes[0] * 100;

在您的情况下,attributes 数组永远不会被修改。这可能就是 'setAttribute' 调用(显然)没有效果的原因。