<?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-splinechart.qdoc --> <title>SplineChart Example | Qt Charts 5.12.6</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.12</td><td ><a href="qtcharts-index.html">Qt Charts</a></td><td ><a href="qtcharts-examples.html">Qt Charts Examples</a></td><td >SplineChart Example</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right"><a href="qtcharts-index.html">Qt Charts | Commercial or GPLv3</a></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-spline-charts">Creating Spline Charts</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">SplineChart Example</h1> <span class="subtitle"></span> <!-- $$$splinechart-brief --> <p>The example shows how to create a simple spline chart.</p> <!-- @@@splinechart --> <!-- $$$splinechart-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/examples_splinechart.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-spline-charts"></a> <h2 id="creating-spline-charts">Creating Spline Charts</h2> <p>To create spline charts we need to put our data into <a href="qsplineseries.html">QSplineSeries</a>. <a href="qsplineseries.html">QSplineSeries</a> automatically calculates spline segment control points that are needed to properly draw the spline.</p> <pre class="cpp"> <span class="type"><a href="qsplineseries.html">QSplineSeries</a></span> <span class="operator">*</span>series <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qsplineseries.html">QSplineSeries</a></span>(); series<span class="operator">-</span><span class="operator">></span>setName(<span class="string">"spline"</span>); </pre> <p>Now let's add some data points to the series.</p> <pre class="cpp"> series<span class="operator">-</span><span class="operator">></span>append(<span class="number">0</span><span class="operator">,</span> <span class="number">6</span>); series<span class="operator">-</span><span class="operator">></span>append(<span class="number">2</span><span class="operator">,</span> <span class="number">4</span>); series<span class="operator">-</span><span class="operator">></span>append(<span class="number">3</span><span class="operator">,</span> <span class="number">8</span>); series<span class="operator">-</span><span class="operator">></span>append(<span class="number">7</span><span class="operator">,</span> <span class="number">4</span>); series<span class="operator">-</span><span class="operator">></span>append(<span class="number">10</span><span class="operator">,</span> <span class="number">5</span>); <span class="operator">*</span>series <span class="operator"><</span><span class="operator"><</span> <span class="type">QPointF</span>(<span class="number">11</span><span class="operator">,</span> <span class="number">1</span>) <span class="operator"><</span><span class="operator"><</span> <span class="type">QPointF</span>(<span class="number">13</span><span class="operator">,</span> <span class="number">3</span>) <span class="operator"><</span><span class="operator"><</span> <span class="type">QPointF</span>(<span class="number">17</span><span class="operator">,</span> <span class="number">6</span>) <span class="operator"><</span><span class="operator"><</span> <span class="type">QPointF</span>(<span class="number">18</span><span class="operator">,</span> <span class="number">3</span>) <span class="operator"><</span><span class="operator"><</span> <span class="type">QPointF</span>(<span class="number">20</span><span class="operator">,</span> <span class="number">2</span>); </pre> <p>The data series has been populated. To display it on a chart we create a <a href="qchart.html">QChart</a> object and add the data series to it. We also set the title and the values range on the y-axis, so that our chart's visibility is better.</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>legend()<span class="operator">-</span><span class="operator">></span>hide(); chart<span class="operator">-</span><span class="operator">></span>addSeries(series); chart<span class="operator">-</span><span class="operator">></span>setTitle(<span class="string">"Simple spline chart example"</span>); chart<span class="operator">-</span><span class="operator">></span>createDefaultAxes(); chart<span class="operator">-</span><span class="operator">></span>axes(<span class="type">Qt</span><span class="operator">::</span>Vertical)<span class="operator">.</span>first()<span class="operator">-</span><span class="operator">></span>setRange(<span class="number">0</span><span class="operator">,</span> <span class="number">10</span>); </pre> <p>Then we create a <a href="qchartview.html">QChartView</a> object with <a href="qchart.html">QChart</a> as a parameter. This way we don't need to create a QGraphicsView scene ourselves. We also set the Antialiasing on to have the rendered lines look nicer.</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>Finally, we set the <a href="qchartview.html">QChartView</a> as the windows's central widget.</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">400</span><span class="operator">,</span> <span class="number">300</span>); window<span class="operator">.</span>show(); </pre> <p>Files:</p> <ul> <li><a href="qtcharts-splinechart-main-cpp.html">splinechart/main.cpp</a></li> <li><a href="qtcharts-splinechart-splinechart-pro.html">splinechart/splinechart.pro</a></li> </ul> </div> <!-- @@@splinechart --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2019 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>