def overview_bars(companies, highlight=2, custom_height=True): x = [] y = [] colors = [cliqz_colors["purple"]] * highlight + [ cliqz_colors["inactive_gray"] ] * (len(companies) - highlight) for c in companies.itertuples(): name = c.name x.append(round(c.reach, 3)) y.append(name) data = [ go.Bar(x=x[::-1], y=y[::-1], marker={"color": colors[::-1]}, orientation='h') ] layout = go.Layout( dict(margin=set_margins(t=30, l=150), showlegend=False, autosize=True, height=custom_height if custom_height else None, xaxis=dict(color=cliqz_colors["gray_blue"], tickformat="%", anchor="free", position=0))) fig = dict(data=data, layout=layout) return div_output(fig)
def tracker_reach_ts(ts1, ts2, t): """ Timeseries area plot for two time-series Args: ts1: timeseries 1 ts2: timeseries 2 t: x-axis (time) Returns: hmtl output of an interactive timeseries plot """ trace0 = scatter(x=t, y=ts1, name="Domain Reach", color=cliqz_colors["purple"]) trace1 = scatter(x=t, y=ts2, name="Page Reach", color=cliqz_colors["blue"]) layout = go.Layout(margin=set_margins(t=30), height=300) # makes sure that whichever is smallest # will be on top (displaying color correctly) if mean(ts1) > mean(ts2): data = [trace0, trace1] else: data = [trace1, trace0] fig = dict(data=data, layout=layout) return div_output(fig)
def profile_doughnut(values, labels, name, color_palette=False): center_text = str(name) trace = go.Pie(values=values, labels=labels, hoverinfo="label", hole=0.65, textinfo="none", marker=dict(colors=set_category_colors(labels), line=dict(color=wtm_colors["white"], width=0))) data = [trace] layout = dict( showlegend=False, paper_bgcolor=wtm_colors["transparent"], plot_bgcolor=wtm_colors["transparent"], autosize=True, margin=set_margins(l=0, r=0, b=0, t=0, pad=10), # Center Text annotations=[ annotation(text=center_text.upper(), x=0.5, y=0.5, background_color=wtm_colors["transparent"], shift_x=0, text_size=30, color="#333") ]) fig = dict(data=data, layout=layout) return div_output(fig)
def sankey_plot(sndata): data_trace = dict( type='sankey', domain=dict(x=[0, 1], y=[0, 1]), hoverinfo="none", orientation="h", # valueformat=".0f", # valuesuffix="% of pages - present", node=dict(pad=10, thickness=30, label=list( map(lambda x: x.replace("_", " ").capitalize(), sndata['node']['label'])), color=sndata['node']['color']), link=dict( source=sndata['link']['source'], target=sndata['link']['target'], value=sndata['link']['value'], label=sndata['link']['label'], color=["#dedede" for _ in range(len(sndata['link']['source']))])) layout = dict( height=max(len(sndata['link']['source']) * 13, 400), font=dict(size=12), autosize=True, margin=set_margins(t=20, l=2, r=2), ) fig = dict(data=[data_trace], layout=layout) return div_output(fig)
def ts_trend(ts, t): """ Sparkline for plotting line Args: ts: timeseries data t: x-axis (time) Returns: hmtl output of an interactive timeseries plot """ y = list(map(lambda x: x * 100, ts)) trace0 = line( x=t, y=y, color=cliqz_colors["purple"] ) trace1 = line( x=[t[-1]], y=[y[-1]], color=cliqz_colors["purple"], mode='markers' ) layout = go.Layout( dict( showlegend=False, margin=set_margins(l=10, t=30, r=10), height=100, width=153, hoverlabel=dict( bgcolor=cliqz_colors["black"], bordercolor=cliqz_colors["transparent"], font=dict( family=CliqzFonts.mono, size=13, color=cliqz_colors["bright_gray"] ) ), xaxis=dict( autorange=True, showgrid=False, zeroline=False, showline=False, autotick=True, hoverformat="%b %y", ticks='', showticklabels=False ), yaxis=dict( range=[min(y) * 0.90, max(y) * 1.05 if max(y) != y[-1] else max(y) * 1.15], showgrid=False, zeroline=False, showline=False, autotick=True, ticks='', showticklabels=False ) ) ) data = [trace0, trace1] fig = dict(data=data, layout=layout) return div_output(fig)
def tracker_map(app, site_values, rectangles): print(app["overview"]["id"]) site_where_app = [s.get("site") for s in app.get("sites")] color_brewer = [cliqz_colors["red"] if t[0] in site_where_app else cliqz_colors["bright_gray"] for t in site_values] shapes = [] counter = 0 for r in rectangles: shapes.append( dict( type='rect', x0=r['x'], y0=r['y'], x1=r['x'] + r['dx'], y1=r['y'] + r['dy'], line=dict( color=cliqz_colors["white"], width=0.5 ), fillcolor=color_brewer[counter] ) ) counter = counter + 1 if counter >= len(color_brewer): counter = 0 # For hover text trace0 = go.Scatter( x=[r['x'] + (r['dx'] / 2) for r in rectangles], y=[r['y'] + (r['dy'] / 2) for r in rectangles], text=[v[0] for v in site_values], mode='markers', hoverinfo="text" ) layout = dict( autosize=True, xaxis=dict(showgrid=False, zeroline=False, showticklabels=False), yaxis=dict(showgrid=False, zeroline=False, showticklabels=False), shapes=shapes, hovermode='closest', margin=set_margins(t=0, l=0, r=0) ) figure = dict(data=[trace0], layout=layout) return div_output(figure)
def tracker_cfh(https, fingerprinting, cookies): """ Horizontal Bar chart plot for cookies, fingerprinting and https per tracker Args: https: Requests to tracker that use https fingerprinting: Requests to tracker that could fingerprint cookies: Requests to tracker that use cookies for tracking Returns: Horizontal bar chart wrapped in a div """ trace1 = hbar( label="Yes", https=https, fingerprinting=fingerprinting, cookies=cookies ) trace2 = hbar( label="No", color=cliqz_colors["bright_gray"], https=1 - https, fingerprinting=1 - fingerprinting, cookies=1 - cookies ) data = [trace1, trace2] layout = go.Layout( paper_bgcolor=cliqz_colors["transparent"], plot_bgcolor=cliqz_colors["transparent"], autosize=True, barmode="stack", margin=set_margins(l=120, r=100, t=20), height=150, yaxis=dict( showticklabels=True, tickfont=dict( family=CliqzFonts.regular, size=13, color=cliqz_colors["black"] ), )) fig = go.Figure(data=data, layout=layout) return div_output(fig)
def doughnut_chart(values, labels, name, color_palette=False): """ Doughnut pie chart with text in the middle. Args: values: (list) - values for the pie chart labels: (list) - corresponding to the values name: (str) - text that goes in the middle of the chart Returns: Doughnut pie chart wrapped in a div """ trace = go.Pie( values=values, labels=labels, name=str(name), hoverinfo="label+percent", hole=0.65, pull=0.07, sort=not (color_palette), textinfo="label", textfont=dict(family=CliqzFonts.regular, color=cliqz_colors["white"]), marker=dict( colors=palette(cliqz_colors["blue"], cliqz_colors["purple"], len(labels)) if color_palette else [cliqz_colors["blue"], cliqz_colors["purple"]], line=dict(color=cliqz_colors["white"], width=2))) data = [trace] layout = dict(showlegend=False, paper_bgcolor=cliqz_colors["transparent"], plot_bgcolor=cliqz_colors["transparent"], autosize=True, margin=set_margins(), annotations=[ annotation(text=str(name).upper(), x=0.5, y=0.5, background_color=cliqz_colors["transparent"], shift_x=0, text_size=14) ]) fig = dict(data=data, layout=layout) return div_output(fig)
def overview_reach(companies): data = [] annotations = [] for c in companies: color = random_color() ts = [datetime.strptime(t["ts"], "%Y-%m") for t in c["history"]] name = c["overview"]["id"].capitalize() y = [t['reach']*100 for t in c["history"]] data.append( scatter( x=ts, y=y, fill=False, name=name, color=color ) ) annotations.append( overview_label(text=name, x=ts[-1], y=y[-1], color=color) ) layout = go.Layout( dict( yaxis=dict( title="Percentage of sites where company can track", titlefont=dict( size=12, color="#666666" )), margin=set_margins(r=90), legend=dict( x=0, y=50, orientation="h" ), # annotations=annotations ) ) fig = dict(data=data, layout=layout) return div_output(fig)
def overview_bars(companies): x = [] y = [] colors = [cliqz_colors["purple"]] * 2 + [cliqz_colors["inactive_gray"]] * 8 for c in companies: name = c["name"] x.append(round(c["history"][-1]["reach"], 3)) y.append(name) data = [ go.Bar(x=x[::-1], y=y[::-1], marker={"color": colors[::-1]}, orientation='h') ] layout = go.Layout( dict(margin=set_margins(t=30, l=150), showlegend=False, xaxis=dict(color=cliqz_colors["gray_blue"], tickformat="%", anchor="free", position=0))) fig = dict(data=data, layout=layout) return div_output(fig)
def treemap(): x = 0. y = 0. width = 100. height = 100. values = [500, 433, 78, 25, 25, 7] normed = squarify.normalize_sizes(values, width, height) rects = squarify.squarify(normed, x, y, width, height) # Choose colors from http://colorbrewer2.org/ under "Export" color_brewer = [ 'rgb(166,206,227)', 'rgb(31,120,180)', 'rgb(178,223,138)', 'rgb(51,160,44)', 'rgb(251,154,153)', 'rgb(227,26,28)' ] shapes = [] annotations = [] counter = 0 for r in rects: shapes.append( dict(type='rect', x0=r['x'], y0=r['y'], x1=r['x'] + r['dx'], y1=r['y'] + r['dy'], line=dict(width=2), fillcolor=color_brewer[counter])) annotations.append( dict(x=r['x'] + (r['dx'] / 2), y=r['y'] + (r['dy'] / 2), text=values[counter], showarrow=False)) counter = counter + 1 if counter >= len(color_brewer): counter = 0 # For hover text trace0 = go.Scatter( x=[r['x'] + (r['dx'] / 2) for r in rects], y=[r['y'] + (r['dy'] / 2) for r in rects], text=[str(v) for v in values], mode='text', ) layout = dict(height=700, width=700, xaxis=dict(showgrid=False, zeroline=False), yaxis=dict(showgrid=False, zeroline=False), shapes=shapes, annotations=annotations, hovermode='closest') # With hovertext figure = dict(data=[trace0], layout=layout) # Without hovertext # figure = dict(data=[Scatter()], layout=layout) return div_output(figure)