def __init__(self, chart_type, maxY): self.chart_type = chart_type self.max_y = maxY if chart_type == "line": self.chart = SimpleLineChart(self.width, self.height, y_range=[0, self.max_y]) elif chart_type == "bar": self.chart = StackedVerticalBarChart(self.width, self.height, y_range=[0, self.max_y]) self.chart.set_bar_width(10) self.chart.set_colours(['00ff00', 'ff0000'])
class FGPyGoogleChart: Name = "PyGoogleChart" chart = None chart_type = "" width = 500 height = 200 max_y = 100 filepath = None filename = "user.linechart.png" def __init__(self, chart_type, maxY): self.chart_type = chart_type self.max_y = maxY if chart_type == "line": self.chart = SimpleLineChart(self.width, self.height, y_range=[0, self.max_y]) elif chart_type == "bar": self.chart = StackedVerticalBarChart(self.width, self.height, y_range=[0, self.max_y]) self.chart.set_bar_width(10) self.chart.set_colours(['00ff00', 'ff0000']) def set_option(self, key, value): ''' Passing a function name as an argument in a function ''' self.chart.key(value) def set_data(self, value): self.chart.add_data(value) def set_yaxis(self, value): self.chart.set_axis_labels(Axis.LEFT, value) def set_xaxis(self, value): self.chart.set_axis_labels(Axis.BOTTOM, value) def set_output_path(self, directory): FGUtility.ensure_dir(directory + "/" + self.filename) self.filepath = directory def set_filename(self, filename): self.filename = filename def display(self): if self.filepath: self.chart.download(self.filepath + "/" + self.filename) else: self.chart.download(self.filename) def filledLineExample(self): # Set the vertical range from 0 to 50 max_y = 50 chart = SimpleLineChart(200, 125, y_range=[0, max_y]) # First value is the highest Y value. Two of them are needed to be # plottable. chart.add_data([max_y] * 2) # 3 sets of real data chart.add_data([28, 30, 31, 33, 35, 36, 42, 48, 43, 37, 32, 24, 28]) chart.add_data([16, 18, 18, 21, 23, 23, 29, 36, 31, 25, 20, 12, 17]) chart.add_data([7, 9, 9, 12, 14, 14, 20, 27, 21, 15, 10, 3, 7]) # Last value is the lowest in the Y axis. chart.add_data([0] * 2) # Black lines chart.set_colours(['000000'] * 5) # Filled colours # from the top to the first real data chart.add_fill_range('76A4FB', 0, 1) # Between the 3 data values chart.add_fill_range('224499', 1, 2) chart.add_fill_range('FF0000', 2, 3) # from the last real data to the chart.add_fill_range('80C65A', 3, 4) # Some axis data chart.set_axis_labels(Axis.LEFT, ['', max_y / 2, max_y]) chart.set_axis_labels(Axis.BOTTOM, ['Sep', 'Oct', 'Nov', 'Dec']) chart.download('line-fill.png')
def stacked_vertical(): chart = StackedVerticalBarChart(settings['width'], settings['height'], y_range=(0, 35)) chart.set_bar_width(10) chart.set_colours(['00ff00', 'ff0000']) chart.add_data([1,2,3,4,5]) chart.add_data([1,4,9,16,25]) chart.download('bar-vertical-stacked.png')
scraperwiki.sqlite.attach(sourcescraper, "src") sdata = scraperwiki.sqlite.execute("select Year, Dates, State, Location, Mortality from src.swdata WHERE Mortality > 1000") rows = sdata.get("data") print rows # for row in rows: # pm10val = int(rows[4]) # print pm10val pm10val = int(rows[0][4]) colour = 'ad0000' # Draw the bar chart chart = StackedVerticalBarChart(150, 200, y_range=(0, 4000)) chart.set_colours([colour, 'ffffff', 'ffffff']) chart.set_bar_width(40) chart.add_data([0,pm10val,0]) chart.add_data([35,1,1]) chart.add_data([1,1,1]) chart.set_axis_labels(Axis.LEFT,[0,25,50]) chart.add_marker(1,0,'tLimit (35)','ad0000',11) chart.add_marker(0,1,'fSo far (%s)' % (pm10val),colour,11) chart.add_marker(0,2,'tSafe (0)','45bb00',11) #print '<img src="%s"></img>' % chart.get_url() import scraperwiki.sqlite from pygooglechart import StackedVerticalBarChart, Axis # Get data sourcescraper = 'annual_animal_deaths'
def makeStackedBarChart(barchartData, barChartColors, max_y, maxCol): from pygooglechart import Axis from pygooglechart import StackedVerticalBarChart #depending on the maxCol value set the max value of the Y axis so that the graphics is OK even for the biggest column maxY = max_y left_axis = range(0, maxY + 1, max_y/5) if maxCol < 50: maxY = 50 left_axis = range(0, maxY + 1, 10) elif maxCol > 50 and maxCol <= 100: maxY = 100 left_axis = range(0, maxY + 1, 20) elif maxCol > 100 and maxCol <= 200: maxY = 200 left_axis = range(0, maxY + 1, 40) elif maxCol > 200 and maxCol <= 300: maxY = 300 left_axis = range(0, maxY + 1, 60) elif maxCol > 300 and maxCol <= 400: maxY = 400 left_axis = range(0, maxY + 1, 80) elif maxCol > 400 and maxCol <= 500: maxY = 500 left_axis = range(0, maxY + 1, 100) elif maxCol > 500 and maxCol <= 600: maxY = 600 left_axis = range(0, maxY + 1, 120) elif maxCol > 600 and maxCol <= 900: maxY = 900 left_axis = range(0, maxY + 1, 180) elif maxCol > 900 and maxCol <= 3000: maxY = 3000 left_axis = range(0, maxY + 1, 600) elif maxCol > 3000 and maxCol <= 6000: maxY = 6000 left_axis = range(0, maxY + 1, 1200) elif maxCol > 6000 and maxCol <= 9000: maxY = 9000 left_axis = range(0, maxY + 1, 1800) elif maxCol > 9000 and maxCol <= 12000: maxY = 12000 left_axis = range(0, maxY + 1, 2400) elif maxCol > 12000 and maxCol <= 15000: maxY = 15000 left_axis = range(0, maxY + 1, 3000) elif maxCol > 15000 and maxCol <= 18000: maxY = 18000 left_axis = range(0, maxY + 1, 3600) elif maxCol > 18000 and maxCol <= 30000: maxY = 30000 left_axis = range(0, maxY + 1, 6000) elif maxCol > 30000 and maxCol <= 39000: maxY = 39000 left_axis = range(0, maxY + 1, 7800) elif maxCol > 39000 and maxCol <= 50000: maxY = 50000 left_axis = range(0, maxY + 1, 10000) elif maxCol > 50000 and maxCol <= 80000: maxY = 80000 left_axis = range(0, maxY + 1, 16000) elif maxCol > 80000 and maxCol <= 100000: maxY = 100000 left_axis = range(0, maxY + 1, 20000) elif maxCol > 100000 and maxCol <= 120000: maxY = 120000 left_axis = range(0, maxY + 1, 24000) elif maxCol > 120000 and maxCol <= 150000: maxY = 150000 left_axis = range(0, maxY + 1, 30000) elif maxCol > 150000 and maxCol <= 180000: maxY = 180000 left_axis = range(0, maxY + 1, 36000) elif maxCol > 180000 and maxCol <= 210000: maxY = 210000 left_axis = range(0, maxY + 1, 42000) else: maxY = 250000 left_axis = range(0, maxY + 1, 50000) barchart = None #depending on the number of periods set the width of the chart and width of the bars in the chart if numberPeriods < 20: barchart = StackedVerticalBarChart(50*numberPeriods, 150, y_range=(0, maxY)) barchart.set_bar_width(23) else: barchart = StackedVerticalBarChart(1000, 150, y_range=(0, maxY)) barchart.set_bar_width(20) #set the data for data in barchartData: barchart.add_data(data) #get the labes of the X axis - these are the time periods bottomAxisLabels = getBottomAxisLabels() #make 5 horizontal lines crossing the Y axis barchart.set_grid(0, 20) #set the colors of the time charts barchart.set_colours(barChartColors) #at (0,0) do not set a label, set the X and Y axis left_axis[0] = '' barchart.set_axis_labels(Axis.LEFT, left_axis) barchart.set_axis_labels(Axis.BOTTOM, bottomAxisLabels) return (barchart, maxY, barChartColors)
def makechart(): global name, width, height, bar_width totaltime = [0.0]*len(categories) totalmeetingtime = [0.0]*len(categories) allcategories = [""]*len(categories) i = 0 max = 0 for x in categories: mtime = 0.0 allcategories[i] = x[0:9] if categories[x] > max: max = categories[x] if x in meetingcategories: mtime = meetingcategories[x] totalmeetingtime[i] = mtime totaltime[i] = categories[x] - mtime i+=1 max = int(math.ceil(max / 100.0) * 100) part = int(max/4) bar_width = int(width/(len(categories)+4.5)) #general chart properties chart = StackedVerticalBarChart(width,height,y_range=(0, max)) chart.set_title('Working Hours in ' + get_month(name)) chart.set_legend(['Tasks', 'Meetings']) chart.set_bar_width(bar_width) #left label leftlabel = chart.set_axis_labels(Axis.LEFT, [0, part, part*2, part*3, max]) chart.set_axis_style(leftlabel, '202020', font_size=10, alignment=0) #chart.set_axis_positions(leftlabel, [50]) #bottom label bottomlabel = chart.set_axis_labels(Axis.BOTTOM, allcategories) chart.set_axis_style(bottomlabel, '202020', font_size=10, alignment=0) #colors and data chart.set_colours(['00ff00', 'ff0000']) chart.add_data(totaltime) chart.add_data(totalmeetingtime) #get it chart.download(name+'.png')
def graph_test_vector(candidates, data, directory, title, y_axis): if len(candidates) != len(data): raise AttributeError('Length of candidates and data does not match') chart = StackedVerticalBarChart(700, 428, auto_scale=False, custom_scaling=True) chart.set_bar_width(41) chart.set_colours(('3366CC', '000000')) chart.add_data(data) chart.set_axis_range('y', 0, max(data) + 0.1 * max(data)) chart.set_axis_labels('x', candidates) x_title_index = chart.set_axis_labels('x', ['Candidate']) y_title_index = chart.set_axis_labels('y', [y_axis]) chart.set_axis_positions(x_title_index, [50]) chart.set_axis_positions(y_title_index, [50]) chart.set_title(title) # (dataset, all bars, formating, colour, width) chart.add_marker(0, -1, 'N', '000000', 11) chart.download(os.path.join(directory, '%s.png' % title.replace(' ','')))
def graph_multiple_choice(q): #question = get_object_or_404(Question, pk=q.pk) question = Question.objects.get(pk=q.pk) # collect answers to this question answers = Answer.objects.filter(question=question).order_by('choice') # old code for handling any number of choices # choices = { " " : 0 } # choices = choices.fromkeys(xrange(len(answers)), 0) # hardcode of four choices since ui limits to four choices = {1: 0, 2: 0, 3: 0, 4: 0} # grab the parsed entries for this question entries = Entry.objects.filter(question=question,\ is_unparseable=False) # i'm assuming here that the Entry.text is the # same thing as the Answer.choice, presumably # a number for each choice 1 through n # # iterate entries and tally the choices for e in entries: if int(e.text) in choices: choices[int(e.text)] += 1 choice_counts = sortedDictValues1(choices) # collect the long, textual representation # of the answer choice for labelling the graph # along with the choice counts of each choice # for display on large graphs long_answers_big = [] long_answers_small = [] for a in answers: long_answers_small.append(a.text) long_answers_big.append(a.text + ' (' + str(choices[int(a.choice)]) + ')') for size in GRAPH_SIZES: # configure and save the graph bar = StackedVerticalBarChart(int(size), golden(int(size)),\ y_range=(0, max(choice_counts))) bar.set_colours([choice(['0091C7', '0FBBD0'])]) bar.add_data(choice_counts) bar.set_bar_width(int(int(size) / (len(choices) + 1))) if (size == GRAPH_SIZES[0]): index = bar.set_axis_labels(Axis.BOTTOM, long_answers_small) else: index = bar.set_axis_labels(Axis.BOTTOM, long_answers_big) bar.set_axis_style(index, '202020', font_size=9, alignment=0) filename = GRAPH_DIR + str(question.pk) + '-' + size + '-entries.png' bar.download(filename) print 'saved ' + filename return 'graphed entries ' + question.text
def graph_test_vector(candidates, data, directory, title, y_axis): if len(candidates) != len(data): raise AttributeError('Length of candidates and data does not match') chart = StackedVerticalBarChart(700, 428, auto_scale=False, custom_scaling=True) chart.set_bar_width(41) chart.set_colours(('3366CC', '000000')) chart.add_data(data) chart.set_axis_range('y', 0, max(data) + 0.1 * max(data)) chart.set_axis_labels('x', candidates) x_title_index = chart.set_axis_labels('x', ['Candidate']) y_title_index = chart.set_axis_labels('y', [y_axis]) chart.set_axis_positions(x_title_index, [50]) chart.set_axis_positions(y_title_index, [50]) chart.set_title(title) # (dataset, all bars, formating, colour, width) chart.add_marker(0, -1, 'N', '000000', 11) chart.download(os.path.join(directory, '%s.png' % title.replace(' ', '')))
def activities_bar (): chart_name="activities_created.png" # Create a chart object of 250x100 pixels chart = StackedVerticalBarChart(440,300,x_range=(0, 35)) # Assign the labels to the pie data labels=[] data=[] legend={} act=ActivityType.objects.all() for a in act : labels.append(a.code) legend[a.activity_type]=True # Add some data #colors = [random_color() for x in legend.keys()] chart.set_colours(['0091C7','0591C7']) chart.add_data([10,12]) chart.add_data([0,0]) chart.set_legend(legend.keys()) chart.set_bar_width(30) chart.set_axis_labels(Axis.BOTTOM,labels) chart.set_axis_labels(Axis.BOTTOM, ['','Activity Type', '']) chart.set_axis_labels(Axis.LEFT, ['', 5, 10,15,20,35]) print chart.get_url() chart.download('apps/shabaa/static/graphs/' + chart_name) return chart_name
def GetHtml(self): max = self._GetRescaledMax(self.__max) w = self.__width h = self.__height # We don't really care about StackedVerticalBarChart vs. # GroupedVerticalBarChart since we just have one data-set, but only the # stacked graph seems to respect the bar spacing option chart = StackedVerticalBarChart(w, h) # Compute bar width so that it fits in the overall graph width. bucket_width = (w - _Y_AXIS_SPACE)/len(self.__buckets) bar_width = bucket_width * 4/5 space_width = bucket_width - bar_width chart.set_bar_width(bar_width) chart.set_bar_spacing(space_width) chart.add_data(self._GetRescaledData(self.__buckets, max)) chart.set_axis_range(Axis.LEFT, 0, max) chart.set_axis_labels(Axis.BOTTOM, self._GetBucketLabels()) # We render the title in the template instead of in the chart, to give # stat collections and individual stats similar appearance t = Template( file="templates/bucket-stat.tmpl", searchList = { "id": self.id, "title": self.__title, "width": w, "height": h, "chart_url": chart.get_url() }) return unicode(t)
def stacked_vertical(): chart = StackedVerticalBarChart(settings.width, settings.height, y_range=(0, 35)) chart.set_bar_width(10) chart.set_colours(['00ff00', 'ff0000']) chart.add_data([1,2,3,4,5]) chart.add_data([1,4,9,16,25]) chart.download('bar-vertical-stacked.png')
def makechart(): global name, width, height, bar_width totaltime = [0.0] * len(categories) totalmeetingtime = [0.0] * len(categories) allcategories = [""] * len(categories) i = 0 max = 0 for x in categories: mtime = 0.0 allcategories[i] = x[0:9] if categories[x] > max: max = categories[x] if x in meetingcategories: mtime = meetingcategories[x] totalmeetingtime[i] = mtime totaltime[i] = categories[x] - mtime i += 1 max = int(math.ceil(max / 100.0) * 100) part = int(max / 4) bar_width = int(width / (len(categories) + 4.5)) #general chart properties chart = StackedVerticalBarChart(width, height, y_range=(0, max)) chart.set_title('Working Hours in ' + get_month(name)) chart.set_legend(['Tasks', 'Meetings']) chart.set_bar_width(bar_width) #left label leftlabel = chart.set_axis_labels(Axis.LEFT, [0, part, part * 2, part * 3, max]) chart.set_axis_style(leftlabel, '202020', font_size=10, alignment=0) #chart.set_axis_positions(leftlabel, [50]) #bottom label bottomlabel = chart.set_axis_labels(Axis.BOTTOM, allcategories) chart.set_axis_style(bottomlabel, '202020', font_size=10, alignment=0) #colors and data chart.set_colours(['00ff00', 'ff0000']) chart.add_data(totaltime) chart.add_data(totalmeetingtime) #get it chart.download(name + '.png')
def __bar_graphic__(data, legends, axis_labels, size, steps, type=StackedVerticalBarChart, multiline=False): if multiline: max_values = [] min_values = [] for row in data: max_values.append(max(row)) min_values.append(min(row)) max_value = max(max_values) min_value = min(min_values) else: max_value = max(data) min_value = min(data) step = ((max_value * 1.05) - (min_value * 0.95)) / steps left_axis = range(int(min_value * 0.95), int(max_value * 1.05), int(step)) left_axis[0] = '' if type == StackedHorizontalBarChart: graph = StackedHorizontalBarChart(size[0], size[1], x_range=(0, max_value * 1.05)) graph.set_axis_labels(Axis.BOTTOM, left_axis) graph.set_axis_labels(Axis.LEFT, axis_labels) elif type == StackedVerticalBarChart: graph = StackedVerticalBarChart(size[0], size[1], y_range=(0, max_value * 1.05)) graph.set_axis_labels(Axis.LEFT, left_axis) graph.set_axis_labels(Axis.BOTTOM, axis_labels) elif type == GroupedHorizontalBarChart: graph = GroupedHorizontalBarChart(size[0], size[1], x_range=(0, max_value * 1.05)) graph.set_axis_labels(Axis.BOTTOM, left_axis) graph.set_axis_labels(Axis.LEFT, axis_labels) graph.set_bar_spacing(5) elif type == GroupedVerticalBarChart: graph = GroupedVerticalBarChart(size[0], size[1], y_range=(0, max_value * 1.05)) graph.set_axis_labels(Axis.LEFT, left_axis) graph.set_axis_labels(Axis.BOTTOM, axis_labels) graph.set_bar_spacing(5) else: pass #raise exception if multiline: for fila in data: graph.add_data(fila) else: graph.add_data(data) graph.set_colours( ['FFBC13', '22A410', 'E6EC23', '2B2133', 'BD0915', '3D43BD']) graph.set_bar_width(40) graph.set_legend(legends) graph.set_legend_position('b') return graph
scraperwiki.sqlite.attach(sourcescraper, "src") sdata = scraperwiki.sqlite.execute( "select Year, Dates, State, Location, Mortality from src.swdata WHERE Mortality > 1000" ) rows = sdata.get("data") print rows # for row in rows: # pm10val = int(rows[4]) # print pm10val pm10val = int(rows[0][4]) colour = 'ad0000' # Draw the bar chart chart = StackedVerticalBarChart(150, 200, y_range=(0, 4000)) chart.set_colours([colour, 'ffffff', 'ffffff']) chart.set_bar_width(40) chart.add_data([0, pm10val, 0]) chart.add_data([35, 1, 1]) chart.add_data([1, 1, 1]) chart.set_axis_labels(Axis.LEFT, [0, 25, 50]) chart.add_marker(1, 0, 'tLimit (35)', 'ad0000', 11) chart.add_marker(0, 1, 'fSo far (%s)' % (pm10val), colour, 11) chart.add_marker(0, 2, 'tSafe (0)', '45bb00', 11) #print '<img src="%s"></img>' % chart.get_url() import scraperwiki.sqlite from pygooglechart import StackedVerticalBarChart, Axis # Get data sourcescraper = 'annual_animal_deaths'
def graph_multiple_choice(q): #question = get_object_or_404(Question, pk=q.pk) question = Question.objects.get(pk=q.pk) # collect answers to this question answers = Answer.objects.filter(question=question).order_by('choice') # old code for handling any number of choices # choices = { " " : 0 } # choices = choices.fromkeys(xrange(len(answers)), 0) # hardcode of four choices since ui limits to four choices = { 1 : 0, 2 : 0, 3 : 0, 4 : 0 } # grab the parsed entries for this question entries = Entry.objects.filter(question=question,\ is_unparseable=False) # i'm assuming here that the Entry.text is the # same thing as the Answer.choice, presumably # a number for each choice 1 through n # # iterate entries and tally the choices for e in entries: if int(e.text) in choices: choices[int(e.text)] += 1 choice_counts = sortedDictValues1(choices) # collect the long, textual representation # of the answer choice for labelling the graph # along with the choice counts of each choice # for display on large graphs long_answers_big = [] long_answers_small = [] for a in answers: long_answers_small.append(a.text) long_answers_big.append(a.text + ' (' + str(choices[int(a.choice)]) + ')' ) for size in GRAPH_SIZES: # configure and save the graph bar = StackedVerticalBarChart(int(size), golden(int(size)),\ y_range=(0, max(choice_counts))) bar.set_colours([choice(['0091C7','0FBBD0'])]) bar.add_data(choice_counts) bar.set_bar_width(int(int(size)/(len(choices)+1))) if (size == GRAPH_SIZES[0]): index = bar.set_axis_labels(Axis.BOTTOM, long_answers_small) else: index = bar.set_axis_labels(Axis.BOTTOM, long_answers_big) bar.set_axis_style(index, '202020', font_size=9, alignment=0) filename = GRAPH_DIR + str(question.pk) + '-' + size + '-entries.png' bar.download(filename) print 'saved ' + filename return 'graphed entries ' + question.text
def barGraphFeed(feed): name = feed.getAttribute("name") observations = feed.getElementsByTagName("observation") print " Feed %s has %d observations" % (name,len(observations)) data = [] for obs in observations: value = int(obs.getAttribute("value")) #print " val:%s (%s)" % (value, type(value)) data.insert(0,value/10) #data.reverse # remeber the feed is reversed print "Max Data: %s" % max(data) max_y = int(math.ceil(max(data)/100.0))*100 print "Max_y : %s" % max_y chart = StackedVerticalBarChart(180, 120, y_range=[0, max_y]) chart.set_bar_width(100.0/len(observations)) chart.set_bar_width(1.2) chart.add_data(data) lftAxisMax = max_y/100; print "lftAxisMax %s"%lftAxisMax #left_axis = range(0, lftAxisMax,(lftAxisMax/4.0)) left_axis = [] right_axis = [] for i in range(0,4+1): kw = (i*lftAxisMax/4.0) left_axis.append(kw) right_axis.append(kw*24) left_axis[0] = 'kW' # remove the first label right_axis[0] = 'kWh/d' # remove the first label chart.set_axis_labels(Axis.LEFT, left_axis) #chart.set_axis_labels(Axis.LEFT, right_axis) #chart.set_axis_labels(Axis.RIGHT, right_axis) chart.set_axis_labels(Axis.BOTTOM, ['','','','','','-18h','','','','','','-12h','','','','','','-6h','','','','','','*']) chart.set_title(name) # facebook colors chart.set_title_style('7f93bc',16) #chart.set_colours(['7f93bc']) chart.set_colours(['3b5998']) #darker blue #Colors colors=False if (colors): # Set the line colour to ... chart.set_colours(['FFFFFF']) # 0 here is the axis index ? 0 works for now chart.set_title_style('FFFFFF',16) chart.set_axis_style(0,'FFFFFF') chart.set_axis_style(1,'FFFFFF') chart.fill_linear_gradient(Chart.BACKGROUND,90,'000000',0.9,'007700',0.1) print chart.get_url() chart.download('%s-bar.png'%name)
def GetHtml(self): max = self._GetRescaledMax(self.__max) w = self.__width h = self.__height # We don't really care about StackedVerticalBarChart vs. # GroupedVerticalBarChart since we just have one data-set, but only the # stacked graph seems to respect the bar spacing option chart = StackedVerticalBarChart(w, h) # Compute bar width so that it fits in the overall graph width. bucket_width = (w - _Y_AXIS_SPACE) / len(self.__buckets) bar_width = bucket_width * 4 / 5 space_width = bucket_width - bar_width chart.set_bar_width(bar_width) chart.set_bar_spacing(space_width) chart.add_data(self._GetRescaledData(self.__buckets, max)) chart.set_axis_range(Axis.LEFT, 0, max) chart.set_axis_labels(Axis.BOTTOM, self._GetBucketLabels()) # We render the title in the template instead of in the chart, to give # stat collections and individual stats similar appearance t = Template(file="templates/bucket-stat.tmpl", searchList={ "id": self.id, "title": self.__title, "width": w, "height": h, "chart_url": chart.get_url() }) return unicode(t)
def _bar_graph(data, legends, axis_labels, size, steps, type=StackedVerticalBarChart, multiline=False): if multiline: max_values = [] min_values = [] for row in data: max_values.append(max(row)) min_values.append(min(row)) max_value = max(max_values) min_value = min(min_values) else: max_value = max(data) min_value = min(data) #validando si hay datos para hacer grafico if max_value == 0: return None step = ((max_value * 1.05) - (min_value * 0.95)) / steps #validando en caso de el paso sea menor que uno y de cero en la conversion if step < 1: step = 1 tope = int(round(max_value * 1.05)) if tope < max_value: tope += 2 else: tope += 1 left_axis = range(int(round(min_value * 0.95)), tope, int(step)) left_axis[0] = '' if type == StackedHorizontalBarChart: graph = StackedHorizontalBarChart(size[0], size[1], x_range=(0, max_value * 1.05)) graph.set_axis_labels(Axis.BOTTOM, left_axis) if axis_labels: graph.set_axis_labels(Axis.LEFT, axis_labels) elif type == StackedVerticalBarChart: graph = StackedVerticalBarChart(size[0], size[1], y_range=(0, max_value * 1.05)) graph.set_axis_labels(Axis.LEFT, left_axis) if axis_labels: graph.set_axis_labels(Axis.BOTTOM, axis_labels) elif type == GroupedHorizontalBarChart: graph = GroupedHorizontalBarChart(size[0], size[1], x_range=(0, max_value * 1.05)) graph.set_axis_labels(Axis.BOTTOM, left_axis) if axis_labels: graph.set_axis_labels(Axis.LEFT, axis_labels) graph.set_bar_spacing(5) elif type == GroupedVerticalBarChart: graph = GroupedVerticalBarChart(size[0], size[1], y_range=(0, max_value * 1.05)) graph.set_axis_labels(Axis.LEFT, left_axis) if axis_labels: graph.set_axis_labels(Axis.BOTTOM, axis_labels) graph.set_bar_spacing(5) else: pass #raise exception if multiline: for fila in data: graph.add_data(fila) else: graph.add_data(data) graph.set_colours( ['FFBC13', '22A410', 'E6EC23', '2B2133', 'BD0915', '3D43BD']) graph.set_bar_width(44) graph.set_legend(legends) graph.set_legend_position('b') return graph
sourcescraper = "marylebone_air_monitoring_station_values" scraperwiki.sqlite.attach(sourcescraper, "src") sdata = scraperwiki.sqlite.execute("select td from src.swdata WHERE no = 4") rows = sdata.get("data") pm10val = int(rows[0][0]) # Choose colour based on numbers if 0 < pm10val < 20: colour = "00ff00" elif 20 < pm10val < 30: colour = "f07f00" else: colour = "ad0000" # Draw the bar chart chart = StackedVerticalBarChart(150, 200, y_range=(0, 50)) chart.set_colours([colour, "ffffff", "ffffff"]) chart.set_bar_width(40) chart.add_data([0, pm10val, 0]) chart.add_data([35, 1, 1]) chart.add_data([1, 1, 1]) chart.set_axis_labels(Axis.LEFT, [0, 25, 50]) chart.add_marker(1, 0, "tLimit (35)", "ad0000", 11) chart.add_marker(0, 1, "fSo far (%s)" % (pm10val), colour, 11) chart.add_marker(0, 2, "tSafe (0)", "45bb00", 11) print '<img src="%s"></img>' % chart.get_url() import scraperwiki.sqlite import os, cgi from pygooglechart import StackedVerticalBarChart, Axis # Get data