graphviz-rust icon indicating copy to clipboard operation
graphviz-rust copied to clipboard

Provide example usage of SubgraphAttributes

Open sloganking opened this issue 1 year ago • 2 comments

let subgraph_nodes: Vec<Stmt> = //...
subgraph!(subgraph_id.as_u128(), subgraph_nodes)

Works for me, but my subgraphs have no color or border. I want to assign SubgraphAttributes::label in order to attempt to make the subgraphs visible. What is the valid syntax for that?

sloganking avatar Mar 29 '24 01:03 sloganking

Yeah. Truly, there are no macros associated with graph attr. I created an issue for that.

Meanwhile,

In case, if you want to have a border for the subgraph, then the clusters need to be used. Thus, the id should have the prefix cluster.

For now, you can manually create graph attributes like this:

     let attrs = GraphAttributes::new("graph", vec![
            SubgraphAttributes::style("dotted".to_owned()),
            SubgraphAttributes::color(color_name::green),
            SubgraphAttributes::label("sandwich".to_owned()),
        ]);

or using attr!:

        let attrs = GraphAttributes::new("graph", vec![
            attr!("style","bold"),
            attr!("color","green"),
            attr!("label", "sandwich"),
        ]);

The complete example:

    #[test]
    fn issue_with_node_test() {
        let attrs = GraphAttributes::new("graph", vec![
            SubgraphAttributes::style("dotted".to_owned()),
            SubgraphAttributes::color(color_name::green),
            SubgraphAttributes::label("sandwich".to_owned()),
        ]);
        let food =  subgraph!("cluster_sandwich";
            edge!(node_id!("bread") => node_id!("ham") => node_id!("salat")),
            attrs
        );
        let g = graph!(strict di id!("t"); edge!(node_id!("food") => food));
        let string = g.print(&mut Default::default());
        println!("{}",string);
    }

prints :

strict digraph t {
  food -> subgraph cluster_sandwich {
    bread -> ham -> salat
    graph[style=bold,color=green,label=sandwich]
  }
}

besok avatar Mar 29 '24 09:03 besok

I added the attrs! macro. The example now can look like this:

        let food =  subgraph!("cluster_sandwich";
            edge!(node_id!("bread") => node_id!("ham") => node_id!("salat")),
            attrs!(graph; attr!("style", "bold"), attr!("color","green"), attr!("label", "sandwich"))
        );

besok avatar Mar 30 '24 11:03 besok