-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathabstractchartwidget.cpp
More file actions
301 lines (245 loc) · 7.22 KB
/
abstractchartwidget.cpp
File metadata and controls
301 lines (245 loc) · 7.22 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include "abstractchartwidget.h"
#include <QtMath>
#include <QLabel>
#include <QDebug>
using namespace QtCharts;
class ChartLegendItem: public QWidget
{
public:
struct Legend {
QColor color;
QString title;
};
ChartLegendItem(const QString &title, QWidget *parent = Q_NULLPTR)
: QWidget(parent)
{
mLegendsLayout = new QHBoxLayout;
mLegendsLayout->setSpacing(4);
mLegendsLayout->addStretch();
auto f = font();
f.setBold(true);
f.setPointSize(f.pointSize() + 1);
auto l = new QLabel(title);
l->setFont(f);
auto layout = new QVBoxLayout(this);
layout->addWidget(l);
layout->addLayout(mLegendsLayout);
}
void addLegend(const Legend &l)
{
auto plt = palette();
plt.setBrush(QPalette::Base, l.color);
auto square = new QWidget;
square->setFixedSize(12, 12);
square->setAutoFillBackground(true);
square->setBackgroundRole(QPalette::Base);
square->setPalette(plt);
mLegendsLayout->insertWidget(mLegendsLayout->count()-1, square);
mLegendsLayout->insertWidget(mLegendsLayout->count()-1, new QLabel(l.title));
mLegendsLayout->insertSpacing(mLegendsLayout->count()-1, 10);
}
private:
QHBoxLayout *mLegendsLayout;
};
AbstractChartWidget::AbstractChartWidget(QWidget *parent)
: QWidget(parent)
{
mChart = new QChartView;
mChart->setRubberBand(QChartView::NoRubberBand);
mChart->setAutoFillBackground(true);
mChart->setBackgroundRole(QPalette::Base);
auto chart = mChart->chart();
chart->legend()->hide();
chart->setBackgroundBrush(QColor("#fff"));
mLegendsLayout = new QGridLayout;
mLayout = new QVBoxLayout(this);
mLayout->addLayout(mLegendsLayout);
mLayout->addWidget(mChart);
auto plt = palette();
plt.setColor(QPalette::Base, QColor("#ffffff"));
setAutoFillBackground(true);
setPalette(plt);
setBackgroundRole(QPalette::Base);
}
AbstractChartWidget::~AbstractChartWidget()
{
}
void AbstractChartWidget::setPoints(const QList<SeriesUnit> &activities)
{
mPoints = activities;
}
void AbstractChartWidget::reload()
{
clearSeries();
qreal maxValue = 0;
auto minDate = QDateTime::currentDateTime();
auto maxDate = QDateTime(QDate(1,1,1), QTime(0,0,0));
for (const auto &p: mPoints)
{
auto s = addSeries(p);
maxValue = std::max(maxValue, s.maxValue);
minDate = std::min(minDate, s.minDate);
maxDate = std::max(maxDate, s.maxDate);
}
auto chart = mChart->chart();
mAxisY = new QValueAxis(this);
mAxisY->setMin(0);
int maxValueLogPow = pow(10, floor(log10(maxValue)));
mAxisY->setMax( (1 + floor(maxValue/maxValueLogPow)) * maxValueLogPow );
mAxisX = new QDateTimeAxis(this);
mAxisX->setFormat("yyyy MMM dd");
mAxisX->setMin(minDate);
mAxisX->setMax(maxDate);
mAxisX->setTickCount( std::min<int>(6, minDate.daysTo(maxDate) / 30));
for (auto s: mSeriesHash.values())
{
chart->addSeries(s.series);
chart->setAxisX(mAxisX, s.series);
chart->setAxisY(mAxisY, s.series);
auto legend = static_cast<ChartLegendItem*>(mLegends.value(s.unit.category));
if (!legend)
{
auto idx = mLegends.count();
legend = new ChartLegendItem(s.unit.category);
mLegendsLayout->addWidget(legend, idx/3, idx%3);
mLegends[s.unit.category] = legend;
}
ChartLegendItem::Legend lgn;
lgn.title = s.unit.title;
lgn.color = s.series->color();
legend->addLegend(lgn);
}
}
void AbstractChartWidget::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
}
bool AbstractChartWidget::splineMode() const
{
return mSplineMode;
}
void AbstractChartWidget::setSplineMode(bool newSplineMode)
{
mSplineMode = newSplineMode;
}
bool AbstractChartWidget::stackable() const
{
return mStackable;
}
void AbstractChartWidget::setStackable(bool newStackable)
{
mStackable = newStackable;
}
AbstractChartWidget::SeriesType AbstractChartWidget::addSeries(const SeriesUnit &unit)
{
removeSeries(unit.uniqueId);
std::map<QDateTime, qreal> durationMap;
const auto currentDate = QDate::currentDate();
const auto add_days = currentDate.daysInYear() - currentDate.dayOfYear();
for (const auto &a: unit.points)
{
if (a.datetime < mStartDate)
continue;
if (a.datetime > mEndDate)
continue;
const auto date = a.datetime.date();
switch (static_cast<int>(mDuration))
{
case Day:
durationMap[a.datetime] += a.value;
break;
case Week:
{
auto dt = QDate::fromJulianDay(std::floor(date.toJulianDay() / 7) * 7 + 7);
durationMap[QDateTime(dt, QTime(0,0,0))] += a.value;
}
break;
case Month:
durationMap[QDateTime(QDate(date.year(), date.month(), 1).addMonths(1).addDays(-1), QTime(0,0,0))] += a.value;
break;
case Year:
durationMap[QDateTime(QDate(date.addDays(add_days).year()+1, 1, 1).addDays(-1), QTime(0,0,0))] += a.value;
break;
}
}
AbstractChartWidget::SeriesType s;
s.unit = unit;
s.maxValue = 0;
s.minDate = QDateTime::currentDateTime();
s.maxDate = QDateTime(QDate(1,1,1), QTime(0,0,0));
s.series = (mSplineMode? new QSplineSeries(this) : new QLineSeries(this));
s.series->setName(unit.uniqueId);
if (unit.color.isValid())
s.series->setColor(unit.color);
qreal stack = 0;
for (const auto &[k, v]: durationMap)
{
if (!mStackable)
stack = v;
else
stack += v;
s.minDate = std::min(s.minDate, k);
s.maxDate = std::max(s.maxDate, k);
s.maxValue = std::max(s.maxValue, stack);
s.series->append(k.toMSecsSinceEpoch(), stack);
}
mSeriesHash[unit.uniqueId] = s;
return s;
}
void AbstractChartWidget::clearSeries()
{
for (auto l: mLegends)
delete l;
mLegends.clear();
for (const auto &s: mSeriesHash.keys())
removeSeries(s);
auto chart = mChart->chart();
if (mAxisX)
{
chart->removeAxis(mAxisX);
delete mAxisX;
}
if (mAxisY)
{
chart->removeAxis(mAxisY);
delete mAxisY;
}
}
void AbstractChartWidget::removeSeries(const QString &category)
{
if (!mSeriesHash.contains(category))
return;
if (mLegends.contains(category))
delete mLegends.take(category);
auto chart = mChart->chart();
auto s = mSeriesHash.take(category);
if (s.series)
{
chart->removeSeries(s.series);
delete s.series;
}
}
AbstractChartWidget::Duration AbstractChartWidget::duration() const
{
return mDuration;
}
void AbstractChartWidget::setDuration(Duration newDuration)
{
mDuration = newDuration;
}
QDateTime AbstractChartWidget::endDate() const
{
return mEndDate;
}
void AbstractChartWidget::setEndDate(const QDateTime &newEndDate)
{
mEndDate = newEndDate;
}
QDateTime AbstractChartWidget::startDate() const
{
return mStartDate;
}
void AbstractChartWidget::setStartDate(const QDateTime &newStartDate)
{
mStartDate = newStartDate;
}