import json
import pygal
import math
from itertools import groupby
filename='btc_close_2017.json'
with open (filename) as f:
btc_data = json.load(f)
dates = []
months = []
weeks =[]
weekdays = []
close = []
for btc_dict in btc_data:
dates.append(btc_dict['date'])
months.append(int(btc_dict['month']))
weeks.append(int(btc_dict['week']))
weekdays.append(btc_dict['weekday'])
close.append(int(float(btc_dict['close'])))
line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False)
line_chart.title = ' (¥)'
line_chart.x_labels = dates
N = 20
line_chart.x_labels_major = dates[::N]
line_chart.add(' ', close)
line_chart.render_to_file(' (¥).svg')
line_chart = pygal.Line(x_label_rotation=20,show_minor_x_labels=False)
line_chart.title = ' (dollar)'
line_chart.x_labels = dates
N=20
line_chart.x_labels_major = dates[::N]
close_log = [math.log10(_) for _ in close]
line_chart.add('log ',close_log)
line_chart.render_to_file(' (¥).svg')
def draw_line(x_data,y_data,title,y_legend):
xy_map=[]
for x,y in groupby(sorted(zip(x_data,y_data)),key=lambda _: _[0]):
y_list = [v for _, v in y]
xy_map.append([x,sum(y_list)/len(y_list)])
x_unique,y_mean=[*zip(*xy_map)]
line_chart = pygal.Line()
line_chart._title=title
line_chart.x_labels=x_unique
line_chart.add(y_legend,y_mean)
line_chart.render_to_file(title+'.svg')
return line_chart
idx_month = dates.index('2017-12-01')
line_chart_month = draw_line(
months[:idx_month], close[:idx_month], ' (¥)', ' ')
line_chart_month
idx_week = dates.index('2017-12-11')
line_chart_week = draw_line(
weeks[1:idx_week], close[1:idx_week], ' (¥)', ' ')
line_chart_week
idx_week = dates.index('2017-12-11')
wd = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday', 'Sunday']
weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]]
line_chart_weekday = draw_line(
weekdays_int, close[1:idx_week], ' (¥)', ' ')
line_chart_weekday.x_labels = [' ', ' ', ' ', ' ', ' ', ' ', ' ']
line_chart_weekday.render_to_file(' (¥).svg')
line_chart_weekday
with open(' Dashboard.html', 'w', encoding='utf8') as html_file:
html_file.write(
' Dashboard
')
for svg in [
' (¥).svg', ' (¥).svg', ' (¥).svg',
' (¥).svg', ' (¥).svg'
]:
html_file.write(
'