Add computeRatio method to display. Enable it in browsers display plugin

This commit is contained in:
Gregory Soutade 2015-01-13 18:55:46 +01:00
parent 1d9bf71b4b
commit 616b0d8052
2 changed files with 30 additions and 0 deletions

View File

@ -102,6 +102,21 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
self.rows.append(listToStr(row))
self.rows_cssclasses.append([u''] * len(row))
def insertCol(self, col_number, col_title='', col_css_class=''):
self.cols.insert(col_number, col_title)
for r in self.rows:
r.insert(col_number, u'')
for r in self.rows_cssclasses:
v = r[0]
# If all cells have the same CSS class, set it
for cur_value in r:
if v != cur_value:
v = None
break
v = v or u''
r.insert(col_number, v)
self.cols_cssclasses.insert(col_number, col_css_class)
def getNbRows(self):
return len(self.rows)
@ -160,6 +175,20 @@ class DisplayHTMLBlockTable(DisplayHTMLBlock):
self.cols_cssclasses = listToStr(values)
def computeRatio(self, column, column_insertion=None):
if column_insertion is None:
column_insertion = column+1
total = 0
for r in self.rows:
if r[column]:
total += int(r[column])
self.insertCol(column_insertion, self.iwla._('Ratio'), u'iwla_hit')
for (index, r) in enumerate(self.rows):
val = r[column] and int(r[column]) or 0
self.setCellValue(index, column_insertion, '%.1f%%' % (float(val*100)/float(total)))
def _buildHTML(self):
style = u''
if self.table_css: style = u' class="%s"' % (self.table_css)

View File

@ -128,4 +128,5 @@ class IWLADisplayBrowsers(IPlugin):
total_browsers[1] = self.iwla._(u'Others')
table.appendRow(total_browsers)
table.setCellCSSClass(table.getNbRows()-1, 0, 'iwla_others')
table.computeRatio(2)
index.appendBlock(table)