diff --git a/gpx2html b/gpx2html index eff2fc7..b5916b4 100755 --- a/gpx2html +++ b/gpx2html @@ -9,6 +9,8 @@ matplotlib.use('Agg') import matplotlib.pyplot as plt import numpy import os +import pandas as pd +import collections MONTH_LABELS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] @@ -20,24 +22,32 @@ def parse_args(): return parser.parse_args() -def plot_bar_chart(labels, ticklabels, values, title, xlabel, ylabel, filename): +def plot_bar_chart(labels, ticklabels, values, title, xlabel, ylabel, filename, xtick_rotation=0): fig = plt.figure() ax1 = fig.add_subplot(111) ax1.grid(zorder=0) + ax1.spines["top"].set_visible(False) + ax1.spines["bottom"].set_visible(False) + ax1.spines["left"].set_visible(False) + ax1.spines["right"].set_visible(False) plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) - width = 1.0 / len(values) - 0.025 - x_base = numpy.arange(12) + width = 1.0 / len(values) - 0.03 + x_base = numpy.arange(len(ticklabels)) x_pos = list() for i in range(len(values)): x_pos.append([x + (width / 2) + i * width for x in range(len(x_base))]) plt.bar(x_pos[i], values[i], width=width, label=labels[i], zorder=2) - plt.xticks(x_base, ticklabels) + plt.xticks(x_base, ticklabels, rotation=xtick_rotation) + + # Tweak spacing to prevent clipping of tick-labels + plt.subplots_adjust(bottom=0.2) + plt.legend() plt.savefig(filename) @@ -77,6 +87,60 @@ def main(): avg_file_name = 'avg_spd.png' plot_bar_chart(years, MONTH_LABELS, years_avg_spd, 'Average Speed', 'Month', 'km/h', os.path.join(out_folder, avg_file_name)) + # last n days + n = 14 + end_date = datetime.date.today() + start_date = end_date - datetime.timedelta(days=n) + dates = pd.date_range(start_date, end_date) + + date_distance = dict() + date_duration = dict() + date_avg_spd = dict() + + for date in dates: + date_str = "{0:04d}-{1:02d}-{2:02d}".format(date.year, date.month, date.day) + date_tracks = tracks.get(date.year, date.month, date.day) + for track in date_tracks: + try: + current_dist = date_distance[date_str] + current_duration = date_duration[date_str] + except KeyError: + current_dist = 0 + current_duration = 0 + current_dist += track.distance / 1000 + date_distance.update({date_str:current_dist}) + current_duration += track.duration.total_seconds() / 3600 + date_duration.update({date_str:current_duration}) + # check for empty dates + try: + current_dist = date_distance[date_str] + current_duration = date_duration[date_str] + except KeyError: + date_distance.update({date_str:0}) + date_duration.update({date_str:0}) + + date_duration = collections.OrderedDict(sorted(date_duration.items())) + + for key, value in date_duration.items(): + if value == 0: + date_avg_spd.update({key:0}) + else: + avg_spd = date_distance[key] / value + date_avg_spd.update({key:avg_spd}) + + date_avg_spd = collections.OrderedDict(sorted(date_avg_spd.items())) + date_distance = collections.OrderedDict(sorted(date_distance.items())) + + dst_n_file_name = "distance_last_{}_days.png".format(n) + plot_bar_chart(["Distance", "Average speed"], + date_distance.keys(), + [date_distance.values(), date_avg_spd.values()], + 'Last {} days'.format(n), + 'Date', + 'km, km/h', + os.path.join(out_folder, dst_n_file_name), + 90) + html_file = os.path.join(out_folder, 'index.html') with open(html_file, 'w') as handle: handle.write('\n') @@ -115,6 +179,7 @@ def main(): handle.write('

\n') handle.write('Distance\n'.format(dst_file_name)) handle.write('Distance\n'.format(avg_file_name)) + handle.write('Distance\n'.format(dst_n_file_name)) handle.write('

\n') handle.write('\n')