2个版本

0.1.1 2023年7月31日
0.1.0 2021年11月2日

#169 in 可视化

Download history 4617/week @ 2024-04-16 3221/week @ 2024-04-23 2438/week @ 2024-04-30 2753/week @ 2024-05-07 3669/week @ 2024-05-14 4333/week @ 2024-05-21 4627/week @ 2024-05-28 5028/week @ 2024-06-04 3567/week @ 2024-06-11 3042/week @ 2024-06-18 3063/week @ 2024-06-25 3414/week @ 2024-07-02 4204/week @ 2024-07-09 4941/week @ 2024-07-16 4670/week @ 2024-07-23 3988/week @ 2024-07-30

18,665 个月下载量
用于 28 个crate (8 直接使用)

自定义许可

7KB
147

描述

该库提供了对graphviz格式中图形的基本访问,具有将其导入或导出的能力。

基本示例

解析dot源代码

use dot_generator::*;
use dot_structures::*;

fn parse_test() {
    let g: Graph = parse(
        r#"
        strict digraph t {
            aa[color=green]
            subgraph v {
                aa[shape=square]
                subgraph vv{a2 -> b2}
                aaa[color=red]
                aaa -> bbb
            }
            aa -> be -> subgraph v { d -> aaa}
            aa -> aaa -> v
        }
        "#,
    )
    .unwrap();

    assert_eq!(
        g,
        graph!(strict di id!("t");
          node!("aa";attr!("color","green")),
          subgraph!("v";
            node!("aa"; attr!("shape","square")),
            subgraph!("vv"; edge!(node_id!("a2") => node_id!("b2"))),
            node!("aaa";attr!("color","red")),
            edge!(node_id!("aaa") => node_id!("bbb"))
            ),
          edge!(node_id!("aa") => node_id!("be") => subgraph!("v"; edge!(node_id!("d") => node_id!("aaa")))),
          edge!(node_id!("aa") => node_id!("aaa") => node_id!("v"))
        )
    )
}

将图形打印为dot源代码

use dot_generator::*;
use dot_structures::*;
use graphviz_rust::printer::{DotPrinter, PrinterContext};

fn print_test() {
    let mut g = graph!(strict di id!("id"));
    assert_eq!(
        "strict digraph id {}".to_string(),
        g.print(&mut PrinterContext::default())
    );
}

使用命令引擎将图形转换为外部格式

use dot_generator::*;
use dot_structures::*;
use graphviz_rust::{
    attributes::*,
    cmd::{CommandArg, Format},
    exec, parse,
    printer::{DotPrinter, PrinterContext},
};

fn output_test() {
    let mut g = graph!(id!("id");
         node!("nod"),
         subgraph!("sb";
             edge!(node_id!("a") => subgraph!(;
                node!("n";
                NodeAttributes::color(color_name::black), NodeAttributes::shape(shape::egg))
            ))
        ),
        edge!(node_id!("a1") => node_id!(esc "a2"))
    );
    let graph_svg = exec(
        g,
        &mut PrinterContext::default(),
        vec![Format::Svg.into()],
    )
    .unwrap();
}

结构

该结构尽可能遵循dot 符号,因此具有直接的对应关系。结构可以在 dot_structures::* 中找到,并具有以下表示

strict digraph t {                     : graph with t as id
        aa[color=green]                : node aa and attributes in []
        subgraph v {                   : subgraph v
         aa[shape=square]            : node aa in subgraph
         subgraph vv{a2 -> b2}       : another subgraph carrying edge inside( a type of the edge is Pair)
         aaa[color=red]
         aaa -> subgraph { d -> aaa} : subgraph id is anonymous id
        }
       aa -> be -> d -> aaa            : other edge with a type Chain
   }

生成dot结构

该库提供了一套宏,简化了图形构建的过程。每个宏的详细信息以及示例可以在宏的文档中找到,并可以在 dot_generator::* 中找到

示例

assert_eq!(
    node!("node_id"; attr!("atr1","val1"),attr!("atr2","val2")),
    node!(
        "node_id",
        vec![attr!("atr1", "val1"), attr!("atr2", "val2")]
    )
);

