convenience/df_for_plotly
2024-07-29 13:07:46 +03:00

56 lines
1.7 KiB
Plaintext

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")