Assign attributes to multiple nodes at once

Is it possible to apply attributes based on the label for example, so you dont have to copy paste attributes like fill color etc, but just put all the nodes in one group?

In svg you can set css based on class and id attributes.

In other formats you can use “node [fillcolor=red]” to set the default fillcolor for the next nodes. Then above the next group set “node [fillcolor=blue]”. Not quite the same but it would work.

gvpr tool will have a way to set attributes for matching labels too.

yes & no

this works:

node [shape=square color=green]  // and/or other attributes
a b c
node [shape=circle color=red]  // and/or other attributes
d e f

and this works:

node [shape=rectangle]  // and/or other attributes
A
{
node [shape=square color=green]  // and/or other attributes
a b c
}
{
node [shape=circle color=red]  // and/or other attributes
d e f
}
B  // rectangle applies

But once a node or edge has been defined (referenced), I am pretty sure you can’t change attribute values of that node/edge later in the input.
Neither is there a general-purpose way to define style(s) and apply them throughout the input or from one file to the next (CSS-like) (requested, but not implemented, yet)

See Color a specific node and its children nodes/edges in one color, and grey everything else out and Using node attributes in HTML style label - #2 by steveroush
and Styles for GraphViz (#1566) · Issues · graphviz / graphviz · GitLab and How can I create named edge "types" in Graphviz/dot/neato? - Stack Overflow

1 Like

You can change node or edge attributes after they appear in the graph, but the objects have to be named specifically. e.g.

graph G {
a [color=red]
a [color=blue]  // changes to blue
}

but not

graph G {
node [color=red]
a
node [color=blue]
a 
}

The default object settings only apply to new objects.

My solution is like yours. I define a set of nodes between curly brackets, and they all share the same style.

 {node[shape=none, width=0, height=0, label=""]
    /*list of invisible nodes to put stuff where it looks better*/
    inv1 inv2 inv3 inv4 inv5;
}

Other than that, I know nothing else.

You can explicitly set attributes of a graph object after it is created, and you can put the objects in a list using commas. e.g.

digraph G {
    a, b, c, d [color=blue]  // all four nodes initialized with color set to blue
    a, b, c, d [color=red]    // all four nodes have color set to red
}

The graph language doesn’t have a similar construct for edges, but we could fix that.
You would have to deal with each edge individually and unless the graph is declared strict
you must use the edge key attribute to refer to an individual edge more than once, e.g.

digraph {
  a->b [key=someid_1,color=blue]
  a->b [key=someid_1,color=red]
}

It might be nice if we extended the graph language to have edge lists, and there’s the age-old topic of why we don’t have classes for styling. I think we could add these at least in a simplistic fashion and get a lot of bang for the buck. We have to think out both the API and graph language implications, including that we ensure that all changes effected through the API are represented when writing graphs. This could be a little subtle if we allow objects to belong to multiple classes, allow class membership and class definitions and setting of individual attributes on objects to be intermixed in any order, allow class definitions to be changed dynamically, etc.