fn graph_test() {
    use dot_generator::*;
    use dot_structures::*;

    let g = r#"
            strict digraph t {
                aa[color=green]
                subgraph v {
                    aa[shape=square]
                    subgraph vv{a2 -> b2}
                    aaa[color=red]
                    aaa -> bbb
                }
                aa -> be -> subgraph v { d -> aaa}
                aa -> aaa -> v
            }
            "#;

    graph!(strict di id!("t");
      node!("aa";attr!("color","green")),
      subgraph!("v";
        node!("aa"; attr!("shape","square")),
        subgraph!("vv"; edge!(node_id!("a2") => node_id!("b2"))),
        node!("aaa";attr!("color","red")),
        edge!(node_id!("aaa") => node_id!("bbb"))
        ),
      edge!(node_id!("aa") => node_id!("be") => subgraph!("v"; edge!(node_id!("d") => node_id!("aaa")))),
      edge!(node_id!("aa") => node_id!("aaa") => node_id!("v"))
    );
}

属性

graphviz提供了大量的可能的 属性,为了支持它,该库提供了一套结构简化了在这些属性之间的导航,即

  • 可以通过宏 attr!(id,id) 将自定义属性轻松地组合起来,尽管还有其他可能的格式
  • 使用命名属性如 graphviz_rust::attributes::color 用于 color 属性
  • 使用特定的结构 graphviz_rust::attributes::{EdgeAttributes,SubgraphAttributes GraphAttributes, NodeAttributes} 对属性进行分组和显示,以表明哪个属性属于该结构。
use dot_generator::*;
use dot_structures::*;
use graphviz_rust::attributes::{
    color, color_name, GraphAttributes, NodeAttributes,
};
use into_attr::IntoAttribute;

fn test() {
    assert_eq!(GraphAttributes::center(true), attr!("center", true));
    assert_eq!(
        NodeAttributes::color(color_name::antiquewhite1),
        attr!("color", "antiquewhite1")
    );
    assert_eq!(color::default().into_attr(), attr!("color", "black"));
}

转换为遵循dot格式的字符串

调用特质 DotPrinter 将图结构转换为字符串。

use dot_generator::*;
use dot_structures::*;
use graphviz_rust::printer::{DotPrinter, PrinterContext};

fn subgraph_test() {
    let mut ctx = PrinterContext::default();
    let s =
        subgraph!("id"; node!("abc"), edge!(node_id!("a") => node_id!("b")));

    assert_eq!(
        s.print(&mut ctx),
        "subgraph id {\n    abc\n    a -- b \n}".to_string()
    );
}

该模块允许使用 PrinterContext 调整一些参数,如缩进步长或行分隔符。

fn ctx() {
    use self::graphviz_rust::printer::PrinterContext;
    let mut ctx = PrinterContext::default();

    ctx.always_inline(); // everything in one line
    ctx.with_semi(); // semicolon at the end of every element
    ctx.with_indent_step(4); // indent 4 (default 2)
    ctx.with_inline_size(60); // size indicating the line needs to break into multilines
}

使用命令引擎的外部格式和其他格式

该库提供从 Rust 代码中使用 命令命令 的功能。详细信息请参阅 graphviz_rust::{exec}graphviz_rust::{exec_dot} 方法

fn output_test() {
    let mut g = graph!(id!("id"));
    exec(
        g,
        PrinterContext::default(),
        vec![
            Format::Svg.into(),
            CommandArg::Output("path_to_file".to_string()),
        ],
    );
}

注意事项

应安装 命令客户端

由于该库通过命令客户端执行命令,因此客户端应预先安装,否则将出现类似:未找到文件或目录找不到程序(根据操作系统)的错误。


lib.rs:

dot 语言的结构

Graphviz dot 表示法组件集合,力求与语言 notation 比较接近

描述

    strict digraph t {           <= graph
        aa[color=green]          <= node aa and attributes in [..]
        subgraph v {             <= subgraph v
	     aa[shape=square]
	     subgraph vv{a2 -> b2}
	     aaa[color=red]
	     aaa -> subgraph { d -> aaa}  <= subgraph id is anon
        }
       aa -> be -> d -> aaa       <= type of the edge is chain
   }

无运行时依赖