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 genfoodchart(start, end): '''Generate food chart with Pygal''' now = datetime.datetime.now().strftime("%Y-%m-%d") goodcount = session.query(Food) \ .filter_by(user_id=current_user.id) \ .filter(Food.food_date.between(start, end)).join(Ranks) \ .filter_by(rank=1).add_columns(Ranks.rank).count() okaycount = session.query(Food) \ .filter_by(user_id=current_user.id) \ .filter(Food.food_date.between(start, end)).join(Ranks) \ .filter_by(rank=2).add_columns(Ranks.rank).count() badcount = session.query(Food) \ .filter_by(user_id=current_user.id) \ .filter(Food.food_date.between(start, end)).join(Ranks) \ .filter_by(rank=3).add_columns(Ranks.rank).count() custom_style = Style( background='transparent', value_font_size=24, title_font_size=36, margin=1, 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 = True config.legend_at_bottom=True config.legend_at_bottom_columns=1 config.legend_box_size=10 config.human_readable = True config.fill = True config.style=custom_style config.print_labels=True config.print_values=True config.no_data_text='Need to add some food!' pie_chart = pygal.Pie(config) pie_chart.title = "Current Food Stats" pie_chart.add('Good', goodcount) pie_chart.add('Okay', okaycount) pie_chart.add('Bad', badcount) chart = pie_chart.render(is_unicode=True) return chart
def genfoodchart(start, end): '''Generate food chart with Pygal''' now = datetime.datetime.now().strftime("%Y-%m-%d") goodcount = session.query(Food) \ .filter_by(user_id=current_user.id) \ .filter(Food.food_date.between(start, end)).join(Ranks) \ .filter_by(rank=1).add_columns(Ranks.rank).count() okaycount = session.query(Food) \ .filter_by(user_id=current_user.id) \ .filter(Food.food_date.between(start, end)).join(Ranks) \ .filter_by(rank=2).add_columns(Ranks.rank).count() badcount = session.query(Food) \ .filter_by(user_id=current_user.id) \ .filter(Food.food_date.between(start, end)).join(Ranks) \ .filter_by(rank=3).add_columns(Ranks.rank).count() custom_style = Style( background='transparent', value_font_size=24, title_font_size=36, margin=1, 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 = True config.legend_at_bottom=True config.legend_at_bottom_columns=1 config.legend_box_size=10 config.human_readable = True config.fill = True config.style=custom_style config.print_labels=True config.print_values=True config.no_data_text='Need to add some food!' pie_chart = pygal.Pie(config) pie_chart.title = "Current Food Stats" pie_chart.add('Good', goodcount) pie_chart.add('Okay', okaycount) pie_chart.add('Bad', badcount) chart = pie_chart.render(is_unicode=True) return chart
def make_buildmd(allmetadata, fullreport, reportFile, metadataFolder): """ Creates several plots from some metadata and a report.md markdown file embedding the plots. """ allmetadata = pd.DataFrame(allmetadata) import pygal from pygal.style import CleanStyle from pygal.style import Style mystyle = CleanStyle(font_family="sans-serif") # Pygal chart configuration from pygal import Config config = Config() config.legend_at_bottom = True config.legend_at_bottom_columns = 3 config.print_values = True config.style = mystyle #config.width=500 #config.height=300 config.font_family = "Arial" # Donut chart for author genders genders = dict(Counter(allmetadata.loc[:, "au-gender"])) chart = pygal.Pie(config, title="Number of novels per author gender", inner_radius=.60, font_family="sans-serif") chart.add("male", genders["M"]) chart.add("female", genders["F"]) chart.add("other", 0) chart.render_to_file(join(metadataFolder, "au-genders.svg")) # Bar chart for time periods timeslots = dict(Counter(allmetadata.loc[:, "time-slot"])) chart = pygal.Bar(config, range=(0, 30), title="Number of novels per 20-year period", font_family="sans-serif", legend_at_bottom_columns=4) chart.add("1840-1859", timeslots["T1"]) chart.add("1860-1879", timeslots["T2"]) chart.add("1880-1899", timeslots["T3"]) chart.add("1900-1919", timeslots["T4"]) chart.render_to_file(join(metadataFolder, "timeslots.svg")) # Save report.md with embedded charts reportmd = "## Corpus composition for ELTeC-fra\n\n<img src=\"/Metadata/au-genders.svg\">\n<img src=\"/Metadata/timeslots.svg\">" with open(reportFile, "w", encoding="utf8") as outfile: outfile.write(reportmd)
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")