From 59d4fa9f74da59d9ea19b3883dedbf532f21137c Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Mon, 20 Jul 2015 11:29:55 +0200 Subject: [PATCH] Add auto ``print_value`` color + a configurable ``value_colors`` list in style + Update doc --- demo/moulinrouge/tests.py | 15 ++++++- docs/changelog.rst | 1 + docs/documentation/configuration/data.rst | 19 -------- docs/documentation/configuration/label.rst | 30 ------------- docs/documentation/configuration/legend.rst | 12 ----- docs/documentation/configuration/title.rst | 10 ----- docs/documentation/configuration/tooltip.rst | 9 ---- docs/documentation/custom_styles.rst | 46 ++++++++++++++++++++ docs/ext/pygal_sphinx_directives.py | 2 +- pygal/colors.py | 7 +++ pygal/style.py | 25 +++++++++-- 11 files changed, 91 insertions(+), 85 deletions(-) diff --git a/demo/moulinrouge/tests.py b/demo/moulinrouge/tests.py index 49192c6..01d0152 100644 --- a/demo/moulinrouge/tests.py +++ b/demo/moulinrouge/tests.py @@ -60,7 +60,8 @@ def get_test_routes(app): 'label': 'Ten', 'xlink': 'http://google.com?q=10'}, {'value': 20, - 'label': 'Twenty is a good number yada yda yda yada yadaaaaaaaaaaaaaaaaaaaaaa', + 'label': 'Twenty is a good number yada yda yda yada ' + 'yadaaaaaaaaaaaaaaaaaaaaaa', 'xlink': 'http://google.com?q=20'}, 30, {'value': 40, @@ -143,6 +144,18 @@ def get_test_routes(app): bar.add('Lol', [2, None, 12]) return bar.render_response() + @app.route('/test/print_values') + def test_bar_print_values(): + bar = Bar(print_values=True, js=[], + style=styles['default']( + value_font_family='googlefont:Raleway', + value_font_size=30, + value_colors=(None, None, 'blue', 'red', 'green') + )) + for i in range(12): + bar.add('', i) + return bar.render_response() + @app.route('/test/treemap') def test_treemap(): treemap = Treemap(style=RotateStyle('#ff5995', opacity=.6)) diff --git a/docs/changelog.rst b/docs/changelog.rst index d947906..789ba6b 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -35,6 +35,7 @@ Changelog * Add ``font_family`` for various elements in style * Add ``googlefont:font`` support for style fonts * Add ``tooltip_fancy_mode`` to revert to old tooltips +* Add auto ``print_value`` color + a configurable ``value_colors`` list in style 1.7.0 ===== diff --git a/docs/documentation/configuration/data.rst b/docs/documentation/configuration/data.rst index 207b29a..81683d9 100644 --- a/docs/documentation/configuration/data.rst +++ b/docs/documentation/configuration/data.rst @@ -47,15 +47,6 @@ zero values are hidden by default but you can use this option to print them anyw chart.add('line', [0, 12, 31, 8, 28, 0]) -value_font_size ---------------- - -.. pygal-code:: - - chart = pygal.Bar(js=[], print_values=True, value_font_size=24) - chart.add('line', [0, 12, 31, 8, 28, 0]) - - human_readable -------------- @@ -86,13 +77,3 @@ Text to display instead of the graph when no data is supplied: chart = pygal.Line(no_data_text='No result found') chart.add('line', []) - - -no_data_font_size ------------------ - - -.. pygal-code:: - - chart = pygal.Line(no_data_font_size=32) - chart.add('line', []) diff --git a/docs/documentation/configuration/label.rst b/docs/documentation/configuration/label.rst index a01a067..da1c909 100644 --- a/docs/documentation/configuration/label.rst +++ b/docs/documentation/configuration/label.rst @@ -191,36 +191,6 @@ show_minor_y_labels chart.add('line', [0, .0002, .0005, .00035]) -label_font_size ---------------- - -.. pygal-code:: - - chart = pygal.Line(x_label_rotation=20, label_font_size=8) - chart.x_labels = [ - 'This is the first point !', - 'This is the second point !', - 'This is the third point !', - 'This is the fourth point !'] - chart.x_labels_major = ['This is the first point !', 'This is the fourth point !'] - chart.add('line', [0, .0002, .0005, .00035]) - - -major_label_font_size ---------------------- - -.. pygal-code:: - - chart = pygal.Line(x_label_rotation=20, major_label_font_size=12) - chart.x_labels = [ - 'This is the first point !', - 'This is the second point !', - 'This is the third point !', - 'This is the fourth point !'] - chart.x_labels_major = ['This is the first point !', 'This is the fourth point !'] - chart.add('line', [0, .0002, .0005, .00035]) - - truncate_label -------------- diff --git a/docs/documentation/configuration/legend.rst b/docs/documentation/configuration/legend.rst index 61b414d..9f8917e 100644 --- a/docs/documentation/configuration/legend.rst +++ b/docs/documentation/configuration/legend.rst @@ -55,18 +55,6 @@ legend_box_size chart.add('Serie 4', [3, 1, 5]) -legend_font_size ----------------- - -.. pygal-code:: - - chart = pygal.Line(legend_font_size=20) - chart.add('Serie 1', [1, 2, 3]) - chart.add('Serie 2', [4, 2, 0]) - chart.add('Serie 3', [1, -1, 1]) - chart.add('Serie 4', [3, 1, 5]) - - truncate_legend --------------- diff --git a/docs/documentation/configuration/title.rst b/docs/documentation/configuration/title.rst index cfa9a95..f5fe195 100644 --- a/docs/documentation/configuration/title.rst +++ b/docs/documentation/configuration/title.rst @@ -34,13 +34,3 @@ You can add a title to the y axis by setting the ``y_title`` option: chart = pygal.Line(title=u'Some points', y_title='Y Axis') chart.add('line', [.0002, .0005, .00035]) - - -title_font_size ---------------- - -.. pygal-code:: - - chart = pygal.Line(title=u'Some points', x_title='X Axis', y_title='Y Axis', - title_font_size=24) - chart.add('line', [.0002, .0005, .00035]) diff --git a/docs/documentation/configuration/tooltip.rst b/docs/documentation/configuration/tooltip.rst index 0442e34..ca8b8dc 100644 --- a/docs/documentation/configuration/tooltip.rst +++ b/docs/documentation/configuration/tooltip.rst @@ -11,12 +11,3 @@ tooltip_border_radius chart = pygal.Line(tooltip_border_radius=10) chart.add('line', [.0002, .0005, .00035]) - - -tooltip_font_size ------------------ - -.. pygal-code:: - - chart = pygal.Line(tooltip_font_size=24) - chart.add('line', [.0002, .0005, .00035]) diff --git a/docs/documentation/custom_styles.rst b/docs/documentation/custom_styles.rst index 3a79914..c785dd7 100644 --- a/docs/documentation/custom_styles.rst +++ b/docs/documentation/custom_styles.rst @@ -31,6 +31,52 @@ You can instantiate the ``Style`` class with some customizations for quick styli chart.add('E', [7, 4, 2, 1, 2, 10, 0]) +Properties +~~~~~~~~~~ + +Style objects supports the following properties: + +============================ ========================= +Properties Description +============================ ========================= +``plot_background`` The color of the chart area background +``background`` The color of the image background +``foreground`` The main foregrond color +``foreground_strong`` The emphasis foreground color +``foreground_subtle`` The subtle foreground color +``font_family`` The main font family +``label_font_family`` The label font family +``major_label_font_family`` The major label font family +``value_font_family`` The ``print_value`` font family +``tooltip_font_family`` The tooltip font family +``title_font_family`` The title font family +``legend_font_family`` The legend font family +``no_data_font_family`` The no data text font family +``label_font_size`` The label font size +``major_label_font_size`` The major label font size +``value_font_size`` The ``print_value`` font size +``tooltip_font_size`` The tooltip font size +``title_font_size`` The title font size +``legend_font_size`` The legend font size +``no_data_font_size`` The no data font size +``opacity`` The opacity of chart element +``opacity_hover`` The opacity of chart element on mouse hover +``transition`` Define the global transition property for animation +``colors`` The serie color list +``value_colors`` The ``print_value`` color list +============================ ========================= + + +Google font +~~~~~~~~~~~ + +It is possible to give a google font to any font family property by specifying the ``googlefont:`` prefix: + +.. code-block:: python + + style = Style(font_family='googlefont:Raleway') + + Using a custom css ------------------ diff --git a/docs/ext/pygal_sphinx_directives.py b/docs/ext/pygal_sphinx_directives.py index 49cad0d..8302f24 100644 --- a/docs/ext/pygal_sphinx_directives.py +++ b/docs/ext/pygal_sphinx_directives.py @@ -33,7 +33,7 @@ pygal.config.Config.style.value = pygal.style.RotateStyle( plot_background='#ffffff', foreground='#707070', foreground_strong='#404040', - foreground_subtle='#a0a0a0', + foreground_subtle='#909090', opacity='.8', opacity_hover='.9', transition='400ms ease-in') diff --git a/pygal/colors.py b/pygal/colors.py index 9f6df7a..bc8ea55 100644 --- a/pygal/colors.py +++ b/pygal/colors.py @@ -147,6 +147,13 @@ def unparse_color(r, g, b, a, type): return 'rgba(%d, %d, %d, %g)' % (r, g, b, a) +def is_foreground_light(color): + """ + Determine if the background color need a light or dark foreground color + """ + return rgb_to_hsl(*parse_color(color)[:3])[2] < 17.9 + + _clamp = lambda x: max(0, min(100, x)) diff --git a/pygal/style.py b/pygal/style.py index 2545a7f..1af17aa 100644 --- a/pygal/style.py +++ b/pygal/style.py @@ -19,9 +19,9 @@ """Charts styling classes""" from __future__ import division - +from itertools import chain from pygal import colors -from pygal.colors import darken, lighten +from pygal.colors import darken, lighten, is_foreground_light class Style(object): @@ -79,6 +79,8 @@ class Style(object): '#607D8B', # 18 ) + value_colors = () + def __init__(self, **kwargs): """Create the style""" self.__dict__.update(kwargs) @@ -106,6 +108,13 @@ class Style(object): ' fill: {1};\n' '}}\n') % (prefix, prefix)).format(*tupl) + def value_color(tupl): + """Make a value color css""" + return (( + '%s .text-overlay .color-{0} text {{\n' + ' fill: {1};\n' + '}}\n') % (prefix,)).format(*tupl) + if len(self.colors) < len_: missing = len_ - len(self.colors) cycles = 1 + missing // len(self.colors) @@ -121,7 +130,17 @@ class Style(object): else: colors = self.colors[:len_] - return '\n'.join(map(color, enumerate(colors))) + # Auto compute foreground value color when color is missing + value_colors = [] + for i in range(len_): + if i < len(self.value_colors) and self.value_colors[i] is not None: + value_colors.append(self.value_colors[i]) + else: + value_colors.append('white' if is_foreground_light(self.colors[i]) else 'black') + + return '\n'.join(chain( + map(color, enumerate(colors)), + map(value_color, enumerate(value_colors)))) def to_dict(self): """Convert instance to a serializable mapping."""