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 |
@@ -1,29 +1,30 | |||||
1 | /*! |
|
1 | /*! | |
2 | \page examples.html |
|
2 | \page examples.html | |
3 | \title Examples |
|
3 | \title Examples | |
4 | \keyword Examples |
|
4 | \keyword Examples | |
5 |
|
5 | |||
6 | \raw HTML |
|
6 | \raw HTML | |
7 | <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable"> |
|
7 | <table cellpadding="2" cellspacing="1" border="0" width="100%" class="indextable"> | |
8 | <tr> |
|
8 | <tr> | |
9 | <th class="titleheader" width="33%"> |
|
9 | <th class="titleheader" width="33%"> | |
10 | List of examples |
|
10 | List of examples | |
11 | </th> |
|
11 | </th> | |
12 | </tr> |
|
12 | </tr> | |
13 | <tr> |
|
13 | <tr> | |
14 | <td valign="top"> |
|
14 | <td valign="top"> | |
15 | <ul> |
|
15 | <ul> | |
16 | <li><a href="example-areachart.html">Area Chart example</a></li> |
|
16 | <li><a href="example-areachart.html">Area Chart example</a></li> | |
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> | |
23 | <li><a href="example-splinechart.html">Spline Chart example</a></li> |
|
24 | <li><a href="example-splinechart.html">Spline Chart example</a></li> | |
24 | </ul> |
|
25 | </ul> | |
25 | </td> |
|
26 | </td> | |
26 | </tr> |
|
27 | </tr> | |
27 | </table> |
|
28 | </table> | |
28 | \endraw |
|
29 | \endraw | |
29 | */ |
|
30 | */ |
@@ -1,152 +1,152 | |||||
1 | #include <QtGui/QApplication> |
|
1 | #include <QtGui/QApplication> | |
2 | #include <QMainWindow> |
|
2 | #include <QMainWindow> | |
3 | #include <qchartglobal.h> |
|
3 | #include <qchartglobal.h> | |
4 | #include <qchartview.h> |
|
4 | #include <qchartview.h> | |
5 | #include <qstackedbarseries.h> |
|
5 | #include <qstackedbarseries.h> | |
6 | #include <qbarset.h> |
|
6 | #include <qbarset.h> | |
7 | #include <qchartaxis.h> |
|
7 | #include <qchartaxis.h> | |
8 | #include <QStringList> |
|
8 | #include <QStringList> | |
9 | #include <QDebug> |
|
9 | #include <QDebug> | |
10 |
|
10 | |||
11 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | QTCOMMERCIALCHART_USE_NAMESPACE | |
12 |
|
12 | |||
13 | //! [1] |
|
13 | //! [1] | |
14 | class DrilldownBarSeries : public QStackedBarSeries |
|
14 | class DrilldownBarSeries : public QStackedBarSeries | |
15 | { |
|
15 | { | |
16 | Q_OBJECT |
|
16 | Q_OBJECT | |
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 |
|
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 | |||
31 | public Q_SLOTS: |
|
30 | public Q_SLOTS: | |
32 |
|
31 | |||
33 | private: |
|
32 | private: | |
34 |
|
33 | |||
35 | QMap<QString, DrilldownBarSeries*> mDrilldownSeries; |
|
34 | QMap<QString, DrilldownBarSeries*> mDrilldownSeries; | |
36 | }; |
|
35 | }; | |
37 | //! [1] |
|
36 | //! [1] | |
38 |
|
37 | |||
39 | //! [2] |
|
38 | //! [2] | |
40 | class DrilldownChart : public QChartView |
|
39 | class DrilldownChart : public QChartView | |
41 | { |
|
40 | { | |
42 | Q_OBJECT |
|
41 | Q_OBJECT | |
43 | public: |
|
42 | public: | |
44 | explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {} |
|
43 | explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {} | |
45 |
|
44 | |||
46 | void changeSeries(QSeries* series) |
|
45 | void changeSeries(QSeries* series) | |
47 | { |
|
46 | { | |
48 | if (m_currentSeries) |
|
47 | if (m_currentSeries) | |
49 | removeSeries(m_currentSeries); |
|
48 | removeSeries(m_currentSeries); | |
50 | m_currentSeries = series; |
|
49 | m_currentSeries = series; | |
51 | addSeries(series); |
|
50 | addSeries(series); | |
52 | setChartTitle(series->title()); |
|
51 | setChartTitle(series->title()); | |
53 | } |
|
52 | } | |
54 |
|
53 | |||
55 | public Q_SLOTS: |
|
54 | public Q_SLOTS: | |
56 |
void |
|
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 | } | |
63 |
|
61 | |||
64 | private: |
|
62 | private: | |
65 | QSeries* m_currentSeries; |
|
63 | QSeries* m_currentSeries; | |
66 | }; |
|
64 | }; | |
67 | //! [2] |
|
65 | //! [2] | |
68 |
|
66 | |||
69 | int main(int argc, char *argv[]) |
|
67 | int main(int argc, char *argv[]) | |
70 | { |
|
68 | { | |
71 | QApplication a(argc, argv); |
|
69 | QApplication a(argc, argv); | |
72 | QMainWindow window; |
|
70 | QMainWindow window; | |
73 |
|
71 | |||
74 | DrilldownChart* drilldownChart = new DrilldownChart(&window); |
|
72 | DrilldownChart* drilldownChart = new DrilldownChart(&window); | |
75 | drilldownChart->setChartTheme(QChart::ChartThemeIcy); |
|
73 | drilldownChart->setChartTheme(QChart::ChartThemeIcy); | |
76 |
|
74 | |||
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; | |
84 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; |
|
82 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; | |
85 | //! [3] |
|
83 | //! [3] | |
86 |
|
84 | |||
87 | //! [4] |
|
85 | //! [4] | |
88 | // Create drilldown structure |
|
86 | // Create drilldown structure | |
89 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); |
|
87 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); | |
90 | seasonSeries->setTitle("Crop by month - Season"); |
|
88 | seasonSeries->setTitle("Crop by month - Season"); | |
91 |
|
89 | |||
92 | // Each month in season series has drilldown series for weekly data |
|
90 | // Each month in season series has drilldown series for weekly data | |
93 | foreach (QString month, months) { |
|
91 | foreach (QString month, months) { | |
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-> |
|
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-> |
|
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] | |
111 | // Fill monthly and weekly series with data |
|
112 | // Fill monthly and weekly series with data | |
112 | foreach (QString plant, plants) { |
|
113 | foreach (QString plant, plants) { | |
113 | QBarSet* monthlyCrop = new QBarSet(plant); |
|
114 | QBarSet* monthlyCrop = new QBarSet(plant); | |
114 | foreach (QString month, months) { |
|
115 | foreach (QString month, months) { | |
115 | QBarSet* weeklyCrop = new QBarSet(plant); |
|
116 | QBarSet* weeklyCrop = new QBarSet(plant); | |
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 |
|
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(); | |
123 |
|
124 | |||
124 | QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); |
|
125 | QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); | |
125 | } |
|
126 | } | |
126 | seasonSeries->addBarSet(monthlyCrop); |
|
127 | seasonSeries->addBarSet(monthlyCrop); | |
127 | QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); |
|
128 | QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); | |
128 | } |
|
129 | } | |
129 | //! [5] |
|
130 | //! [5] | |
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); | |
142 | drilldownChart->axisX()->setGridVisible(false); |
|
142 | drilldownChart->axisX()->setGridVisible(false); | |
143 | drilldownChart->axisX()->setLabelsVisible(false); |
|
143 | drilldownChart->axisX()->setLabelsVisible(false); | |
144 |
|
144 | |||
145 | window.setCentralWidget(drilldownChart); |
|
145 | window.setCentralWidget(drilldownChart); | |
146 | window.resize(400, 300); |
|
146 | window.resize(400, 300); | |
147 | window.show(); |
|
147 | window.show(); | |
148 |
|
148 | |||
149 | return a.exec(); |
|
149 | return a.exec(); | |
150 | } |
|
150 | } | |
151 |
|
151 | |||
152 | #include "main.moc" |
|
152 | #include "main.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now