def initialize(self): """Initialize the chart properties. This method is overloaded by child classes. """ self.table = TableLite(cellpadding=3, cellspacing=0) self.barfiles = barfiles # color keys for low, norm and hi indications self.barcolors = ('yellow', 'blue', 'red') # numeric values which determine the boundaries between color ranges self.thresholds = (0, 1E+09) # self.bound_zero = "no"
def initialize(self): """Define StackedBarChart specific attributes. """ self.table = TableLite(cellpadding=3, cellspacing=0) self.barfiles = barfiles self.colors = ('blue', 'red', 'yellow', 'purple', 'orange', 'green') self.bound_zero = "yes"
def initialize(self): """Initialize the chart properties. This method is overloaded by child classes. """ self.table = TableLite(cellpadding=3, cellspacing=0) self.barfiles = barfiles # color keys for low, norm and hi indications self.barcolors = ("yellow", "blue", "red") # numeric values which determine the boundaries between color ranges self.thresholds = (0, 1e09) # self.bound_zero = "no"
class BarChart: """Class which takes a DataList object and creates the HTML to represent a color coded bar chart. Values to be charted are limited to non-negative values. """ title = "" zero = 0 label_shade = WHITE value_shade = CADETBLUE bar_shade = GRAY2 max_width = 400 def __init__(self, datalist=None): "datalist is an instance of class DataList" if datalist is None: self.datalist = DataList() else: self.datalist = datalist self.initialize() def initialize(self): """Initialize the chart properties. This method is overloaded by child classes. """ self.table = TableLite(cellpadding=3, cellspacing=0) self.barfiles = barfiles # color keys for low, norm and hi indications self.barcolors = ("yellow", "blue", "red") # numeric values which determine the boundaries between color ranges self.thresholds = (0, 1e09) # self.bound_zero = "no" def normalize(self): """Scale data to values between 0 and 400. Determine peak value and set scale accordingly. If the values are clustered within 30% of each other, will shift the zero point for the barchart up to exagerate the value differences. To inhibit this, set the .bound_zero attribute to "yes". """ self.datalist.sum_totals() self.average = self.datalist.mean("total") # Compute max and min low, hi = self.datalist.min("total"), self.datalist.max("total") # if data is clustered together rescale by shifting zero point if hi < (1.3 * low) and self.bound_zero != "yes" and self.zero == 0: self.zero = low * 0.8 s = "%9.2e" % self.zero # hack to round to a natural value x = string.atoi(s[-3:]) self.zero = round(self.zero, -x) self.scale = float(hi - self.zero) / self.max_width def __str__(self): """Generate HTML for the entire table and caption. """ self.normalize() # calculate the scaling of the data self.table.prepend(Caption(Bold(self.title))) # add title as caption for item in self.datalist: row = TR() # 1st cell is the text description row.append(TD(item["label"], align="left", width=70, bgcolor=self.label_shade)) # 2nd cell is the decimal sum value row.append(TD("%9.1f" % item["total"], align="right", width=70, bgcolor=self.value_shade)) # 3rd cell contains the scaled bar graphic row.append(self.make_bar_cell(item)) self.table.append(row) # add the row to the table # now tack on a legend at the bottom self.table.append( TR( TD("AVERAGE", align="left", width=70, bgcolor=self.label_shade), TD("%9.1f" % self.average, align="right", width=70, bgcolor=self.value_shade), TD(self.gen_legend(), bgcolor=self.label_shade, html_escape="OFF"), ) ) return str(self.table) def make_bar_cell(self, dict): """return a TD object containing the scaled bar """ cell = TD(bgcolor=self.bar_shade, html_escape="OFF") cell.append(self.gen_bar(dict["value"], self.color_code(dict["value"]))) return cell def color_code(self, value): """return a color string based on whether the given datum falls beyond thresholds. Works off self.thresholds=(low,hi). """ low, hi = self.thresholds if value > hi: return self.barcolors[2] #'red' elif value < low: return self.barcolors[0] #'yellow' else: return self.barcolors[1] #'blue' def pixels(self, magnitude): """returns the integer number of pixels to represent a given magnitude. """ return int((magnitude - self.zero) / self.scale) def gen_bar(self, value, color="blue"): """return Image object for the scaled bar graphic """ if value <= 0: return "" else: bar = Image(self.barfiles[color], width=self.pixels(value), height=13, alt=value) return bar def gen_legend(self): """Return an HTML string which displays the legend for the chart. """ sample = Image(self.barfiles["blue"], height=13, width=40) return "<b>^%7.1f</b> lower bound<br> SCALE: %s = %7.1f units" % (self.zero, sample, self.scale * 40)
class BarChart: """Class which takes a DataList object and creates the HTML to represent a color coded bar chart. Values to be charted are limited to non-negative values. """ title = '' zero = 0 label_shade = WHITE value_shade = CADETBLUE bar_shade = GRAY2 max_width = 400 def __init__(self, datalist=None): "datalist is an instance of class DataList" if datalist is None: self.datalist = DataList() else: self.datalist = datalist self.initialize() def initialize(self): """Initialize the chart properties. This method is overloaded by child classes. """ self.table = TableLite(cellpadding=3, cellspacing=0) self.barfiles = barfiles # color keys for low, norm and hi indications self.barcolors = ('yellow', 'blue', 'red') # numeric values which determine the boundaries between color ranges self.thresholds = (0, 1E+09) # self.bound_zero = "no" def normalize(self): """Scale data to values between 0 and 400. Determine peak value and set scale accordingly. If the values are clustered within 30% of each other, will shift the zero point for the barchart up to exagerate the value differences. To inhibit this, set the .bound_zero attribute to "yes". """ self.datalist.sum_totals() self.average = self.datalist.mean('total') # Compute max and min low, hi = self.datalist.min('total'), self.datalist.max('total') # if data is clustered together rescale by shifting zero point if hi < (1.3 * low) and self.bound_zero != "yes" and self.zero == 0: self.zero = low * 0.8 s = '%9.2e' % self.zero # hack to round to a natural value x = string.atoi(s[-3:]) self.zero = round(self.zero, -x) self.scale = float(hi - self.zero) / self.max_width def __str__(self): """Generate HTML for the entire table and caption. """ self.normalize() #calculate the scaling of the data self.table.prepend(Caption(Bold(self.title))) #add title as caption for item in self.datalist: row = TR() # 1st cell is the text description row.append( TD(item['label'], align='left', width=70, bgcolor=self.label_shade)) # 2nd cell is the decimal sum value row.append( TD("%9.1f" % item['total'], align='right', width=70, bgcolor=self.value_shade)) # 3rd cell contains the scaled bar graphic row.append(self.make_bar_cell(item)) self.table.append(row) # add the row to the table # now tack on a legend at the bottom self.table.append( TR( TD('AVERAGE', align='left', width=70, bgcolor=self.label_shade), TD("%9.1f" % self.average, align='right', width=70, bgcolor=self.value_shade), TD(self.gen_legend(), bgcolor=self.label_shade, html_escape="OFF"))) return str(self.table) def make_bar_cell(self, dict): """return a TD object containing the scaled bar """ cell = TD(bgcolor=self.bar_shade, html_escape="OFF") cell.append(self.gen_bar(dict['value'], self.color_code(dict['value']))) return cell def color_code(self, value): """return a color string based on whether the given datum falls beyond thresholds. Works off self.thresholds=(low,hi). """ low, hi = self.thresholds if value > hi: return self.barcolors[2] #'red' elif value < low: return self.barcolors[0] #'yellow' else: return self.barcolors[1] #'blue' def pixels(self, magnitude): """returns the integer number of pixels to represent a given magnitude. """ return int((magnitude - self.zero) / self.scale) def gen_bar(self, value, color='blue'): """return Image object for the scaled bar graphic """ if value <= 0: return "" else: bar = Image(self.barfiles[color], width=self.pixels(value), height=13, alt=value) return bar def gen_legend(self): """Return an HTML string which displays the legend for the chart. """ sample = Image(self.barfiles['blue'], height=13, width=40) return '<b>^%7.1f</b> lower bound<br> SCALE: %s = %7.1f units' % \ (self.zero, sample, self.scale * 40)