##// 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
NO CONTENT: new file 100644, binary diff hidden
1 NO CONTENT: new file 100644, binary diff hidden
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
@@ -17,6 +17,7
17 <li><a href="example-barchart.html">Bar Chart example</a></li>
17 <li><a href="example-barchart.html">Bar Chart example</a></li>
18 <li><a href="example-stackedbarchart.html">Stacked Bar Chart example</a></li>
18 <li><a href="example-stackedbarchart.html">Stacked Bar Chart example</a></li>
19 <li><a href="example-percentbarchart.html">Percent Bar Chart example</a></li>
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 <li><a href="example-linechart.html">Line Chart example</a></li>
21 <li><a href="example-linechart.html">Line Chart example</a></li>
21 <li><a href="example-piechart.html">Pie Chart example</a></li>
22 <li><a href="example-piechart.html">Pie Chart example</a></li>
22 <li><a href="example-gdpbarchart.html">GDP Chart example</a></li>
23 <li><a href="example-gdpbarchart.html">GDP Chart example</a></li>
@@ -17,14 +17,13 class DrilldownBarSeries : public QStackedBarSeries
17 public:
17 public:
18 DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {}
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 mDrilldownSeries[category] = drilldownSeries;
22 mDrilldownSeries[category] = drilldownSeries;
23 }
23 }
24
24
25 DrilldownBarSeries* drilldownSeries(QString category)
25 DrilldownBarSeries* drilldownSeries(QString category)
26 {
26 {
27 // qDebug() << "DrilldownBarSeries::drilldownSeries" << category << mDrilldownSeries[category];
28 return mDrilldownSeries[category];
27 return mDrilldownSeries[category];
29 }
28 }
30
29
@@ -55,8 +54,7 public:
55 public Q_SLOTS:
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;
57 // qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category;
59 // TODO: continue from here
60 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
58 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
61 changeSeries(series->drilldownSeries(category));
59 changeSeries(series->drilldownSeries(category));
62 }
60 }
@@ -77,7 +75,7 int main(int argc, char *argv[])
77 //! [3]
75 //! [3]
78 // Define categories
76 // Define categories
79 QStringList months;
77 QStringList months;
80 months << "Jun" << "Jul" << "Aug" << "Sep";
78 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
81 QStringList weeks;
79 QStringList weeks;
82 weeks << "week 1" << "week 2" << "week 3" << "week 4";
80 weeks << "week 1" << "week 2" << "week 3" << "week 4";
83 QStringList plants;
81 QStringList plants;
@@ -94,17 +92,20 int main(int argc, char *argv[])
94
92
95 // Create drilldown series for every week
93 // Create drilldown series for every week
96 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
94 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
97 seasonSeries->addDrilldownSeries(month, weeklySeries);
95 seasonSeries->mapDrilldownSeries(month, weeklySeries);
98
96
99 // Drilling down from weekly data brings us back to season data.
97 // Drilling down from weekly data brings us back to season data.
100 foreach (QString week, weeks) {
98 foreach (QString week, weeks) {
101 weeklySeries->addDrilldownSeries(week, seasonSeries);
99 weeklySeries->mapDrilldownSeries(week, seasonSeries);
102 weeklySeries->setTitle(QString("Crop by week - " + month));
100 weeklySeries->setTitle(QString("Crop by week - " + month));
103 }
101 }
104
102
105 // Use right click signal to implement drilldown
103 // Use right click signal to implement drilldown
106 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
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 //! [4]
109 //! [4]
109
110
110 //! [5]
111 //! [5]
@@ -116,7 +117,7 int main(int argc, char *argv[])
116 foreach (QString week, weeks ) {
117 foreach (QString week, weeks ) {
117 *weeklyCrop << (qrand() % 20);
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 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
121 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
121 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
122 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
122 *monthlyCrop << weeklyCrop->total();
123 *monthlyCrop << weeklyCrop->total();
@@ -130,12 +131,11 int main(int argc, char *argv[])
130
131
131 seasonSeries->setToolTipEnabled(true);
132 seasonSeries->setToolTipEnabled(true);
132
133
133 // Enable drilldown from season series using right click.
134 //! [6]
134 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
135
136 // Show season series in initial view
135 // Show season series in initial view
137 drilldownChart->changeSeries(seasonSeries);
136 drilldownChart->changeSeries(seasonSeries);
138 drilldownChart->setChartTitle(seasonSeries->title());
137 drilldownChart->setChartTitle(seasonSeries->title());
138 //! [6]
139
139
140 // Disable axis, since they don't really apply to bar chart
140 // Disable axis, since they don't really apply to bar chart
141 drilldownChart->axisX()->setAxisVisible(false);
141 drilldownChart->axisX()->setAxisVisible(false);
General Comments 0
You need to be logged in to leave comments. Login now