Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/common/graph_attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace graph_layout {
constexpr std::string_view ATTR_KEY_SPLINES = "splines";
constexpr std::string_view ATTR_KEY_ARROW_HEAD = "arrowhead";
constexpr std::string_view ATTR_KEY_ARROW_TAIL = "arrowtail";
constexpr std::string_view ATTR_KEY_COLOR = "color";
constexpr std::string_view ATTR_KEY_FILL_COLOR = "fillcolor";
constexpr std::string_view ATTR_KEY_FONT_COLOR = "fontcolor";

class Attribute {
public:
Expand Down Expand Up @@ -119,6 +122,17 @@ namespace graph_layout {
void setEdgeArrowHead(int edgeId, const std::string& value);
void setEdgeArrowTail(int edgeId, const std::string& value);

void setVertexDefaultColor(const std::string& value);
void setVertexDefaultFillColor(const std::string& value);
void setVertexDefaultFontColor(const std::string& value);
void setEdgeDefaultColor(const std::string& value);
void setEdgeDefaultFontColor(const std::string& value);
void setVertexColor(int u, const std::string& value);
void setVertexFillColor(int u, const std::string& value);
void setVertexFontColor(int u, const std::string& value);
void setEdgeColor(int edgeId, const std::string& value);
void setEdgeFontColor(int edgeId, const std::string& value);

private:
static const std::string MONOSPACE_FONT_FAMILY;

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build"

[project]
name = "sp-graph-layout"
version = "1.3.0"
version = "1.4.0"
description = "A library for visualizing directed graphs."
requires-python = ">=3.8"
readme = "python/README.md"
Expand Down
16 changes: 13 additions & 3 deletions python/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,28 @@ PYBIND11_MODULE(_core, m, py::mod_gil_not_used()) {
;
py::class_<Attributes>(m, "Attributes")
.def(py::init<>())
.def("set_rank_dir", &Attributes::setRankDir)
.def("set_vertex_shape", &Attributes::setVertexShape)
.def("set_rank_dir", &Attributes::setRankDir, py::arg("value"))
.def("set_vertex_shape", &Attributes::setVertexShape, py::arg("u"), py::arg("value"))
.def("set_edge_tail_label", &Attributes::setEdgeTailLabel, py::arg("edge_id"), py::arg("label"))
.def("set_edge_head_label", &Attributes::setEdgeHeadLabel, py::arg("edge_id"), py::arg("label"))
.def("set_edge_label_distance", &Attributes::setEdgeLabelDistance, py::arg("edge_id"), py::arg("scale"))
.def("set_vertex_default_shape", &Attributes::setVertexDefaultShape)
.def("set_vertex_default_shape", &Attributes::setVertexDefaultShape, py::arg("value"))
.def("set_vertex_default_monospace", &Attributes::setVertexDefaultMonospace)
.def("set_edge_default_monospace", &Attributes::setEdgeDefaultMonospace)
.def("set_edge_default_arrow_head", &Attributes::setEdgeDefaultArrowHead, py::arg("value"))
.def("set_edge_default_arrow_tail", &Attributes::setEdgeDefaultArrowTail, py::arg("value"))
.def("set_edge_arrow_head", &Attributes::setEdgeArrowHead, py::arg("edge_id"), py::arg("value"))
.def("set_edge_arrow_tail", &Attributes::setEdgeArrowTail, py::arg("edge_id"), py::arg("value"))
.def("set_vertex_default_color", &Attributes::setVertexDefaultColor, py::arg("value"))
.def("set_vertex_default_fill_color", &Attributes::setVertexDefaultFillColor, py::arg("value"))
.def("set_vertex_default_font_color", &Attributes::setVertexDefaultFontColor, py::arg("value"))
.def("set_edge_default_color", &Attributes::setEdgeDefaultColor, py::arg("value"))
.def("set_edge_default_font_color", &Attributes::setEdgeDefaultFontColor, py::arg("value"))
.def("set_vertex_color", &Attributes::setVertexColor, py::arg("u"), py::arg("value"))
.def("set_vertex_fill_color", &Attributes::setVertexFillColor, py::arg("u"), py::arg("value"))
.def("set_vertex_font_color", &Attributes::setVertexFontColor, py::arg("u"), py::arg("value"))
.def("set_edge_color", &Attributes::setEdgeColor, py::arg("edge_id"), py::arg("value"))
.def("set_edge_font_color", &Attributes::setEdgeFontColor, py::arg("edge_id"), py::arg("value"))
;
py::class_<DirectedGraphHierarchicalLayout>(m, "DirectedGraphHierarchicalLayout")
.def(py::init<>())
Expand Down
43 changes: 43 additions & 0 deletions src/common/graph_attributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ unordered_map<string_view, string> Attributes::DEFAULT_GRAPH_ATTRIBUTE_VALUES =
{ATTR_KEY_BG_COLOR, "none"},
{ATTR_KEY_FONT_NAME, "Times,serif"},
{ATTR_KEY_FONT_SIZE, "14"},
{ATTR_KEY_COLOR, "black"},
{ATTR_KEY_FILL_COLOR, "none"},
{ATTR_KEY_FONT_COLOR, "black"},
};

unordered_map<string_view, string> Attributes::DEFAULT_VERTEX_ATTRIBUTE_VALUES = {
Expand Down Expand Up @@ -193,3 +196,43 @@ void Attributes::setEdgeArrowHead(const int edgeId, const string& value) {
void Attributes::setEdgeArrowTail(const int edgeId, const string& value) {
setEdgeAttributes(edgeId, ATTR_KEY_ARROW_TAIL, value);
}

void Attributes::setVertexDefaultColor(const string& value) {
_vertexDefaultAttributes[ATTR_KEY_COLOR] = value;
}

void Attributes::setVertexDefaultFillColor(const string& value) {
_vertexDefaultAttributes[ATTR_KEY_FILL_COLOR] = value;
}

void Attributes::setVertexDefaultFontColor(const string& value) {
_vertexDefaultAttributes[ATTR_KEY_FONT_COLOR] = value;
}

void Attributes::setEdgeDefaultColor(const string& value) {
_edgeDefaultAttributes[ATTR_KEY_COLOR] = value;
}

void Attributes::setEdgeDefaultFontColor(const string& value) {
_edgeDefaultAttributes[ATTR_KEY_FONT_COLOR] = value;
}

void Attributes::setVertexColor(const int u, const string& value) {
setVertexAttributes(u, ATTR_KEY_COLOR, value);
}

void Attributes::setVertexFillColor(const int u, const string& value) {
setVertexAttributes(u, ATTR_KEY_FILL_COLOR, value);
}

void Attributes::setVertexFontColor(const int u, const string& value) {
setVertexAttributes(u, ATTR_KEY_FONT_COLOR, value);
}

void Attributes::setEdgeColor(const int edgeId, const string& value) {
setEdgeAttributes(edgeId, ATTR_KEY_COLOR, value);
}

void Attributes::setEdgeFontColor(const int edgeId, const string& value) {
setEdgeAttributes(edgeId, ATTR_KEY_FONT_COLOR, value);
}
7 changes: 7 additions & 0 deletions src/graph_layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ string DirectedGraphHierarchicalLayout::render() const {
node->setShape(_attributes.vertexAttributes(u, ATTR_KEY_SHAPE));
node->setLabel(_attributes.vertexAttributes(u, ATTR_KEY_LABEL));
node->setFont(_attributes.vertexAttributes(u, ATTR_KEY_FONT_NAME), stod(_attributes.vertexAttributes(u, ATTR_KEY_FONT_SIZE)));
node->setColor(_attributes.vertexAttributes(u, ATTR_KEY_COLOR));
node->setFillColor(_attributes.vertexAttributes(u, ATTR_KEY_FILL_COLOR));
node->setFontColor(_attributes.vertexAttributes(u, ATTR_KEY_FONT_COLOR));
} else {
node->setShape(string("none"));
node->setMargin(0, 0);
Expand All @@ -194,6 +197,8 @@ string DirectedGraphHierarchicalLayout::render() const {
e->setMargin(2);
e->setArrowHead(_attributes.edgeAttributes(edge.id, ATTR_KEY_ARROW_HEAD));
e->setArrowTail(_attributes.edgeAttributes(edge.id, ATTR_KEY_ARROW_TAIL));
e->setColor(_attributes.edgeAttributes(edge.id, ATTR_KEY_COLOR));
e->setFontColor(_attributes.edgeAttributes(edge.id, ATTR_KEY_FONT_COLOR));
if (edge.u != edge.v) {
if (outEdges[edge.v].contains(edge.u)) {
// There is a reverse edge
Expand Down Expand Up @@ -244,6 +249,8 @@ string DirectedGraphHierarchicalLayout::render() const {
e->setArrowHead(_attributes.edgeAttributes(edgeId, ATTR_KEY_ARROW_HEAD));
e->setArrowTail(_attributes.edgeAttributes(edgeId, ATTR_KEY_ARROW_TAIL));
e->setMargin(2);
e->setColor(_attributes.edgeAttributes(edgeId, ATTR_KEY_COLOR));
e->setFontColor(_attributes.edgeAttributes(edgeId, ATTR_KEY_FONT_COLOR));
for (int i = 0; i + 1 < static_cast<int>(edgeIds.size()); ++i) {
const auto& edge = _graph->getEdge(edgeIds[i]);
e->addConnectionPoint(_xs[edge.v], _ys[edge.v]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ TEST(TestDirectedGraphHierarchialLayout, Monospace) {
layout.attributes().setEdgeTailLabel(0, "tail");
layout.attributes().setEdgeHeadLabel(1, "head");
layout.attributes().setEdgeLabelDistance(0, 1.0);
layout.attributes().setVertexDefaultColor("darkgray");
layout.attributes().setVertexDefaultFillColor("gray");
layout.attributes().setVertexDefaultFontColor("white");
layout.attributes().setEdgeDefaultColor("darkgray");
layout.attributes().setEdgeDefaultFontColor("white");
layout.setGraph(graph);
layout.setVertexLabels({"A", "BB", "C|C|C", "{DD|DD}"});
layout.layoutGraph();
Expand Down
12 changes: 11 additions & 1 deletion wasm/graph_layout_wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ EMSCRIPTEN_BINDINGS(GraphLayoutWASM) {
.function("setEdgeDefaultArrowTail", &Attributes::setEdgeDefaultArrowTail)
.function("setEdgeArrowHead", &Attributes::setEdgeArrowHead)
.function("setEdgeArrowTail", &Attributes::setEdgeArrowTail)
.function("setVertexDefaultColor", &Attributes::setVertexDefaultColor)
.function("setVertexDefaultFillColor", &Attributes::setVertexDefaultFillColor)
.function("setVertexDefaultFontColor", &Attributes::setVertexDefaultFontColor)
.function("setEdgeDefaultColor", &Attributes::setEdgeDefaultColor)
.function("setEdgeDefaultFontColor", &Attributes::setEdgeDefaultFontColor)
.function("setVertexColor", &Attributes::setVertexColor)
.function("setVertexFillColor", &Attributes::setVertexFillColor)
.function("setVertexFontColor", &Attributes::setVertexFontColor)
.function("setEdgeColor", &Attributes::setEdgeColor)
.function("setEdgeFontColor", &Attributes::setEdgeFontColor)
;
class_<DirectedGraphHierarchicalLayout>("DirectedGraphHierarchicalLayout")
.constructor<>()
Expand All @@ -88,5 +98,5 @@ EMSCRIPTEN_BINDINGS(GraphLayoutWASM) {
.function("attributes", &DirectedGraphHierarchicalLayout::attributes, return_value_policy::reference())
.function("layoutGraph", &DirectedGraphHierarchicalLayout::layoutGraph)
.function("render", select_overload<string()const>(&DirectedGraphHierarchicalLayout::render))
;
;
}
10 changes: 10 additions & 0 deletions wasm/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ declare module 'sp-graph-layout' {
setEdgeDefaultArrowTail(value: string): void
setEdgeArrowHead(edgeId: number, value: string): void
setEdgeArrowTail(edgeId: number, value: string): void
setVertexDefaultColor(value: string): void
setVertexDefaultFillColor(value: string): void
setVertexDefaultFontColor(value: string): void
setEdgeDefaultColor(value: string): void
setEdgeDefaultFontColor(value: string): void
setVertexColor(u: number, value: string): void
setVertexFillColor(u: number, value: string): void
setVertexFontColor(u: number, value: string): void
setEdgeColor(edgeId: number, value: string): void
setEdgeFontColor(edgeId: number, value: string): void
}

export class DirectedGraphHierarchicalLayout {
Expand Down
2 changes: 1 addition & 1 deletion wasm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sp-graph-layout",
"version": "1.3.0",
"version": "1.4.0",
"type": "module",
"main": "index.js",
"types": "index.d.ts",
Expand Down