def create_graph(self, particle_data, time_data): # Set the vertical range from 0 to 2 max_y = 2 # Chart size of 500x500 pixels and specifying the range for the Y axis chart = SimpleLineChart(500, 500, y_range=[0, max_y]) # Add the chart data data = particle_data chart.add_data(data) # Set the line color blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels left_axis = ['0.25', '0.5', '0.75', '1'] left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) left_axis2 = "Proton Flux"[::-1] chart.set_axis_labels(Axis.LEFT, left_axis2) # X axis labels chart.set_axis_labels(Axis.BOTTOM, time_data[0:3][::-1]) chart.set_axis_labels(Axis.BOTTOM, "Time") chart.download('chart.png') return True
def velocity_chart(self): graph_width = 600 graph_height = 300 x_max = self.total_iterations y_max = max(max(self.estimate_stories_data()), max(self.estimate_tasks_data()), max(self.work_data())) chart = SimpleLineChart(graph_width, graph_height, x_range=(1, x_max + 1), y_range=(0, y_max + 1)) chart.add_data(self.estimate_stories_data()) chart.add_data(self.estimate_tasks_data()) chart.add_data(self.work_data()) chart.set_grid(0, 100.0 / y_max + 1, 5, 5) chart.set_colours(['FF0000', '00FF00', '0000FF']) chart.set_legend([_('rough story estimates'), _('task estimates'), _('worked')]) chart.set_legend_position('b') chart.set_axis_labels(Axis.LEFT, ['', '', _('days')]) chart.set_axis_labels(Axis.BOTTOM, range(1, x_max + 1)) chart.set_axis_range(Axis.LEFT, 0, y_max + 1) return chart.get_url(data_class=ExtendedData)
def velocity_chart(self): graph_width = 600 graph_height = 300 x_max = self.total_iterations y_max = max(max(self.estimate_stories_data()), max(self.estimate_tasks_data()), max(self.work_data())) chart = SimpleLineChart(graph_width, graph_height, x_range=(1, x_max + 1), y_range=(0, y_max + 1)) chart.add_data(self.estimate_stories_data()) chart.add_data(self.estimate_tasks_data()) chart.add_data(self.work_data()) chart.set_grid(0, 100.0 / y_max + 1, 5, 5) chart.set_colours(['FF0000', '00FF00', '0000FF']) chart.set_legend( [_('rough story estimates'), _('task estimates'), _('worked')]) chart.set_legend_position('b') chart.set_axis_labels(Axis.LEFT, ['', '', _('days')]) chart.set_axis_labels(Axis.BOTTOM, list(range(1, x_max + 1))) chart.set_axis_range(Axis.LEFT, 0, y_max + 1) return chart.get_url(data_class=ExtendedData)
def getLineGraph(data,line1color,line2color): # Set the vertical range based on data values tweets = [d[0] for d in data] retweets = [d[1] for d in data] ratio = [str(int(d[1]/d[0]*100))+'%' for d in data] hours = [d[2] for d in data] mx = max(tweets) mn = min(retweets) buffer = (mx - mn)/100 # average retweets have consitently been less min_y = mn - buffer max_y = mx + buffer # Chart size of 750x400 pixels and specifying the range for the Y axis chart = SimpleLineChart(750, 400, y_range=[min_y, max_y]) chart.set_legend(['Tweets','Retweets']) chart.set_legend_position('t') # Add the chart data chart.add_data(tweets) chart.add_data(retweets) # Set the line colour to blue chart.set_colours([line1color,line2color]) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) #set left axis to values range left_axis = range(min_y, max_y, (max_y-min_y)/10) chart.set_axis_labels(Axis.BOTTOM, hours) chart.set_axis_labels(Axis.BOTTOM, ratio) chart.set_axis_labels(Axis.LEFT, left_axis) return chart.get_url()
class DiceChart: chart = None def __init__(self, data, iter=0, width=300, height=300): self.chart = SimpleLineChart(width, height, y_range=(0, 10)) legend = [] colors = ["cc0000", "00cc00", "0000cc", "990000", "009900", "000099", "0099ff", "FF9900", "9900ff", "ff0099"] title = "die rolls per objective" if iter > 0: title = title + " (%s samples)" % iter for i in data.keys(): self.chart.add_data(data[i]) legend.append(str(i)) logging.debug(legend) logging.debug(colors) self.chart.set_colours(colors) self.chart.set_legend(legend) grid_x_amount = 100 / (len(data[i]) - 1) self.chart.set_grid(grid_x_amount, 10, 5, 5) left_axis = range(0, 11, 1) left_axis[0] = "" self.chart.set_axis_labels(Axis.LEFT, left_axis) bottom_len = len(data[i]) + 2 bottom_axis = range(2, bottom_len, 1) self.chart.set_axis_labels(Axis.BOTTOM, bottom_axis) self.chart.set_title(title) def download(self, name="dicechart.png"): self.chart.download(name)
def simpleChart2(bottom_labels, data): # round min and max to nearest integers datamin = int(min(data) - 0.5) datamax = int(max(data) + 0.5) chart = SimpleLineChart(200, 125, y_range=[datamin, datamax]) chart.add_data(data) left_axis = ['min', 0, 'max'] chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, bottom_labels) chart.set_axis_labels(Axis.BOTTOM, ['X Axis']) # second label below first one chart.set_axis_positions( 2, [50.0]) # position, left is 0., right is 100., 50. is center # Set the line colour chart.set_colours(['0000FF']) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # Set vertical stripes stripes = ['CCCCCC', 0.2, 'FFFFFF', 0.2] chart.fill_linear_stripes(Chart.CHART, 0, *stripes) return chart
def simpleChart3(bottom_labels, data1, data2): datamin, datamax = min(min(data1, data2)), max(max(data1, data2)) datamin = int(datamin - 0.5) datamax = int(datamax + 0.5) chart = SimpleLineChart(200, 125, y_range=[datamin, datamax]) chart.add_data(data1) chart.add_data(data2) left_axis = [datamin, 0, datamax] left_axis[0] = '' # no label at 0 chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, bottom_labels) chart.set_axis_labels(Axis.BOTTOM, ['X Axis']) # second label below first one chart.set_axis_positions( 2, [50.0]) # position, left is 0., right is 100., 50. is center # Set the line colour chart.set_colours(['0000FF', 'FF0000']) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # Set vertical stripes stripes = ['CCCCCC', 0.2, 'FFFFFF', 0.2] chart.fill_linear_stripes(Chart.CHART, 0, *stripes) return chart
def _partyline(self, party_results): if self.request.GET.get('percentages') == 'true': key = 'percentage' else: key = 'count' maxcount = 0 allcounts = [] granularity = self.request.GET.get('granularity') months = [] for party, results in party_results.iteritems(): counts = [x.get(key) for x in results['results']] allcounts.append(counts) if max(counts) > maxcount: maxcount = max(counts) if granularity == 'month': months = [x['month'] for x in results['results']] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [int((x / float(len(months))) * 100) for x in january_indexes] #times = [x.get(granularity) for x in results['results']] width = int(self.request.GET.get('width', 575)) height = int(self.request.GET.get('height', 318)) chart = SimpleLineChart(width, height, y_range=(0, max(counts))) chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_grid(0, 50, 2, 5) # Set gridlines if granularity == 'month': index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) if key == 'percentage': label = '%.4f' % maxcount else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [label, ]) chart.set_axis_positions(index, [100, ]) for n, counts in enumerate(allcounts): chart.add_data(counts) chart.set_line_style(n, thickness=2) # Set line thickness colors = {'R': 'bb3110', 'D': '295e72', } chart_colors = [] chart_legend = [] for k in party_results.keys(): chart_colors.append(colors.get(k, '000000')) chart_legend.append(k) chart.legend_position = 'b' chart.set_colours(chart_colors) if self.request.GET.get('legend', 'true') != 'false': chart.set_legend(chart_legend) return chart.get_url()
def simpleChart3(bottom_labels, data1, data2): datamin, datamax = min(min(data1, data2)), max(max(data1, data2)) datamin = int(datamin - 0.5) datamax = int(datamax + 0.5) chart = SimpleLineChart(200, 125, y_range=[datamin, datamax]) chart.add_data(data1) chart.add_data(data2) left_axis = [datamin,0,datamax] left_axis[0] = '' # no label at 0 chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, bottom_labels) chart.set_axis_labels(Axis.BOTTOM, ['X Axis']) # second label below first one chart.set_axis_positions(2, [50.0]) # position, left is 0., right is 100., 50. is center # Set the line colour chart.set_colours(['0000FF', 'FF0000']) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # Set vertical stripes stripes = ['CCCCCC', 0.2, 'FFFFFF', 0.2] chart.fill_linear_stripes(Chart.CHART, 0, *stripes) return chart
def get(self): list = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep'] list2 = [] for title in list: list2.append( int(getdata(title,'funds').custom['amount'].text)) max_y = 10000 chart = SimpleLineChart(200, 125, y_range=[0, max_y]) chart.add_data([2000,3000,5000,1200,5000,4000,1000,3000,5900]) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, list) url2 = chart.get_url() self.response.out.write(template.render('template/donate.html',{ 'url2' :url2, }))
def get(self): list = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep'] list2 = [] for title in list: list2.append(int(getdata(title, 'funds').custom['amount'].text)) max_y = 10000 chart = SimpleLineChart(200, 125, y_range=[0, max_y]) chart.add_data([2000, 3000, 5000, 1200, 5000, 4000, 1000, 3000, 5900]) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, list) url2 = chart.get_url() self.response.out.write( template.render('template/donate.html', { 'url2': url2, }))
def RenderGoogleChart(data, filename): """ create a GoogleChart from decoded data under filename (.png) """ print "Rendering GoogleChart [%s]" % filename # Retrieve chart data elements=[] max_y = 0 min_y = 9999 for i in range(len(data["time"])): if data["cps"][i] > max_y: max_y = data["cps"][i] if data["cps"][i] < min_y: min_y = data["cps"][i] elements.append(data["cps"][i]) # Chart size of 600x375 pixels and specifying the range for the Y axis chart = SimpleLineChart(600, 375, y_range=[min_y-0.1, max_y+0.1]) # Add the chart data chart.add_data(elements) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # Define the Y axis labels left_axis = [x * 0.1 for x in range(0, int(max_y/0.1))] left_axis[0] = 'CPS' chart.set_axis_labels(Axis.LEFT, left_axis) chart.download(filename)
def get_chart_image(self, data, **kw): """Return a image file path Arguments:: data -- a list containing the X,Y data representation """ from pygooglechart import SimpleLineChart, Axis # Set the vertical range from 0 to 100 try: max_y = max(data['y']) min_y = min(data['y']) except: min_y = 0 max_y = 100 width = int(kw.get('width', 600)) height = int(kw.get('height', 250)) # Chart size of widthxheight pixels and specifying the range for the Y axis chart = SimpleLineChart(width, height, y_range=[0, max_y]) # Add the chart data chart.add_data(data['y']) # Set the line colour to blue chart.set_colours(['0000FF']) try: step_x = int(100/(len(data['x'])-1)) except: step_x = 0 chart.set_grid(step_x, 10, 5, 5) # The Y axis labels contains min_y to max_y spling it into 10 equal parts, #but remove the first number because it's obvious and gets in the way #of the first X label. left_axis = [utils.intcomma(x) for x in range(0, max_y + 1, (max_y)/10)] left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, data['x']) #Generate an hash from arguments kw_hash = hash(tuple(sorted(kw.items()))) data_hash = hash(tuple(sorted([(k, tuple(v)) for k, v in data.iteritems()]))) args_hash = str(kw_hash) + str(data_hash) image_path = os.path.join(TARGET_DIR, "%s.png" % args_hash) if bool(kw.get('refresh', False)) or args_hash not in self.charts: #Get image from google chart api chart.download(image_path) if args_hash not in self.charts: self.charts.append(args_hash) self._p_changed = True return image_path
def report(request): # data report if Invoice.objects.all().count() == 0: return redirect("/openbook") min_year = Invoice.objects.order_by("date")[0].date.year max_year = Invoice.objects.order_by("-date")[0].date.year report = [] for year in range(min_year,max_year+1): year_report = [] for month in range(1,13): total = get_total(year, month) year_report.append(total) s = sum(year_report) tax = round((s * (0.13 / 1.13)), 2) year_report.append(tax) year_report.append(s) year_report.insert(0, year) report.append(year_report) # graphical data year report year_total_data = [i[-1] for i in report] max_y = max(year_total_data) min_y = min(year_total_data) gr = SimpleLineChart(800, 300, y_range=[0,max_y]) gr.add_data(year_total_data) gr.set_grid(0, 25, 5, 5) left_axis = range(min_y, max_y + 1, (max_y/10)) left_axis[0] = '' gr.set_axis_labels(Axis.LEFT, left_axis) gr.set_axis_labels(Axis.BOTTOM, \ [str(i) for i in range(min_year, max_year+1)]) # yearly report gs = [] months = ["JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"] for data in report: max_y = max(data[1:-2]) # excluding the first and the last 2 entries g = SimpleLineChart(800, 300, y_range=[0, max_y]) g.add_data(data[1:-2]) # excluding the frist and the last 2 entries g.set_grid(0, 25, 5, 5) left_axis = range(0, max_y + 1, (max_y/10)) left_axis[0] = '' g.set_axis_labels(Axis.LEFT, left_axis) g.set_axis_labels(Axis.BOTTOM, \ months) gs.append((data[0], g.get_url())) gs.reverse() grurl = gr.get_url() if max_year == min_year: grurl = None return render_to_response( "admin/invoice/report.html", {'report' : report, 'gr': grurl, 'gs':gs}, RequestContext(request, {}), )
def generate_freq_chart_url_from_qset(qset, sizex, sizey): ''' returns url for chart from given queryset ''' end_year = qset.order_by('-publication_date')[0].publication_date.year start_year = qset.order_by('publication_date')[0].publication_date.year #print "start {0}, end {1}".format(start_year, end_year) year_range=range(start_year,end_year + 1) month_range=range(1, 13) count_rules = [] count_proprules = [] count_notices = [] count_presdocs = [] count_unknown = [] count_total = [] today = datetime.date.today() for y in year_range: start_date=datetime.date(y,1,1) end_date=datetime.date(y,12,31) year_qset = qset.filter(publication_date__range=(start_date, end_date)) count_rules.append(year_qset.filter(document_type='Rule').count()) count_proprules.append(year_qset.filter(document_type='Proposed Rule').count()) count_notices.append(year_qset.filter(document_type='Notice').count()) count_presdocs.append(year_qset.filter(document_type='Presidential Document').count()) count_unknown.append(year_qset.filter(document_type='Document of Unknown Type').count()) count_total.append(count_unknown[-1] + count_presdocs[-1] + count_rules[-1] + count_proprules[-1] + count_notices[-1]) #for i in [count_rules, count_proprules, count_notices, count_presdocs, count_unknown, count_total]: #print i # set up chart axis parameters largest_y = max(count_total) left_axis_step = int(pow(10, int(log10(largest_y)))) max_y = (int(largest_y / left_axis_step) * left_axis_step) + (left_axis_step * 2) left_axis = range(0, max_y + left_axis_step, left_axis_step) left_axis[0] = "" bottom_axis = year_range # generate chart url chart = SimpleLineChart(sizex, sizey, y_range=[0, max_y]) chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, bottom_axis) chart.set_grid(0, max(10, int(left_axis_step / 10)), 5, 5) chart.set_colours(['0000FF', '00FF00', 'FF0000', 'FFFF00', '00FFFF', 'aaaaaa']) chart.set_legend(['Rules', 'Proposed Rules', 'Notices', 'Presidential Docs', 'Unknown', 'Total']) chart.add_data(count_rules) chart.add_data(count_proprules) chart.add_data(count_notices) chart.add_data(count_presdocs) chart.add_data(count_unknown) chart.add_data(count_total) chart_url = chart.get_url() return chart_url
def _line(self, results): key = 'count' if self.request.GET.get('percentages') == 'true': key = 'percentage' counts = [x.get(key) for x in results] maxcount = max(counts) granularity = self.request.GET.get('granularity') times = [x.get(granularity) for x in results] width = int(self.request.GET.get('width', 575)) height = int(self.request.GET.get('height', 300)) chart = SimpleLineChart(width, height, y_range=(0, max(counts))) chart.add_data(counts) chart.set_line_style(0, thickness=2) # Set line thickness chart.set_colours([ 'E0B300', ]) chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_grid(0, 50, 2, 5) # Set gridlines if self.request.GET.get('granularity') == 'month': months = [x['month'] for x in results] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [ int((x / float(len(months))) * 100) for x in january_indexes ] index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) if key == 'percentage': label = '%.4f' % maxcount label += '%' else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [ label, ]) chart.set_axis_positions(index, [ 100, ]) if self.request.GET.get('legend', 'true') != 'false': chart.set_legend([ self.request.GET.get('phrase'), ]) return chart.get_url()
def plotter(fund): (dates, values) = dataparser("data/%s" % fund) left_axis = [int(min(values)), int(max(values) + 1)] chart = SimpleLineChart(600, 375, y_range=[min(values), max(values) + 1]) chart.add_data(values) chart.add_data([0] * 2) chart.set_colours(['76A4FB'] * 5) chart.add_fill_range('76A4FB', 0, 1) chart.set_grid(0, 5, 1, 25) chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, dates) chart.download("charts/%s.png" % fund)
def stripes(): # Set the vertical range from 0 to 100 max_y = 100 # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(600, 500, y_range=[0, max_y]) # Add the chart data # data = [ # 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, # 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55, # 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32 # ] # data2 = [ # 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32, 62, 60, 55, # 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, # 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62 # ] data = xrange(0, 100, 20) data2 = [0, 20, 20, 40, 40, 80, 80, 100, 100] chart.add_data(data) chart.add_data(data2) # Set the line colour to blue chart.set_colours(['0000FF', '00CC00']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, \ ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']) chart.download('line-stripes.png')
def stripes(): # Set the vertical range from 0 to 100 max_y = 100 # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(600, 500, y_range=[0, max_y]) # Add the chart data # data = [ # 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, # 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55, # 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32 # ] # data2 = [ # 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32, 62, 60, 55, # 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, # 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62 # ] data = xrange(0, 100, 20) data2 = [0, 20, 20, 40, 40, 80, 80, 100, 100] chart.add_data(data) chart.add_data(data2) # Set the line colour to blue chart.set_colours(['0000FF','00CC00']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, \ ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']) chart.download('line-stripes.png')
def ShowChartByGoogle(handler, data, book, ups): oneday = datetime.timedelta(days=1) date = ups[0].date xlabel = [str(date.day)] ylabel = range(0, book.pages + 1, ((book.pages / 8) / 50 + 1) * 50) for (i, up) in enumerate(ups): if i == 0: continue days = (up.date - ups[i-1].date).days for j in range(days): date += oneday if date.weekday() == 0: # only records sunday if date.day < 7: xlabel.append(str(date.month)) else: xlabel.append('.') chart = SimpleLineChart(600, 320, y_range=[0, ylabel[-1]]) chart.add_data(data) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = ylabel left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, xlabel) doRender(handler, 'progress.html', { 'book': book, 'imgurl': chart.get_url(), 'method': 'google'})
def generateGraph(fMesureGlobalPerCent, nbComparison, resPath): max_y = 100#Fmesure à 100% chart = SimpleLineChart(500, 500, y_range=[0, max_y]) chart.add_data(fMesureGlobalPerCent) chart.set_colours(['0000FF']) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 25, 5, 5) left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) y_axis = [] for x in range(nbComparison): if x%5 == 0: y_axis.append(x) chart.set_axis_labels(Axis.BOTTOM, y_axis) chart.download(resPath)
def get_chart(self, data, date_start, date_end, colours=None): # Set the vertical range from max_value plus 10 if not data: return None max_y = max([max(l) for l in data]) if max_y < 10: mult = 1 else: y_scale = max_y mult = 1 while y_scale/10 > 1: mult = mult*10 y_scale = y_scale/10 max_y = max_y + mult # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(600, 300, y_range=[0, max_y]) for i in data: chart.add_data(i) if not colours: chart.set_colours(['0000FF']) # Set the line colour to blue else: chart.set_colours(colours) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'F2F2F2', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) left_axis = range(0, max_y + 1, mult) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, \ self.get_chart_x_axis_label(date_start, date_end)) return chart.get_url()
def linechart(name, dataset, size=(400,400)): max_y = maxy(dataset) chart = SimpleLineChart(size[0], size[1], y_range=[0, max_y]) legend = [] for series_name, s in dataset: chart.add_data([y for x, y in s]) legend.append(series_name) chart.set_colours(['057D9F', '8106A9', 'E9FB00', 'FF8100']) chart.set_legend(legend) chart.set_grid(0, 25, 5, 5) left_axis = range(0, int(max_y + 1), 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) bottom_axis = [x for x, y in dataset[0][1]] chart.set_axis_labels(Axis.BOTTOM, bottom_axis) chart.download(name)
def getchart(history): data = [] created = [] for h in history: data.append(float(h.average)) created.append(int(h.created.day)) max_y = int(math.ceil(max(data))) + 1 min_y = int(math.floor(min(data))) - 1 chart = SimpleLineChart(550, 225, y_range=[min_y, max_y]) chart.add_data(data) chart.set_colours(["0000FF"]) # chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 25, 5, 5) left_axis = range(min_y, max_y + 1, 2) left_axis[0] = "" chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, created) return chart
def _plot_timeseries(self, options, args, fh): """Plot a timeseries graph""" from pygooglechart import Chart, SimpleLineChart, Axis delimiter = options.delimiter field = options.field - 1 datefield = options.datefield - 1 pts = [] for l in imap(lambda x: x.strip(), fh): splitted_line = l.split(delimiter) v = float(splitted_line[field]) t = datetime.strptime(splitted_line[datefield], options.dateformat) pts.append((t, v)) if options.get('limit', None): # Only wanna use top (earliest) N samples by key, sort and truncate pts = sorted(pts, key=itemgetter(0), reverse=True)[:options.limit] if not pts: raise ValueError("No data to plot") max_y = int(max((v for t, v in pts))) chart = SimpleLineChart(options.width, options.height, y_range=[0, max_y]) # Styling chart.set_colours(['0000FF']) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 25, 5, 5) ts, vals = zip(*pts) chart.add_data(vals) # Axis labels chart.set_axis_labels(Axis.BOTTOM, ts) left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) return chart
def _plot_timeseries(self, options, args, fh): """Plot a timeseries graph""" from pygooglechart import Chart, SimpleLineChart, Axis delimiter = options.delimiter field = options.field-1 datefield = options.datefield-1 pts = [] for l in imap(lambda x: x.strip(), fh): splitted_line = l.split(delimiter) v = float(splitted_line[field]) t = datetime.strptime(splitted_line[datefield], options.dateformat) pts.append((t, v)) if options.get('limit', None): # Only wanna use top (earliest) N samples by key, sort and truncate pts = sorted(pts, key=itemgetter(0), reverse=True)[:options.limit] if not pts: raise ValueError("No data to plot") max_y = int(max((v for t, v in pts))) chart = SimpleLineChart(options.width, options.height,y_range=[0, max_y]) # Styling chart.set_colours(['0000FF']) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 25, 5, 5) ts, vals = zip(*pts) chart.add_data(vals) # Axis labels chart.set_axis_labels(Axis.BOTTOM, ts) left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) return chart
def linegraph(self,days,bars,output,title = ""): data = [] min_count = 0 max_count = 0 date = lambda i:datetime.date.today() + datetime.timedelta(-days + i) for i in range(0,days+1): count = bars[date(i)] max_count = max(count,max_count) min_count = min(count,min_count) data.append(count) chart = SimpleLineChart(800,350,y_range=[min_count, 60000]) chart.add_data(data) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes d = max(1/float(days),round(7/float(days),2)) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', d, 'FFFFFF', d) fmt="%d/%m" chart.set_axis_labels(Axis.BOTTOM, \ [date(i).strftime(fmt) for i in range(0,days,7)]) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. delta = float(max_count-min_count) / 100 skip = int(delta) / 5 * 100 left_axis = range(0, 60000 + 1, skip) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) if len(title) > 0: chart.set_title(title % days) chart.download(output)
def linegraph(self, days, bars, output, title=""): data = [] min_count = 0 max_count = 0 date = lambda i: datetime.date.today() + datetime.timedelta(-days + i) for i in range(0, days + 1): count = bars[date(i)] max_count = max(count, max_count) min_count = min(count, min_count) data.append(count) chart = SimpleLineChart(800, 350, y_range=[min_count, 60000]) chart.add_data(data) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes d = max(1 / float(days), round(7 / float(days), 2)) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', d, 'FFFFFF', d) fmt = "%d/%m" chart.set_axis_labels(Axis.BOTTOM, \ [date(i).strftime(fmt) for i in range(0,days,7)]) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. delta = float(max_count - min_count) / 100 skip = int(delta) / 5 * 100 left_axis = range(0, 60000 + 1, skip) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) if len(title) > 0: chart.set_title(title % days) chart.download(output)
def generate_freq_chart_url_from_fedreg(search_term, start_year, end_year): ''' returns url for chart after querying FR API ''' year_range=range(start_year,end_year + 1) month_range=range(1, 13) base_url = "http://api.federalregister.gov/v1/articles.json" # fr api base url base_url_plus_search_term = base_url + "?conditions[term]=" + search_term count = [] for y in year_range: date_conditions = "conditions[publication_date][year]=" + str(y) url = base_url_plus_search_term + "&" + date_conditions url = quote(url, safe='/:+=?&"|') #print "url", url search_result = fetch_fedreg_results_page(url) # request page if search_result: count.append(int(search_result['count'])) #print "count:", search_result['count'] # set up chart to display if max(count) < 100: left_axis_step = 10 max_y = 100 else: left_axis_step = int(pow(10, int(log10(max(count)))) / 10) max_y = (int(max(count) / left_axis_step) * left_axis_step) + (left_axis_step * 2) #print "left", left_axis_step, "max_y", max_y #print "max_y", max_y left_axis = range(0, max_y + left_axis_step, left_axis_step) left_axis[0] = "" bottom_axis = year_range chart = SimpleLineChart(600, 300, y_range=[0, max_y]) chart.set_colours(['0000FF']) chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, bottom_axis) chart.set_grid(0, max(10, int(left_axis_step / 10)), 5, 5) chart.add_data(count) chart_url = chart.get_url() return chart_url
def makeChart(): # Set the vertical range from 0 to 100 max_y = 100 # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(400, 250, y_range=[0, max_y]) # Add the chart data # Aggregate data into array before adding to chart data = [ 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55, 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32 ] #for i in data: # chart.add_data(i) chart.add_data(data) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, \ ['9 am', '6 pm', '12 am']) return chart
def _line(self, results): key = 'count' if self.request.GET.get('percentages') == 'true': key = 'percentage' counts = [x.get(key) for x in results] maxcount = max(counts) granularity = self.request.GET.get('granularity') times = [x.get(granularity) for x in results] width = int(self.request.GET.get('width', 575)) height = int(self.request.GET.get('height', 300)) chart = SimpleLineChart(width, height, y_range=(0, max(counts))) chart.add_data(counts) chart.set_line_style(0, thickness=2) # Set line thickness chart.set_colours(['E0B300', ]) chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_grid(0, 50, 2, 5) # Set gridlines if self.request.GET.get('granularity') == 'month': months = [x['month'] for x in results] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [int((x / float(len(months))) * 100) for x in january_indexes] index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) if key == 'percentage': label = '%.4f' % maxcount label += '%' else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [label, ]) chart.set_axis_positions(index, [100, ]) if self.request.GET.get('legend', 'true') != 'false': chart.set_legend([self.request.GET.get('phrase'), ]) return chart.get_url()
def _plot_line(self, options, args, fh): """Plot a line chart""" from pygooglechart import Chart, SimpleLineChart, Axis delimiter = options.delimiter field = options.field pts = [] for l in imap(lambda x: x.strip(), fh): splitted_line = l.split(delimiter) k = int(splitted_line.pop(field-1)) pts.append((k, ' '.join(splitted_line))) if options.get('limit', None): # Only wanna use top N samples by key, sort and truncate pts = sorted(pts, key=itemgetter(0), reverse=True)[:options.limit] if not pts: raise ValueError("No data to plot") max_y = max((v for v, label in pts)) chart = SimpleLineChart(options.width, options.height,y_range=[0, max_y]) # Styling chart.set_colours(['0000FF']) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 25, 5, 5) data, labels = zip(*pts) chart.add_data(data) # Axis labels chart.set_axis_labels(Axis.BOTTOM, labels) left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) return chart
def draw_plot(data, generation): # Set the vertical range from 0 to 100 max_y = data[0] # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(600, 325, y_range=[0, max_y]) # Add the chart data """ data = [ 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55, 55, 52, 47, 44, 44, 40, 40, 37, 0,0,0,0,0,0,0 ] """ chart.add_data(data) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, [str(x) for x in xrange(1, generation + 1)][::14]) chart.download('plot.png')
def simpleChart2(bottom_labels, data): # round min and max to nearest integers datamin = int(min(data) - 0.5) datamax = int(max(data) + 0.5) chart = SimpleLineChart(200, 125, y_range=[datamin, datamax]) chart.add_data(data) left_axis = ['min',0,'max'] chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, bottom_labels) chart.set_axis_labels(Axis.BOTTOM, ['X Axis']) # second label below first one chart.set_axis_positions(2, [50.0]) # position, left is 0., right is 100., 50. is center # Set the line colour chart.set_colours(['0000FF']) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # Set vertical stripes stripes = ['CCCCCC', 0.2, 'FFFFFF', 0.2] chart.fill_linear_stripes(Chart.CHART, 0, *stripes) return chart
def stripes(): # Set the vertical range from 0 to 100 max_y = 2 # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(500, 500, y_range=[0, max_y]) # Add the chart data data = [ 0.2121649980545044, 0.1335739940404892, 0.1369339972734451, 0.1327839940786362, 0.3375900089740753 ] chart.add_data(data) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 10, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = list(range(0, max_y + 1, 1)) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) left_axis2 = "Proton Flux"[::-1] chart.set_axis_labels(Axis.LEFT, left_axis2) # X axis labels chart.set_axis_labels(Axis.BOTTOM, \ ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']) chart.set_axis_labels(Axis.BOTTOM, "Time") chart.download('chart.png')
def draw_plot(data, generation): # Set the vertical range from 0 to 100 max_y = data[0] # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(600, 325, y_range=[0, max_y]) # Add the chart data """ data = [ 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55, 55, 52, 47, 44, 44, 40, 40, 37, 0,0,0,0,0,0,0 ] """ chart.add_data(data) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, [str(x) for x in xrange(1, generation+1)][::14]) chart.download('plot.png')
def get_chart_url(): cache_key = 'phonedb-chart-url-%s' % settings.LANGUAGE_CODE url = cache.get(cache_key) if url is not None: return url enddate = datetime.datetime.now() # This works badly, we will rather render only chart for month after # it has finished #+ datetime.timedelta(days=30) endyear = enddate.year endmonthlast = enddate.month endmonth = 12 dates = [] unsupported = [] supported = [] totals = [] alls = [] years = [] for year in range(2006, endyear + 1): if year == endyear: endmonth = endmonthlast for month in range(1, endmonth + 1): if month == 1: years.append('%d' % year) else: years.append('') time_range = (datetime.date(1900, 1, 1), datetime.date(year, month, 1)) supported_val = Phone.objects.exclude(state='deleted').filter( connection__isnull=False).filter( created__range=time_range).count() unsupported_val = Phone.objects.exclude(state='deleted').filter( connection__isnull=True).filter( created__range=time_range).count() all_val = Phone.objects.filter( created__lt=datetime.date(year, month, 1)).count() supported.append(supported_val) unsupported.append(unsupported_val) totals.append(unsupported_val + supported_val) alls.append(all_val) dates.append('%d-%02d' % (year, month)) #print dates #print unsupported #print supported #print totals #print alls max_y = int(((max(alls) / 100) + 1) * 100) chart = SimpleLineChart(800, 300, y_range=[0, max_y]) # chart.fill_solid(chart.BACKGROUND, 'ffd480') # chart.fill_solid(chart.CHART, 'ffd480') # Chart data chart.add_data(supported) chart.add_data(totals) chart.add_data(alls) # Lowest value chart.add_data([0] * 2) # Set the line colour to blue chart.set_colours(['00FF00', 'FF0000', '0000FF', '00000000']) #chart.add_fill_range('76A4FB', 2, 3) # Set the vertical stripes month_stripes = 3.0 chart.fill_linear_stripes(Chart.CHART, 0, 'ffffff', month_stripes / len(alls), 'cccccc', month_stripes / len(alls)) # Set the horizontal dotted lines chart.set_grid(0, 10, 5, 5) chart.set_legend([ _('Supported phones').encode('utf-8'), _('Approved records').encode('utf-8'), _('Total records').encode('utf-8') ]) left_axis = ['%d' % x for x in range(0, max_y + 1, int(max_y / 10))] left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, years) url = chart.get_url().replace('http:', 'https:') cache.set(cache_key, url, 3600) return url
def _line_strip_graph(data, legends, axis_labels, size, steps, type=SimpleLineChart, multiline=False, **kwargs): if multiline: max_values = [] min_values = [] for row in data: max_values.append(max(row)) min_values.append(min(row)) max_y = max(max_values) min_y = min(min_values) else: max_y = max(data) min_y = min(data) #validando si hay datos para hacer grafico if max_y==0: return None chart = SimpleLineChart(size[0], size[1], y_range=[0, max_y*1.05]) if multiline: for row in data: chart.add_data(row) else: chart.add_data(data) step = ((max_y*1.05)-(min_y*0.95))/steps #validando en caso de el paso sea menor que uno y de cero en la conversion if step<1: step = 1 max_value = max_y tope = int(round(max_value*1.05)) if tope < max_value: tope+=2 else: tope+=1 try: left_axis = range(int(round(min_y*0.95)), tope, int(step)) except ValueError: #error por que los range no soportan decimales left_axis = range(0, 2) left_axis[0]='' chart.set_axis_range(Axis.LEFT, min_y, tope) if 'units' in kwargs: chart.set_axis_labels(Axis.LEFT, kwargs['units']) if 'time' in kwargs: chart.set_axis_labels(Axis.BOTTOM, kwargs['time']) chart.set_grid(0, 25, 4, 4,) chart.chls=4|4 #chart.fill_linear_stripes(Chart.CHART, 0, 'FFFFEF', 0.2, 'FFFFFF', 0.2) chart.set_colours(COLORS) if axis_labels: chart.set_axis_labels(Axis.BOTTOM, axis_labels) chart.set_legend(legends) chart.set_legend_position('b') if 'thickness' in kwargs: if multiline: for i in range(len(data)): chart.set_line_style(index = i, thickness=kwargs['thickness']) else: chart.set_line_style(index=0, thickness=kwargs['thickness']) return chart
def generateGraphs(): version() conndio = sqlite3.connect(dbfiledionaea) c = conndio.cursor() connglas = sqlite3.connect(dbfileglastopf) x = connglas.cursor() # # # Dionaea Queries starting here! # # #Most attacked ports - Dionaea querySQL = 'SELECT COUNT(local_port) AS hitcount,local_port AS port FROM connections WHERE connection_type = "accept" GROUP BY local_port HAVING COUNT(local_port) > 10' print querySQL c.execute(querySQL) #Count attacks on port 80 - Glastopf querySQL = "SELECT COUNT(request_url) AS hitcount,'80 - http' AS port FROM events" print querySQL x.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in c: print(row) flist.append(row[0]) seclist.append(str(row[1])) for row in x: print(row) flist.append(row[0]) seclist.append(str(row[1])) seclist = [port.replace('21','21 - ftp') for port in seclist] seclist = [port.replace('42','42 - wins') for port in seclist] seclist = [port.replace('135','135 - msrpc') for port in seclist] seclist = [port.replace('445','445 - smb') for port in seclist] seclist = [port.replace('1433','1433 - ms-sql') for port in seclist] seclist = [port.replace('3306','3306 - mysql') for port in seclist] seclist = [port.replace('5060','5060 - sip') for port in seclist] chart.add_data(flist) chart.set_legend(seclist) chart.download('attacked_ports.png') #Top10 Malware querySQL = 'SELECT COUNT(download_md5_hash), download_md5_hash FROM downloads GROUP BY download_md5_hash ORDER BY COUNT(download_md5_hash) DESC LIMIT 10' print querySQL c.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in c: print(row) flist.append(row[0]) seclist.append(str(row[1])) chart.add_data(flist) chart.set_legend(seclist) chart.download('popular_malware.png') #Connections per Day Dionaea and Glastopf - 7 Days - Dionaea querySQL = "SELECT strftime('%Y', connection_timestamp,'unixepoch') as 'year', strftime('%m', connection_timestamp,'unixepoch') as 'month', strftime('%d', connection_timestamp,'unixepoch') as 'day', count(strftime('%m', connection_timestamp,'unixepoch')) as 'num' FROM connections GROUP BY strftime('%Y', connection_timestamp,'unixepoch'), strftime('%m', connection_timestamp,'unixepoch'), strftime('%d', connection_timestamp,'unixepoch') ORDER BY strftime('%Y', connection_timestamp,'unixepoch') DESC, strftime('%m', connection_timestamp,'unixepoch') DESC, strftime('%d', connection_timestamp,'unixepoch') DESC LIMIT 7" print querySQL c.execute(querySQL) #Connections per Day Dionaea and Glastopf - 7 Days - Glastopf querySQL = "SELECT COUNT(time), SUBSTR(time,-20,12) AS stripped FROM events GROUP BY stripped ORDER BY stripped DESC LIMIT 7" print querySQL x.execute(querySQL) chart = SimpleLineChart(pieChartWidth, pieChartHeight, y_range=[0, 5000]) flist = [] seclist = [] for (row,rowx) in zip(c,x): print(row) print(rowx) l = list(row) l[3]=row[3]+rowx[0] flist.append(l[3]) date = '%s-%s-%s' % (str(row[0]),str(row[1]),str(row[2])) seclist.append(date) flist.reverse() seclist.reverse() chart.add_data(flist) chart.set_axis_labels(Axis.BOTTOM,seclist) chart.set_colours(['0000FF']) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 20, 5, 5) left_axis = range(0, 5001, 1000) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) chart.download('connections_per_day.png') # # # Glastopf Queries starting here! # # #Top15 intext requests querySQL = 'SELECT count, content FROM intext ORDER BY count DESC LIMIT 15' print querySQL x.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in x: print(row) flist.append(row[0]) seclist.append(str(row[1])) chart.add_data(flist) chart.set_legend(seclist) chart.download('popular_intext.png') #Top15 intitle requests querySQL = 'SELECT count, content FROM intitle ORDER BY count DESC LIMIT 15' print querySQL x.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in x: print(row) flist.append(row[0]) seclist.append(str(row[1])) chart.add_data(flist) chart.set_legend(seclist) chart.download('popular_intitle.png') #Top10 inurl requests querySQL = 'SELECT count, SUBSTR(content,0,40) FROM inurl ORDER BY count DESC LIMIT 10' print querySQL x.execute(querySQL) chart = PieChart2D(pieChartWidth, pieChartHeight, colours=pieColours) flist = [] seclist = [] for row in x: print(row) flist.append(row[0]) seclist.append(str(row[1])) chart.add_data(flist) chart.set_legend(seclist) chart.download('popular_inurl.png')
def _line_strip_graph(data, legends, axis_labels, size, steps, type=SimpleLineChart, multiline=False, **kwargs): if multiline: max_values = [] min_values = [] for row in data: max_values.append(max(row)) min_values.append(min(row)) max_y = max(max_values) min_y = min(min_values) else: max_y = max(data) min_y = min(data) #validando si hay datos para hacer grafico if max_y == 0: return None chart = SimpleLineChart(size[0], size[1], y_range=[0, max_y * 1.05]) if multiline: for row in data: chart.add_data(row) else: chart.add_data(data) step = ((max_y * 1.05) - (min_y * 0.95)) / steps #validando en caso de el paso sea menor que uno y de cero en la conversion if step < 1: step = 1 max_value = max_y tope = int(round(max_value * 1.05)) if tope < max_value: tope += 2 else: tope += 1 try: left_axis = range(int(round(min_y * 0.95)), tope, int(step)) except ValueError: #error por que los range no soportan decimales left_axis = range(0, 2) left_axis[0] = '' chart.set_axis_range(Axis.LEFT, min_y, tope) if 'units' in kwargs: chart.set_axis_labels(Axis.LEFT, kwargs['units']) if 'time' in kwargs: chart.set_axis_labels(Axis.BOTTOM, kwargs['time']) chart.set_grid( 0, 25, 4, 4, ) chart.chls = 4 | 4 #chart.fill_linear_stripes(Chart.CHART, 0, 'FFFFEF', 0.2, 'FFFFFF', 0.2) chart.set_colours(COLORS) if axis_labels: chart.set_axis_labels(Axis.BOTTOM, axis_labels) chart.set_legend(legends) chart.set_legend_position('b') if 'thickness' in kwargs: if multiline: for i in range(len(data)): chart.set_line_style(index=i, thickness=kwargs['thickness']) else: chart.set_line_style(index=0, thickness=kwargs['thickness']) return chart
def POST_query(self, args): form = request() if not form.validates(): return "Failure. Did you select an option for all the fields?" sym = form['symbol'].value start_string = form['startdate'].value end_string = form['enddate'].value date_format = "%Y-%m-%d" start = datetime.strptime(start_string, date_format).strftime("%Y-%m-%d") end = datetime.strptime(end_string, date_format).strftime("%Y-%m-%d") msb = mysqlmodel.MySqlBase() msb.connect() start_time = time.time() records = msb.get_by_sym_range2(sym, start, end) if len(records) == 0: message = "Zero records returned. Perhaps the date range is incorrect?" return render.error(message) records_unsorted = [] print "number records: %s" % (len(records)) for r in records: temp = r temp['date'] = datetime.strptime(str(temp['date']), "%Y-%m-%d") records_unsorted.append(temp) records_processed = sorted(records_unsorted, key=lambda k: k['date']) elapsed_time = (time.time() - start_time) y_max = 0.0 y_min = 0.0 data = [] for q in records_processed: temp = float(q['price_adj_close']) data.append(temp) if temp > y_max: y_max = temp y_min = y_max for d in records_processed: temp = float(d['price_adj_close']) if temp < y_min: y_min = temp difference = float(y_max - y_min) difference = float(difference / 2) y_min_foo = y_min - difference y_max_foo = y_max + difference if y_min_foo < 0: y_min_foo = 0 chart = SimpleLineChart(1000, 300, y_range=[y_min_foo, y_max_foo]) chart.add_data(data) chart.set_colours(['0000FF']) # chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.1, 'FFFFFF', 0.2) # chart.set_grid(0, 25, 5, 5) # y_max_output = y_max + difference # left_axis = range(y_min_foo, y_max_foo, 1.00) # left_axis[0] = y_min left_axis = [] label = y_min_foo delta_y = 1.00 derp = float(y_max_foo - y_min_foo) # if derp > 15.0: delta_y = derp / (10.00) while y_min_foo < y_max_foo: left_axis.append(y_min_foo) y_min_foo = y_min_foo + delta_y if len(left_axis) < 10: left_axis.append(y_min_foo) lines = len(left_axis) - 1 chart.set_grid(0, lines, 1, 1) x_labels = [] for t in records_processed: label = (self.getMonth(t['date'].month), t['date'].year) if not label in x_labels: x_labels.append(label) chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, x_labels) list_len = float(len(x_labels)) top = float(1) stripe_len = float(top / list_len) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', stripe_len, 'FFFFFF', stripe_len) imgURL = chart.get_url() return render.results(sym, records_processed, elapsed_time, imgURL)
def get_chart_url(force=False): cache_key = "phonedb-chart-url-%s" % settings.LANGUAGE_CODE url = cache.get(cache_key) if url is not None and not force: return url enddate = datetime.datetime.now() endyear = enddate.year endmonthlast = enddate.month endmonth = 12 dates = [] unsupported = [] supported = [] totals = [] alls = [] years = [] for year in range(2006, endyear + 1): if year == endyear: endmonth = endmonthlast for month in range(1, endmonth + 1): if month == 1: years.append("%d" % year) else: years.append("") time_range = (datetime.date(1900, 1, 1), datetime.date(year, month, 1)) supported_val = (Phone.objects.exclude(state="deleted").filter( connection__isnull=False).filter( created__range=time_range).count()) unsupported_val = (Phone.objects.exclude(state="deleted").filter( connection__isnull=True).filter( created__range=time_range).count()) all_val = Phone.objects.filter( created__lt=datetime.date(year, month, 1)).count() supported.append(supported_val) unsupported.append(unsupported_val) totals.append(unsupported_val + supported_val) alls.append(all_val) dates.append("%d-%02d" % (year, month)) max_y = int(((max(alls) / 100) + 1) * 100) chart = SimpleLineChart(800, 300, y_range=[0, max_y]) # Chart data chart.add_data(supported) chart.add_data(totals) chart.add_data(alls) # Lowest value chart.add_data([0] * 2) # Set the line colour to blue chart.set_colours(["00FF00", "FF0000", "0000FF", "00000000"]) # Set the vertical stripes month_stripes = 3.0 chart.fill_linear_stripes( Chart.CHART, 0, "ffffff", month_stripes / len(alls), "cccccc", month_stripes / len(alls), ) # Set the horizontal dotted lines chart.set_grid(0, 10, 5, 5) chart.set_legend([ _("Supported phones").encode("utf-8"), _("Approved records").encode("utf-8"), _("Total records").encode("utf-8"), ]) left_axis = ["%d" % x for x in range(0, max_y + 1, int(max_y / 10))] left_axis[0] = "" chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, years) url = chart.get_url().replace("http:", "https:") cache.set(cache_key, url, 3600 * 24) return url
def read(self, request, *args, **kwargs): self.request = request if kwargs.get('chart_type') == 'timeline': handler = PhraseOverTimeHandler() if request.GET.get('split_by_party') == 'true': resultsets = {} for party in [ 'R', 'D', ]: kwargs['party'] = party resultsets[party] = handler.read(request, *args, **kwargs) return { 'results': { 'url': self._partyline(resultsets), }, } elif request.GET.get('compare') == 'true': phrases = request.GET.get( 'phrases', '').split(',')[:5] # Max of 5 phrases parties = request.GET.get('parties', '').split(',') states = request.GET.get('states', '').split(',') #chambers = request.GET.get('chambers', '').split(',') colors = [ '8E2844', 'A85B08', 'AF9703', ] metadata = [] legend_items = [] months = None key = 'count' if self.request.GET.get('percentages') == 'true': key = 'percentage' granularity = self.request.GET.get('granularity') width = int(request.GET.get('width', 575)) height = int(request.GET.get('height', 300)) chart = SimpleLineChart(width, height) chart.set_grid(0, 50, 2, 5) # Set gridlines chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_colours(colors) maxcount = 0 # Use phrases as a baseline; that is, assume that # there's a corresponding value for the other filters. # If a filter doesn't have as many values as the number # of phrases, the corresponding phrase will not be # filtered. # (However, if a value is set for 'party' or 'state' # in the querystring, that will override any values # set in 'phrases' or 'parties.') for n, phrase in enumerate(phrases): chart.set_line_style(n, thickness=2) # Set line thickness if not phrase.strip(): continue kwargs['phrase'] = phrase legend = phrase try: kwargs['party'] = parties[n] except IndexError: pass try: kwargs['state'] = states[n] except IndexError: pass if kwargs.get('party') and kwargs.get('state'): legend += ' (%(party)s, %(state)s)' % kwargs elif kwargs.get('party'): legend += ' (%(party)s)' % kwargs elif kwargs.get('state'): legend += ' (%(state)s)' % kwargs legend_items.append(legend) data = handler.read(request, *args, **kwargs) results = data['results'] counts = [x.get(key) for x in results] if max(counts) > maxcount: maxcount = max(counts) chart.add_data(counts) metadata.append(kwargs) # Duplicated code; should move into separate function. if self.request.GET.get('granularity') == 'month': if not months: months = [x['month'] for x in results] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [ int((x / float(len(months))) * 100) for x in january_indexes ] index = chart.set_axis_labels( Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions( index, [x for x in january_percentages[::2]]) chart.y_range = (0, maxcount) if key == 'percentage': label = '%.4f' % maxcount label += '%' else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [ label, ]) chart.set_axis_positions(index, [ 100, ]) # Always include a legend when comparing. chart.set_legend(legend_items) return { 'results': { 'metadata': metadata, 'url': chart.get_url() } } #return resultsets else: data = handler.read(request, *args, **kwargs) return { 'results': { 'url': self._line(data['results']), }, } elif kwargs.get('chart_type') == 'pie': handler = PhraseByCategoryHandler() kwargs['entity_type'] = request.GET.get('entity_type') data = handler.read(request, *args, **kwargs) if request.GET.get('output') == 'data': return {'data': self._pie(data['results'])} return { 'results': { 'url': self._pie(data['results']), }, } return { 'error': 'Invalid chart type.', }
def createChart(title, xaxis, datay, firstweekday): # Find the max y value max_y = None for dd in datay.values(): max_y = max(max_y, max(dd)) # Chart size of 400x250 pixels and specifying the range for the Y axis if title: chart = SimpleLineChart(1000, 250, title=title, y_range=[0, max_y]) else: chart = SimpleLineChart(1000, 250, y_range=[0, max_y]) # add the data for dd in datay.values(): chart.add_data(dd) # Set the line colours chart.set_colours(['0000FF', 'FF0000', '00FF00', 'F0F000', '0F0F00']) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # vertical stripes, the width of each stripe is calculated so that it covers one month ndatapoints = len(datay.values()[0]) datapermonth = [] for year in xaxis: for month in xaxis[year]: datapermonth.append(xaxis[year][month]) # mark months using range markers stripes = [] stripcols = ('FFFFFF', 'CCCCCC') wlast = 0 for k in datapermonth: w = k * 1.0 / ndatapoints icol = len(stripes) / 2 % 2 stripes.append(stripcols[icol]) stripes.append(w) wlast += w chart.fill_linear_stripes(Chart.CHART, 0, *stripes) # y axis labels if max_y > 30: left_axis = range(0, int(max_y) + 1, 5) elif max_y > 10: left_axis = range(0, int(max_y) + 1, 1) elif max_y > 5: left_axis = [] v = 0. while v < max_y + 0.5: left_axis.append(v) v += 0.5 else: left_axis = [] v = 0. while v < max_y + 0.1: left_axis.append(v) v += 0.1 left_axis[0] = '' # no label at 0 chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels monthlabels = [] for year in xaxis: for imonth in xaxis[year]: monthlabels.append(months[imonth]) chart.set_axis_labels(Axis.BOTTOM, monthlabels) chart.set_axis_labels(Axis.BOTTOM, xaxis.keys()) # years # the axis is 100 positions long, position month labels in the centre for each month positions = [] p = 0 for y in xaxis: datax = xaxis[y] for k in datax: w = datax[k] * 100.0 / ndatapoints positions.append(p + w / 2) p += w chart.set_axis_positions(1, positions) # position year labels at the centre of the year positions = [] p = 0 for y in xaxis: datax = xaxis[y] w = sum(datax.values()) * 100.0 / ndatapoints positions.append(p + w / 2) p += w chart.set_axis_positions(2, positions) chart.set_legend([k[0] for k in datay.keys()]) # vertical stripes for marking weeks #weeks = [ ] nsundays = 0 daycol = genColourRange(7) for p in range(ndatapoints): d = firstweekday + p if (d % 7) == 0: assertSunday(p) chart.add_marker(0, p, 'V', 'FF0000', 1) #weeks.append(daycol[d % 7]) #weeks.append(1./ndatapoints) # this does not work if the width is less than 0.01 #if len(weeks)/2 == 7: # from now it's repeats # break #chart.fill_linear_stripes(Chart.CHART, 0, *weeks) #chart.add_marker(0, 100, 'V', 'FF0000', 1) return chart
print "CSV dates = " + str(csvDates) print "CSV count = " + str(csvCount) print str(startYear) + '-' + str(startMonth).zfill(2) + '-01' print str(endYear) + '-' + str(endMonth).zfill(2) + '-01' #Generate graph max_y = 300 chart = SimpleLineChart(500, 500, y_range=[0, max_y]) chart.add_data(count) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to the max skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = list(range(0, max_y + 1, 25)) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, numbers) chart.download('chart-output.png')
def read(self, request, *args, **kwargs): self.request = request if kwargs.get('chart_type') == 'timeline': handler = PhraseOverTimeHandler() if request.GET.get('split_by_party') == 'true': resultsets = {} for party in ['R', 'D', ]: kwargs['party'] = party resultsets[party] = handler.read(request, *args, **kwargs) return {'results': {'url': self._partyline(resultsets), }, } elif request.GET.get('compare') == 'true': phrases = request.GET.get('phrases', '').split(',')[:5] # Max of 5 phrases parties = request.GET.get('parties', '').split(',') states = request.GET.get('states', '').split(',') #chambers = request.GET.get('chambers', '').split(',') colors = ['8E2844', 'A85B08', 'AF9703', ] metadata = [] legend_items = [] months = None key = 'count' if self.request.GET.get('percentages') == 'true': key = 'percentage' granularity = self.request.GET.get('granularity') width = int(request.GET.get('width', 575)) height = int(request.GET.get('height', 300)) chart = SimpleLineChart(width, height) chart.set_grid(0, 50, 2, 5) # Set gridlines chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_colours(colors) maxcount = 0 # Use phrases as a baseline; that is, assume that # there's a corresponding value for the other filters. # If a filter doesn't have as many values as the number # of phrases, the corresponding phrase will not be # filtered. # (However, if a value is set for 'party' or 'state' # in the querystring, that will override any values # set in 'phrases' or 'parties.') for n, phrase in enumerate(phrases): chart.set_line_style(n, thickness=2) # Set line thickness if not phrase.strip(): continue kwargs['phrase'] = phrase legend = phrase try: kwargs['party'] = parties[n] except IndexError: pass try: kwargs['state'] = states[n] except IndexError: pass if kwargs.get('party') and kwargs.get('state'): legend += ' (%(party)s, %(state)s)' % kwargs elif kwargs.get('party'): legend += ' (%(party)s)' % kwargs elif kwargs.get('state'): legend += ' (%(state)s)' % kwargs legend_items.append(legend) data = handler.read(request, *args, **kwargs) results = data['results'] counts = [x.get(key) for x in results] if max(counts) > maxcount: maxcount = max(counts) chart.add_data(counts) metadata.append(kwargs) # Duplicated code; should move into separate function. if self.request.GET.get('granularity') == 'month': if not months: months = [x['month'] for x in results] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [int((x / float(len(months))) * 100) for x in january_indexes] index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) chart.y_range = (0, maxcount) if key == 'percentage': label = '%.4f' % maxcount label += '%' else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [label, ]) chart.set_axis_positions(index, [100, ]) # Always include a legend when comparing. chart.set_legend(legend_items) return {'results': {'metadata': metadata, 'url': chart.get_url()}} #return resultsets else: data = handler.read(request, *args, **kwargs) return {'results': {'url': self._line(data['results']), }, } elif kwargs.get('chart_type') == 'pie': handler = PhraseByCategoryHandler() kwargs['entity_type'] = request.GET.get('entity_type') data = handler.read(request, *args, **kwargs) if request.GET.get('output') == 'data': return {'data': self._pie(data['results'])} return {'results': {'url': self._pie(data['results']), }, } return {'error': 'Invalid chart type.', }
def get_chart_url(): cache_key = 'phonedb-chart-url-%s' % settings.LANGUAGE_CODE url = cache.get(cache_key) if url is not None: return url enddate = datetime.datetime.now() # This works badly, we will rather render only chart for month after # it has finished #+ datetime.timedelta(days=30) endyear = enddate.year endmonthlast = enddate.month endmonth = 12 dates = [] unsupported = [] supported = [] totals = [] alls = [] years = [] for year in xrange(2006, endyear + 1): if year == endyear: endmonth = endmonthlast for month in xrange(1, endmonth + 1): if month == 1: years.append('%d' % year) else: years.append('') time_range = (datetime.date(1900, 1, 1), datetime.date(year, month, 1)) supported_val = Phone.objects.exclude(state = 'deleted').filter(connection__isnull = False).filter(created__range = time_range).count() unsupported_val = Phone.objects.exclude(state = 'deleted').filter(connection__isnull = True).filter(created__range = time_range).count() all_val = Phone.objects.filter(created__lt = datetime.date(year, month, 1)).count() supported.append(supported_val) unsupported.append(unsupported_val) totals.append(unsupported_val + supported_val) alls.append(all_val) dates.append('%d-%02d' % (year, month)) #print dates #print unsupported #print supported #print totals #print alls max_y = ((max(alls) / 100) + 1) * 100 chart = SimpleLineChart(800, 300, y_range=[0, max_y]) chart.fill_solid(chart.BACKGROUND, 'ffd480') chart.fill_solid(chart.CHART, 'ffd480') # Chart data chart.add_data(supported) chart.add_data(totals) chart.add_data(alls) # Lowest value chart.add_data([0] * 2) # Set the line colour to blue chart.set_colours(['00FF00', 'FF0000', '0000FF', '00000000']) #chart.add_fill_range('76A4FB', 2, 3) # Set the vertical stripes month_stripes = 3.0 chart.fill_linear_stripes(Chart.CHART, 0, 'ffe4a0', month_stripes / len(alls), 'ffd480', month_stripes / len(alls)) # Set the horizontal dotted lines chart.set_grid(0, 10, 5, 5) chart.set_legend([ _('Supported phones').encode('utf-8'), _('Approved records').encode('utf-8'), _('Total records').encode('utf-8')]) left_axis = map(lambda x: '%d' % x, xrange(0, max_y + 1, max_y / 10)) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) chart.set_axis_labels(Axis.BOTTOM, years) url = chart.get_url() cache.set(cache_key, url, 3600) return url
def main(): size_limit = 3000 * 100; x_size = 1000 y_size = 300 y_max = 100 parser = OptionParser() parser.add_option("-f", "--file", dest="filename", help="input data file") parser.add_option("-o", "--output", dest="chartname", help="output chart file") parser.add_option("-s", "--size", dest="size", help="'xsize,ysize' length of the chart x*y<=%d" % size_limit) parser.add_option("-y", "--y-max", dest="y_max", help="y max limit") (options, args) = parser.parse_args(sys.argv[1:]) if (options.filename == None or options.chartname == None): parser.print_help() sys.exit(1) # init x,y size if (options.size): size = options.size xy_pair = size.split(",") if len(xy_pair) == 2: x_size = string.atoi(xy_pair[0]) y_size = string.atoi(xy_pair[1]) else : parser.print_help() sys.exit(1) if (x_size * y_size > size_limit): print("ERROR: x*y > %s" % size_limit) sys.exit(1) # init y range if (options.y_max): y_max = string.atoi(options.y_max) # input file section ifile = open(options.filename) while True: l = ifile.readline() if len(l) == 0: break # print("get line: %s" % l[:-1]) data = [ 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55, 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32 ] data2 = [ 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32, 62, 60, 55, 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62 ] data_list = [data, data2] # output file section chart = SimpleLineChart(x_size, y_size, y_range=[0, y_max]) # set data for d in data_list: chart.add_data(d) # init color RR=16; GG=16; BB=16 color_list = [] for i in range(len(data_list)): RR+=10;GG+=15;BB+=65 color_list.append("%s%s%s" % (str(hex(RR%255))[2:], str(hex(GG%255))[2:], str(hex(BB%255))[2:]) ) # print(color_list) chart.set_colours(color_list) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 25, 5, 5) # left_axis = range(0, y_max+1, 25) # left_axis[0] = "" chart.set_axis_labels(Axis.LEFT, ["","25%","50%","75%","100%"]) chart.set_axis_labels(Axis.BOTTOM, ["0:00", "3:00", "6:00", "9:00", "12:00", "15:00", "18:00", "21:00", "24:00"]) chart.download(options.chartname)
def _partyline(self, party_results): if self.request.GET.get('percentages') == 'true': key = 'percentage' else: key = 'count' maxcount = 0 allcounts = [] granularity = self.request.GET.get('granularity') months = [] for party, results in party_results.iteritems(): counts = [x.get(key) for x in results['results']] allcounts.append(counts) if max(counts) > maxcount: maxcount = max(counts) if granularity == 'month': months = [x['month'] for x in results['results']] januaries = [x for x in months if x.endswith('01')] january_indexes = [months.index(x) for x in januaries] january_percentages = [ int((x / float(len(months))) * 100) for x in january_indexes ] #times = [x.get(granularity) for x in results['results']] width = int(self.request.GET.get('width', 575)) height = int(self.request.GET.get('height', 318)) chart = SimpleLineChart(width, height, y_range=(0, max(counts))) chart.fill_solid('bg', '00000000') # Make the background transparent chart.set_grid(0, 50, 2, 5) # Set gridlines if granularity == 'month': index = chart.set_axis_labels(Axis.BOTTOM, [x[:4] for x in januaries[::2]]) chart.set_axis_positions(index, [x for x in january_percentages[::2]]) if key == 'percentage': label = '%.4f' % maxcount else: label = int(maxcount) index = chart.set_axis_labels(Axis.LEFT, [ label, ]) chart.set_axis_positions(index, [ 100, ]) for n, counts in enumerate(allcounts): chart.add_data(counts) chart.set_line_style(n, thickness=2) # Set line thickness colors = { 'R': 'bb3110', 'D': '295e72', } chart_colors = [] chart_legend = [] for k in party_results.keys(): chart_colors.append(colors.get(k, '000000')) chart_legend.append(k) chart.legend_position = 'b' chart.set_colours(chart_colors) if self.request.GET.get('legend', 'true') != 'false': chart.set_legend(chart_legend) return chart.get_url()
def _get_chart(self): if not self._chart: datapoints = self._get_datapoints() data = [datapoint['Average'] for datapoint in self._datapoints] # Set the vertical range from 0 to 100 max_y = 1 if data: max_y = max(data) * 1.1 # max_y cannot be zero if max_y == 0: max_y = 1 # Make sure percentage doesn't exceed 100% if self.args['unit'] == 'Percent': max_y = 100 # Chart size of 300x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(380, 125, y_range=[0, max_y]) # Add the chart data chart.add_data(data) chart.add_data([0, 0]) # Set the line colour to blue chart.set_colours(['0000FF', '76A4FB']) # Fill below data line chart.add_fill_range('76A4FB', 0, 1) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. step = (max_y * 1.0) / 4 if not step: step = 1 #left_axis = range(0, max_y + 1, step) left_axis = [] left_axis.append('') left_axis.append(step) left_axis.append(step * 2) left_axis.append(step * 3) left_axis.append(step * 4) i = 1 if self.args['unit'] == 'Percent': for point in left_axis[1:]: left_axis[i] = '%s%%' % left_axis[i] i = i + 1 elif self.args['unit'] == 'Bytes': for point in left_axis[1:]: left_axis[i] = convert_bytes(int(left_axis[i])) i = i + 1 elif self.args['unit'] == 'Bytes/Second': for point in left_axis[1:]: left_axis[i] = '%s/s' % convert_bytes(int(left_axis[i])) i = i + 1 elif self.args['unit'] == 'Seconds': for point in left_axis[1:]: left_axis[i] = '%ss' % round_float(left_axis[i]) i = i + 1 elif self.args['unit'] == 'Count/Second': for point in left_axis[1:]: left_axis[i] = '%s/s' % round_float(left_axis[i]) i = i + 1 chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels bottom_axis = [] # hourly chart if self.range['timedelta'] == timedelta(hours=1): bottom_axis.append(self.args['start_time'].strftime('%H:%M')) bottom_axis.append((self.args['end_time'] - timedelta(minutes=45)).strftime('%H:%M')) bottom_axis.append((self.args['end_time'] - timedelta(minutes=30)).strftime('%H:%M')) bottom_axis.append((self.args['end_time'] - timedelta(minutes=15)).strftime('%H:%M')) bottom_axis.append(self.args['end_time'].strftime('%H:%M')) # daily chart if self.range['timedelta'] == timedelta(days=1): bottom_axis.append(self.args['start_time'].strftime('%H:%M')) bottom_axis.append((self.args['end_time'] - timedelta(hours=18)).strftime('%H:%M')) bottom_axis.append((self.args['end_time'] - timedelta(hours=12)).strftime('%H:%M')) bottom_axis.append((self.args['end_time'] - timedelta(hours=6)).strftime('%H:%M')) bottom_axis.append(self.args['end_time'].strftime('%H:%M')) # weekly chart if self.range['timedelta'] == timedelta(days=7): bottom_axis.append(self.args['start_time'].strftime('%d')) bottom_axis.append( (self.args['end_time'] - timedelta(days=6)).strftime('%d')) bottom_axis.append( (self.args['end_time'] - timedelta(days=5)).strftime('%d')) bottom_axis.append( (self.args['end_time'] - timedelta(days=4)).strftime('%d')) bottom_axis.append( (self.args['end_time'] - timedelta(days=3)).strftime('%d')) bottom_axis.append( (self.args['end_time'] - timedelta(days=2)).strftime('%d')) bottom_axis.append( (self.args['end_time'] - timedelta(days=1)).strftime('%d')) bottom_axis.append(self.args['end_time'].strftime('%d')) # monthly chart if self.range['timedelta'] == timedelta(days=30): bottom_axis.append(self.args['start_time'].strftime('%d')) bottom_axis.append((self.args['end_time'] - timedelta(days=25)).strftime('%d')) bottom_axis.append((self.args['end_time'] - timedelta(days=20)).strftime('%d')) bottom_axis.append((self.args['end_time'] - timedelta(days=15)).strftime('%d')) bottom_axis.append((self.args['end_time'] - timedelta(days=10)).strftime('%d')) bottom_axis.append( (self.args['end_time'] - timedelta(days=5)).strftime('%d')) bottom_axis.append(self.args['end_time'].strftime('%d')) chart.set_axis_labels(Axis.BOTTOM, bottom_axis) self._chart = chart return self._chart
def main(): size_limit = 3000 * 100 x_size = 1000 y_size = 300 y_max = 100 parser = OptionParser() parser.add_option("-f", "--file", dest="filename", help="input data file") parser.add_option("-o", "--output", dest="chartname", help="output chart file") parser.add_option("-s", "--size", dest="size", help="'xsize,ysize' length of the chart x*y<=%d" % size_limit) parser.add_option("-y", "--y-max", dest="y_max", help="y max limit") (options, args) = parser.parse_args(sys.argv[1:]) if (options.filename == None or options.chartname == None): parser.print_help() sys.exit(1) # init x,y size if (options.size): size = options.size xy_pair = size.split(",") if len(xy_pair) == 2: x_size = string.atoi(xy_pair[0]) y_size = string.atoi(xy_pair[1]) else: parser.print_help() sys.exit(1) if (x_size * y_size > size_limit): print("ERROR: x*y > %s" % size_limit) sys.exit(1) # init y range if (options.y_max): y_max = string.atoi(options.y_max) # input file section ifile = open(options.filename) while True: l = ifile.readline() if len(l) == 0: break # print("get line: %s" % l[:-1]) data = [ 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55, 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32 ] data2 = [ 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32, 62, 60, 55, 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62 ] data_list = [data, data2] # output file section chart = SimpleLineChart(x_size, y_size, y_range=[0, y_max]) # set data for d in data_list: chart.add_data(d) # init color RR = 16 GG = 16 BB = 16 color_list = [] for i in range(len(data_list)): RR += 10 GG += 15 BB += 65 color_list.append("%s%s%s" % (str(hex(RR % 255))[2:], str(hex( GG % 255))[2:], str(hex(BB % 255))[2:])) # print(color_list) chart.set_colours(color_list) chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) chart.set_grid(0, 25, 5, 5) # left_axis = range(0, y_max+1, 25) # left_axis[0] = "" chart.set_axis_labels(Axis.LEFT, ["", "25%", "50%", "75%", "100%"]) chart.set_axis_labels(Axis.BOTTOM, [ "0:00", "3:00", "6:00", "9:00", "12:00", "15:00", "18:00", "21:00", "24:00" ]) chart.download(options.chartname)
chart = SimpleLineChart(200, 125, y_range=[0, max_y]) # Add the chart data data = [ 32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37, 37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55, 55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32 ] chart.add_data(data) # Set the line colour to blue chart.set_colours(['0000FF']) # Set the vertical stripes chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines chart.set_grid(0, 25, 5, 5) # The Y axis labels contains 0 to 100 skipping every 25, but remove the # first number because it's obvious and gets in the way of the first X # label. left_axis = range(0, max_y + 1, 25) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, \ ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']) chart.download('line-stripes.png')
def get_chart_image(self, data, **kw): """Return a image file path Arguments:: data -- a list containing the X,Y data representation """ from pygooglechart import SimpleLineChart, Axis # Set the vertical range from 0 to 100 try: max_y = max(data['y']) min_y = min(data['y']) except: min_y = 0 max_y = 100 width = int(kw.get('width', 600)) height = int(kw.get('height', 250)) # Chart size of widthxheight pixels and specifying the range for the Y axis chart = SimpleLineChart(width, height, y_range=[0, max_y]) # Add the chart data chart.add_data(data['y']) # Set the line colour to blue chart.set_colours(['0000FF']) try: step_x = int(100 / (len(data['x']) - 1)) except: step_x = 0 chart.set_grid(step_x, 10, 5, 5) # The Y axis labels contains min_y to max_y spling it into 10 equal parts, #but remove the first number because it's obvious and gets in the way #of the first X label. left_axis = [ utils.intcomma(x) for x in range(0, max_y + 1, (max_y) / 10) ] left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, data['x']) #Generate an hash from arguments kw_hash = hash(tuple(sorted(kw.items()))) data_hash = hash( tuple(sorted([(k, tuple(v)) for k, v in data.iteritems()]))) args_hash = str(kw_hash) + str(data_hash) image_path = os.path.join(TARGET_DIR, "%s.png" % args_hash) if bool(kw.get('refresh', False)) or args_hash not in self.charts: #Get image from google chart api chart.download(image_path) if args_hash not in self.charts: self.charts.append(args_hash) self._p_changed = True return image_path