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