设置预定义节点样式?

7

我已经在谷歌上搜索了15分钟,试图找到这个问题的答案。但我好像无法弄清楚。

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

他们有6种预定义的形状/颜色,所以我想使用它们。我已经在dot中构建了所有这些形状……但如果我打算多次重复使用它们,我想找到一种方法将它们保存为某种模板。

这可行吗?

例如……这些是预定义形状。

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"];
}

Nodes produced from above code


您可以使用子图/簇来给一组节点设置相同的样式,详情请见 https://graphviz.org/Gallery/directed/cluster.html。 - Progman
也许我误解了,但我不确定这适用于我的问题?我想知道是否有一种方法可以重复使用我上面布置的形状,而无需每次都复制整个节点?或者您是说创建所有节点,然后使用群集来定义它们的样式(例如,cluster_file、cluster_database等)?那可能行得通,直到我需要为布局使用群集。除非它允许您将节点放入多个群集中而不重复显示。 - Chad Baldwin
是的,您可以为每种样式创建群集,并将节点添加到其中。不确定是否可以将节点放置在多个群集中。 - Progman
谢谢你的提示。虽然我没有完全使用它,但它引导了我找到答案。显然,一个单一节点可以存在于多个子图中。我从那里开始,分离了视觉设置和标签。然后我在没有子图的情况下进行了测试,也行!请查看我添加的答案。效果不错。谢谢! - Chad Baldwin
3个回答

6

好的,我弄清楚了。我之前不知道可以这样做......但显然,你可以将节点定义拆分为多个部分......所以这就是我想到的解决方案。

我有一个位于顶部的“Styles”部分,在这里我可以定义每个节点的样式。我使用注释作为它们的名称。我不需要进行复制和粘贴,因为我可以将多个节点定义为逗号分隔的列表。

我还发现你也可以将它们放在子图中,例如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;
}

这将产生如下结果:

在此输入图片描述


3

很遗憾,无法定义宏或对象以便重复使用 - 尤其是在多个图形之间。然而有其他工具可用。一些人使用m4(宏语言)或cpp(C预处理器)。两者都可以使用,但存在可能的操作系统问题。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 | dot -Tpng > predefined2.png


谢谢你的提示,我得去了解一下,以前从未使用过gvpr。看起来很方便。顺便说一句,我找到了我的问题的答案,你可以看看。 - Chad Baldwin

0

本人无关联,但Excel to Graphviz应用程序可以创建可重复使用的样式,如下图所示:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接