##// END OF EJS Templates
stacked bar drilldown example documentation update
sauimone -
r1415:3a9150ae63dc
parent child
Show More
@@ -1,42 +1,45
1 /*!
1 /*!
2 \example examples/stackedbarchartdrilldown
2 \example examples/stackedbarchartdrilldown
3 \title StackedBarChart Drilldown Example
3 \title StackedBarChart Drilldown Example
4 \subtitle
4 \subtitle
5
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 :)
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
7
8 Season view looks like this:
8 Season view looks like this:
9 \image examples_stackedbarchartdrilldown1.png
9 \image examples_stackedbarchartdrilldown1.png
10
10
11 Mouse button clicked on a month, shows the harvest from that month:
11 Mouse button clicked on a month, shows the harvest from that month:
12 \image examples_stackedbarchartdrilldown2.png
12 \image examples_stackedbarchartdrilldown2.png
13
13
14 Here we define a drilldown series, which adds mapping for categories to other drilldown series.
14 First we define a drilldown series class, which adds categories to stacked bar series and 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.
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(int category) function.
16
16
17 \snippet ../examples/stackedbarchartdrilldown/main.cpp 1
17 \snippet ../examples/stackedbarchartdrilldown/drilldownseries.h 1
18
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 occurred.
19 Next we define our own drilldown chart, which implements handler for mouse click. All QBarSeries derived classes send out clicked(QBarSet*, int) signal when series is clicked with mouse. The parameter QBarSet contains pointer to clicked bar set and parameter int contains the index of clicked category.
20
21 \snippet ../examples/stackedbarchartdrilldown/drilldownchart.h 1
20
22
21 In our DrilldownChart we implement handler, which selects the drilldown chart with the category.
23 Now we have our drilldown classes and we can start using them.
24 First create the chart.
25
26 \snippet ../examples/stackedbarchartdrilldown/main.cpp 1
22
27
28 We define categories, from wich the drilldown series will be constructed.
29
23 \snippet ../examples/stackedbarchartdrilldown/main.cpp 2
30 \snippet ../examples/stackedbarchartdrilldown/main.cpp 2
24
31
25 Here we define data, which we use to construct the chart.
26
27 \snippet ../examples/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.
32 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 add mapping to the series. The seasonSeries is mapped to weeklySeries for each month. Every weeklySeries is mapped back to the seasonSeries.
33 In weeklySeries, we use the drilldown handler to bring us back to seasonSeries. To do this we add mapping to the series. The seasonSeries is mapped to weeklySeries for each month. Every weeklySeries is mapped back to the seasonSeries.
31 To make mapping work, we connect the rightClicked signals from our series to the drilldownChart.
34 To make mapping work, we connect the clicked signals from our series to the drilldownChart.
32
35
33 \snippet ../examples/stackedbarchartdrilldown/main.cpp 4
36 \snippet ../examples/stackedbarchartdrilldown/main.cpp 3
34
37
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.
38 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 and is set as value to monthly series.
36
39
37 \snippet ../examples/stackedbarchartdrilldown/main.cpp 5
40 \snippet ../examples/stackedbarchartdrilldown/main.cpp 4
38
41
39 Here we set the chart to show top level series initially.
42 Here we set the chart to show top level series initially.
40
43
41 \snippet ../examples/stackedbarchartdrilldown/main.cpp 6
44 \snippet ../examples/stackedbarchartdrilldown/main.cpp 5
42 */
45 */
@@ -1,46 +1,46
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef DRILLDOWNCHART_H
21 #ifndef DRILLDOWNCHART_H
22 #define DRILLDOWNCHART_H
22 #define DRILLDOWNCHART_H
23
23
24 #include <QChart>
24 #include <QChart>
25 #include "drilldownseries.h"
25 #include "drilldownseries.h"
26
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
28
29 //! [2]
29 //! [1]
30 class DrilldownChart : public QChart
30 class DrilldownChart : public QChart
31 {
31 {
32 Q_OBJECT
32 Q_OBJECT
33 public:
33 public:
34 explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
34 explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
35
35
36 void changeSeries(DrilldownBarSeries *series);
36 void changeSeries(DrilldownBarSeries *series);
37
37
38 public Q_SLOTS:
38 public Q_SLOTS:
39 void handleClicked(QBarSet *barset, int index);
39 void handleClicked(QBarSet *barset, int index);
40
40
41 private:
41 private:
42 DrilldownBarSeries* m_currentSeries;
42 DrilldownBarSeries* m_currentSeries;
43 };
43 };
44 //! [2]
44 //! [1]
45
45
46 #endif // DRILLDOWNCHART_H
46 #endif // DRILLDOWNCHART_H
@@ -1,117 +1,115
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include <QtGui/QApplication>
21 #include <QtGui/QApplication>
22 #include <QMainWindow>
22 #include <QMainWindow>
23 #include <QChartView>
23 #include <QChartView>
24 #include <QBarSet>
24 #include <QBarSet>
25 #include <QLegend>
25 #include <QLegend>
26 #include "drilldownseries.h"
26 #include "drilldownseries.h"
27 #include "drilldownchart.h"
27 #include "drilldownchart.h"
28
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
30
31 int main(int argc, char *argv[])
31 int main(int argc, char *argv[])
32 {
32 {
33 //! [1]
34 QApplication a(argc, argv);
33 QApplication a(argc, argv);
35 QMainWindow window;
34 QMainWindow window;
36 //! [1]
37
35
38 //! [2]
36 //! [1]
39 DrilldownChart* drilldownChart = new DrilldownChart();
37 DrilldownChart* drilldownChart = new DrilldownChart();
40 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
38 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
41 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
39 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
42 //! [2]
40 //! [1]
43
41
44 //! [3]
42 //! [2]
45 // Define categories
43 // Define categories
46 QStringList months;
44 QStringList months;
47 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
45 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
48 QStringList weeks;
46 QStringList weeks;
49 weeks << "week 1" << "week 2" << "week 3" << "week 4";
47 weeks << "week 1" << "week 2" << "week 3" << "week 4";
50 QStringList plants;
48 QStringList plants;
51 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
49 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
52 //! [3]
50 //! [2]
53
51
54 //! [4]
52 //! [3]
55 // Create drilldown structure
53 // Create drilldown structure
56 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
54 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
57 seasonSeries->setName("Crop by month - Season");
55 seasonSeries->setName("Crop by month - Season");
58
56
59 // Each month in season series has drilldown series for weekly data
57 // Each month in season series has drilldown series for weekly data
60 for (int month=0; month < months.count(); month++) {
58 for (int month=0; month < months.count(); month++) {
61
59
62 // Create drilldown series for every week
60 // Create drilldown series for every week
63 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
61 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
64 seasonSeries->mapDrilldownSeries(month, weeklySeries);
62 seasonSeries->mapDrilldownSeries(month, weeklySeries);
65
63
66 // Drilling down from weekly data brings us back to season data.
64 // Drilling down from weekly data brings us back to season data.
67 for (int week=0; week < weeks.count(); week++) {
65 for (int week=0; week < weeks.count(); week++) {
68 weeklySeries->mapDrilldownSeries(week, seasonSeries);
66 weeklySeries->mapDrilldownSeries(week, seasonSeries);
69 weeklySeries->setName(QString("Crop by week - " + months.at(month)));
67 weeklySeries->setName(QString("Crop by week - " + months.at(month)));
70 }
68 }
71
69
72 // Use right click signal to implement drilldown
70 // Use right click signal to implement drilldown
73 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,int)), drilldownChart, SLOT(handleClicked(QBarSet*,int)));
71 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,int)), drilldownChart, SLOT(handleClicked(QBarSet*,int)));
74 }
72 }
75
73
76 // Enable drilldown from season series using right click.
74 // Enable drilldown from season series using right click.
77 QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,int)), drilldownChart, SLOT(handleClicked(QBarSet*,int)));
75 QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,int)), drilldownChart, SLOT(handleClicked(QBarSet*,int)));
78 //! [4]
76 //! [3]
79
77
80 //! [5]
78 //! [4]
81 // Fill monthly and weekly series with data
79 // Fill monthly and weekly series with data
82 foreach (QString plant, plants) {
80 foreach (QString plant, plants) {
83 QBarSet* monthlyCrop = new QBarSet(plant);
81 QBarSet* monthlyCrop = new QBarSet(plant);
84 for (int month=0; month<months.count(); month++) {
82 for (int month=0; month<months.count(); month++) {
85 QBarSet* weeklyCrop = new QBarSet(plant);
83 QBarSet* weeklyCrop = new QBarSet(plant);
86 for (int week=0; week<weeks.count(); week++) {
84 for (int week=0; week<weeks.count(); week++) {
87 *weeklyCrop << (qrand() % 20);
85 *weeklyCrop << (qrand() % 20);
88 }
86 }
89 // Get the drilldown series from season series and add crop to it.
87 // Get the drilldown series from season series and add crop to it.
90 seasonSeries->drilldownSeries(month)->append(weeklyCrop);
88 seasonSeries->drilldownSeries(month)->append(weeklyCrop);
91 *monthlyCrop << weeklyCrop->sum();
89 *monthlyCrop << weeklyCrop->sum();
92 }
90 }
93 seasonSeries->append(monthlyCrop);
91 seasonSeries->append(monthlyCrop);
94 }
92 }
95 //! [5]
93 //! [4]
96
94
97 //! [6]
95 //! [5]
98 // Show season series in initial view
96 // Show season series in initial view
99 drilldownChart->changeSeries(seasonSeries);
97 drilldownChart->changeSeries(seasonSeries);
100 drilldownChart->setTitle(seasonSeries->name());
98 drilldownChart->setTitle(seasonSeries->name());
101 //! [6]
99 //! [5]
102
100
103 //! [7]
101 //! [6]
104 drilldownChart->axisX()->setGridLineVisible(false);
102 drilldownChart->axisX()->setGridLineVisible(false);
105 drilldownChart->axisY()->setNiceNumbersEnabled(true);
103 drilldownChart->axisY()->setNiceNumbersEnabled(true);
106 drilldownChart->legend()->setVisible(true);
104 drilldownChart->legend()->setVisible(true);
107 drilldownChart->legend()->setAlignment(Qt::AlignBottom);
105 drilldownChart->legend()->setAlignment(Qt::AlignBottom);
108 //! [7]
106 //! [6]
109
107
110 QChartView *chartView = new QChartView(drilldownChart);
108 QChartView *chartView = new QChartView(drilldownChart);
111 window.setCentralWidget(chartView);
109 window.setCentralWidget(chartView);
112 window.resize(400, 300);
110 window.resize(400, 300);
113 window.show();
111 window.show();
114
112
115 return a.exec();
113 return a.exec();
116 }
114 }
117
115
General Comments 0
You need to be logged in to leave comments. Login now