-
Notifications
You must be signed in to change notification settings - Fork 12
/
datetimechart.cpp
68 lines (58 loc) · 2 KB
/
datetimechart.cpp
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
#include "datetimechart.h"
#include "normalchartdata.h"
class DateTimeChart::DateTimeChartPrivate
{
public:
explicit DateTimeChartPrivate(ChartView *parent)
: q_ptr(parent)
{
dateTimeAxisX = new QDateTimeAxis(q_ptr);
dateTimeAxisX->setFormat("yyyy-MM");
//dateTimeAxisX->setTitleText(QObject::tr("Date"));
valueAxisY = new QValueAxis(q_ptr);
//valueAxisY->setTitleText(QObject::tr("Value"));
valueAxisY->setRange(0, 100);
lineSeries = new QLineSeries(q_ptr);
scatterSeries = new QScatterSeries(q_ptr);
scatterSeries->setMarkerSize(8);
PointList pointList = generateRandomDataPoints(5, 101);
int year = 2020;
int mouth = 1;
for (const QPointF &p : std::as_const(pointList)) {
QDateTime momentInTime(QDate(year, mouth, 15), QTime());
scatterSeries->append(momentInTime.toMSecsSinceEpoch(), p.y());
lineSeries->append(momentInTime.toMSecsSinceEpoch(), p.y());
year++;
mouth++;
}
chart = new QChart;
chart->setAnimationOptions(QChart::AllAnimations);
chart->setTitle(QObject::tr("DateTime Chart"));
chart->addSeries(lineSeries);
chart->addSeries(scatterSeries);
chart->addAxis(dateTimeAxisX, Qt::AlignBottom);
chart->addAxis(valueAxisY, Qt::AlignLeft);
lineSeries->attachAxis(dateTimeAxisX);
lineSeries->attachAxis(valueAxisY);
scatterSeries->attachAxis(dateTimeAxisX);
scatterSeries->attachAxis(valueAxisY);
}
ChartView *q_ptr;
QChart *chart;
QDateTimeAxis *dateTimeAxisX;
QValueAxis *valueAxisY;
QLineSeries *lineSeries;
QScatterSeries *scatterSeries;
};
DateTimeChart::DateTimeChart(QWidget *parent)
: ChartView(parent)
, d_ptr(new DateTimeChartPrivate(this))
{
setupUI();
}
DateTimeChart::~DateTimeChart() = default;
void DateTimeChart::setupUI()
{
setRenderHint(QPainter::Antialiasing);
setChart(d_ptr->chart);
}