Skip to content

Commit e1d0905

Browse files
authored
Merge pull request #1 from fgiuba/master
Add `value` attribute to <td> elements for CSS conditional formatting
2 parents 5e25c98 + 86fdbb7 commit e1d0905

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

docs/documentation/table.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,21 @@ Transposed
7979
line_chart.value_formatter = lambda x: '%.2f%%' % x if x is not None else '∅'
8080
line_chart.render_table(style=True, total=True, transpose=True)
8181

82+
83+
Value Attribute (for CSS Conditional Formatting)
84+
------------------------------------------------
85+
86+
Add to each <td> element the `value` attribute with the same content of the element itself.
87+
This can be useful for applying CSS conditional formatting rules.
88+
89+
.. pygal-table-code::
90+
91+
line_chart = pygal.Bar()
92+
line_chart.title = 'Browser usage evolution (in %)'
93+
line_chart.x_labels = map(str, range(2002, 2013))
94+
line_chart.add('Firefox', [None, None, 0, 16.6, 25, 31, 36.4, 45.5, 46.3, 42.8, 37.1])
95+
line_chart.add('Chrome', [None, None, None, None, None, None, 0, 3.9, 10.8, 23.8, 35.3])
96+
line_chart.add('IE', [85.8, 84.6, 84.7, 74.5, 66, 58.6, 54.7, 44.8, 36.2, 26.6, 20.1])
97+
line_chart.add('Others', [14.2, 15.4, 15.3, 8.9, 9, 10.4, 8.9, 5.8, 6.7, 6.8, 7.5])
98+
line_chart.value_formatter = lambda x: '%.2f%%' % x if x is not None else '∅'
99+
line_chart.render_table(style=True, total=True, value_attribute=True)

pygal/table.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
from pygal.util import template
2929

3030

31+
def _default_value_attribute_fn(value):
32+
return value
33+
34+
3135
class HTML(object):
3236
"""Lower case adapter of lxml builder"""
3337

@@ -45,14 +49,21 @@ def __init__(self, chart):
4549
"""Init the table"""
4650
self.chart = chart
4751

48-
def render(self, total=False, transpose=False, style=False):
52+
def render(self, total=False, transpose=False, style=False, value_attribute=None):
4953
"""Render the HTMTL table of the chart.
5054
5155
`total` can be specified to include data sums
5256
`transpose` make labels becomes columns
5357
`style` include scoped style for the table
58+
`value_attribute` replicate element value into `value` attribute (<td> elements)
5459
5560
"""
61+
if value_attribute is True:
62+
value_attribute_fn = _default_value_attribute_fn
63+
elif callable(value_attribute):
64+
value_attribute_fn = value_attribute
65+
else:
66+
value_attribute_fn = None
5667
self.chart.setup()
5768
ln = self.chart._len
5869
html = HTML()
@@ -138,13 +149,19 @@ def render(self, total=False, transpose=False, style=False):
138149
if tbody:
139150
parts.append(
140151
html.tbody(
141-
*[html.tr(*[html.td(_(col)) for col in r]) for r in tbody]
152+
*[html.tr(*(
153+
[html.td(_(col), value=_(value_attribute_fn(col))) for col in r]
154+
if value_attribute_fn else [html.td(_(col)) for col in r]
155+
)) for r in tbody]
142156
)
143157
)
144158
if tfoot:
145159
parts.append(
146160
html.tfoot(
147-
*[html.tr(*[html.th(_(col)) for col in r]) for r in tfoot]
161+
*[html.tr(*(
162+
[html.th(_(col), value=_(value_attribute_fn(col))) for col in r]
163+
if value_attribute_fn else [html.td(_(col)) for col in r]
164+
)) for r in tfoot]
148165
)
149166
)
150167

0 commit comments

Comments
 (0)