-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.py
144 lines (110 loc) · 3.89 KB
/
charts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from collections import Counter
import base64
import pandas as pd
from io import BytesIO
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import plotly.express as px
from chatvisualizer import dfmain, emoji_df
def chart1(df):
"""Generate time series figure chart."""
date_df = df.groupby("Date").sum()
date_df.reset_index(inplace=True)
fig = px.line(date_df, x="Date", y="Word's", title='Number of Words as time moves on.')
fig.update_xaxes(nticks=20)
return fig
def chart2(df):
fig = px.pie(emoji_df, values='count', names='emoji',
title='Emoji Distribution')
fig.update_traces(textposition='inside', textinfo='percent+label')
return fig
def dayofweek(i):
l = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
return l[i];
def chart3(df):
"""Daywise Distribution"""
day_df=pd.DataFrame(df["Message"])
day_df['day_of_date'] = df['Date'].dt.weekday
day_df['day_of_date'] = day_df["day_of_date"].apply(dayofweek)
day_df["messagecount"] = 1
day = day_df.groupby("day_of_date").sum()
day.reset_index(inplace=True)
fig = px.line_polar(day, r='messagecount', theta='day_of_date', line_close=True)
fig.update_traces(fill='toself')
fig.update_layout(
polar=dict(
radialaxis=dict(
visible=True,
range=[0,250]
)),
showlegend=False
)
return fig
def chart4(df):
"""Generate WordCloud."""
text = " ".join(review for review in df.Message).strip()
list_words = text.lower().split(" ")
stopwords = set(STOPWORDS)
counter = dict(Counter(list_words))
counter = {key: counter[key] for key in counter if key not in stopwords and key.isalpha()}
wc = WordCloud(stopwords=stopwords, prefer_horizontal=0.9, colormap='tab10', background_color='white', min_font_size=10, width=1000, height=250, scale=2)
wc_img = wc.generate_from_frequencies(frequencies=counter).to_image()
with BytesIO() as buffer:
wc_img.save(buffer, 'png')
img = "data:image/png;base64," + base64.b64encode(buffer.getvalue()).decode()
return img
def chart5(df):
"""Most happening Days."""
vr = df['Date'].value_counts().to_frame().head(10)
# Barplot
fig = px.bar(vr,
y='Date',
color="Date",
labels = {'index' : 'Date', 'Date' : 'Number of Messages'},
title = "Most happening Days"
)
return fig
def chart6(df):
"""When are group members Most active?."""
vr = df['Time'].value_counts().to_frame().head(10)
# Barplot
fig = px.bar(vr,
y='Time',
color="Time",
labels = {'index' : 'Time', 'Time' : 'Number of Messages'},
title = "Most Active Times"
)
return fig
def chart7(df):
"""Most Active Author in the Group."""
vr = df['Author'].value_counts().to_frame().head(10)
# Barplot
fig = px.bar(vr,
y='Author',
color="Author",
labels = {'index' : 'Authors', 'Author' : 'No. Of Messages'},
title = "Most Active Members of the Group."
)
return fig
def chart8(df):
"""Most active day in the week."""
vr = df['Day'].value_counts().to_frame().head(10)
# Barplot
fig = px.bar(vr,
y='Day',
color="Day",
labels = {'index' : 'Day', 'Day' : 'No. Of Messages'},
title = "Mostly active day of Week in the Group"
)
return fig
def chart9(df):
"""Top-10 media contributor of Group."""
mm = df[df['Message'] == r' <Media omitted>']
vr = mm['Author'].value_counts().to_frame().head(10)
# Barplot
fig = px.bar(vr,
y='Author',
color="Author",
labels = {'index' : 'Author', 'Author' : 'No. Of Messages'},
title = "Top-10 media contributor of Group"
)
return fig