Browse Source

Add auto ``print_value`` color + a configurable ``value_colors`` list in style + Update doc

pull/264/head
Florian Mounier 10 years ago
parent
commit
59d4fa9f74
  1. 15
      demo/moulinrouge/tests.py
  2. 1
      docs/changelog.rst
  3. 19
      docs/documentation/configuration/data.rst
  4. 30
      docs/documentation/configuration/label.rst
  5. 12
      docs/documentation/configuration/legend.rst
  6. 10
      docs/documentation/configuration/title.rst
  7. 9
      docs/documentation/configuration/tooltip.rst
  8. 46
      docs/documentation/custom_styles.rst
  9. 2
      docs/ext/pygal_sphinx_directives.py
  10. 7
      pygal/colors.py
  11. 25
      pygal/style.py

15
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))

1
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
=====

19
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', [])

30
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
--------------

12
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
---------------

10
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])

9
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])

46
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
------------------

2
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')

7
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))

25
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."""

Loading…
Cancel
Save