Attachment 'BarChart-1.5.4-2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - BarChart Parser
   4 
   5     This parser is used to create bar charts based on CSS code from http://concepts.waetech.com/bargraph/.
   6     
   7     Syntax:
   8         {{{#!BarChart
   9            -title title
  10            -loadcss
  11            name=value
  12            ...
  13         }}}
  14         
  15         {{{#!BarChart
  16            name=value
  17            ...
  18         }}}
  19 
  20     Parameters:
  21         
  22         -title title: the title of the bar chart
  23         -loadcss: the css code needs to be loaded ones with the forst bar chart
  24         name=value: name is used as description. The value is used to set the length of the bar. The sum of all values
  25                     is 100%. 
  26     Procedure:                 
  27         Please remove the version number. 
  28 
  29     Examples:
  30        {{{#!BarChart
  31        -loadcss
  32        yellow=10
  33        blue=20
  34        black=50
  35        white = 90
  36        orange=10
  37        green=20
  38        }}}
  39        
  40        {{{#!BarChart
  41        -title Colors
  42        yellow=10
  43        blue=20
  44        black=50
  45        white = 90
  46        orange=10
  47        green=20
  48        }}}   
  49 
  50     @copyright:
  51         Reimar Bauer:
  52             2006-07-28 intitial version for MoinMoin 1.5.4-1
  53             2006-07-29 1.5.4-2 PEP 8 and      
  54                        parameter -title and -loadcss added
  55     
  56     @license: GNU GPL, see COPYING for details. 
  57     
  58     For the css part I have send a request to joshua@waetech.com
  59 """
  60 
  61 
  62 
  63 def barcss():
  64     return """
  65     <!--
  66     code from
  67     http://concepts.waetech.com/bargraph/
  68     !-->
  69 
  70     <style type="text/css">
  71       .graph {
  72         background-color: #C8C8C8;
  73         border: solid 1px black;
  74       }
  75 
  76       .graph td {
  77         font-family: verdana, arial, sans serif;
  78       }
  79 
  80       .graph thead th {
  81         border-bottom: double 3px black;
  82         font-family: verdana, arial, sans serif;
  83         padding: 1em;
  84       }
  85 
  86       .graph tfoot td {
  87         border-top: solid 1px #999999;
  88         font-size: x-small;
  89         text-align: center;
  90         padding: 0.5em;
  91         color: #666666;
  92       }
  93 
  94       .bar {
  95         background-color: white;
  96         text-align: right;
  97         border-left: solid 1px black;
  98         padding-right: 0.5em;
  99         width: 700px;
 100       }
 101 
 102       .bar div {
 103         border-top: solid 2px #0077DD;
 104         background-color: #004080;
 105         border-bottom: solid 2px #002266;
 106         text-align: right;
 107         color: white;
 108         float: left;
 109         padding-top: 0;
 110         height: 1em;
 111       }
 112       body {
 113         background-color: white;
 114       }
 115     </style>"""
 116 
 117 class Parser:
 118 
 119     def __init__(self, raw, request, **kw):
 120         self.raw = raw
 121         self.request = request
 122 
 123     def format(self, formatter):
 124         raw = self.raw
 125         raw = raw.split('\n')
 126         request = self.request
 127 
 128         title = ''
 129 
 130         html = """
 131         <table width='730' class='graph' cellspacing='6' cellpadding='0'>
 132         <thead>"""
 133 
 134         sum = 0
 135         n = 0
 136         for arg in raw:
 137             if '=' in arg:
 138                 name, value = arg.split('=', 1)
 139                 sum += float(value)
 140                 n += 1
 141             elif arg.startswith('-loadcss'):
 142                 request.write(barcss())
 143             elif arg.startswith('-title'):
 144                 cmd, title = arg.split(' ', 1)
 145                 html += "<tr><th colspan='3'>%(title)s</th></tr>" % {"title": title}
 146 
 147         html += "</thead><tbody>"
 148 
 149         for arg in raw:
 150             if '=' in arg:
 151                 name, value = arg.split('=', 1)
 152                 line = """
 153                 <tr>
 154                 <td width='100'>%(name)s</td><td width='700' class='bar'><div style='width: %(value)s'></div>%(value)s</td></tr>""" % {
 155                 "name": name,
 156                 "value": str(round((float(value)/sum) * 100, 1))+'%',
 157                 }
 158 
 159                 html += line
 160 
 161         html += "</tbody></table>"
 162 
 163         request.write(html)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-07-28 20:56:56, 3.6 KB) [[attachment:BarChart-1.5.4-1.py]]
  • [get | view] (2006-07-29 15:36:16, 3.8 KB) [[attachment:BarChart-1.5.4-2.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.