##// END OF EJS Templates
Documentation for stacked barchart drilldown example
sauimone -
r462:9d290192c2c6
parent child
Show More
1 NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
@@ -0,0 +1,42
1 /*!
2 \example example/stackedbarchartdrilldown
3 \title StackedBarChart Drilldown Example
4 \subtitle
5
6 The example shows how to implement drilldown using stacked barchart. In drilldown example we create stacked barchart, 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, user clicks selected month with right mouse button. On weekly view, the harvest of clicked month is shown by week. For example purposes each month is 4 weeks long :)
7
8 Season view looks like this:
9 \image stackedbarchart_drilldown_season.png
10
11 Right mouse button clicked on august, shows the harvest from august:
12 \image stackedbarchart_drilldown_august.png
13
14 Here we define a drilldown series, which adds mapping for categories to other drilldown series.
15 Purpose of drilldown series is to contain knowledge of the drilldown structure. The mapDrilldownSeries function maps the category to given series. We can ask the mapping for category with drilldownSeries(QString category) function.
16
17 \snippet ../example/stackedbarchartdrilldown/main.cpp 1
18
19 To enable drilldown we create own view, which implements handler for right click. All QBarSeries derived classes send out rightClicked(QBarSet*, QString) signal when series is clicked with right mouse button. The parameters QBarSet and QString contain the pointer to clicked bar set and name of category, where click occured.
20
21 In our DrilldownChart we implement handler, which selects the drilldown chart with the category.
22
23 \snippet ../example/stackedbarchartdrilldown/main.cpp 2
24
25 Here we define data, which we use to construct the chart.
26
27 \snippet ../example/stackedbarchartdrilldown/main.cpp 3
28
29 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.
30 In weeklySeries, we use the drilldown handler to bring us back to seasonSeries. To do this, we connect the rightClicked signal from each weeklySeries to the handler in our DrilldownChart.
31 Finally we connect the rightClicked signal to DrilldownChart.
32
33 \snippet ../example/stackedbarchartdrilldown/main.cpp 4
34
35 When we have our drilldown structure ready, we can add the data to it. Here we generate random crop for each plant in each week. The monthly crop is calculated from weekly crops. To enable floating values, we connect the clicked signal to toggle the value of corresponding set.
36
37 \snippet ../example/stackedbarchartdrilldown/main.cpp 5
38
39 Here we set the chart to show top level series initially.
40
41 \snippet ../example/stackedbarchartdrilldown/main.cpp 6
42 */ No newline at end of file
@@ -1,29 +1,30
1 1 /*!
2 2 \page examples.html
3 3 \title Examples
4 4 \keyword Examples
5 5
6 6 \raw HTML
7 7 <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable">
8 8 <tr>
9 9 <th class="titleheader" width="33%">
10 10 List of examples
11 11 </th>
12 12 </tr>
13 13 <tr>
14 14 <td valign="top">
15 15 <ul>
16 16 <li><a href="example-areachart.html">Area Chart example</a></li>
17 17 <li><a href="example-barchart.html">Bar Chart example</a></li>
18 18 <li><a href="example-stackedbarchart.html">Stacked Bar Chart example</a></li>
19 19 <li><a href="example-percentbarchart.html">Percent Bar Chart example</a></li>
20 <li><a href="example-stackedbarchartdrilldown.html">Stacked Bar Chart Drilldown example</a></li>
20 21 <li><a href="example-linechart.html">Line Chart example</a></li>
21 22 <li><a href="example-piechart.html">Pie Chart example</a></li>
22 23 <li><a href="example-gdpbarchart.html">GDP Chart example</a></li>
23 24 <li><a href="example-splinechart.html">Spline Chart example</a></li>
24 25 </ul>
25 26 </td>
26 27 </tr>
27 28 </table>
28 29 \endraw
29 30 */
@@ -1,152 +1,152
1 1 #include <QtGui/QApplication>
2 2 #include <QMainWindow>
3 3 #include <qchartglobal.h>
4 4 #include <qchartview.h>
5 5 #include <qstackedbarseries.h>
6 6 #include <qbarset.h>
7 7 #include <qchartaxis.h>
8 8 #include <QStringList>
9 9 #include <QDebug>
10 10
11 11 QTCOMMERCIALCHART_USE_NAMESPACE
12 12
13 13 //! [1]
14 14 class DrilldownBarSeries : public QStackedBarSeries
15 15 {
16 16 Q_OBJECT
17 17 public:
18 18 DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {}
19 19
20 void addDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
20 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
21 21 {
22 22 mDrilldownSeries[category] = drilldownSeries;
23 23 }
24 24
25 25 DrilldownBarSeries* drilldownSeries(QString category)
26 26 {
27 // qDebug() << "DrilldownBarSeries::drilldownSeries" << category << mDrilldownSeries[category];
28 27 return mDrilldownSeries[category];
29 28 }
30 29
31 30 public Q_SLOTS:
32 31
33 32 private:
34 33
35 34 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
36 35 };
37 36 //! [1]
38 37
39 38 //! [2]
40 39 class DrilldownChart : public QChartView
41 40 {
42 41 Q_OBJECT
43 42 public:
44 43 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
45 44
46 45 void changeSeries(QSeries* series)
47 46 {
48 47 if (m_currentSeries)
49 48 removeSeries(m_currentSeries);
50 49 m_currentSeries = series;
51 50 addSeries(series);
52 51 setChartTitle(series->title());
53 52 }
54 53
55 54 public Q_SLOTS:
56 void handleRightClick(QBarSet *barset, QString category)
55 void handleRightClick(QBarSet *barset, QString category)
57 56 {
58 qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category;
59 // TODO: continue from here
57 // qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category;
60 58 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
61 59 changeSeries(series->drilldownSeries(category));
62 60 }
63 61
64 62 private:
65 63 QSeries* m_currentSeries;
66 64 };
67 65 //! [2]
68 66
69 67 int main(int argc, char *argv[])
70 68 {
71 69 QApplication a(argc, argv);
72 70 QMainWindow window;
73 71
74 72 DrilldownChart* drilldownChart = new DrilldownChart(&window);
75 73 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
76 74
77 75 //! [3]
78 76 // Define categories
79 77 QStringList months;
80 months << "Jun" << "Jul" << "Aug" << "Sep";
78 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
81 79 QStringList weeks;
82 80 weeks << "week 1" << "week 2" << "week 3" << "week 4";
83 81 QStringList plants;
84 82 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
85 83 //! [3]
86 84
87 85 //! [4]
88 86 // Create drilldown structure
89 87 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
90 88 seasonSeries->setTitle("Crop by month - Season");
91 89
92 90 // Each month in season series has drilldown series for weekly data
93 91 foreach (QString month, months) {
94 92
95 93 // Create drilldown series for every week
96 94 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
97 seasonSeries->addDrilldownSeries(month, weeklySeries);
95 seasonSeries->mapDrilldownSeries(month, weeklySeries);
98 96
99 97 // Drilling down from weekly data brings us back to season data.
100 98 foreach (QString week, weeks) {
101 weeklySeries->addDrilldownSeries(week, seasonSeries);
99 weeklySeries->mapDrilldownSeries(week, seasonSeries);
102 100 weeklySeries->setTitle(QString("Crop by week - " + month));
103 101 }
104 102
105 103 // Use right click signal to implement drilldown
106 104 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
107 105 }
106
107 // Enable drilldown from season series using right click.
108 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
108 109 //! [4]
109 110
110 111 //! [5]
111 112 // Fill monthly and weekly series with data
112 113 foreach (QString plant, plants) {
113 114 QBarSet* monthlyCrop = new QBarSet(plant);
114 115 foreach (QString month, months) {
115 116 QBarSet* weeklyCrop = new QBarSet(plant);
116 117 foreach (QString week, weeks ) {
117 118 *weeklyCrop << (qrand() % 20);
118 119 }
119 // Get the drilldown series from upper level series and add crop to it.
120 // Get the drilldown series from season series and add crop to it.
120 121 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
121 122 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
122 123 *monthlyCrop << weeklyCrop->total();
123 124
124 125 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
125 126 }
126 127 seasonSeries->addBarSet(monthlyCrop);
127 128 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
128 129 }
129 130 //! [5]
130 131
131 132 seasonSeries->setToolTipEnabled(true);
132 133
133 // Enable drilldown from season series using right click.
134 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
135
134 //! [6]
136 135 // Show season series in initial view
137 136 drilldownChart->changeSeries(seasonSeries);
138 137 drilldownChart->setChartTitle(seasonSeries->title());
138 //! [6]
139 139
140 140 // Disable axis, since they don't really apply to bar chart
141 141 drilldownChart->axisX()->setAxisVisible(false);
142 142 drilldownChart->axisX()->setGridVisible(false);
143 143 drilldownChart->axisX()->setLabelsVisible(false);
144 144
145 145 window.setCentralWidget(drilldownChart);
146 146 window.resize(400, 300);
147 147 window.show();
148 148
149 149 return a.exec();
150 150 }
151 151
152 152 #include "main.moc"
General Comments 0
You need to be logged in to leave comments. Login now