Welcome to yEd Q&A!
Here you can ask questions and receive answers from other members of the community and yEd developers. And you can tell us your most wanted feature requests.

Categories

How to properly generate graphml with Perl

0 votes

I'm trying to use Perl's Graph::Easy to create a graphml file for yEd but can't get the node labels to show up. E.g.

 

#!perl

use strict;

use warnings;

 

$|++;

 

use Graph::Easy;

 

my $g = Graph::Easy->new();

 

my $a = $g->add_node('a');

my $b = $g->add_node('b');

my $c = $g->add_node('c');

my $d = $g->add_node('d');

 

$g->add_edge($a, $b);

$g->add_edge($b, $c);

$g->add_edge($c, $d);

$g->add_edge($d, $a);

$g->add_edge($d, $b);

 

print $g->as_graphml();

 
When I run this script and redirect it to a file (e.g. test.graphml), then open it in yEd, the labels 'a', 'b', 'c' and 'd' are not visible in yEd.
 
Do I have to set some yEd-specific attribute on the node to assign the label?
 
Cheers,
Larry
in Help by

2 Answers

0 votes
 
Best answer

Your script results in the following output:

<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">

  <graph id="G" edgedefault="directed">
    <node id="a">
    </node>
    <node id="b">
    </node>
    <node id="c">
    </node>
    <node id="d">
    </node>

    <edge source="a" target="b">
    </edge>
    <edge source="b" target="c">
    </edge>
    <edge source="c" target="d">
    </edge>
    <edge source="d" target="a">
    </edge>
    <edge source="d" target="b">
    </edge>
  </graph>
</graphml>

So, Graph::Easy uses 'a', 'b', 'c', and 'd' as unique identifiers for nodes. In GraphML (and in yEd's dialect of GraphML specifically) there is a difference between a node's unique identifier and its text labels. Text labels are part of the visualization data associated to a node. E.g. the simplest way to display the label 'a' for a node looks like this:

<graphml
 xmlns="http://graphml.graphdrawing.org/xmlns"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:y="http://www.yworks.com/xml/graphml"
 xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.0/ygraphml.xsd">

  <key for="node" id="d0" yfiles.type="nodegraphics"/>

  <graph id="G" edgedefault="directed">
    <node id="a">
      <data key="d0">
        <y:ShapeNode>
          <y:NodeLabel>a</y:NodeLabel>
        </y:ShapeNode>
      </data>
    </node>

  </graph>
</graphml>

(Note: The <y:NodeLabel> element may have any text content and you may have several such elements in a <y:ShapeNode> element).

If you use

print $g->as_graphml( format => 'yED' );

instead of

print $g->as_graphml();

Graph::Easy v0.72 will automatically add the necessary y namespace declaration

xmlns:y="http://www.yworks.com/xml/graphml"

Unfortunately, I have no idea how (or even if) it is possible to specify attributes with custom types (as required for yfiles.type="nodegraphics") in Graph::Easy.

However, Graph::Easy does support simple attributes. A simple attribute for node a is set like so:

$a->set_attribute('label', 'label text');

yEd can read these simple attributes. After opening your GraphML in yEd, select one node, go to the properties table in yEd's lower right corner, and find the "Data" section. Aside fron the default attributes URL and Description, this section should also contain a corresponding "label" property.
After you have verified that your custom data was properly imported, use yEd's properties mapper to map the label property to your node's label text.

Alternatively, you can use XSLT to transform your Graph:Easy generated GraphML with simple attributes into yEd's specific GraphML dialect.

by [yWorks] (160k points)
edited by
Thanks Thomas. I tried the properties mapping and it works well, exactly as you said.
Note that this example no longer works.
I copy-pasted the XML (to create a label 'a') into a text file,
added the <xml> header onto the top and tried to open it in the latest yEd,

It gave me this error:
y.H.B.B.a: SAX error: Element type "graphml" must be followed by either attribute specifications, ">" or "/>".

I think it is looking for the xmlns:yed line which is missing in the above example.

cheers,
Paul
The examples work - however, you have to make sure that there is no newline within attribute values. E.g. the xsi:schemaLocation values contain newlines because the forum software automatically inserts line breaks. These must be removed when copying/pasting for import in yEd.
0 votes

Hi,

I don't know if it's still relevant to you but I just wanted to let you (and everyone else who finds this thread) know:

I just uploaded the first version of yEd::Document to cpan: http://search.cpan.org/search?query=yed&mode=all

It is a perl API for creating yEd documents without having to deal with XML.

It is far away from being full featured but still offers enough potential for most purposes I think. :)

by
edited
Legal Disclosure | Privacy Policy
...