Skip to content

Commit

Permalink
Merge pull request #304 from melexis/pie-chart-size
Browse files Browse the repository at this point in the history
item-piechart: support hidden titles and render long labels
  • Loading branch information
Letme authored Sep 9, 2022
2 parents ec79743 + e947fa8 commit 25c0917
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions doc/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -651,6 +652,10 @@ are comma-separated lists.
in the *:<<attribute>>:* 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
Expand Down
16 changes: 11 additions & 5 deletions mlx/directives/item_pie_chart_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +52,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()
Expand Down Expand Up @@ -287,24 +288,26 @@ 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
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)
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')
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()
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
Expand Down Expand Up @@ -344,6 +347,7 @@ class ItemPieChartDirective(TraceableBaseDirective):
:colors: <<color>> ...
:sourcetype: <<relationship>> ...
:targettype: <<relationship>> ...
:hidetitle:
"""
# Optional argument: title (whitespace allowed)
optional_arguments = 1
Expand All @@ -355,6 +359,7 @@ class ItemPieChartDirective(TraceableBaseDirective):
'colors': directives.unchanged,
'sourcetype': directives.unchanged,
'targettype': directives.unchanged,
'hidetitle': directives.flag,
}
# Content disallowed
has_content = False
Expand Down Expand Up @@ -382,6 +387,7 @@ def run(self):
)
self.check_relationships(node['sourcetype'], env)
self.check_relationships(node['targettype'], env)
self.check_option_presence(node, 'hidetitle')

return [node]

Expand Down

0 comments on commit 25c0917

Please sign in to comment.