Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > 19952c5e751bf7e3108c3c59625b0f35 > files > 293

qtcharts5-doc-5.9.4-1.mga6.noarch.rpm

<?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.4 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">&gt;</span>setName(<span class="string">&quot;Acme Ltd&quot;</span>);
  acmeSeries<span class="operator">-</span><span class="operator">&gt;</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">&gt;</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">&quot;:acme&quot;</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">&amp;</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">&gt;</span>append(set);
          categories <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type">QDateTime</span><span class="operator">::</span>fromMSecsSinceEpoch(set<span class="operator">-</span><span class="operator">&gt;</span>timestamp())<span class="operator">.</span>toString(<span class="string">&quot;dd&quot;</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">&gt;</span>addSeries(acmeSeries);
  chart<span class="operator">-</span><span class="operator">&gt;</span>setTitle(<span class="string">&quot;Acme Ltd Historical Data (July 2015)&quot;</span>);
  chart<span class="operator">-</span><span class="operator">&gt;</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">&gt;</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">&lt;</span><span class="type"><a href="qbarcategoryaxis.html">QBarCategoryAxis</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(chart<span class="operator">-</span><span class="operator">&gt;</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">&gt;</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">&lt;</span><span class="type"><a href="qvalueaxis.html">QValueAxis</a></span> <span class="operator">*</span><span class="operator">&gt;</span>(chart<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>setMax(axisY<span class="operator">-</span><span class="operator">&gt;</span>max() <span class="operator">*</span> <span class="number">1.01</span>);
  axisY<span class="operator">-</span><span class="operator">&gt;</span>setMin(axisY<span class="operator">-</span><span class="operator">&gt;</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">&gt;</span>legend()<span class="operator">-</span><span class="operator">&gt;</span>setVisible(<span class="keyword">true</span>);
  chart<span class="operator">-</span><span class="operator">&gt;</span>legend()<span class="operator">-</span><span class="operator">&gt;</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">&gt;</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">&quot;#&quot;</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">&quot; &quot;</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">&gt;</span>setOpen(open);
  candlestickSet<span class="operator">-</span><span class="operator">&gt;</span>setHigh(high);
  candlestickSet<span class="operator">-</span><span class="operator">&gt;</span>setLow(low);
  candlestickSet<span class="operator">-</span><span class="operator">&gt;</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">&copy;</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>