From 944bc4e529fe1b3d9f3fe5f17514a28ca4e10772 Mon Sep 17 00:00:00 2001 From: "adsam.job" Date: Mon, 29 Jul 2024 13:07:46 +0300 Subject: [PATCH] queryset => dataframe => plot --- df_for_plotly | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 df_for_plotly diff --git a/df_for_plotly b/df_for_plotly new file mode 100644 index 0000000..60eb497 --- /dev/null +++ b/df_for_plotly @@ -0,0 +1,55 @@ +from django_pandas.io import read_frame +import plotly.express as px +import uuid + + +def pie(df, values, names): + #https://plotly.com/python/pie-charts/ + + fig = px.pie(df, + values=values, + names=names, + #hover_data=['lifeExp'] + ) + #title=None, orientation="h", y=1, yanchor="bottom", x=0.5, xanchor="center"\ + legend=dict( + x=0.9, + y=0.9, + xref="container", + yref="container", + bgcolor="Gold", + ) + fig.update_layout( + font_family="Rockwell", + legend=dict( + #title=None, orientation="h", y=1, yanchor="bottom", x=0.5, xanchor="center" + x=0.9, + y=0.9, + xref="container", + yref="container", + bgcolor="Gold", + ) + ) + config = {'displayModeBar': False} + plot = fig.to_html(config=config, full_html=False, div_id=f"pie_plot_{uuid.uuid4()}", default_width="100%") + return plot + + +def bars(df, oy, ox, horizontal=False): + + fig = px.bar(df, + x=ox, + y=oy, + #color="medal", + orientation='h' if horizontal else 'v', + ) + config = {'displayModeBar': False} + plot = fig.to_html(config=config, full_html=False, div_id=f"bar_plot_{uuid.uuid4()}", default_width="100%") + return plot + +def touched_view(request): + + touched_data = Author.objects.values('update_touch_date').annotate(co=Count('update_touch_date')).order_by('update_touch_date').values('co', 'update_touch_date') + + touched_df = read_frame(qs=touched_data) + context['authors_bars'] = bars(touched_df, ox="update_touch_date", oy="co")