def graph(graph, type, year, month): """Create graphs with the parameters passed.""" db = get_db() now = datetime.now() timestamp = now.timestamp() time_first_day = datetime(year, month, 1).timestamp() first_day = datetime(year, month, 1) cal = calendar.monthrange(year, month) time_last_day = datetime(year, month, cal[1], 23, 59, 59).timestamp() last_day = datetime(year, month, cal[1]) if graph in ['ligne', 'histogramme']: line_chart = Line(interpolate='cubic') if graph == 'ligne' else Bar() if type == 'hour': line_chart.title = ( 'Somme des hoquets par heure en %s' % calendar.month_name[month]) requete = db.execute( 'select CAST(strftime(\'%H\', datetime(moment, \'unixepoch\',' ' \'localtime\')) as integer) as hour, count(*) from ok ' 'where moment between (?) and (?) group by hour', [time_first_day, time_last_day]) hoquet = dict(requete) line_chart.x_labels = [ str(hour) for hour, val in sorted(hoquet.items())] line_chart.add('Annabelle', [ val for hour, val in sorted(hoquet.items())]) else: hoquet = [] x_labels = [] x_labels_major = [] line_chart.title = ( 'Nombre de hoquets par jour en %s' % calendar.month_name[month]) for i,day in enumerate(range (int(time_first_day), int(time_last_day), 86400)): if is_in_weekend(day): requete = None else: requete = db.execute( 'select count(*) from ok where moment >= (?) ' 'and moment <= (?)', [day, day+86400]).fetchone()[0] x_labels_major.append(i+1) x_labels.append(i+1) hoquet.append(requete) line_chart.x_labels = map(str, x_labels) line_chart.x_labels_major = list(map(str, x_labels_major)) line_chart.add('Annabelle', hoquet) return line_chart.render_response() return gauge_chart.render_response()
def test_simple_line(): """Simple line test""" line = Line() rng = range(-30, 31, 5) line.add('test1', [cos(x / 10) for x in rng]) line.add('test2', [sin(x / 10) for x in rng]) line.add('test3', [cos(x / 10) - sin(x / 10) for x in rng]) line.x_labels = map(str, rng) line.title = "cos sin and cos - sin" q = line.render_pyquery() assert len(q(".axis.x")) == 1 assert len(q(".axis.y")) == 1 assert len(q(".plot .series path")) == 3 assert len(q(".legend")) == 3 assert len(q(".x.axis .guides")) == 13 assert len(q(".y.axis .guides")) == 13 assert len(q(".dots")) == 3 * 13 assert q(".axis.x text").map(texts) == [ '-30', '-25', '-20', '-15', '-10', '-5', '0', '5', '10', '15', '20', '25', '30' ] assert q(".axis.y text").map(texts) == [ '-1.2', '-1', '-0.8', '-0.6', '-0.4', '-0.2', '0', '0.2', '0.4', '0.6', '0.8', '1', '1.2' ] assert q(".title").text() == 'cos sin and cos - sin' assert q(".legend text").map(texts) == ['test1', 'test2', 'test3']
def test_simple_line(): """Simple line test""" line = Line() rng = range(-30, 31, 5) line.add('test1', [cos(x / 10) for x in rng]) line.add('test2', [sin(x / 10) for x in rng]) line.add('test3', [cos(x / 10) - sin(x / 10) for x in rng]) line.x_labels = map(str, rng) line.title = "cos sin and cos - sin" q = line.render_pyquery() assert len(q(".axis.x")) == 1 assert len(q(".axis.y")) == 1 assert len(q(".plot .series path")) == 3 assert len(q(".legend")) == 3 assert len(q(".x.axis .guides")) == 13 assert len(q(".y.axis .guides")) == 13 assert len(q(".dots")) == 3 * 13 assert q(".axis.x text").map(texts) == [ '-30', '-25', '-20', '-15', '-10', '-5', '0', '5', '10', '15', '20', '25', '30'] assert q(".axis.y text").map(texts) == [ '-1.2', '-1', '-0.8', '-0.6', '-0.4', '-0.2', '0', '0.2', '0.4', '0.6', '0.8', '1', '1.2'] assert q(".title").text() == 'cos sin and cos - sin' assert q(".legend text").map(texts) == ['test1', 'test2', 'test3']
def draw_number_line(name, list_x, list_y, max_y = 0): line = Line() line.disable_xml_declaration = True line.js = [] line.x_label_rotation = 45 line.x_labels_major_count = 31 #line.y_labels_major_every = 3 line.show_legend = False line.print_values = False line.print_zeroes = False line.width = 1280 line.height = 720 line.value_formatter = lambda x:str(int(x)) #line.major_label_font_size = 20 #line.print_zeroes = True line.show_minor_x_labels = False #line.show_minor_y_labels = True line.show_dots = False #line.interpolate = 'hermite' line.title = name list_int_y = [0] for y in list_y: if y != None: list_int_y.append(y) line.range = (0, max(int(max(list_int_y)*1.1), max_y)) line.x_labels = list_x #line.y_labels = map(lambda x:x*100, range(int((min(list_y)/100)-1), int((max(list_y)/100+4)))) line.add(name, list_y) return line.render()
def draw_multi_price_line(name, dict_data): line = Line() line.disable_xml_declaration = True line.js = [] line.x_label_rotation = 45 line.y_labels_major_every = 3 #line.show_legend = False line.legend_at_bottom = True line.print_values = False line.width = 1280 line.height = 720 line.value_formatter = lambda x:str(int(x)) #line.major_label_font_size = 20 #line.print_zeroes = True #line.show_minor_x_labels = True #line.show_minor_y_labels = True line.title = name min_y = None max_y = None for name in dict_data: #print(name) list_x, list_y = dict_data[name] line.add(name, list_y) min_list_y = min(list_y) max_list_y = max(list_y) if min_y == None or min_list_y < min_y : min_y = min_list_y if max_y == None or max_list_y > max_y : max_y = max_list_y if min_y == None : min_y = 0 if max_y == None : max_y = 0 line.range = (min_y-100, max_y+300) line.x_labels = map(str, list_x) line.y_labels = map(lambda x:x*100, range(int((min_y/100)-1), int((max_y/100+4)))) return line.render()
def draw_price_line(name, list_x, list_y): line = Line() line.disable_xml_declaration = True line.js = [] line.x_label_rotation = 45 line.y_labels_major_every = 3 line.show_legend = False line.print_values = False line.width = 1280 line.height = 720 line.value_formatter = lambda x:str(int(x)) #line.major_label_font_size = 20 #line.print_zeroes = True #line.show_minor_x_labels = True #line.show_minor_y_labels = True line.title = name digital_y = list(filter(lambda x:x != None, list_y)) if digital_y == [] : digital_y = [0] line.range = (min(digital_y)-100, max(digital_y)+300) line.x_labels = list_x line.y_labels = map(lambda x:x*100, range(int((min(digital_y)/100)-1), int((max(digital_y)/100+4)))) line.add(name, list_y) return line.render()
def graphing(budget_list): the_graph = Line() the_graph.title = 'Maximum profits based on given prices' the_graph.x_labels = sorted(budget_list) the_graph.add('Profits', all_profits_from_prices(budget_list)) # option to save to local dir # the_graph.render_to_file('profits.svg') return the_graph.render_data_uri()
def word_count(text_sample_directory): print "Opening '" + text_sample_directory + "'" # Dictionary of each word that gets encountered words = {} for text_file in os.listdir(text_sample_directory): with open(text_sample_directory + text_file) as f: sys.stdout.write(".") sys.stdout.flush() for line in f: line = line.lower() line = clean(line) replace_characters = '0123456789~-+=!?@#$%^&*:;<>(){}[]|\/\'`"_,.' for character in replace_characters: line = line.replace(character, "") word_list = line.split(" ") for word in word_list: if word and word in words: words[word] += 1 else: words[word] = 1 output_name = text_sample_directory.strip("/") writer = csv.writer(open(output_name + ".csv", "wb")) sorted_dict = sorted(words.items(), key=lambda x:x[1], reverse=True) # Get the count of occurance for the word that occurs the most max_value = int(sorted_dict[0][1]) keys = [] values = [] for key, value in sorted_dict: keys.append(key) values.append(value) writer.writerow([key, value]) line_chart = Line() line_chart.title = "Word Occurance Graph" line_chart.x_labels = keys line_chart.add(output_name, values) svg_data = line_chart.render() graphfile = open(output_name + ".svg", "w") graphfile.write(svg_data) print "\nWriting '" + output_name + "'"
def test_line(): line = Line() rng = [8, 12, 23, 73, 39, 57] line.add('Single serie', rng) line.title = "One serie" q = line.render_pyquery() assert len(q(".axis.x")) == 0 assert len(q(".axis.y")) == 1 assert len(q(".plot .series path")) == 1 assert len(q(".x.axis .guides")) == 0 assert len(q(".y.axis .guides")) == 7
def word_count(text_sample_directory): print "Opening '" + text_sample_directory + "'" # Dictionary of each word that gets encountered words = {} for text_file in os.listdir(text_sample_directory): with open(text_sample_directory + text_file) as f: sys.stdout.write(".") sys.stdout.flush() for line in f: line = line.lower() line = clean(line) replace_characters = '0123456789~-+=!?@#$%^&*:;<>(){}[]|\/\'`"_,.' for character in replace_characters: line = line.replace(character, "") word_list = line.split(" ") for word in word_list: if word and word in words: words[word] += 1 else: words[word] = 1 output_name = text_sample_directory.strip("/") writer = csv.writer(open(output_name + ".csv", "wb")) sorted_dict = sorted(words.items(), key=lambda x: x[1], reverse=True) # Get the count of occurance for the word that occurs the most max_value = int(sorted_dict[0][1]) keys = [] values = [] for key, value in sorted_dict: keys.append(key) values.append(value) writer.writerow([key, value]) line_chart = Line() line_chart.title = "Word Occurance Graph" line_chart.x_labels = keys line_chart.add(output_name, values) svg_data = line_chart.render() graphfile = open(output_name + ".svg", "w") graphfile.write(svg_data) print "\nWriting '" + output_name + "'"
def test_line_secondary(): line = Line() rng = [8, 12, 23, 73, 39, 57] line.add('First serie', rng) line.add('Secondary serie', map(lambda x: x * 2, rng), secondary=True) line.title = "One serie" q = line.render_pyquery() assert len(q(".axis.x")) == 0 assert len(q(".axis.y")) == 1 assert len(q(".plot .series path")) == 2 assert len(q(".x.axis .guides")) == 0 assert len(q(".y.axis .guides")) == 7
def test_line_secondary(): """Test line with a secondary serie""" line = Line() rng = [8, 12, 23, 73, 39, 57] line.add('First serie', rng) line.add('Secondary serie', map(lambda x: x * 2, rng), secondary=True) line.title = "One serie" q = line.render_pyquery() assert len(q(".axis.x")) == 0 assert len(q(".axis.y")) == 1 assert len(q(".plot .series path")) == 2 assert len(q(".x.axis .guides")) == 0 assert len(q(".y.axis .guides")) == 7
def generate_chart(data: pd.DataFrame) -> Line: line_chart = Line( js=(), # The tooltips are really nice, but I don't want any JS. style=GruvboxStyle, x_label_rotation=30) # Water those datetimes down so they don't overlap and we can read them! datetimes = data['Datetime'] dilution_factor = datetimes.shape[0] // 10 datetimes = dilute_datetimes(datetimes, factor=dilution_factor) line_chart.title = 'HTTP GET by IP vs. HTTP GET by Hostname' line_chart.y_title = 'Seconds' line_chart.x_labels = datetimes line_chart.add(title='By IP', values=data['Seconds for HTTP GET by IP']) line_chart.add(title='By Hostname', values=data['Seconds for HTTP GET by Hostname']) return line_chart
def get_line_chart(self, title, lines): """Make chart of type line and return it. title: Title of chart (string) step: x label step and poll interval (integer) start: x label start value (integer) stop: x label end value and end of polling (integer) lines: tuple or list of tuples or lists (line_name, data_points) line_name (string) data_points (list of ints) returns line_chart (pygal.Line()) """ chart = Line() chart.title = title chart.x_labels = self.points if type(lines[0]) == type(str()): lines = list(lines) for line in lines: label = line[0] data = line[1] chart.add(label, data) return chart