-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphForm.py
136 lines (121 loc) · 5.55 KB
/
graphForm.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- coding: utf-8 -*-
import sys
import os
from PyQt4.Qt import *
from PyQt4 import uic
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import timedelta
from dataManager import *
from matplotlib import rc
import mainForm
DIR = os.path.dirname(__file__)
DATEFORMAT = "%Y-%m-%d %H:%M:%S.%f"
class FormGraph(QMainWindow):
def __init__(self,profileId):
super(QMainWindow, self).__init__()
uic.loadUi('%s/ui/frm_graph.ui' % DIR, self)
self.profileId = profileId
self.distanceId = -1
self.cb_distance_load()
self.move(QDesktopWidget().availableGeometry().center() - self.frameGeometry().center())
self.connect(self.bt_averageSpeed, SIGNAL("clicked()"), self.bt_averageSpeed_clicked)
self.connect(self.bt_averageTime, SIGNAL("clicked()"), self.bt_averageTime_clicked)
self.connect(self.bt_distance, SIGNAL("clicked()"), self.bt_distance_clicked)
self.connect(self.bt_back, SIGNAL("clicked()"), self.bt_back_clicked)
#включаем кирилицу
font = {'family': 'Droid Sans',
'weight': 'normal',
'size': 14}
rc('font', **font)
def cb_distance_load(self):
self.cb_distance.clear()
self.cb_distance.addItem(u"По всем дистанциям",-1)
dm = DataManager()
data = dm.getDistancesAndIds()
for rec in data:
id, dist = rec
self.cb_distance.addItem(str(dist),id)
def bt_distance_clicked(self):
self.distanceId = int(self.cb_distance.itemData(self.cb_distance.currentIndex()).toString())
dm = DataManager()
minDate = datetime.today() - timedelta(days=30)
dates = []
distances = []
for date in (minDate + timedelta(n) for n in range(33)):
distance = 0
data = dm.getRaceIdByDateAndProfileId(date,self.profileId)
if data:
print(data)
for raceIds in data:
raceId = raceIds
distance += dm.getRaceDistance(raceId)
distances.append(distance)
dates.append(date)
plt.plot_date(dates, distances,'b')
plt.plot_date(dates, distances,'bo')
distancesSum = sum(distances)
plt.xlabel(u"Всего пройдено = %s м" % float(distancesSum))
plt.ylabel(u"Пройдено в день (м)")
plt.title(u"График пройденого расстояния профиля %s" % dm.getProfileNameById(self.profileId))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y'))
plt.gcf().autofmt_xdate()
plt.grid(True)
plt.show()
def bt_averageTime_clicked(self):
self.distanceId = int(self.cb_distance.itemData(self.cb_distance.currentIndex()).toString())
dm = DataManager()
data = dm.getDatesAndRaceTimesAndRaceIdsByProfileId(self.profileId)
dates = []
times = []
for rec in data:
date, time, id = rec
if date and time:
if self.distanceId == -1:
dates.append(datetime.strptime(date,DATEFORMAT))
times.append(time)
elif dm.getRaceDistance(id) == dm.getDistanceNameById(self.distanceId):
dates.append(datetime.strptime(date,DATEFORMAT))
times.append(time)
plt.plot_date(dates, times,'b')
plt.plot_date(dates, times,'bo')
timesSum = sum(times)
h = ((timesSum / 3600)) % 24
m = (timesSum / 60) % 60
s = timesSum % 60
plt.xlabel(u"Всего времени = %s с или %d:%02d:%02d" % (timesSum,h,m,s))
plt.ylabel(u"Время гонки (с)")
plt.title(u"График времени гонки профиля %s" % dm.getProfileNameById(self.profileId))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y'))
plt.gcf().autofmt_xdate()
plt.grid(True)
plt.show()
def bt_averageSpeed_clicked(self):
self.distanceId = int(self.cb_distance.itemData(self.cb_distance.currentIndex()).toString())
dm = DataManager()
data = dm.getDatesAndRaceTimesAndRaceIdsByProfileId(self.profileId)
dates = []
values = []
for rec in data:
date, time, id = rec
if date and dm.getAverageSpeedByRace(id):
if self.distanceId == -1:
dates.append(datetime.strptime(date,DATEFORMAT))
values.append(dm.getAverageSpeedByRace(id))
elif dm.getRaceDistance(id) == dm.getDistanceNameById(self.distanceId):
dates.append(datetime.strptime(date,DATEFORMAT))
values.append(dm.getAverageSpeedByRace(id))
plt.plot_date(dates, values,'b')
plt.plot_date(dates, values,'bo')
averageSpeed = len(values) > 0 and (lambda: sum(values) / len(values)) or (lambda: 0)
plt.xlabel(u"Средняя-средняя скорость= %.2f м/с или %.2f км/ч" % (float(averageSpeed()),float(averageSpeed()) / 1000 * 3600))
plt.ylabel(u"Средняя скорость (м/с)")
plt.title(u"График скоростей профиля %s" % dm.getProfileNameById(self.profileId))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y'))
plt.gcf().autofmt_xdate()
plt.grid(True)
plt.show()
def bt_back_clicked(self):
self.formProfile = mainForm.FormProfile()
self.formProfile.show()
self.hide()