''' Copyright 2005 Gary Perez http://neurobashing.com/monkey/ Version: 0.1 December 8, 2005 Original CSS code for the sparklines from "marciusmr" at: http://datawisualizing.blogspot.com/ ''' def makePlus(x, y, divid): lines = [] for combo in ((x, y), (x, y-1), (x, y+1), (x-1, y), (x+1, y)): lines.append('
' % (divid, combo[0], combo[-1])) return lines def makeSparkline(datalist): # Assume datalist is a list of values (ints or floats) in # chronological order. max_value = max(datalist) min_value = min(datalist) # Create a working copy of the data list. working = datalist[:] # Find latest occurrence of max & min values as indices of # original data list. working.reverse() max_value_index = len(working) - working.index(max(working)) - 1 min_value_index = len(working) - working.index(min(working)) - 1 # Reduce the range of data to a range of zero to ten. # This will be used later to obtain the sparkline dot height. data_range = max_value - min_value divider = data_range/10.0 x, htmllines = 1, [] for num in datalist: x_value = x # Higher values get lower 'top' args & vice versa y_value = int(abs(10.0-round((num-min_value) / divider))) if x-1 == max_value_index: htmllines += makePlus(x_value, y_value, 'max') elif x-1 == min_value_index: htmllines += makePlus(x_value, y_value, 'min') else: if x == len(datalist): htmllines += makePlus(x_value, y_value, 'current') else: # Standard points this_point = '
' % (x_value, y_value) htmllines.append(this_point) x += 1 head = '
' % (x+5) htmllines.insert(0, head) # Find the height of the average line avg_value = sum(datalist) / len(datalist) avg_line_height = abs(10 - int(round((avg_value - min_value) / divider))) avg = '
' % (avg_line_height, x) htmllines.insert(1, avg) htmllines.append('
') return htmllines