【问题标题】:Set pre-defined node styles?设置预定义的节点样式?
【发布时间】:2020-08-16 07:01:10
【问题描述】:

在过去的 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"];
}

【问题讨论】:

  • 您可以使用子图/集群给一组节点相同的样式,见graphviz.org/Gallery/directed/cluster.html
  • 也许我理解错了,但我不确定这是否适用于我的要求?我在问是否有一种方法可以重复使用我在上面布置的形状而不必每次都复制粘贴整个节点?或者你是说创建我的所有节点,然后使用集群来定义它们的样式(又名,cluster_file,cluster_database 等)?这可以工作,直到我需要使用集群进行布局。除非它允许您将节点放置到多个集群中而无需复制显示。
  • 是的,您为每种样式创建集群并在其中添加节点。不确定是否可以将节点放置在多个集群中。
  • 感谢您的提示。虽然我没有完全使用它,但它让我找到了答案。显然,您可以在多个子图中存在一个节点。我从那个开始,分离了视觉设置和标签。然后我在没有子图的情况下对其进行了测试,这也有效!查看我添加的答案。效果很好。谢谢!

标签: graphviz dot


【解决方案1】:

不幸的是,没有办法定义宏或对象并重用——尤其是跨多个图表。但是,也有使用其他工具的方法。有些人使用 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 预定义.gvpr 预定义2.gv | dot -Tpng > 预定义2.png

【讨论】:

  • 感谢您的提示,我将不得不对此进行调查,以前从未使用过 gvpr。看起来很方便。仅供参考,我找到了问题的答案,请查看。
【解决方案2】:

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

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

我还发现您也可以将它们放入子图中,例如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】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 2023-03-16
      相关资源
      最近更新 更多