From facc2941efbfb8c9f40e363f0ea881653f6b4393 Mon Sep 17 00:00:00 2001 From: Alexander Mishin Date: Tue, 9 Feb 2016 21:49:53 +0300 Subject: Added candlestick chart type - added QCandlestickSeries - added QCandlestickSet - added QCandlestickLegendMarker - added model mappers - added Candlestick, CandlestickChartItem, CandlestickData - added SeriesTypeCandlestick to SeriesType enum - added LegendMarkerTypeCandlestick to LegendMarkerType enum - added candlestick chart example - added QML candlestick chart example - added candlestick tester - added autotests - added documentation [ChangeLog][CandlestickChart] Added new chart type: Candlestick Chart. Task-number: QTBUG-50544 Change-Id: I17d18dfa23e0ea209bf51ab1e349585b9cb50a8f Reviewed-by: Miikka Heikkinen --- examples/charts/candlestickchart/acme_data.txt | 24 +++++ .../charts/candlestickchart/candlestickchart.pro | 14 +++ .../charts/candlestickchart/candlestickdata.qrc | 5 + .../candlestickchart/candlestickdatareader.cpp | 77 +++++++++++++++ .../candlestickchart/candlestickdatareader.h | 48 ++++++++++ examples/charts/candlestickchart/main.cpp | 106 +++++++++++++++++++++ examples/charts/charts.pro | 2 + examples/charts/qmlcandlestick/main.cpp | 59 ++++++++++++ .../qmlcandlestick/qml/qmlcandlestick/main.qml | 52 ++++++++++ examples/charts/qmlcandlestick/qmlcandlestick.pro | 10 ++ examples/charts/qmlcandlestick/resources.qrc | 5 + 11 files changed, 402 insertions(+) create mode 100644 examples/charts/candlestickchart/acme_data.txt create mode 100644 examples/charts/candlestickchart/candlestickchart.pro create mode 100644 examples/charts/candlestickchart/candlestickdata.qrc create mode 100644 examples/charts/candlestickchart/candlestickdatareader.cpp create mode 100644 examples/charts/candlestickchart/candlestickdatareader.h create mode 100644 examples/charts/candlestickchart/main.cpp create mode 100644 examples/charts/qmlcandlestick/main.cpp create mode 100644 examples/charts/qmlcandlestick/qml/qmlcandlestick/main.qml create mode 100644 examples/charts/qmlcandlestick/qmlcandlestick.pro create mode 100644 examples/charts/qmlcandlestick/resources.qrc (limited to 'examples') diff --git a/examples/charts/candlestickchart/acme_data.txt b/examples/charts/candlestickchart/acme_data.txt new file mode 100644 index 00000000..eaac318c --- /dev/null +++ b/examples/charts/candlestickchart/acme_data.txt @@ -0,0 +1,24 @@ +# Acme Ltd Historical Data (July 2015) +# timestamp, open, high, low, close +1435708800000 126.90 126.94 125.99 126.60 +1435795200000 126.69 126.69 126.69 126.69 +1436140800000 124.85 126.23 124.85 126.00 +1436227200000 125.89 126.15 123.77 125.69 +1436313600000 124.64 124.64 122.54 122.54 +1436400000000 123.85 124.06 119.22 120.07 +1436486400000 121.94 123.85 121.21 123.28 +1436745600000 125.03 125.76 124.32 125.66 +1436832000000 126.04 126.37 125.04 125.61 +1436918400000 125.72 127.15 125.58 126.82 +1437004800000 127.74 128.57 127.35 128.51 +1437091200000 129.08 129.62 128.31 129.62 +1437350400000 130.97 132.97 130.70 132.07 +1437436800000 132.85 132.92 130.32 130.75 +1437523200000 121.99 125.50 121.99 125.22 +1437609600000 126.20 127.09 125.06 125.16 +1437696000000 125.32 125.74 123.90 124.50 +1437955200000 123.09 123.61 122.12 122.77 +1438041600000 123.38 123.91 122.55 123.38 +1438128000000 123.15 123.50 122.27 122.99 +1438214400000 122.32 122.57 121.71 122.37 +1438300800000 122.60 122.64 120.91 121.30 diff --git a/examples/charts/candlestickchart/candlestickchart.pro b/examples/charts/candlestickchart/candlestickchart.pro new file mode 100644 index 00000000..9a334bd6 --- /dev/null +++ b/examples/charts/candlestickchart/candlestickchart.pro @@ -0,0 +1,14 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +TARGET = candlestickchart + +SOURCES += main.cpp \ + candlestickdatareader.cpp + +HEADERS += \ + candlestickdatareader.h + +RESOURCES += \ + candlestickdata.qrc diff --git a/examples/charts/candlestickchart/candlestickdata.qrc b/examples/charts/candlestickchart/candlestickdata.qrc new file mode 100644 index 00000000..e311cadb --- /dev/null +++ b/examples/charts/candlestickchart/candlestickdata.qrc @@ -0,0 +1,5 @@ + + + acme_data.txt + + diff --git a/examples/charts/candlestickchart/candlestickdatareader.cpp b/examples/charts/candlestickchart/candlestickdatareader.cpp new file mode 100644 index 00000000..83228efd --- /dev/null +++ b/examples/charts/candlestickchart/candlestickdatareader.cpp @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Charts module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "candlestickdatareader.h" + +CandlestickDataReader::CandlestickDataReader(QIODevice *device) + : QTextStream(device) +{ +} + +CandlestickDataReader::~CandlestickDataReader() +{ +} + +void CandlestickDataReader::readFile(QIODevice *device) +{ + QTextStream::setDevice(device); +} + +QCandlestickSet *CandlestickDataReader::readCandlestickSet() +{ + //! [1] + QString line = readLine(); + if (line.startsWith("#") || line.isEmpty()) + return 0; + //! [1] + + //! [2] + QStringList strList = line.split(" ", QString::SkipEmptyParts); + if (strList.count() != 5) + return 0; + //! [2] + + //! [3] + const qreal timestamp = strList.at(0).toDouble(); + const qreal open = strList.at(1).toDouble(); + const qreal high = strList.at(2).toDouble(); + const qreal low = strList.at(3).toDouble(); + const qreal close = strList.at(4).toDouble(); + //! [3] + + //! [4] + QCandlestickSet *candlestickSet = new QCandlestickSet(timestamp); + candlestickSet->setOpen(open); + candlestickSet->setHigh(high); + candlestickSet->setLow(low); + candlestickSet->setClose(close); + //! [4] + + return candlestickSet; +} diff --git a/examples/charts/candlestickchart/candlestickdatareader.h b/examples/charts/candlestickchart/candlestickdatareader.h new file mode 100644 index 00000000..aaa83bf2 --- /dev/null +++ b/examples/charts/candlestickchart/candlestickdatareader.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Charts module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CANDLESTICKDATAREADER_H +#define CANDLESTICKDATAREADER_H + +#include +#include + +QT_CHARTS_USE_NAMESPACE + +class CandlestickDataReader : public QTextStream +{ +public: + explicit CandlestickDataReader(QIODevice *device); + ~CandlestickDataReader(); + + void readFile(QIODevice *device); + QCandlestickSet *readCandlestickSet(); +}; + +#endif // CANDLESTICKDATAREADER_H diff --git a/examples/charts/candlestickchart/main.cpp b/examples/charts/candlestickchart/main.cpp new file mode 100644 index 00000000..243b0f61 --- /dev/null +++ b/examples/charts/candlestickchart/main.cpp @@ -0,0 +1,106 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Charts module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include + +#include "candlestickdatareader.h" + +QT_CHARTS_USE_NAMESPACE + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + //! [1] + QCandlestickSeries *acmeSeries = new QCandlestickSeries(); + acmeSeries->setName("Acme Ltd"); + acmeSeries->setIncreasingColor(QColor(Qt::green)); + acmeSeries->setDecreasingColor(QColor(Qt::red)); + //! [1] + + //! [2] + QFile acmeData(":acme"); + if (!acmeData.open(QIODevice::ReadOnly | QIODevice::Text)) + return 1; + + 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] + QChart *chart = new QChart(); + chart->addSeries(acmeSeries); + chart->setTitle("Acme Ltd Historical Data (July 2015)"); + chart->setAnimationOptions(QChart::SeriesAnimations); + //! [3] + + //! [4] + chart->createDefaultAxes(); + + QBarCategoryAxis *axisX = qobject_cast(chart->axes(Qt::Horizontal).at(0)); + axisX->setCategories(categories); + + QValueAxis *axisY = qobject_cast(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] + QChartView *chartView = new QChartView(chart); + chartView->setRenderHint(QPainter::Antialiasing); + //! [6] + + //! [7] + QMainWindow window; + window.setCentralWidget(chartView); + window.resize(800, 600); + window.show(); + //! [7] + + return a.exec(); +} diff --git a/examples/charts/charts.pro b/examples/charts/charts.pro index 5caa03c6..704d9cc0 100644 --- a/examples/charts/charts.pro +++ b/examples/charts/charts.pro @@ -14,6 +14,7 @@ SUBDIRS += areachart \ modeldata \ barchart \ boxplotchart \ + candlestickchart \ legend \ barmodelmapper \ lineandbar \ @@ -36,6 +37,7 @@ SUBDIRS += areachart \ qtHaveModule(quick) { SUBDIRS += qmlboxplot \ + qmlcandlestick \ qmlpiechart \ qmlweather \ qmlf1legends \ diff --git a/examples/charts/qmlcandlestick/main.cpp b/examples/charts/qmlcandlestick/main.cpp new file mode 100644 index 00000000..b399e15c --- /dev/null +++ b/examples/charts/qmlcandlestick/main.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Charts module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + // Qt Charts uses Qt Graphics View Framework for drawing, therefore QApplication must be used. + QApplication app(argc, argv); + + QQuickView viewer; + + // The following are needed to make examples run without having to install the module + // in desktop environments. +#ifdef Q_OS_WIN + QString extraImportPath(QStringLiteral("%1/../../../../%2")); +#else + QString extraImportPath(QStringLiteral("%1/../../../%2")); +#endif + viewer.engine()->addImportPath(extraImportPath.arg(QApplication::applicationDirPath(), + QString::fromLatin1("qml"))); + QObject::connect(viewer.engine(), &QQmlEngine::quit, &viewer, &QWindow::close); + + viewer.setTitle(QStringLiteral("QML Candlestick")); + viewer.setSource(QUrl("qrc:/qml/qmlcandlestick/main.qml")); + viewer.setResizeMode(QQuickView::SizeRootObjectToView); + viewer.show(); + + return app.exec(); +} diff --git a/examples/charts/qmlcandlestick/qml/qmlcandlestick/main.qml b/examples/charts/qmlcandlestick/qml/qmlcandlestick/main.qml new file mode 100644 index 00000000..18fca4fe --- /dev/null +++ b/examples/charts/qmlcandlestick/qml/qmlcandlestick/main.qml @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the Qt Charts module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 or (at your option) any later version +** approved by the KDE Free Qt Foundation. The licenses are as published by +** the Free Software Foundation and appearing in the file LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtCharts 2.2 + +ChartView { + title: "Candlestick series" + width: 800 + height: 600 + theme: ChartView.ChartThemeLight + legend.alignment: Qt.AlignBottom + antialiasing: true + + CandlestickSeries { + name: "Acme Ltd." + increasingColor: "green" + decreasingColor: "red" + + CandlestickSet { timestamp: 1435708800000; open: 6.90; high: 6.94; low: 5.99; close: 6.60 } + CandlestickSet { timestamp: 1435795200000; open: 6.69; high: 6.69; low: 6.69; close: 6.69 } + CandlestickSet { timestamp: 1436140800000; open: 4.85; high: 6.23; low: 4.85; close: 6.00 } + CandlestickSet { timestamp: 1436227200000; open: 5.89; high: 6.15; low: 3.77; close: 5.69 } + CandlestickSet { timestamp: 1436313600000; open: 4.64; high: 4.64; low: 2.54; close: 2.54 } + } +} diff --git a/examples/charts/qmlcandlestick/qmlcandlestick.pro b/examples/charts/qmlcandlestick/qmlcandlestick.pro new file mode 100644 index 00000000..58e63037 --- /dev/null +++ b/examples/charts/qmlcandlestick/qmlcandlestick.pro @@ -0,0 +1,10 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} + +RESOURCES += resources.qrc + +SOURCES += main.cpp + +OTHER_FILES += \ + qml/qmlcandlestick/main.qml diff --git a/examples/charts/qmlcandlestick/resources.qrc b/examples/charts/qmlcandlestick/resources.qrc new file mode 100644 index 00000000..ccfab44e --- /dev/null +++ b/examples/charts/qmlcandlestick/resources.qrc @@ -0,0 +1,5 @@ + + + qml/qmlcandlestick/main.qml + + -- cgit v1.2.3