aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
dotElements.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
49
50namespace gum::dot {
51
53 const DotStyle& style,
54 const DotOptions& options,
55 std::string_view defaultLabel) {
56 const auto pick
57 = [](const std::string& given, const std::string& byDefault) -> const std::string& {
58 return given.empty() ? byDefault : given;
59 };
60
61 DotAttributes graph{{"bgcolor", "transparent"},
62 {"size", pick(options.size(), style.size)},
63 {"rankdir", pick(options.rankDir(), style.rankDir)},
64 {"layout", pick(options.layout(), style.layout)}};
65
66 const std::string label = options.label().empty() ? std::string(defaultLabel) : options.label();
67 if (!label.empty()) {
68 graph.push_back({"label", label});
69 graph.push_back({"fontcolor", style.arcColor});
70 }
71 writer.graphAttributes(graph);
72
73 writer.nodeDefaults({{"style", "filled"},
74 {"fillcolor", style.nodeBgColor},
75 {"fontcolor", style.nodeFgColor},
76 {"color", style.arcColor}});
77 writer.edgeDefaults({{"color", style.arcColor}});
78 }
79
81 const DotNodeOptions* options,
82 std::string_view defaultLabel,
83 std::string_view defaultTooltip) {
84 DotAttributes attributes;
85
86 const std::string label = (options != nullptr && !options->label.empty())
87 ? options->label
88 : std::string(defaultLabel);
89 if (!label.empty()) attributes.push_back({"label", label});
90
91 const std::string tooltip = (options != nullptr && !options->tooltip.empty())
92 ? options->tooltip
93 : std::string(defaultTooltip);
94 if (!tooltip.empty()) attributes.push_back({"tooltip", tooltip});
95
96 if (options == nullptr) return attributes;
97
98 if (!options->bgColor.empty()) {
99 attributes.push_back({"fillcolor", options->bgColor});
100 const std::string fg
101 = options->fgColor.empty() ? std::string(ADAPTIVE_COLOR) : options->fgColor;
102 attributes.push_back({"fontcolor", resolveFgColor(fg, options->bgColor, style.arcColor)});
103 } else if (!options->fgColor.empty()) {
104 attributes.push_back(
105 {"fontcolor", resolveFgColor(options->fgColor, style.nodeBgColor, style.arcColor)});
106 }
107
108 attributes.insert(attributes.end(), options->attributes.begin(), options->attributes.end());
109 return attributes;
110 }
111
113 DotAttributes attributes;
114 if (options == nullptr) return attributes;
115
116 if (!options->color.empty()) attributes.push_back({"color", options->color});
117 if (options->width.has_value())
118 attributes.push_back({"penwidth", DotWriter::number(*options->width)});
119 if (!options->label.empty()) attributes.push_back({"label", options->label});
120 if (!options->tooltip.empty()) attributes.push_back({"tooltip", options->tooltip});
121
122 attributes.insert(attributes.end(), options->attributes.begin(), options->attributes.end());
123 return attributes;
124 }
125
126 void
127 addDefaultAttribute(DotAttributes& attributes, std::string_view key, std::string_view value) {
128 for (const auto& attribute: attributes)
129 if (attribute.key == key) return;
130 attributes.insert(attributes.begin(), {std::string(key), std::string(value)});
131 }
132
133} // namespace gum::dot
The options given to a toDot(const DotOptions&).
Definition dotOptions.h:110
const std::string & layout() const
direction of the layout, TB or LR
const std::string & label() const
direction of the layout, TB or LR
const std::string & size() const
direction of the layout, TB or LR
const std::string & rankDir() const
direction of the layout, TB or LR
Writes a graph in the dot language, statement after statement, in the order of the calls.
Definition dotWriter.h:100
void nodeDefaults(const DotAttributes &attributes)
node [attributes];, the default attributes of the nodes that follow (none : does nothing).
void graphAttributes(const DotAttributes &attributes)
graph [attributes];, the attributes of the graph (does nothing if there is none).
void edgeDefaults(const DotAttributes &attributes)
edge [attributes];, the default attributes of the edges that follow (none : does nothing).
static std::string number(double value)
The shortest text that reads back as value (0.5, 1, 1e-05).
Definition dotWriter.cpp:93
Colors of the graphs drawn with graphviz: reading of a color, brightness, readable text color.
The statements common to the graphs drawn with graphviz : preamble, attributes of nodes and arcs.
constexpr std::string_view ADAPTIVE_COLOR
the special value of a font color: black or white, according to the brightness of the background
Definition dotColor.h:68
std::vector< DotAttribute > DotAttributes
a list of attributes
Definition dotWriter.h:74
std::string resolveFgColor(std::string_view fgColor, std::string_view bgColor, std::string_view fallback)
The font color to use on a background, once the special value adaptive is resolved.
Definition dotColor.cpp:280
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
void addDefaultAttribute(DotAttributes &attributes, std::string_view key, std::string_view value)
Adds an attribute at the beginning of the list, unless the list already gives a value for this key : ...
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...
the options of an arc or an edge : an empty text (no value for the width) means "not given"
Definition dotOptions.h:79
std::optional< double > width
Definition dotOptions.h:81
DotAttributes attributes
other attributes, written after the ones above
Definition dotOptions.h:85
the options of a node : an empty text means "not given", so that the value of the configuration is us...
Definition dotOptions.h:65
std::string fgColor
font color (if only the background is given, the font color is adaptive against it)
Definition dotOptions.h:69
std::string tooltip
text displayed when the mouse is over the node
Definition dotOptions.h:73
std::string bgColor
background color
Definition dotOptions.h:67
DotAttributes attributes
other attributes, written after the ones above
Definition dotOptions.h:75
std::string label
text displayed in the node
Definition dotOptions.h:71
The look shared by all the graphs: size, direction, layout, colors of the arcs and of the nodes.
Definition dotStyle.h:76
std::string size
graphviz size of the graph, in inches (5, or 12! for an exact size)
Definition dotStyle.h:78
std::string nodeFgColor
font color of the nodes
Definition dotStyle.h:88
std::string layout
graphviz layout engine (dot, neato...)
Definition dotStyle.h:82
std::string rankDir
direction of the layout : TB (top-bottom) or LR (left-right)
Definition dotStyle.h:80
std::string nodeBgColor
background color of the nodes
Definition dotStyle.h:86
std::string arcColor
color of the arcs and edges, and of the border of the nodes
Definition dotStyle.h:84