-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweightplotmanager.cpp
More file actions
194 lines (158 loc) · 5.8 KB
/
weightplotmanager.cpp
File metadata and controls
194 lines (158 loc) · 5.8 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "weightplotmanager.h"
#include "weightdataprovider.h"
#include "ticks.h"
#include <QDebug>
namespace weighttracker
{
WeightPlotManager::WeightPlotManager(QCustomPlot* plot, WeightDataManager& wdm, WeightDataAnalyzer& wda, QObject *parent)
: QObject(parent), plot_(plot), wdm_(wdm), wda_(wda)
{
plot_->setLocale(QLocale(QLocale::English, QLocale::UnitedStates));
plot_->setMinimumSize(450, 350);
shift_ = 0;
setupPlot();
}
WeightPlotManager::~WeightPlotManager() { }
void WeightPlotManager::setShift(int shift)
{
shift_ = shift;
if(trendData_ && !trendData_->empty())
refreshTrend();
}
void WeightPlotManager::setupPlot()
{
// add title layout element:
// first we create and prepare a plot title layout element:
QCPPlotTitle *title = new QCPPlotTitle(plot_);
title->setText("Weight Evolution");
title->setFont(QFont("sans", 12, QFont::Bold));
plot_->plotLayout()->insertRow(0);
plot_->plotLayout()->addElement(0, 0, title);
// configure bottom axis to show date and time instead of number:
plot_->xAxis->setTickLabelType(QCPAxis::ltDateTime);
plot_->xAxis->setDateTimeFormat("MMM dd");
// set axis labels:
plot_->xAxis->setLabel("Date");
plot_->yAxis->setLabel("Weight");
// make top and right axes visible but without ticks and labels:
plot_->xAxis2->setVisible(true);
plot_->yAxis2->setVisible(true);
plot_->xAxis2->setTicks(false);
plot_->yAxis2->setTicks(false);
plot_->xAxis2->setTickLabels(false);
plot_->yAxis2->setTickLabels(false);
plot_->xAxis->setAutoSubTicks(false);
plot_->yAxis->setAutoTickStep(false);
plot_->yAxis->setAutoSubTicks(false);
plot_->yAxis->setSubTickCount(0);
weightGraph_ = plot_->addGraph();
weightGraph_->setName("Weight");
trendGraph_ = plot_->addGraph();
trendGraph_->setName("Trend");
weightData_ = weightGraph_->data();
trendData_ = trendGraph_->data();
QPen thickRedPen;
thickRedPen.setColor(Qt::darkRed);
thickRedPen.setWidth(4);
QPen lightGrayPen;
lightGrayPen.setColor(Qt::darkGray);
lightGrayPen.setWidth(2);
trendGraph_->setPen(thickRedPen);
weightGraph_->setPen(lightGrayPen);
weightGraph_->setLineStyle(QCPGraph::lsNone);
weightGraph_->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc, 5));
adjustDateRange();
adjustWeightRange();
}
void WeightPlotManager::refreshAll()
{
weightData_->clear();
trendData_->clear();
for (int i = 0; i < wdm_.dataSize(); ++i)
{
double date = QDateTime(wdm_.at(i).date).toTime_t();
double shiftedDate = QDateTime(wdm_.at(i).date.addDays(shift_)).toTime_t();
double weight = wdm_.at(i).value;
double trend = wda_.trend().at(i);
weightData_->insert(weightData_->constEnd(), date, QCPData(date, weight));
trendData_->insert(trendData_->constEnd(), shiftedDate, QCPData(shiftedDate, trend));
}
plot_->legend->setVisible(wdm_.dataSize() > 0);
if (wdm_.dataSize() == 1)
trendGraph_->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 4));
else
trendGraph_->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssNone, 4));
adjustDateRange();
adjustWeightRange();
plot_->replot();
}
void WeightPlotManager::refreshTrend()
{
trendData_->clear();
for (int i = 0; i < wdm_.dataSize(); ++i)
{
double shiftedDate = QDateTime(wdm_.at(i).date.addDays(shift_)).toTime_t();
double trend = wda_.trend().at(i);
trendData_->insert(trendData_->constEnd(), shiftedDate, QCPData(shiftedDate, trend));
}
plot_->replot();
}
void WeightPlotManager::adjustDateRange()
{
QDate lastDate, firstDate;
if (wdm_.getData().empty())
{
lastDate = QDate::currentDate();
firstDate = lastDate.addMonths(-1);
}
else
{
lastDate = wdm_.getData().back().date;
firstDate = wdm_.getData().front().date;
}
auto dateTicks = ticksCalculations::niceTicks(firstDate, lastDate, 8);
double minRangeInSeconds = QDateTime(dateTicks.dates.front()).toTime_t();
double maxRangeInSeconds = QDateTime(dateTicks.dates.back()).toTime_t();
double dateRange = maxRangeInSeconds - minRangeInSeconds;
if (dateTicks.dates.front() == dateTicks.dates.back()) dateRange = minRangeInSeconds;
if (dateTicks.dates.front() == firstDate) minRangeInSeconds -= 0.05 * dateRange;
if (dateTicks.dates.back() == lastDate) maxRangeInSeconds += 0.05 * dateRange;
plot_->xAxis->setRange(minRangeInSeconds, maxRangeInSeconds);
// apply manual tick and tick label for date axis:
plot_->xAxis->setAutoTicks(false);
plot_->xAxis->setAutoTickLabels(false);
QVector<double> tickVector;
QVector<QString> tickLabels;
for (int i = 0; i < dateTicks.dates.size(); ++i)
{
double dateinSeconds = QDateTime(dateTicks.dates[i]).toTime_t();
tickVector.push_back(dateinSeconds);
tickLabels.push_back(dateTicks.dates[i].toString(dateTicks.format));
}
plot_->xAxis->setTickVector(tickVector);
plot_->xAxis->setTickVectorLabels(tickLabels);
plot_->xAxis->setSubTickCount( dateTicks.dates.size() > 1 ? dateTicks.dates[0].daysTo(dateTicks.dates[1])-1 : 0 );
}
void WeightPlotManager::adjustWeightRange()
{
double minWeight, maxWeight;
if (wdm_.getData().empty())
{
minWeight = 50.0;
maxWeight = 90.0;
}
else
{
minWeight = wdm_.at(0).value;
maxWeight = wdm_.at(0).value;
for (auto& w : wdm_.getData())
{
minWeight = std::min(minWeight, w.value);
maxWeight = std::max(maxWeight, w.value);
}
}
auto ticks = ticksCalculations::niceWeightTicks(minWeight, maxWeight, 8);
plot_->yAxis->setTickStep(ticks.tickSpacing);
plot_->yAxis->setRange(ticks.niceMin, ticks.niceMax);
}
}