/usr/share/qt5/doc/qtcharts/qtcharts-candlestickchart-example.html is in qtcharts5-doc-html 5.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- examples-candlestickchart.qdoc -->
<title>Candlestick Chart Example | Qt Charts 5.9</title>
<link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
<script type="text/javascript">
document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
// loading style sheet breaks anchors that were jumped to before
// so force jumping to anchor again
setTimeout(function() {
var anchor = location.hash;
// need to jump to different anchor first (e.g. none)
location.hash = "#";
setTimeout(function() {
location.hash = anchor;
}, 0);
}, 0);
</script>
</head>
<body>
<div class="header" id="qtdocheader">
<div class="main">
<div class="main-rounded">
<div class="navigationbar">
<table><tr>
<td >Qt 5.9</td><td ><a href="qtcharts-index.html">Qt Charts</a></td><td ><a href="qtcharts-examples.html">Qt Charts Examples</a></td><td >Candlestick Chart Example</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 Reference Documentation</td>
</tr></table>
</div>
</div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#running-the-example">Running the Example</a></li>
<li class="level1"><a href="#creating-candlestick-charts">Creating Candlestick Charts</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Candlestick Chart Example</h1>
<span class="subtitle"></span>
<!-- $$$candlestickchart-description -->
<div class="descr"> <a name="details"></a>
<p class="centerAlign"><img src="images/examples_candlestickchart.png" alt="" /></p><a name="running-the-example"></a>
<h2 id="running-the-example">Running the Example</h2>
<p>To run the example from Qt Creator, open the <b>Welcome</b> mode and select the example from <b>Examples</b>. For more information, visit Building and Running an Example.</p>
<a name="creating-candlestick-charts"></a>
<h2 id="creating-candlestick-charts">Creating Candlestick Charts</h2>
<p>To display a candlestick chart, we start by creating <a href="qcandlestickseries.html">QCandlestickSeries</a> to handle daily data. We are also specifying custom increasing and decreasing body colors.</p>
<pre class="cpp">
<span class="type"><a href="qcandlestickseries.html">QCandlestickSeries</a></span> <span class="operator">*</span>acmeSeries <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcandlestickseries.html">QCandlestickSeries</a></span>();
acmeSeries<span class="operator">-</span><span class="operator">></span>setName(<span class="string">"Acme Ltd"</span>);
acmeSeries<span class="operator">-</span><span class="operator">></span>setIncreasingColor(<span class="type">QColor</span>(<span class="type">Qt</span><span class="operator">::</span>green));
acmeSeries<span class="operator">-</span><span class="operator">></span>setDecreasingColor(<span class="type">QColor</span>(<span class="type">Qt</span><span class="operator">::</span>red));
</pre>
<p>QFile is used for accessing a text file where the non-continuous data is kept. The <code>CandlestickDataReader</code> is an auxiliary class for reading the text file and finding the open, high, low, close, and timestamp values from the data. The <code>CandlestickDataReader</code> is explained in more detail later. The method <code>readCandlestickSet()</code> reads the values and sets them to the <a href="qcandlestickset.html">QCandlestickSet</a> item which the method returns for the caller. The returned <a href="qcandlestickset.html">QCandlestickSet</a> item is added to the series. We are also saving custom categories list for later use.</p>
<pre class="cpp">
<span class="type">QFile</span> acmeData(<span class="string">":acme"</span>);
<span class="keyword">if</span> (<span class="operator">!</span>acmeData<span class="operator">.</span>open(<span class="type">QIODevice</span><span class="operator">::</span>ReadOnly <span class="operator">|</span> <span class="type">QIODevice</span><span class="operator">::</span>Text))
<span class="keyword">return</span> <span class="number">1</span>;
<span class="type">QStringList</span> categories;
CandlestickDataReader dataReader(<span class="operator">&</span>acmeData);
<span class="keyword">while</span> (<span class="operator">!</span>dataReader<span class="operator">.</span>atEnd()) {
<span class="type"><a href="qcandlestickset.html">QCandlestickSet</a></span> <span class="operator">*</span>set <span class="operator">=</span> dataReader<span class="operator">.</span>readCandlestickSet();
<span class="keyword">if</span> (set) {
acmeSeries<span class="operator">-</span><span class="operator">></span>append(set);
categories <span class="operator"><</span><span class="operator"><</span> <span class="type">QDateTime</span><span class="operator">::</span>fromMSecsSinceEpoch(set<span class="operator">-</span><span class="operator">></span>timestamp())<span class="operator">.</span>toString(<span class="string">"dd"</span>);
}
}
</pre>
<p>Below, a new <a href="qchart.html">QChart</a> instance is created and the previously created series object is added to it. We also define a title, and set an animation as <a href="qchart.html#AnimationOption-enum">QChart::SeriesAnimation</a>.</p>
<pre class="cpp">
<span class="type"><a href="qchart.html">QChart</a></span> <span class="operator">*</span>chart <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qchart.html">QChart</a></span>();
chart<span class="operator">-</span><span class="operator">></span>addSeries(acmeSeries);
chart<span class="operator">-</span><span class="operator">></span>setTitle(<span class="string">"Acme Ltd Historical Data (July 2015)"</span>);
chart<span class="operator">-</span><span class="operator">></span>setAnimationOptions(<span class="type"><a href="qchart.html">QChart</a></span><span class="operator">::</span>SeriesAnimations);
</pre>
<p>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.</p>
<pre class="cpp">
chart<span class="operator">-</span><span class="operator">></span>createDefaultAxes();
<span class="type"><a href="qbarcategoryaxis.html">QBarCategoryAxis</a></span> <span class="operator">*</span>axisX <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type"><a href="qbarcategoryaxis.html">QBarCategoryAxis</a></span> <span class="operator">*</span><span class="operator">></span>(chart<span class="operator">-</span><span class="operator">></span>axes(<span class="type">Qt</span><span class="operator">::</span>Horizontal)<span class="operator">.</span>at(<span class="number">0</span>));
axisX<span class="operator">-</span><span class="operator">></span>setCategories(categories);
<span class="type"><a href="qvalueaxis.html">QValueAxis</a></span> <span class="operator">*</span>axisY <span class="operator">=</span> qobject_cast<span class="operator"><</span><span class="type"><a href="qvalueaxis.html">QValueAxis</a></span> <span class="operator">*</span><span class="operator">></span>(chart<span class="operator">-</span><span class="operator">></span>axes(<span class="type">Qt</span><span class="operator">::</span>Vertical)<span class="operator">.</span>at(<span class="number">0</span>));
axisY<span class="operator">-</span><span class="operator">></span>setMax(axisY<span class="operator">-</span><span class="operator">></span>max() <span class="operator">*</span> <span class="number">1.01</span>);
axisY<span class="operator">-</span><span class="operator">></span>setMin(axisY<span class="operator">-</span><span class="operator">></span>min() <span class="operator">*</span> <span class="number">0.99</span>);
</pre>
<p>Below, we set the legend to be visible and place it at the bottom of the chart.</p>
<pre class="cpp">
chart<span class="operator">-</span><span class="operator">></span>legend()<span class="operator">-</span><span class="operator">></span>setVisible(<span class="keyword">true</span>);
chart<span class="operator">-</span><span class="operator">></span>legend()<span class="operator">-</span><span class="operator">></span>setAlignment(<span class="type">Qt</span><span class="operator">::</span>AlignBottom);
</pre>
<p>Finally, we add the chart onto a view. We also turn on the antialiasing for the chartView.</p>
<pre class="cpp">
<span class="type"><a href="qchartview.html">QChartView</a></span> <span class="operator">*</span>chartView <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qchartview.html">QChartView</a></span>(chart);
chartView<span class="operator">-</span><span class="operator">></span>setRenderHint(<span class="type">QPainter</span><span class="operator">::</span>Antialiasing);
</pre>
<p>The chart is ready to be shown. We set the chart to be the central widget of the window. We also set the size for the chart window and show it.</p>
<pre class="cpp">
<span class="type">QMainWindow</span> window;
window<span class="operator">.</span>setCentralWidget(chartView);
window<span class="operator">.</span>resize(<span class="number">800</span><span class="operator">,</span> <span class="number">600</span>);
window<span class="operator">.</span>show();
</pre>
<p>Here, the method <code>readCandlestickSet()</code> is explained in detail. First, a line is read from the file, rejecting any lines starting with # as they are considered comment lines.</p>
<pre class="cpp">
<span class="type">QString</span> line <span class="operator">=</span> readLine();
<span class="keyword">if</span> (line<span class="operator">.</span>startsWith(<span class="string">"#"</span>) <span class="operator">|</span><span class="operator">|</span> line<span class="operator">.</span>isEmpty())
<span class="keyword">return</span> <span class="number">0</span>;
</pre>
<p>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.</p>
<pre class="cpp">
<span class="type">QStringList</span> strList <span class="operator">=</span> line<span class="operator">.</span>split(<span class="string">" "</span><span class="operator">,</span> <span class="type">QString</span><span class="operator">::</span>SkipEmptyParts);
<span class="keyword">if</span> (strList<span class="operator">.</span>count() <span class="operator">!</span><span class="operator">=</span> <span class="number">5</span>)
<span class="keyword">return</span> <span class="number">0</span>;
</pre>
<p>To select values from the continuous data, following code is used. The values in a <code>strList</code> are stored in the following order: timestamp, open, high, low, close.</p>
<pre class="cpp">
<span class="keyword">const</span> <span class="type">qreal</span> timestamp <span class="operator">=</span> strList<span class="operator">.</span>at(<span class="number">0</span>)<span class="operator">.</span>toDouble();
<span class="keyword">const</span> <span class="type">qreal</span> open <span class="operator">=</span> strList<span class="operator">.</span>at(<span class="number">1</span>)<span class="operator">.</span>toDouble();
<span class="keyword">const</span> <span class="type">qreal</span> high <span class="operator">=</span> strList<span class="operator">.</span>at(<span class="number">2</span>)<span class="operator">.</span>toDouble();
<span class="keyword">const</span> <span class="type">qreal</span> low <span class="operator">=</span> strList<span class="operator">.</span>at(<span class="number">3</span>)<span class="operator">.</span>toDouble();
<span class="keyword">const</span> <span class="type">qreal</span> close <span class="operator">=</span> strList<span class="operator">.</span>at(<span class="number">4</span>)<span class="operator">.</span>toDouble();
</pre>
<p>Finally, the following snippet shows how to create a new <a href="qcandlestickset.html">QCandlestickSet</a> and provide it with all the necessary values.</p>
<pre class="cpp">
<span class="type"><a href="qcandlestickset.html">QCandlestickSet</a></span> <span class="operator">*</span>candlestickSet <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcandlestickset.html">QCandlestickSet</a></span>(timestamp);
candlestickSet<span class="operator">-</span><span class="operator">></span>setOpen(open);
candlestickSet<span class="operator">-</span><span class="operator">></span>setHigh(high);
candlestickSet<span class="operator">-</span><span class="operator">></span>setLow(low);
candlestickSet<span class="operator">-</span><span class="operator">></span>setClose(close);
</pre>
<p>Files:</p>
<ul>
<li><a href="qtcharts-candlestickchart-candlestickdatareader-cpp.html">candlestickchart/candlestickdatareader.cpp</a></li>
<li><a href="qtcharts-candlestickchart-candlestickdatareader-h.html">candlestickchart/candlestickdatareader.h</a></li>
<li><a href="qtcharts-candlestickchart-main-cpp.html">candlestickchart/main.cpp</a></li>
<li><a href="qtcharts-candlestickchart-candlestickchart-pro.html">candlestickchart/candlestickchart.pro</a></li>
<li><a href="qtcharts-candlestickchart-candlestickdata-qrc.html">candlestickchart/candlestickdata.qrc</a></li>
</ul>
</div>
<!-- @@@candlestickchart -->
</div>
</div>
</div>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2017 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.<br> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.<br> Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. </p>
</div>
</body>
</html>
|