From 38f496dd8d5348e4ba560d45234025e89e58ee8d Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Thu, 8 Aug 2019 14:25:19 +0200 Subject: [PATCH] Accumulated Distance: Change from monthly to daily --- gpx2html/__init__.py | 70 +++++++++++++++++++++++++------------------- 1 file changed, 40 insertions(+), 30 deletions(-) diff --git a/gpx2html/__init__.py b/gpx2html/__init__.py index b01ae73..0ac831e 100755 --- a/gpx2html/__init__.py +++ b/gpx2html/__init__.py @@ -13,6 +13,7 @@ import os import pandas as pd import collections from gpx_parser import Tracks +import pytz MONTH_LABELS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] @@ -54,9 +55,8 @@ def plot_line_chart(values, ticklabels, title, xlabel, ylabel, filename, xtick_r Args: values (dict): key: line name value (list): line values - ticklabels (list): Names for the tick labels (must be same length as value list). + ticklabels (list): Names for the tick labels. title (str): Title of the chart. - ''' fig = plt.figure() ax1 = fig.add_subplot(111) @@ -66,27 +66,27 @@ def plot_line_chart(values, ticklabels, title, xlabel, ylabel, filename, xtick_r ax1.spines["left"].set_visible(False) ax1.spines["right"].set_visible(False) + step = len(values[min(values.keys())]) / len(ticklabels) + ax1.set_xticks(numpy.arange(1, len(ticklabels) * step, step)) + ax1.set_xticklabels(ticklabels, rotation=xtick_rotation) + plt.title(title) plt.xlabel(xlabel) plt.ylabel(ylabel) for key in values.keys(): - if len(ticklabels) == len(values[key]): - plt.plot(ticklabels, values[key], label=key) - else: - short_ticklabels = list() - for i in range(0, len(values[key])): - short_ticklabels.append(ticklabels[i]) - plt.plot(short_ticklabels, values[key], label=key) - - x_base = numpy.arange(len(ticklabels)) - plt.xticks(x_base, ticklabels, rotation=xtick_rotation) + plt.plot(values[key], label=key) plt.legend() plt.savefig(filename) plt.close('all') +def generate_date_list(start_date, end_date): + r = (end_date + datetime.timedelta(days=1) - start_date).days + return[start_date + datetime.timedelta(days=i) for i in range(r)] + + class Gpx2Html(object): def __init__(self, infolder, outfolder, logger): self.logger = logger @@ -122,26 +122,36 @@ class Gpx2Html(object): os.path.join(self.outfolder, 'avg_spd.png')) # Accumulated distance: - accumulated_distances = dict() - for year in distances_dict.keys(): - accumulated_distance = list() - accumulated_distance.append(0) - for i in range(0, len(distances_dict[year])): - accumulated_distance.append(accumulated_distance[i] + distances_dict[year][i]) - accumulated_distances[year] = accumulated_distance - - current_year = datetime.datetime.today().year - current_month = datetime.datetime.today().month - current_year_distance = list() - self.logger.info("Current month: %s", current_month) - for i in range(0, current_month + 1): - current_year_distance.append(accumulated_distances[current_year][i]) - accumulated_distances[current_year] = current_year_distance - - plot_line_chart(accumulated_distances, [""] + MONTH_LABELS, - "accumulated distance", 'Month', 'km', + accumulated_distance = dict() + for year in self.tracks.years(): + acc_year_dist = list() + start_date = datetime.datetime(year, 1, 1) + now = datetime.datetime.now() + end_date = datetime.datetime(year, 12, 31) + if year == now.year: + end_date = now + tracks = self.tracks.tracks(start_date, end_date) + for date in generate_date_list(start_date, end_date): + for track in tracks: + if track.start_time.month == date.month and track.start_time.day == date.day: + if len(acc_year_dist) < date.timetuple().tm_yday: + acc_year_dist.append(track.distance / 1000) + else: + acc_year_dist.append(track.distance / 1000 + acc_year_dist[-1]) + elif len(acc_year_dist) < date.timetuple().tm_yday: + if len(acc_year_dist) > 0: + acc_year_dist.append(acc_year_dist[-1]) + else: + acc_year_dist.append(0) + if len(acc_year_dist) == 365: + acc_year_dist.append(acc_year_dist[-1]) + accumulated_distance[year] = acc_year_dist + plot_line_chart(accumulated_distance, MONTH_LABELS, + "Accumulated Distance", 'Month', 'km', os.path.join(self.outfolder, 'acc_dist.png')) + + end_date = datetime.datetime.today() start_date = end_date - datetime.timedelta(days=14) last_n_tracks = self.tracks.tracks(start_date, end_date)