blob: f21037c9157b028506c2030ec2a1b32a4d94d001 (
plain)
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
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "candlestickdatareader.h"
#include "candlestickwidget.h"
#include <QBarCategoryAxis>
#include <QCandlestickSeries>
#include <QCandlestickSet>
#include <QChart>
#include <QDateTime>
#include <QFile>
#include <QLegend>
#include <QValueAxis>
CandleStickWidget::CandleStickWidget(QWidget *parent)
: ContentWidget(parent)
{
}
bool CandleStickWidget::doLoad()
{
//! [1]
auto acmeSeries = new QCandlestickSeries;
acmeSeries->setName("Acme Ltd");
acmeSeries->setIncreasingColor(QColor(Qt::green));
acmeSeries->setDecreasingColor(QColor(Qt::red));
//! [1]
//! [2]
QFile acmeData(":candlestick");
if (!acmeData.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_loadError = QStringLiteral("Failed to load '%1' file.").arg(acmeData.fileName());
return false;
}
QStringList categories;
CandlestickDataReader dataReader(&acmeData);
while (!dataReader.atEnd()) {
QCandlestickSet *set = dataReader.readCandlestickSet();
if (set) {
acmeSeries->append(set);
categories << QDateTime::fromMSecsSinceEpoch(set->timestamp()).toString("dd");
}
}
//! [2]
//! [3]
auto chart = new QChart;
chart->addSeries(acmeSeries);
chart->setTitle("Acme Ltd. Historical Data (July 2015)");
chart->setAnimationOptions(QChart::SeriesAnimations);
//! [3]
//! [4]
chart->createDefaultAxes();
auto axisX = qobject_cast<QBarCategoryAxis *>(chart->axes(Qt::Horizontal).at(0));
axisX->setCategories(categories);
auto axisY = qobject_cast<QValueAxis *>(chart->axes(Qt::Vertical).at(0));
axisY->setMax(axisY->max() * 1.01);
axisY->setMin(axisY->min() * 0.99);
//! [4]
//! [5]
chart->legend()->setVisible(true);
chart->legend()->setAlignment(Qt::AlignBottom);
//! [5]
//! [6]
createDefaultChartView(chart);
//! [6]
return true;
}
|