-
Notifications
You must be signed in to change notification settings - Fork 2
/
topicsBarDynamic.py
66 lines (58 loc) · 1.22 KB
/
topicsBarDynamic.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
jsonFile = open("tweets12.json", "r")
data = json.load(jsonFile)
jsonFile.close()
COLORS = {
1: '#4885ed',
2: "#db3236",
3: "#f4c20d",
4: "#4885ed",
5: "#3cba54",
6: '#4885ed',
7: "#db3236",
8: "#f4c20d",
9: "#4885ed",
10: "#3cba54"
}
# Docs is the list of results from the search
# I'm assuming it's a list of dictionaries just like data
def getTopics(docs):
all_topics = []
for tweet in docs:
all_topics.append(tweet['topic'])
return all_topics
def getLabels(docs):
labels = []
for tweet in docs:
t = tweet['topic']
if t not in labels:
labels.append(t)
return labels
def getValues(topic, docs):
value = 0
all_topics = getTopics(docs)
for t in all_topics:
if t == topic:
value += 1
return value
def barData(docs):
labels = getLabels(docs)
values = []
colors = []
n = 1
for l in labels:
val = getValues(l, docs)
values.append(val)
colors.append(COLORS[n])
n+=1
data = [{
'x': labels,
'y': values,
'type': 'bar',
'marker': {
'color': colors
}
}]
return data