Browse Source

Add ``print_labels`` option to print label too. (Fix #197) Add ``value_label_font_family`` and ``value_label_font_size`` style options for ``print_labels``. Default ``print_zeroes`` to True (Re)Add xlink in desc to show on tooltip Activate element on tooltip hovering. (Fix #106)

pull/264/head
Florian Mounier 10 years ago
parent
commit
c175c08d8b
  1. 53
      demo/moulinrouge/tests.py
  2. 5
      docs/changelog.rst
  3. 41
      docs/documentation/configuration/data.rst
  4. 8
      docs/documentation/custom_styles.rst
  5. 8
      pygal/config.py
  6. 7
      pygal/css/base.css
  7. 5
      pygal/css/style.css
  8. 2
      pygal/graph/bar.py
  9. 2
      pygal/graph/box.py
  10. 2
      pygal/graph/dot.py
  11. 2
      pygal/graph/funnel.py
  12. 2
      pygal/graph/gauge.py
  13. 16
      pygal/graph/graph.py
  14. 2
      pygal/graph/histogram.py
  15. 3
      pygal/graph/line.py
  16. 4
      pygal/graph/pie.py
  17. 3
      pygal/graph/treemap.py
  18. 4
      pygal/style.py
  19. 4
      pygal/svg.py
  20. 2
      pygal/util.py

53
demo/moulinrouge/tests.py

@ -50,7 +50,7 @@ def get_test_routes(app):
def test_bar_links(): def test_bar_links():
bar = StackedLine(style=styles['default']( bar = StackedLine(style=styles['default'](
font_family='googlefont:Raleway')) font_family='googlefont:Raleway'))
# bar.js = ('http://l:2343/2.0.x/pygal-tooltips.js',) bar.js = ('http://l:2343/2.0.x/pygal-tooltips.js',)
bar.title = 'Wow ! Such Chart !' bar.title = 'Wow ! Such Chart !'
bar.x_title = 'Many x labels' bar.x_title = 'Many x labels'
bar.y_title = 'Much y labels' bar.y_title = 'Much y labels'
@ -145,31 +145,42 @@ def get_test_routes(app):
bar.x_labels = range(1, 4) bar.x_labels = range(1, 4)
return bar.render_response() return bar.render_response()
@app.route('/test/print_values') @app.route('/test/print_values/<chart>')
def test_bar_print_values(): def test_print_values_for(chart):
bar = Bar(print_values=True, js=[], graph = CHARTS_BY_NAME[chart](
style=styles['default']( print_values=True, print_labels=True, print_zeroes=True,
value_font_family='googlefont:Raleway', style=styles['default'](
value_font_size=30, value_font_family='googlefont:Raleway',
value_colors=(None, None, 'blue', 'red', 'green') value_colors=(None, None, 'blue', 'red', 'green')
)) ))
graph.js = ('http://l:2343/2.0.x/pygal-tooltips.js',)
for i in range(12): for i in range(12):
bar.add('', i) graph.add('', [{
return bar.render_response() 'value': i + j,
'label': 'abcdefghijklmnopqrstuvwxyz'[i + j]
} for j in range(5)])
return graph.render_response()
@app.route('/test/treemap') @app.route('/test/treemap')
def test_treemap(): def test_treemap():
treemap = Treemap(style=RotateStyle('#ff5995', opacity=.6)) treemap = Treemap(style=RotateStyle('#ff5995', opacity=.6,
value_font_size=32,
value_colors=['#ffffff']))
treemap.title = 'Binary TreeMap' treemap.title = 'Binary TreeMap'
treemap.add('A', [2, 1, 12, 4, 2, 1, 1, 3, 12, 3, 4, None, 9]) treemap.print_values = True
treemap.add('B', [4, 2, 5, 10, 3, 4, 2, 7, 4, -10, None, 8, 3, 1]) treemap.print_labels = True
treemap.add('C', [3, 8, 3, 3, 5, 3, 3, 5, 4, 12]) for i in range(1, 5):
treemap.add('D', [23, 18]) treemap.add('', [{'label': 'Area %d' % i, 'value': i}])
treemap.add('E', [1, 2, 1, 2, 3, 3, 1, 2, 3, treemap.add('', [2])
4, 3, 1, 2, 1, 1, 1, 1, 1]) # treemap.add('A', [2, 1, 12, 4, 2, 1, 1, 3, 12, 3, 4, None, 9])
treemap.add('F', [31]) # treemap.add('B', [4, 2, 5, 10, 3, 4, 2, 7, 4, -10, None, 8, 3, 1])
treemap.add('G', [5, 9.3, 8.1, 12, 4, 3, 2]) # treemap.add('C', [3, 8, 3, 3, 5, 3, 3, 5, 4, 12])
treemap.add('H', [12, 3, 3]) # treemap.add('D', [23, 18])
# treemap.add('E', [1, 2, 1, 2, 3, 3, 1, 2, 3,
# 4, 3, 1, 2, 1, 1, 1, 1, 1])
# treemap.add('F', [31])
# treemap.add('G', [5, 9.3, 8.1, 12, 4, 3, 2])
# treemap.add('H', [12, 3, 3])
return treemap.render_response() return treemap.render_response()
@app.route('/test/gauge') @app.route('/test/gauge')

5
docs/changelog.rst

@ -7,6 +7,11 @@ Changelog
* Fix the missing title on x_labels with labels. * Fix the missing title on x_labels with labels.
* Auto cast to str x labels in non dual charts (#178) * Auto cast to str x labels in non dual charts (#178)
* Add ``print_labels`` option to print label too. (#197)
* Add ``value_label_font_family`` and ``value_label_font_size`` style options for ``print_labels``.
* Default ``print_zeroes`` to True
* (Re)Add xlink in desc to show on tooltip
* Activate element on tooltip hovering. (#106)
2.0.0 2.0.0
===== =====

41
docs/documentation/configuration/data.rst

@ -28,12 +28,12 @@ Same on x axis for xy like charts:
print_values print_values
------------ ------------
When using pygal without javascript for printing for example you can chose to activate this option to print all values as text. When using pygal to display static charts for printing for example you can chose to activate this option to print all values as text.
.. pygal-code:: .. pygal-code::
from pygal.style import DefaultStyle from pygal.style import DefaultStyle
chart = pygal.Bar(js=[], print_values=True, style=DefaultStyle( chart = pygal.Bar(print_values=True, style=DefaultStyle(
value_font_family='googlefont:Raleway', value_font_family='googlefont:Raleway',
value_font_size=30, value_font_size=30,
value_colors=('white',))) value_colors=('white',)))
@ -43,14 +43,47 @@ When using pygal without javascript for printing for example you can chose to ac
print_zeroes print_zeroes
------------ ------------
zero values are hidden by default but you can use this option to print them anyway. zero values are shown by default but you can use this option to hide them.
.. pygal-code:: .. pygal-code::
chart = pygal.Bar(js=[], print_values=True, print_zeroes=True) chart = pygal.Bar(print_values=True, print_zeroes=False)
chart.add('line', [0, 12, 31, 8, 28, 0]) chart.add('line', [0, 12, 31, 8, 28, 0])
print_labels
------------
You can activate value label display:
.. pygal-code::
chart = pygal.Bar(print_labels=True)
chart.add('line', [
0,
{'value': 12, 'label': 'Twelve'},
31,
{'value': 8, 'label': 'eight'},
28,
0
])
Displaying both is also possible:
.. pygal-code::
chart = pygal.Bar(print_labels=True, print_values=True)
chart.add('line', [
0,
{'value': 12, 'label': 'Twelve'},
31,
{'value': 8, 'label': 'eight'},
28,
0
])
human_readable human_readable
-------------- --------------

8
docs/documentation/custom_styles.rst

@ -47,7 +47,8 @@ Properties Description
``font_family`` The main font family ``font_family`` The main font family
``label_font_family`` The label font family ``label_font_family`` The label font family
``major_label_font_family`` The major label font family ``major_label_font_family`` The major label font family
``value_font_family`` The ``print_value`` font family ``value_font_family`` The ``print_values`` font family
``value_label_font_family`` The ``print_labels`` font family
``tooltip_font_family`` The tooltip font family ``tooltip_font_family`` The tooltip font family
``title_font_family`` The title font family ``title_font_family`` The title font family
``legend_font_family`` The legend font family ``legend_font_family`` The legend font family
@ -56,7 +57,8 @@ Properties Description
``major_guide_stroke_dasharray`` The dasharray for major guide line ``major_guide_stroke_dasharray`` The dasharray for major guide line
``label_font_size`` The label font size ``label_font_size`` The label font size
``major_label_font_size`` The major label font size ``major_label_font_size`` The major label font size
``value_font_size`` The ``print_value`` font size ``value_font_size`` The ``print_values`` font size
``value_label_font_size`` The ``print_labels`` font size
``tooltip_font_size`` The tooltip font size ``tooltip_font_size`` The tooltip font size
``title_font_size`` The title font size ``title_font_size`` The title font size
``legend_font_size`` The legend font size ``legend_font_size`` The legend font size
@ -65,7 +67,7 @@ Properties Description
``opacity_hover`` The opacity of chart element on mouse hover ``opacity_hover`` The opacity of chart element on mouse hover
``transition`` Define the global transition property for animation ``transition`` Define the global transition property for animation
``colors`` The serie color list ``colors`` The serie color list
``value_colors`` The ``print_value`` color list ``value_colors`` The ``print_values`` color list
================================ ========================= ================================ =========================

8
pygal/config.py

@ -448,11 +448,15 @@ class Config(CommonConfig):
print_values = Key( print_values = Key(
False, bool, False, bool,
"Text", "Print values when graph is in non interactive mode") "Text", "Display values as text over plot")
print_zeroes = Key( print_zeroes = Key(
True, bool,
"Text", "Display zero values as well")
print_labels = Key(
False, bool, False, bool,
"Text", "Print zeroes when graph is in non interactive mode") "Text", "Display value labels")
truncate_legend = Key( truncate_legend = Key(
None, int, "Text", None, int, "Text",

7
pygal/css/base.css

@ -48,11 +48,16 @@
font-size: {{ style.major_label_font_size }}px; font-size: {{ style.major_label_font_size }}px;
} }
{{ id }}.series text { {{ id }}.text-overlay text.value {
font-family: {{ style.value_font_family }}; font-family: {{ style.value_font_family }};
font-size: {{ style.value_font_size }}px; font-size: {{ style.value_font_size }}px;
} }
{{ id }}.text-overlay text.label {
font-family: {{ style.value_label_font_family }};
font-size: {{ style.value_label_font_size }}px;
}
{{ id }}.tooltip { {{ id }}.tooltip {
font-family: {{ style.tooltip_font_family }}; font-family: {{ style.tooltip_font_family }};
font-size: {{ style.tooltip_font_size }}px; font-size: {{ style.tooltip_font_size }}px;

5
pygal/css/style.css

@ -130,6 +130,11 @@
fill: {{ style.foreground_strong }}; fill: {{ style.foreground_strong }};
} }
{{ id }}.tooltip .xlink {
font-size: .5em;
text-decoration: underline;
}
{{ id }}.tooltip .value { {{ id }}.tooltip .value {
font-size: 1.5em; font-size: 1.5em;
} }

2
pygal/graph/bar.py

@ -91,7 +91,7 @@ class Bar(Graph):
self._tooltip_data( self._tooltip_data(
bar, val, x_center, y_center, "centered", bar, val, x_center, y_center, "centered",
self._get_x_label(i)) self._get_x_label(i))
self._static_value(serie_node, val, x_center, y_center) self._static_value(serie_node, val, x_center, y_center, metadata)
def _compute(self): def _compute(self):
"""Compute y min and max and y scale and set labels""" """Compute y min and max and y scale and set labels"""

2
pygal/graph/box.py

@ -113,7 +113,7 @@ class Box(Graph):
box, serie.values[1:6], serie.outliers, serie.index, metadata) box, serie.values[1:6], serie.outliers, serie.index, metadata)
self._tooltip_data(box, val, x_center, y_center, "centered", self._tooltip_data(box, val, x_center, y_center, "centered",
self._get_x_label(serie.index)) self._get_x_label(serie.index))
self._static_value(serie_node, val, x_center, y_center) self._static_value(serie_node, val, x_center, y_center, metadata)
def _draw_box(self, parent_node, quartiles, outliers, box_index, metadata): def _draw_box(self, parent_node, quartiles, outliers, box_index, metadata):
""" """

2
pygal/graph/dot.py

@ -71,7 +71,7 @@ class Dot(Graph):
self._tooltip_data( self._tooltip_data(
dots, value, x, y, 'centered', dots, value, x, y, 'centered',
self._get_x_label(i)) self._get_x_label(i))
self._static_value(serie_node, value, x, y) self._static_value(serie_node, value, x, y, metadata)
def _compute(self): def _compute(self):
"""Compute y min and max and y scale and set labels""" """Compute y min and max and y scale and set labels"""

2
pygal/graph/funnel.py

@ -62,7 +62,7 @@ class Funnel(Graph):
self._tooltip_data( self._tooltip_data(
funnels, value, x, y, 'centered', funnels, value, x, y, 'centered',
self._get_x_label(serie.index)) self._get_x_label(serie.index))
self._static_value(serie_node, value, x, y) self._static_value(serie_node, value, x, y, metadata)
def _center(self, x): def _center(self, x):
return x - 1 / (2 * self._order) return x - 1 / (2 * self._order)

2
pygal/graph/gauge.py

@ -90,7 +90,7 @@ class Gauge(Graph):
self._tooltip_data( self._tooltip_data(
gauges, value, x, y, gauges, value, x, y,
xlabel=self._get_x_label(i)) xlabel=self._get_x_label(i))
self._static_value(serie_node, value, x, y) self._static_value(serie_node, value, x, y, metadata)
def _y_axis(self, draw_axes=True): def _y_axis(self, draw_axes=True):
"""Override y axis to plot a polar axis""" """Override y axis to plot a polar axis"""

16
pygal/graph/graph.py

@ -457,12 +457,24 @@ class Graph(PublicApi):
self.svg.node(node, 'desc', self.svg.node(node, 'desc',
class_="x_label").text = to_str(xlabel) class_="x_label").text = to_str(xlabel)
def _static_value(self, serie_node, value, x, y): def _static_value(self, serie_node, value, x, y, metadata):
"""Write the print value""" """Write the print value"""
label = metadata and metadata.get('label')
if self.print_labels and label:
if self.print_values:
y -= self.style.value_font_size / 2
self.svg.node(
serie_node['text_overlay'], 'text',
class_='centered label',
x=x,
y=y + self.style.value_font_size / 3
).text = label
y += self.style.value_font_size
if self.print_values: if self.print_values:
self.svg.node( self.svg.node(
serie_node['text_overlay'], 'text', serie_node['text_overlay'], 'text',
class_='centered', class_='centered value',
x=x, x=x,
y=y + self.style.value_font_size / 3 y=y + self.style.value_font_size / 3
).text = value if self.print_zeroes or value != '0' else '' ).text = value if self.print_zeroes or value != '0' else ''

2
pygal/graph/histogram.py

@ -97,7 +97,7 @@ class Histogram(Dual):
self._tooltip_data( self._tooltip_data(
bar, val, x_center, y_center, "centered", bar, val, x_center, y_center, "centered",
self._get_x_label(i)) self._get_x_label(i))
self._static_value(serie_node, val, x_center, y_center) self._static_value(serie_node, val, x_center, y_center, metadata)
def _compute(self): def _compute(self):
"""Compute x/y min and max and x/y scale and set labels""" """Compute x/y min and max and x/y scale and set labels"""

3
pygal/graph/line.py

@ -125,7 +125,8 @@ class Line(Graph):
self._static_value( self._static_value(
serie_node, val, serie_node, val,
x + self.style.value_font_size, x + self.style.value_font_size,
y + self.style.value_font_size) y + self.style.value_font_size,
metadata)
if serie.stroke: if serie.stroke:
if self.interpolate: if self.interpolate:

4
pygal/graph/pie.py

@ -84,7 +84,7 @@ class Pie(Graph):
alter(self.svg.slice( alter(self.svg.slice(
serie_node, slice_, big_radius, small_radius, serie_node, slice_, big_radius, small_radius,
angle, start_angle, center, val, i), metadata) angle, start_angle, center, val, i, metadata), metadata)
start_angle += angle start_angle += angle
total_perc += perc total_perc += perc
@ -93,7 +93,7 @@ class Pie(Graph):
self.svg.slice(serie_node, self.svg.slice(serie_node,
self.svg.node(slices, class_="big_slice"), self.svg.node(slices, class_="big_slice"),
radius * .9, 0, serie_angle, radius * .9, 0, serie_angle,
original_start_angle, center, val, i) original_start_angle, center, val, i, metadata)
return serie_angle return serie_angle
def _compute_x_labels(self): def _compute_x_labels(self):

3
pygal/graph/treemap.py

@ -65,7 +65,8 @@ class Treemap(Graph):
self._static_value( self._static_value(
serie_node, value, serie_node, value,
rx + rw / 2, rx + rw / 2,
ry + rh / 2) ry + rh / 2,
metadata)
def _binary_tree(self, data, total, x, y, w, h, parent=None): def _binary_tree(self, data, total, x, y, w, h, parent=None):
if total == 0: if total == 0:

4
pygal/style.py

@ -43,6 +43,7 @@ class Style(object):
label_font_family = None label_font_family = None
major_label_font_family = None major_label_font_family = None
value_font_family = None value_font_family = None
value_label_font_family = None
tooltip_font_family = None tooltip_font_family = None
title_font_family = None title_font_family = None
legend_font_family = None legend_font_family = None
@ -50,7 +51,8 @@ class Style(object):
label_font_size = 10 label_font_size = 10
major_label_font_size = 10 major_label_font_size = 10
value_font_size = 8 value_font_size = 16
value_label_font_size = 10
tooltip_font_size = 14 tooltip_font_size = 14
title_font_size = 16 title_font_size = 16
legend_font_size = 14 legend_font_size = 14

4
pygal/svg.py

@ -239,7 +239,7 @@ class Svg(object):
def slice( def slice(
self, serie_node, node, radius, small_radius, self, serie_node, node, radius, small_radius,
angle, start_angle, center, val, i): angle, start_angle, center, val, i, metadata):
"""Draw a pie slice""" """Draw a pie slice"""
project = lambda rho, alpha: ( project = lambda rho, alpha: (
rho * sin(-alpha), rho * cos(-alpha)) rho * sin(-alpha), rho * cos(-alpha))
@ -278,7 +278,7 @@ class Svg(object):
node, val, x, y, "centered", node, val, x, y, "centered",
self.graph._x_labels and self.graph._x_labels[i][0]) self.graph._x_labels and self.graph._x_labels[i][0])
if angle >= 0.3: # 0.3 radians is about 17 degrees if angle >= 0.3: # 0.3 radians is about 17 degrees
self.graph._static_value(serie_node, val, x, y) self.graph._static_value(serie_node, val, x, y, metadata)
return rv return rv
def pre_render(self): def pre_render(self):

2
pygal/util.py

@ -232,6 +232,8 @@ def decorate(svg, node, metadata):
if not isinstance(xlink, dict): if not isinstance(xlink, dict):
xlink = {'href': xlink, 'target': '_blank'} xlink = {'href': xlink, 'target': '_blank'}
node = svg.node(node, 'a', **xlink) node = svg.node(node, 'a', **xlink)
svg.node(node, 'desc', class_='xlink').text = to_unicode(
xlink.get('href'))
if 'color' in metadata: if 'color' in metadata:
color = metadata.pop('color') color = metadata.pop('color')

Loading…
Cancel
Save