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(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 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 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 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 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 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 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 queryData(ID=1): # connect # create a cursor cursor = db.cursor() #cursor.execute("SELECT available, readtime FROM occupancy WHERE site='%s'" % ID) cursor.execute("SELECT available, readtime FROM occupancy WHERE site='%s'" % ID) query = cursor.fetchall() log(query) # Set the vertical range from 0 to 100 max_y = 50 # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(800, 300, y_range=[0, max_y]) data, y_axis = ([[x[i] for x in query] for i in [0, 1]]) length = len(y_axis) if length > 20: step = abs(len(y_axis)/20) y_axis = y_axis[0:length:step] 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, 2) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM, y_axis) return chart
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 _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 _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 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 _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 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 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 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 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(): 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 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 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 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
max_y = 1000 # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(300, 325, y_range=[0, max_y]) # Add the chart data data = [] data.append(tests[0][2]) data.append(tests[1][2]) 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, \ [1,2])
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
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
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 size of 200x125 pixels and specifying the range for the Y axis 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'])
def drawline(date1,date2,date3,date4,abscissa): #Line Stripes # Set the vertical range from 0 to 50 max_y = 80 # Chart size of 200x125 pixels and specifying the range for the Y axis chart = SimpleLineChart(600, 400, y_range=[0, max_y]) # Add the chart data data_anquanbao = date1 chart.add_data(data_anquanbao) data_360wzb = date3 chart.add_data(data_360wzb) # Set the line colour to blue chart.set_colours(['0080FF','00FF00']) # 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) left_axis = range(0, max_y + 1, 2) left_axis[0] = '' chart.set_axis_labels(Axis.LEFT, left_axis) # X axis labels chart.set_axis_labels(Axis.BOTTOM,abscissa) chart.download('soup.png') #draw ns cut out line max_yo = 50 # Chart size of 200x125 pixels and specifying the range for the Y axis charto = SimpleLineChart(600, 400, y_range=[0, max_yo]) # Add the chart data data_anquanbaoo = date2 charto.add_data(data_anquanbaoo) data_360wzbo = date4 charto.add_data(data_360wzbo) # Set the line colour to blue charto.set_colours(['FF0000','FFFF00']) # Set the vertical stripes charto.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2) # Set the horizontal dotted lines charto.set_grid(0, 25, 5, 5) left_axis = range(0, max_yo + 1, 2) left_axis[0] = '' charto.set_axis_labels(Axis.LEFT, left_axis) # X axis labels charto.set_axis_labels(Axis.BOTTOM,abscissa) charto.download('soupout.png')