<?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-linechart.qdoc --> <title>LineChart 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 >LineChart 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-line-charts">Creating Line Charts</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">LineChart Example</h1> <span class="subtitle"></span> <!-- $$$linechart-description --> <div class="descr"> <a name="details"></a> <p class="centerAlign"><img src="images/examples_linechart.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-line-charts"></a> <h2 id="creating-line-charts">Creating Line Charts</h2> <p>To create a line chart, a <a href="qlineseries.html">QLineSeries</a> instance is needed. Let's create one.</p> <pre class="cpp"> <span class="type"><a href="qlineseries.html">QLineSeries</a></span> <span class="operator">*</span>series <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineseries.html">QLineSeries</a></span>(); </pre> <p>Then we add data to the series. We can use the append() member function or use the stream operator.</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>To present the data on the chart we need a <a href="qchart.html">QChart</a> instance. We add the series to it, create the default axes, and set the title of the chart.</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>createDefaultAxes(); chart<span class="operator">-</span><span class="operator">></span>setTitle(<span class="string">"Simple line chart example"</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>The chart is ready to be shown.</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-linechart-main-cpp.html">linechart/main.cpp</a></li> <li><a href="qtcharts-linechart-linechart-pro.html">linechart/linechart.pro</a></li> </ul> </div> <!-- @@@linechart --> </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>