Revision control
Copy as Markdown
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
/*!
\page qtcharts-candlestickchart-example.html
\title Creating Candlestick Charts
\brief Shows how to create a candlestick chart.
\include gallery.qdocinc
\image examples_candlestickchart.png {Screenshot showing a candlestick chart example}
To display a candlestick chart, we start by creating QCandlestickSeries to handle daily data. We
are also specifying custom increasing and decreasing body colors.
\snippet chartsgallery/candlestickwidget.cpp 1
QFile is used for accessing a text file where the non-continuous data is kept. The
\c CandlestickDataReader is an auxiliary class for reading the text file and finding the open,
high, low, close, and timestamp values from the data. The \c CandlestickDataReader is explained
in more detail later. The method \c readCandlestickSet() reads the values and sets them to the
QCandlestickSet item which the method returns for the caller. The returned QCandlestickSet item
is added to the series. We are also saving custom categories list for later use.
\snippet chartsgallery/candlestickwidget.cpp 2
Below, a new QChart instance is created and the previously created series object is added to it.
We also define a title, and set an animation as
\l {QChart::AnimationOption} {QChart::SeriesAnimation}.
\snippet chartsgallery/candlestickwidget.cpp 3
Here, we ask the chart to create default axes for our presentation. Then, we set custom
categories for the horizontal axis by querying the pointer for the axis from the chart, and then
setting the categories from previously saved custom categories list. We also set the range for
the vertical axis by querying the pointer for the axis from the chart, and then setting the min
and max values for that axis.
\snippet chartsgallery/candlestickwidget.cpp 4
Below, we set the legend to be visible and place it at the bottom of the chart.
\snippet chartsgallery/candlestickwidget.cpp 5
Finally, we add the chart onto a view.
\snippet chartsgallery/candlestickwidget.cpp 6
The chart is ready to be shown.
Here, the method \c readCandlestickSet() is explained in detail. First, a line is read from
the file, rejecting any lines starting with # as they are considered comment lines.
\snippet chartsgallery/candlestickdatareader.cpp 1
In the file, the data is arranged as a space-delimited sequence of numbers. On this snippet the
line is split into single number strings which are stored in a QStringList.
\snippet chartsgallery/candlestickdatareader.cpp 2
To select values from the continuous data, following code is used. The values in a \c strList
are stored in the following order: timestamp, open, high, low, close.
\snippet chartsgallery/candlestickdatareader.cpp 3
Finally, the following snippet shows how to create a new QCandlestickSet and provide it with all
the necessary values.
\snippet chartsgallery/candlestickdatareader.cpp 4
*/