def renderGraph(self,graphingParams): config = Config() config.legend_at_bottom = True config.legend_at_bottom_columns = 2 config.margin_bottom = 30 aStyle = DefaultStyle(font_family='Arial',colors=self.getColors(),background='rgba(255,255,255,1)') pie_chart = pygal.Pie(config,style=aStyle) pie_chart.title = graphingParams.get('title','') for each in graphingParams.get('items',[]): descr = each.get('descr','') value = each.get('value',0.0) pie_chart.add(descr,value) return pie_chart.render()
def genweightchart(): '''Generate weight chart with Pygal''' weighthistory = session.query(Weight) \ .filter_by(user_id=current_user.id) \ .order_by(Weight.id.asc()) \ .all() maxweight = session.query(Weight) \ .filter_by(user_id=current_user.id) \ .order_by(Weight.weight.desc()) \ .first() if maxweight is None: maxrange = 100 else: maxrange = (int(maxweight.weight) + 5) custom_style = Style( background='transparent', value_font_size=24, title_font_size=36, label_font_size=24, margin=5, plot_background='transparent', foreground='#53E89B', foreground_strong='#53A0E8', foreground_subtle='#630C0D', opacity='.6', opacity_hover='.9', transition='400ms ease-in', colors=('#5cb85c', '#f0ad4e', '#d9534f')) config = Config() config.show_legend = False config.y_labels = range(0, maxrange, 5) config.margin_bottom=50 config.human_readable = True config.fill = True config.style=custom_style config.print_labels=True config.no_data_text='Add weight measurements!' wlist = [] for entry in enumerate(weighthistory): wlist.append(entry[1].weight) line_chart = pygal.Line(config) line_chart.title = "Weight History" line_chart.add('Values', wlist) chart = line_chart.render(is_unicode=True) return chart
def make_image(data, filepath, title, labels, major_labels): custom_style = Style(background='#fff', plot_background='transparent', title_font_size=14, guide_stroke_dasharray='1,0', major_guide_stroke_dasharray='1,0', foreground='rgba(0, 0, 0, .87)', foreground_strong='rgba(0, 0, 0, .87)', foreground_subtle='rgba(0, 0, 0, .87)', stroke_opacity='1', stroke_opacity_hover='1', stroke_width=10, stroke_width_hover=10, opacity='1', opacity_hover='1', colors=('#C5D4B5BB', '#3D7930')) print(custom_style.to_dict()) r = get_range(data) config = Config() config.interpolate = 'cubic' config.style = custom_style config.width = 400 config.height = 225 config.explicit_size = True config.margin_left = 0 config.margin_right = 0 config.margin_top = 10 config.margin_bottom = 30 config.show_minor_x_labels = False config.truncate_label = -1 config.show_legend = False config.include_x_axis = True config.range = r config.show_dots = False chart = pygal.Line(config) chart.title = ("Throughput (%s, Mb/s)" % (title)) chart.x_labels = labels chart.x_labels_major = major_labels chart.y_labels = [ x for x in range(0, r[1] + 1, 100 if (r[1] > 550) else 50) ] chart.add(None, data, fill=True) chart.add(None, data) #with open(filepath, 'w') as output: # output.write(chart.render()) chart.render_to_png(filepath)
def draw_line(valxy): import pygal from pygal import Config from pygal.style import Style config = Config() custom_style = Style(background='white', foreground='#000', foreground_strong='#000', foreground_subtle='#000', opacity='.9', opacity_hover='.6', plot_background='#fff', transition='100ms', label_font_size=12, major_label_font_size=12, value_font_family='Arial', font_family='Arial', major_label_font_family='Sans', colors=('#3333CC', '#3333CC', '#3333CC', '#3333CC'), guide_stroke_dasharray='2,7', major_guide_stroke_dasharray='2,7') config.margin_left = 0 config.margin_bottom = 0 config.width = 350 config.height = 350 config.x_title = "Angle, degrees" config.y_title = "Overlaps, %" config.explicit_size = True config.show_x_guides = False config.show_y_guides = True xy_chart = pygal.XY(config, show_legend=False, dots_size=3, style=custom_style) #interpolate='quadratic', #xy_chart.title = "Overlaps in oscillation range" xy_chart.add('', valxy) #xy_chart.y_labels = [int(phibg), int(phend)] # col=20 # stp=1 # for val in range(int(phibg), int(phend), stp): # xy_chart.add("", [{'value': val, 'color': 'rgb(%s, 0, 0)' % (col)}], # stroke_style={'width': 1, 'linejoin': 'round', 'linecap': 'round', 'linejoin': 'round'}) # col=col+stp return xy_chart.render(is_unicode=True)
def create_chartguardian(id_station, station): data_status = dict() data_time = dict() list_status = list() list_time = list() station_name = str() station_id = str() data_down = list() data_up = list() data_label = list() last_problem = str() status_alert = str() provider_alert = str() mediatype_alert = str() streamuri_alert = str() stationid_alert = str() ts = int(time.time()) date_query = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d') with drt.connect(db=data_config[env_app]['RETHINK-DB']) as conn: cursor_up = drt.table(data_config[env_app]['RETHINK-TABLE']).filter((drt.row["date"] == date_query) & (drt.row["station_id"] == id_station)).run(conn) cursor_down = drt.table(data_config[env_app]['RETHINK-TABLE']).filter((drt.row["date"] == date_query) & (drt.row["station_id"] == id_station)).run(conn) for document_down in cursor_down: station_name = document_down["station_name"] station_id = document_down["station_id"] if len(document_down["status_array"]) < 31: print("Size array status DOWN") print(len(document_down["status_array"])) start_point = 0 else: start_point = len(document_down["status_array"]) - 31 stat_array = document_down["status_array"] provider_alert = document_down["provider"] mediatype_alert = stream_config[env_app][station]['media-type'] streamuri_alert = stream_config[env_app][station]['uri-stream'] stationid_alert = id_station for index, item in enumerate(stat_array[start_point:]): status_len = len(document_down["status_array"]) ts = int(item["time"]) date_down = datetime.datetime.fromtimestamp(ts).strftime('%H:%M:%S') #[None, None, None, None, None, None, {'value': 0, 'label': '10:10:10'} if item["status"] == "down": item_down = { 'value': 0, 'label': date_down } item_label = date_down data_down.append(item_down) data_label.append(item_label) last_problem = date_down status_alert = item["status"] else: item_none = None data_down.append(item_none) item_label = None data_label.append(item_label) ts = int(item["time"]) date_day = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d') data_status.update({ index: item["status"] }) data_time.update({ index: date_day }) list_status.append(item["status"]) list_time.append(date_day) for document_up in cursor_up: print("==== document_up ===") print(document_up) station_name = document_up["station_name"] station_id = document_up["station_id"] if len(document_up["status_array"]) < 31: print("Size array status UP") len(document_up["status_array"]) start_point = 0 else: start_point = len(document_up["status_array"]) - 31 stat_array = document_up["status_array"] for index, item in enumerate(stat_array[start_point:]): status_len = len(document_up["status_array"]) #{'value': 1, 'label': ''} if item["status"] == "up": print("====== ITEM STATUS =====") print(item) status_val = 1 status_label = "Sin Problemas" item_up = { 'value': 1, 'label': '' } data_up.append(item_up) else: status_val = 0 status_label = "Problemas" item_down = None data_up.append(item_down) try: custom_style = Style( opacity='.6', opacity_hover='.9', transition='400ms ease-in', label_font_family='Liberation Mono', label_font_size=8, major_label_font_size=10, colors=('#e74c3c', '#2980b9', '#e67e22', '#E87653', '#E89B53')) configpg = Config() configpg.human_readable = True configpg.fill = False configpg.x_title='Ultimo Problema: ' + last_problem configpg.show_y_labels=False configpg.legend_at_bottom_columns=False configpg.legend_at_bottom=True configpg.width=850 configpg.height=400 configpg.print_labels=False configpg.margin_bottom=30 configpg.x_label_rotation=60 configpg.range=(0, 1) configpg.show_y_labels=True configpg.pretty_print=True extra_args = { 'ACL': 'public-read', 'CacheControl': 'no-cache, no-store, must-revalidate' } line_chart = pygal.Line(style=custom_style, config=configpg) line_chart.title = station_name + ' - ' + date_query line_chart.x_labels = (data_label) line_chart.x_labels_major = (data_label) line_chart.add('Problemas', data_down , allow_interruptions=True, dots_size=5, stroke_style={'width': 4, 'linecap': 'round', 'linejoin': 'round'}) line_chart.add('Activo',data_up , allow_interruptions=True, show_dots=True, dots_size=5, stroke_style={'width': 4, 'linecap': 'round', 'linejoin': 'round'}) line_chart.add('Rango: 30 minutos', None) line_chart.render_to_png(station_id + '-' + date_query + '.png') #connection.close() #connection.close() upload_s3 = s3.upload_file(station_id + '-' + date_query + '.png', bucket_name, env_app+'/'+station_id + '-' + str(ts) + '.png', ExtraArgs=extra_args) os.remove(station_id + '-' + date_query + '.png') print(last_problem) alert_data = { 's3_key': env_app+'/'+station_id + '-' + str(ts) + '.png', 'time_alert': last_problem } if status_alert == "down": #alert_error(status_alert, station_name, provider_alert, ts, mediatype_alert, streamuri_alert, stationid_alert, date_query, alert_data) streaming_alert(status_alert, station_name, provider_alert, ts, mediatype_alert, streamuri_alert, stationid_alert, date_query, alert_data) return alert_data else: message = "No se tiene data que mostrar" print(message) return message except IOError as e: print("Se tiene un problema") print(e) message = e return message #create_chartguardian("tv-capital", "capital-tv")