/usr/share/qt5/doc/qtcharts/qtcharts-stackedbarchartdrilldown-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 188 189 190 191 192 193 194 195 196 197 198 199 200 | <?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-stackedbarchartdrilldown.qdoc -->
<title>StackedBarChart Drilldown 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 >StackedBarChart Drilldown 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="#implementing-drilldown">Implementing Drilldown</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">StackedBarChart Drilldown Example</h1>
<span class="subtitle"></span>
<!-- $$$stackedbarchartdrilldown-description -->
<div class="descr"> <a name="details"></a>
<p>In the drilldown example we create a stacked bar chart, which shows the harvest of various chili peppers during season. In season view the harvest is grouped by month. To drill down to weekly view, right-click the selected month. On weekly view, the harvest of the month clicked is shown by week.</p>
<p>The season view looks like this:</p>
<p class="centerAlign"><img src="images/examples_stackedbarchartdrilldown1.png" alt="" /></p><p>Clicking on a month shows that month's harvest:</p>
<p class="centerAlign"><img src="images/examples_stackedbarchartdrilldown2.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="implementing-drilldown"></a>
<h2 id="implementing-drilldown">Implementing Drilldown</h2>
<p>First we define a drilldown series class, which adds categories to the stacked bar series and mapping for categories to other drilldown series. The purpose of the drilldown series is to contain knowledge of the drilldown structure. The mapDrilldownSeries function maps the category to a given series. We can request the mapping for a category with the drilldownSeries(int category) function.</p>
<pre class="cpp">
<span class="keyword">class</span> DrilldownBarSeries : <span class="keyword">public</span> <span class="type"><a href="qstackedbarseries.html">QStackedBarSeries</a></span>
{
Q_OBJECT
<span class="keyword">public</span>:
DrilldownBarSeries(<span class="type">QStringList</span> categories<span class="operator">,</span> <span class="type">QObject</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span>);
<span class="type">void</span> mapDrilldownSeries(<span class="type">int</span> index<span class="operator">,</span> DrilldownBarSeries <span class="operator">*</span>drilldownSeries);
DrilldownBarSeries <span class="operator">*</span>drilldownSeries(<span class="type">int</span> index);
<span class="type">QStringList</span> categories();
<span class="keyword">private</span>:
<span class="type">QMap</span><span class="operator"><</span><span class="type">int</span><span class="operator">,</span> DrilldownBarSeries <span class="operator">*</span><span class="operator">></span> m_DrilldownSeries;
<span class="type">QStringList</span> m_categories;
};
</pre>
<p>Next we define our own drilldown chart, which implements the handler for the mouse click. All <a href="qbarseries.html">QBarSeries</a> derived classes send out the clicked(<a href="qbarset.html">QBarSet</a>*, int) signal when a series is clicked with the mouse. The parameter <a href="qbarset.html">QBarSet</a> contains the pointer to the clicked bar set and parameter int contains the index of the clicked category.</p>
<pre class="cpp">
<span class="keyword">class</span> DrilldownChart : <span class="keyword">public</span> <span class="type"><a href="qchart.html">QChart</a></span>
{
Q_OBJECT
<span class="keyword">public</span>:
<span class="keyword">explicit</span> DrilldownChart(<span class="type">QGraphicsItem</span> <span class="operator">*</span>parent <span class="operator">=</span> <span class="number">0</span><span class="operator">,</span> <span class="type">Qt</span><span class="operator">::</span>WindowFlags wFlags <span class="operator">=</span> <span class="number">0</span>);
<span class="type">void</span> changeSeries(DrilldownBarSeries <span class="operator">*</span>series);
<span class="keyword">public</span> Q_SLOTS:
<span class="type">void</span> handleClicked(<span class="type">int</span> index<span class="operator">,</span> <span class="type"><a href="qbarset.html">QBarSet</a></span> <span class="operator">*</span>barset);
<span class="keyword">private</span>:
DrilldownBarSeries <span class="operator">*</span>m_currentSeries;
};
</pre>
<p>Now we have our drilldown classes and we can start using them. First create the chart.</p>
<pre class="cpp">
DrilldownChart <span class="operator">*</span>drilldownChart <span class="operator">=</span> <span class="keyword">new</span> DrilldownChart();
drilldownChart<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>We define the categories from which the drilldown series will be constructed.</p>
<pre class="cpp">
<span class="comment">// Define categories</span>
<span class="keyword">const</span> <span class="type">QStringList</span> months <span class="operator">=</span> {
<span class="string">"May"</span><span class="operator">,</span> <span class="string">"Jun"</span><span class="operator">,</span> <span class="string">"Jul"</span><span class="operator">,</span> <span class="string">"Aug"</span><span class="operator">,</span> <span class="string">"Sep"</span>
};
<span class="keyword">const</span> <span class="type">QStringList</span> weeks <span class="operator">=</span> {
<span class="string">"week 1"</span><span class="operator">,</span> <span class="string">"week 2"</span><span class="operator">,</span> <span class="string">"week 3"</span><span class="operator">,</span> <span class="string">"week 4"</span>
};
<span class="keyword">const</span> <span class="type">QStringList</span> plants <span class="operator">=</span> {
<span class="string">"Habanero"</span><span class="operator">,</span> <span class="string">"Lemon Drop"</span><span class="operator">,</span> <span class="string">"Starfish"</span><span class="operator">,</span> <span class="string">"Aji Amarillo"</span>
};
</pre>
<p>To create the drilldown structure, we first create our top level series, which we call seasonSeries. For each month in seasonSeries we create a drilldown series called weeklySeries which contains more detailed data for that month. In weeklySeries, we use the drilldown handler to bring us back to seasonSeries. To do so we add mapping to the series. The seasonSeries is mapped to weeklySeries for each month. Every weeklySeries is mapped back to the seasonSeries. To make mapping work, we connect the clicked signals from our series to the drilldownChart.</p>
<pre class="cpp">
<span class="comment">// Create drilldown structure</span>
DrilldownBarSeries <span class="operator">*</span>seasonSeries <span class="operator">=</span> <span class="keyword">new</span> DrilldownBarSeries(months<span class="operator">,</span> drilldownChart);
seasonSeries<span class="operator">-</span><span class="operator">></span>setName(<span class="string">"Crop by month - Season"</span>);
<span class="comment">// Each month in season series has drilldown series for weekly data</span>
<span class="keyword">for</span> (<span class="type">int</span> month <span class="operator">=</span> <span class="number">0</span>; month <span class="operator"><</span> months<span class="operator">.</span>count(); month<span class="operator">+</span><span class="operator">+</span>) {
<span class="comment">// Create drilldown series for every week</span>
DrilldownBarSeries <span class="operator">*</span>weeklySeries <span class="operator">=</span> <span class="keyword">new</span> DrilldownBarSeries(weeks<span class="operator">,</span> drilldownChart);
seasonSeries<span class="operator">-</span><span class="operator">></span>mapDrilldownSeries(month<span class="operator">,</span> weeklySeries);
<span class="comment">// Drilling down from weekly data brings us back to season data.</span>
<span class="keyword">for</span> (<span class="type">int</span> week <span class="operator">=</span> <span class="number">0</span>; week <span class="operator"><</span> weeks<span class="operator">.</span>count(); week<span class="operator">+</span><span class="operator">+</span>) {
weeklySeries<span class="operator">-</span><span class="operator">></span>mapDrilldownSeries(week<span class="operator">,</span> seasonSeries);
weeklySeries<span class="operator">-</span><span class="operator">></span>setName(<span class="type">QString</span>(<span class="string">"Crop by week - "</span> <span class="operator">+</span> months<span class="operator">.</span>at(month)));
}
<span class="comment">// Use clicked signal to implement drilldown</span>
<span class="type">QObject</span><span class="operator">::</span>connect(weeklySeries<span class="operator">,</span> <span class="operator">&</span>DrilldownBarSeries<span class="operator">::</span>clicked<span class="operator">,</span>
drilldownChart<span class="operator">,</span> <span class="operator">&</span>DrilldownChart<span class="operator">::</span>handleClicked);
}
<span class="comment">// Enable drilldown from season series using clicked signal</span>
<span class="type">QObject</span><span class="operator">::</span>connect(seasonSeries<span class="operator">,</span> <span class="operator">&</span>DrilldownBarSeries<span class="operator">::</span>clicked<span class="operator">,</span>
drilldownChart<span class="operator">,</span> <span class="operator">&</span>DrilldownChart<span class="operator">::</span>handleClicked);
</pre>
<p>When we have our drilldown structure ready, we can add the data to it. Here we generate a random crop for each plant in each week. The monthly crop is calculated from weekly crops and is set as value to the monthly series.</p>
<pre class="cpp">
<span class="comment">// Fill monthly and weekly series with data</span>
<span class="keyword">for</span> (<span class="keyword">const</span> <span class="type">QString</span> <span class="operator">&</span>plant : plants) {
<span class="type"><a href="qbarset.html">QBarSet</a></span> <span class="operator">*</span>monthlyCrop <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qbarset.html">QBarSet</a></span>(plant);
<span class="keyword">for</span> (<span class="type">int</span> month <span class="operator">=</span> <span class="number">0</span>; month <span class="operator"><</span> months<span class="operator">.</span>count(); month<span class="operator">+</span><span class="operator">+</span>) {
<span class="type"><a href="qbarset.html">QBarSet</a></span> <span class="operator">*</span>weeklyCrop <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qbarset.html">QBarSet</a></span>(plant);
<span class="keyword">for</span> (<span class="type">int</span> week <span class="operator">=</span> <span class="number">0</span>; week <span class="operator"><</span> weeks<span class="operator">.</span>count(); week<span class="operator">+</span><span class="operator">+</span>)
<span class="operator">*</span>weeklyCrop <span class="operator"><</span><span class="operator"><</span> (qrand() <span class="operator">%</span> <span class="number">20</span>);
<span class="comment">// Get the drilldown series from season series and add crop to it.</span>
seasonSeries<span class="operator">-</span><span class="operator">></span>drilldownSeries(month)<span class="operator">-</span><span class="operator">></span>append(weeklyCrop);
<span class="operator">*</span>monthlyCrop <span class="operator"><</span><span class="operator"><</span> weeklyCrop<span class="operator">-</span><span class="operator">></span>sum();
}
seasonSeries<span class="operator">-</span><span class="operator">></span>append(monthlyCrop);
}
</pre>
<p>Here we set the chart to show the top level series initially.</p>
<pre class="cpp">
<span class="comment">// Show season series in initial view</span>
drilldownChart<span class="operator">-</span><span class="operator">></span>changeSeries(seasonSeries);
drilldownChart<span class="operator">-</span><span class="operator">></span>setTitle(seasonSeries<span class="operator">-</span><span class="operator">></span>name());
</pre>
<p>Files:</p>
<ul>
<li><a href="qtcharts-stackedbarchartdrilldown-drilldownchart-cpp.html">stackedbarchartdrilldown/drilldownchart.cpp</a></li>
<li><a href="qtcharts-stackedbarchartdrilldown-drilldownchart-h.html">stackedbarchartdrilldown/drilldownchart.h</a></li>
<li><a href="qtcharts-stackedbarchartdrilldown-drilldownseries-cpp.html">stackedbarchartdrilldown/drilldownseries.cpp</a></li>
<li><a href="qtcharts-stackedbarchartdrilldown-drilldownseries-h.html">stackedbarchartdrilldown/drilldownseries.h</a></li>
<li><a href="qtcharts-stackedbarchartdrilldown-main-cpp.html">stackedbarchartdrilldown/main.cpp</a></li>
<li><a href="qtcharts-stackedbarchartdrilldown-stackedbarchartdrilldown-pro.html">stackedbarchartdrilldown/stackedbarchartdrilldown.pro</a></li>
</ul>
</div>
<!-- @@@stackedbarchartdrilldown -->
</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>
|