From f30f9c5b2886a3245d316cb811e6cd76d462ec3c Mon Sep 17 00:00:00 2001 From: jce <28319872+JasperCraeghs@users.noreply.github.com> Date: Fri, 9 Sep 2022 10:54:16 +0200 Subject: [PATCH 1/8] Configure matplotlib font --- doc/conf.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/conf.py b/doc/conf.py index 8c988011..0f9b722e 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -16,6 +16,7 @@ import subprocess import sys +import matplotlib import mlx.traceability from mlx.traceability import report_warning from pkg_resources import get_distribution @@ -450,6 +451,10 @@ def traceability_inspect_item(name, collection): print("You need to add path to 'plantuml.jar' file to your PATH variable.") sys.exit(os.strerror(errno.EPERM)) +# Configure Matplotlib to use Lato font +matplotlib.rcParams['font.sans-serif'] = 'Lato' + + def setup(app): from traceability import ItemDirective From 40cc3be14c24abfe7c93bd1d296813199123e1de Mon Sep 17 00:00:00 2001 From: jce <28319872+JasperCraeghs@users.noreply.github.com> Date: Fri, 9 Sep 2022 10:55:25 +0200 Subject: [PATCH 2/8] Define flag to hide title --- mlx/directives/item_pie_chart_directive.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mlx/directives/item_pie_chart_directive.py b/mlx/directives/item_pie_chart_directive.py index 2f19000a..2ac24dc1 100644 --- a/mlx/directives/item_pie_chart_directive.py +++ b/mlx/directives/item_pie_chart_directive.py @@ -344,6 +344,7 @@ class ItemPieChartDirective(TraceableBaseDirective): :colors: <> ... :sourcetype: <> ... :targettype: <> ... + :hidetitle: """ # Optional argument: title (whitespace allowed) optional_arguments = 1 @@ -355,6 +356,7 @@ class ItemPieChartDirective(TraceableBaseDirective): 'colors': directives.unchanged, 'sourcetype': directives.unchanged, 'targettype': directives.unchanged, + 'hidetitle': directives.flag, } # Content disallowed has_content = False @@ -382,6 +384,7 @@ def run(self): ) self.check_relationships(node['sourcetype'], env) self.check_relationships(node['targettype'], env) + self.check_option_presence(node, 'hidetitle') return [node] From 573a322657c52d4db8248816411567d66a72dda4 Mon Sep 17 00:00:00 2001 From: jce <28319872+JasperCraeghs@users.noreply.github.com> Date: Fri, 9 Sep 2022 11:17:05 +0200 Subject: [PATCH 3/8] Document the new 'hidetitle' flag --- doc/usage.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/usage.rst b/doc/usage.rst index ec9bfe53..97504f11 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -596,6 +596,7 @@ A pie chart of documentation items can be generated using: :result: error, fail, pass :functional: .* :colors: orange c b darkred #FF0000 g + :hidetitle: where the *id_set* arguments can be replaced by any Python regular expression. The *label_set* and *result* arguments are comma-separated lists. @@ -651,6 +652,10 @@ are comma-separated lists. in the *:<>:* option (*:result:* in the example). Matplotlib supports many formats, explained in their demo_. +:hidetitle: *optional*, *flag* + + By providing the *hidetitle* flag, the title will be hidden. + .. note:: In this example, if an RQT-item is linked to one or more TEST-items and at least one TEST-item is not linked From 63f40efd4ed26486181a1bc85484471178a0f294 Mon Sep 17 00:00:00 2001 From: jce <28319872+JasperCraeghs@users.noreply.github.com> Date: Fri, 9 Sep 2022 11:18:50 +0200 Subject: [PATCH 4/8] Implement 'hidetitle' flag --- mlx/directives/item_pie_chart_directive.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlx/directives/item_pie_chart_directive.py b/mlx/directives/item_pie_chart_directive.py index 2ac24dc1..d98194b5 100644 --- a/mlx/directives/item_pie_chart_directive.py +++ b/mlx/directives/item_pie_chart_directive.py @@ -51,7 +51,7 @@ def perform_replacement(self, app, collection): collection (TraceableCollection): Collection for which to generate the nodes. """ env = app.builder.env - top_node = self.create_top_node(self['title']) + top_node = self.create_top_node(self['title'], hide_title=self['hidetitle']) self.collection = collection self.source_relationships = self['sourcetype'] if self['sourcetype'] else self.collection.iter_relations() self.target_relationships = self['targettype'] if self['targettype'] else self.collection.iter_relations() From 422aea7cd3c8dbce78a54daa4ef05414c6c7ef04 Mon Sep 17 00:00:00 2001 From: jce <28319872+JasperCraeghs@users.noreply.github.com> Date: Fri, 9 Sep 2022 11:19:27 +0200 Subject: [PATCH 5/8] Add class 'pie-chart' to piechart images; ensure that all labels are fully rendered --- mlx/directives/item_pie_chart_directive.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mlx/directives/item_pie_chart_directive.py b/mlx/directives/item_pie_chart_directive.py index d98194b5..ebde19d8 100644 --- a/mlx/directives/item_pie_chart_directive.py +++ b/mlx/directives/item_pie_chart_directive.py @@ -290,10 +290,9 @@ def build_pie_chart(self, sizes, labels, colors, env): explode = self._get_explode_values(labels, self['label_set']) if not colors: colors = None - fig, axes = plt.subplots() + fig, axes = plt.subplots(subplot_kw=dict(aspect="equal")) _, texts, autotexts = axes.pie(sizes, explode=explode, labels=labels, autopct=pct_wrapper(sizes), startangle=90, colors=colors) - axes.axis('equal') folder_name = path.join(env.app.srcdir, '_images') if not path.exists(folder_name): mkdir(folder_name) @@ -301,10 +300,11 @@ def build_pie_chart(self, sizes, labels, colors, env): hash_value = sha256(hash_string.encode()).hexdigest() # create hash value based on chart parameters rel_file_path = path.join('_images', 'piechart-{}.png'.format(hash_value)) if rel_file_path not in env.images: - fig.savefig(path.join(env.app.srcdir, rel_file_path), format='png') + fig.savefig(path.join(env.app.srcdir, rel_file_path), format='png', bbox_inches='tight') env.images[rel_file_path] = ['_images', path.split(rel_file_path)[-1]] # store file name in build env image_node = nodes.image() + image_node['classes'].append('pie-chart') image_node['uri'] = rel_file_path image_node['candidates'] = '*' # look at uri value for source path, relative to the srcdir folder return image_node From 61a70d198b88083fd7ba9533100b215558dcad55 Mon Sep 17 00:00:00 2001 From: jce <28319872+JasperCraeghs@users.noreply.github.com> Date: Fri, 9 Sep 2022 11:41:07 +0200 Subject: [PATCH 6/8] Revert "Configure matplotlib font" This reverts commit f30f9c5b2886a3245d316cb811e6cd76d462ec3c. --- doc/conf.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/doc/conf.py b/doc/conf.py index 0f9b722e..8c988011 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -16,7 +16,6 @@ import subprocess import sys -import matplotlib import mlx.traceability from mlx.traceability import report_warning from pkg_resources import get_distribution @@ -451,10 +450,6 @@ def traceability_inspect_item(name, collection): print("You need to add path to 'plantuml.jar' file to your PATH variable.") sys.exit(os.strerror(errno.EPERM)) -# Configure Matplotlib to use Lato font -matplotlib.rcParams['font.sans-serif'] = 'Lato' - - def setup(app): from traceability import ItemDirective From c3cf3d9ceaf30c6cedce6abc2e8449a76d38d8e2 Mon Sep 17 00:00:00 2001 From: jce <28319872+JasperCraeghs@users.noreply.github.com> Date: Fri, 9 Sep 2022 11:41:46 +0200 Subject: [PATCH 7/8] Use Lato instead of matplotlib's default font --- mlx/directives/item_pie_chart_directive.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mlx/directives/item_pie_chart_directive.py b/mlx/directives/item_pie_chart_directive.py index ebde19d8..6148e9dc 100644 --- a/mlx/directives/item_pie_chart_directive.py +++ b/mlx/directives/item_pie_chart_directive.py @@ -287,6 +287,7 @@ def build_pie_chart(self, sizes, labels, colors, env): Returns: (nodes.image) Image node containing the pie chart image. """ + mpl.rcParams['font.sans-serif'] = 'Lato' explode = self._get_explode_values(labels, self['label_set']) if not colors: colors = None From e947fa8a199683907c9d077fe37e05fd3bd8d640 Mon Sep 17 00:00:00 2001 From: jce <28319872+JasperCraeghs@users.noreply.github.com> Date: Fri, 9 Sep 2022 11:49:09 +0200 Subject: [PATCH 8/8] Use svg or pdf instead of png --- mlx/directives/item_pie_chart_directive.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mlx/directives/item_pie_chart_directive.py b/mlx/directives/item_pie_chart_directive.py index 6148e9dc..62823f20 100644 --- a/mlx/directives/item_pie_chart_directive.py +++ b/mlx/directives/item_pie_chart_directive.py @@ -8,6 +8,7 @@ if not environ.get('DISPLAY'): mpl.use('Agg') import matplotlib.pyplot as plt # pylint: disable=wrong-import-order +from sphinx.builders.latex import LaTeXBuilder from mlx.traceability_exception import report_warning from mlx.traceable_base_directive import TraceableBaseDirective @@ -299,9 +300,10 @@ def build_pie_chart(self, sizes, labels, colors, env): mkdir(folder_name) hash_string = str(colors) + str(texts) + str(autotexts) hash_value = sha256(hash_string.encode()).hexdigest() # create hash value based on chart parameters - rel_file_path = path.join('_images', 'piechart-{}.png'.format(hash_value)) + image_format = 'pdf' if isinstance(env.app.builder, LaTeXBuilder) else 'svg' + rel_file_path = path.join('_images', 'piechart-{}.{}'.format(hash_value, image_format)) if rel_file_path not in env.images: - fig.savefig(path.join(env.app.srcdir, rel_file_path), format='png', bbox_inches='tight') + fig.savefig(path.join(env.app.srcdir, rel_file_path), format=image_format, bbox_inches='tight') env.images[rel_file_path] = ['_images', path.split(rel_file_path)[-1]] # store file name in build env image_node = nodes.image()