设置预定义的节点样式?

Set pre-defined node styles?

过去 15 分钟我一直在谷歌上搜索,试图找到这个问题的答案。但是我好像想不通。

我的任务是为我在工作中开发的一些应用程序构建一些小流程图。他们不需要任何花哨的东西,因为他们会在 vizio 中将其转换成他们喜欢的格式。他们甚至说我们可以用笔和纸来做。所以我想我会玩 graphviz/dot.

他们有 6 个他们喜欢使用的预定义 shapes/colors,所以我想我会使用它们。我已经将它们全部构建在点中......但是如果我打算多次重复使用它们,我想找到一种方法将它们保存为一种模板。

这可能吗?

例如...这些是预定义的形状。

digraph G {
    node [color="#4271C6"]

    process [
        shape=Mrecord,
        style=filled, fillcolor="#E1F4FF",
        label="{1. Process\l | Description}"];

    subprocess [
        shape=record,
        style=filled, color="#FFFFFF", fillcolor="#A5A5A5",
        label="| Sub-Process |"];

    database [
        shape=cylinder, color="#18589A",
        label="Database"];

    inputoutput [
        shape=polygon,
        style=filled, fontcolor=white,
        fixedsize=true, skew=0.3, margin=0,
        width=2, label="Input / Output"];

    file [
        shape=folder,
        label="File"];

    external [
        shape=box3d,
        label="External entity"];
}

遗憾的是,无法定义宏或对象并重用 - 尤其是跨多个图形。但是,还有使用其他工具的方法。有些人使用 m4(宏语言)或 cpp(C 预处理器)都可以,但存在潜在的 OS 问题。 Python, awk, ... 也可以。
这是一个 gvpr 程序(gvpr 是 Graphviz 包的一部分),它也可以做你想做的事(我认为):

 digraph pre{
  a [_type=process label="{1. Process\l | Something}"]
  b [_type=process label="{2. Process\l | Something else}"]
  c [_type=subprocess label="do it"]
  d [_type=database label="lots of data"]
  e [_type=database label="a bit of data"]
  f [_type=inputoutput label="inOut"]
  g [_type=file label="nail file"]
  h [_type=external label="outside"]  

 a->b->c->d->e->f->g->h
}

gvpr 程序:

BEG_G{
  $G.newrank="true";
}
N{
  $.color="#4271C6";  // default
}
N[_type=="process"]{
  $.shape="Mrecord";
  $.style="filled";
  $.fillcolor="#E1F4FF";
  // maybe redo $.label
}
N[_type=="subprocess"]{
  $.shape="record";
  $.style="filled";
  $.color="#FFFFFF";
  $.fillcolor="#A5A5A5";
  $.label=sprintf("|%s|", $.label);  // embed in pipes
}
N[_type=="database"]{
  $.shape="cylinder";
  $.color="#18589A";
}
N[_type=="inputoutput"]{
  $.shape="polygon";
  $.style='filled';
  $.fontcolor="white",
  $.ixedsize="true";
  $.skew="0.3";
  $.margin="0";
  $.width="2";
}
N[_type=="file"]{
  $.shape="folder";
}
N[_type=="external"]{
  $.shape="box3d";
}

产生:

目前 Windows 上的 gvpr 可能存在问题,但我知道开发团队正在努力

命令行如下:
gvpr -c -f predefined.gvpr predefined2.gv |点-Tpng > predefined2.png

好的,我明白了。我没有意识到你可以这样做......但显然你可以将一个节点定义分解成多个部分......所以这就是我想出的,它解决了我的问题......

我有一个位于顶部的“样式”部分。在这里我可以定义每个节点样式。我使用评论作为命名它们的方式。而且我不需要复制粘贴,因为我可以将多个节点定义为逗号分隔列表。

我还发现您也可以将它们放入子图中,例如 subgraph style_file {...}。但是使用注释作为样式命名的方式似乎更简单。

digraph G {
    newrank=true;

    ///////////////////////////////////////////////////////////
    // Styles
    ///////////////////////////////////////////////////////////
        node [color="#4271C6"];
        edge [color="#4271C6"];

        //process
            createfile, uploadfile
            [shape=Mrecord, style=filled, fillcolor="#E1F4FF"];
        //subprocess
            exportfile, wait
            [shape=record, style=filled, color="#FFFFFF", fillcolor="#A5A5A5"];
        //external
            ftp
            [shape=box3d];
        //datastore
            database
            [shape=cylinder, color="#18589A"];
        //io
            exportproc
            [shape=polygon, style=filled, fontcolor=white, margin=0, width=3.1, fixedsize=true, skew=0.3];
        //file
            workfile
            [shape=folder];

    ///////////////////////////////////////////////////////////
    // Clusters
    ///////////////////////////////////////////////////////////
        subgraph cluster_0 {
            createfile  [label="{1. Process\l | Create file}"];
            exportfile  [label="|Export Data\nfrom DB|"];
            database    [label="Database"];
            exportproc  [label="Export Data"];
            workfile    [label="Generated file\n(Archived on server)"];
        }

        subgraph cluster_1 {
            uploadfile  [label="{2. Process\l | Upload file}"];
            ftp         [label="FTP Server"];
            wait        [label="|Wait for\nresponse file|"];
        }

    ///////////////////////////////////////////////////////////
    // Relationships
    ///////////////////////////////////////////////////////////
        {
            rank=same;
            createfile;
            uploadfile;
        }

    ///////////////////////////////////////////////////////////
    // Relationships
    ///////////////////////////////////////////////////////////
        # cluster_0
        createfile -> exportfile;
        exportfile -> database;
        database   -> exportproc;
        exportproc -> workfile [style=dashed];

        workfile -> uploadfile;

        # cluster_1
        uploadfile -> ftp [style=dashed];
        ftp -> wait;
}

产生这个:

没有从属关系,但 Excel to Graphviz 应用程序可以创建 re-usable 样式,如以下屏幕截图所示: