-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_chart.py
More file actions
40 lines (36 loc) · 1.22 KB
/
plot_chart.py
File metadata and controls
40 lines (36 loc) · 1.22 KB
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
import time
from kafka import KafkaConsumer
from threading import Thread
from os import path
import json
import matplotlib.pyplot as plt
import matplotlib.animation as anim
kafka_consumer = KafkaConsumer('sensex-events', group_id='sensex-chart-maker')
def consumer():
print('Starting consumer')
for message in kafka_consumer:
decoded_message = message.value.decode('utf-8')
if path.isfile('./sensex.txt'):
with open('sensex.txt', 'a', encoding='utf-8') as text_file:
print(decoded_message, file = text_file)
else:
with open('sensex.txt', 'w', encoding='utf-8') as text_file:
print(decoded_message, file = text_file)
def animate(fig):
data = open('./sensex.txt', 'r').read()
data = data.split('\n')
x = []
y = []
for points in data:
points = points.strip()
if (points != None) and (points != ''):
point = json.loads(points)
y.append(point.get('currentValue'))
x.append(point.get('lastRefreshed'))
plt.plot(x, y)
consumer_thread = Thread(target=consumer)
consumer_thread.start()
time.sleep(5)
fig = plt.figure()
line_chart = anim.FuncAnimation(fig, animate, interval=3000)
plt.show()