【问题标题】:setting default node attributes using Image_Graphviz package使用 Image_Graphviz 包设置默认节点属性
【发布时间】:2011-06-22 22:44:12
【问题描述】:

我可以使用点语言直接使用GraphViz 或使用PEAR 包Image_GraphViz 使用PHP 生成下图。

//DOT language
digraph test{
    URL="http://example.com/fish/";
    bgcolor="#BBCAF2";

    //defaults for all nodes
    node[style=filled, 
         fillcolor=white, 
         color="#8A94B4", 
         fixedsize=true, 
         fontname="sans-serif", 
         fontsize=8, 
         URL="?fish_id=\N", 
         margin="0.02,0.02"];

    //defaults for all edges
    edge[arrowsize=0.6,  
         sametail=true, 
         fontsize=8, 
         fontname="sans-serif"];

    //a few edges
    57->23[color="blue"];  42->23[color="red"];
    25->26[color="blue", label="10M"];  25->26[color="red", label="10F"];
    //etc.

    //a few nodes
    29[label="100128 AB"];
    38[label="100730 AB"];
    39[label="110208"];
    //etc.
}

点文件可以为所有四种元素类型(图形、集群、节点、边)设置属性默认值。看来 Image_GraphViz 只能为图级属性设置默认值。

<?php
$gatts=array( //defaults for graph level attributes
    'URL'=>"http://example.com/fish/",
    'bgcolor'=>"#ff0000",
    'font'=>"sans-serif",
);

$gv=new Image_GraphViz(true,$gatts,'test',false,true);

$q_ne="SELECT parentname, parent_id, childname, child_id, parenttype, parentcount 
       FROM fish_crosses";   
$r_ne=$dbii->query($q_ne);
while($ne=$r_ne->fetch_assoc()){
    $nodeatts=array('label' => $ne['parentname'], 
                     'style'=>"filled", 
                     'fillcolor'=>'#ffffff', 
                     'fixedsize'=>true, 
                     'fontname'=>"sans-serif", 
                     'fontsize'=>8);
    if(!$ne['child_id']) {
        $gv->addNode($ne['parent_id'], $nodeatts);
        continue;
    }
    if($ne['parenttype']=='dam'){
        $ecolor= '#ff0000';
        $elabel= $ne['parentcount'].'F';
    } else {
        $ecolor= '#0000ff';
        $elabel=$ne['parentcount'].'F';
    }
    $edgeatts=array('color'=>$ecolor, 'fontname'=>'sans-serif','fontsize'=>8);
    if($ne['parentcount']) $edgeatts['label']=$elabel;

     $gv->addEdge(array($ne['parent_id']=>$ne['child_id']), $edgeatts);
    $gv->addNode($ne['parent_id'], $nodeatts);
    $gv->addNode($ne['child_id'], $nodeatts);
}

echo $gv->image('png');
?>

有谁知道将节点和边的默认属性值添加到 Image_GraphViz 对象的语法?

【问题讨论】:

    标签: php graphviz image-graphviz


    【解决方案1】:

    这只是一个想法,无法测试:您是否尝试过简单地添加一个名为 node(或 edge)的节点并在该节点上定义属性?

    类似$gv-&gt;addNode('node', array('style'=&gt;'filled', 'fixedsize'=&gt;true))

    【讨论】:

    • 可悲的是,它添加了一个格式精美的额外节点,名为“node”,并且对其余节点的格式完全没有影响。
    • 哦,那太容易了...请在 node 之前或之后使用额外的空格再试一次 - 我认为节点名称不会被修剪添加时,如果有额外的字符,_escape 函数可能无法将其转义为“安全 ID”。
    • 如果这不起作用,添加属性和修改parse() 看起来并不复杂。
    • 看来我必须扩展课程。感谢您的想法。
    【解决方案2】:

    当前的 Image_GraphViz 包似乎无法处理默认的节点/边缘/集群属性。我通过更改这些函数扩展了该类:_escape_escapeArrayparse。这是我的更改:

    function _escape($input, $html = false) {
            switch (strtolower($input)) {
            //removed case 'node' and case 'edge' so they won't be quoted
            case 'graph':
            case 'digraph':
            case 'subgraph':
            case 'strict':
                return '"'.$input.'"';
            } //...
    
    
    function _escapeArray($input) {
    //...
            default:
                if(is_array($v)){
                    $v=$this->_escapeArray($v); //added recursion to allow default node/edge/cluster attribute sets
                } else {
                    $v = $this->_escape($v);
                }
                $k = $this->_escape($k);
            }
    //...
    
    function parse() {
    //...
        foreach ($attr as $key => $value) {
            if(is_array($value)){
                $a=implode(',', 
                array_map(function($v,$k){ return $k . '='.$v;}, 
                    array_values($value),
                    array_keys($value)));
            //default format for node/edge/cluster: thing[att1="blah", att2="foo"];
                $parsedGraph .= $indent.$key.'['.$a."];\n";
            } else {
                $parsedGraph .= $indent.$key.'='.$value.";\n";
            }
        }
    //...
    

    我希望这对某人有用。

    【讨论】:

      猜你喜欢
      • 2016-09-12
      • 2012-01-08
      • 2021-10-29
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 2023-03-10
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多