##// END OF EJS Templates
splitted barchartdrilldown example to separate files
sauimone -
r903:ecdea230fe77
parent child
Show More
@@ -0,0 +1,48
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "drilldownchart.h"
22 #include <QChartAxis>
23
24 QTCOMMERCIALCHART_USE_NAMESPACE
25
26 DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags)
27 : QChart(parent, wFlags)
28 ,m_currentSeries(0)
29 {
30 }
31
32 void DrilldownChart::changeSeries(QSeries* series)
33 {
34 if (m_currentSeries)
35 removeSeries(m_currentSeries);
36 m_currentSeries = series;
37 addSeries(series);
38 setTitle(series->name());
39 axisY()->setNiceNumbers(true);
40 }
41
42 void DrilldownChart::handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button)
43 {
44 Q_UNUSED(barset)
45 Q_UNUSED(button)
46 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
47 changeSeries(series->drilldownSeries(category));
48 }
@@ -0,0 +1,46
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef DRILLDOWNCHART_H
22 #define DRILLDOWNCHART_H
23
24 #include <QChart>
25 #include "drilldownseries.h"
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
29 //! [2]
30 class DrilldownChart : public QChart
31 {
32 Q_OBJECT
33 public:
34 explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0);
35
36 void changeSeries(QSeries* series);
37
38 public Q_SLOTS:
39 void handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button);
40
41 private:
42 QSeries* m_currentSeries;
43 };
44 //! [2]
45
46 #endif // DRILLDOWNCHART_H
@@ -0,0 +1,40
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #include "drilldownseries.h"
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
25 DrilldownBarSeries::DrilldownBarSeries(QStringList categories, QObject *parent)
26 : QStackedBarSeries(categories, parent)
27 {
28 }
29
30 void DrilldownBarSeries::mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
31 {
32 mDrilldownSeries[category] = drilldownSeries;
33 }
34
35 DrilldownBarSeries* DrilldownBarSeries::drilldownSeries(QString category)
36 {
37 return mDrilldownSeries[category];
38 }
39
40 #include "moc_drilldownseries.cpp"
@@ -0,0 +1,45
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
14 **
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
18 **
19 ****************************************************************************/
20
21 #ifndef DRILLDOWNSERIES_H
22 #define DRILLDOWNSERIES_H
23
24 #include <QStackedBarSeries>
25 #include <QMap>
26
27 QTCOMMERCIALCHART_USE_NAMESPACE
28
29 //! [1]
30 class DrilldownBarSeries : public QStackedBarSeries
31 {
32 Q_OBJECT
33 public:
34 DrilldownBarSeries(QStringList categories, QObject *parent = 0);
35
36 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries);
37
38 DrilldownBarSeries* drilldownSeries(QString category);
39
40 private:
41 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
42 };
43 //! [1]
44
45 #endif // DRILLDOWNSERIES_H
@@ -1,173 +1,113
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 #include <QStackedBarSeries>
25 24 #include <QBarSet>
26 25 #include <QLegend>
26 #include "drilldownseries.h"
27 #include "drilldownchart.h"
27 28
28 29 QTCOMMERCIALCHART_USE_NAMESPACE
29 30
30 //! [1]
31 class DrilldownBarSeries : public QStackedBarSeries
32 {
33 Q_OBJECT
34 public:
35 DrilldownBarSeries(QStringList categories, QObject *parent = 0)
36 :QStackedBarSeries(categories, parent)
37 {
38
39 }
40
41 void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
42 {
43 mDrilldownSeries[category] = drilldownSeries;
44 }
45
46 DrilldownBarSeries* drilldownSeries(QString category)
47 {
48 return mDrilldownSeries[category];
49 }
50
51 private:
52 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
53 };
54 //! [1]
55
56 //! [2]
57 class DrilldownChart : public QChart
58 {
59 Q_OBJECT
60 public:
61 explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0)
62 :QChart(parent, wFlags),
63 m_currentSeries(0)
64 {
65
66 }
67
68 void changeSeries(QSeries* series)
69 {
70 if (m_currentSeries)
71 removeSeries(m_currentSeries);
72 m_currentSeries = series;
73 addSeries(series);
74 setTitle(series->name());
75 }
76
77 public Q_SLOTS:
78 void handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button)
79 {
80 Q_UNUSED(barset)
81 Q_UNUSED(button)
82 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
83 changeSeries(series->drilldownSeries(category));
84 }
85
86 private:
87 QSeries* m_currentSeries;
88 };
89 //! [2]
90
91 31 int main(int argc, char *argv[])
92 32 {
93 33 QApplication a(argc, argv);
94 34 QMainWindow window;
95 35
96 36 DrilldownChart* drilldownChart = new DrilldownChart();
97 37 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
98 38 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
99 39
100 //! [3]
40 //! [3]
101 41 // Define categories
102 42 QStringList months;
103 43 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
104 44 QStringList weeks;
105 45 weeks << "week 1" << "week 2" << "week 3" << "week 4";
106 46 QStringList plants;
107 47 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
108 //! [3]
48 //! [3]
109 49
110 //! [4]
50 //! [4]
111 51 // Create drilldown structure
112 52 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
113 53 seasonSeries->setName("Crop by month - Season");
114 54
115 55 // Each month in season series has drilldown series for weekly data
116 56 foreach (QString month, months) {
117 57
118 58 // Create drilldown series for every week
119 59 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
120 60 seasonSeries->mapDrilldownSeries(month, weeklySeries);
121 61
122 62 // Drilling down from weekly data brings us back to season data.
123 63 foreach (QString week, weeks) {
124 64 weeklySeries->mapDrilldownSeries(week, seasonSeries);
125 65 weeklySeries->setName(QString("Crop by week - " + month));
126 66 }
127 67
128 68 // Use right click signal to implement drilldown
129 69 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
130 70 }
131 71
132 72 // Enable drilldown from season series using right click.
133 73 QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
134 //! [4]
74 //! [4]
135 75
136 //! [5]
76 //! [5]
137 77 // Fill monthly and weekly series with data
138 78 foreach (QString plant, plants) {
139 79 QBarSet* monthlyCrop = new QBarSet(plant);
140 80 foreach (QString month, months) {
141 81 QBarSet* weeklyCrop = new QBarSet(plant);
142 82 foreach (QString week, weeks )
143 83 *weeklyCrop << (qrand() % 20);
144 84 // Get the drilldown series from season series and add crop to it.
145 85 seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop);
146 86 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
147 87 *monthlyCrop << weeklyCrop->total();
148 88 }
149 89 seasonSeries->appendBarSet(monthlyCrop);
150 90 }
151 //! [5]
152
153 seasonSeries->setToolTipEnabled(true);
91 //! [5]
154 92
155 //! [6]
93 //! [6]
156 94 // Show season series in initial view
157 95 drilldownChart->changeSeries(seasonSeries);
158 96 drilldownChart->setTitle(seasonSeries->name());
159 //! [6]
97 //! [6]
160 98
99 //! [7]
161 100 drilldownChart->axisX()->setGridLineVisible(false);
101 drilldownChart->axisY()->setNiceNumbers(true);
162 102 drilldownChart->legend()->setVisible(true);
163 103 drilldownChart->legend()->setAlignmnent(QLegend::AlignmentBottom);
104 //! [7]
164 105
165 106 QChartView *chartView = new QChartView(drilldownChart);
166 107 window.setCentralWidget(chartView);
167 108 window.resize(400, 300);
168 109 window.show();
169 110
170 111 return a.exec();
171 112 }
172 113
173 #include "main.moc"
@@ -1,7 +1,7
1 1 !include( ../examples.pri ) {
2 2 error( "Couldn't find the examples.pri file!" )
3 3 }
4 4 TARGET = stackedbarchartdrilldown
5 SOURCES += main.cpp
6 HEADERS +=
5 SOURCES += main.cpp drilldownseries.cpp drilldownchart.cpp
6 HEADERS += drilldownseries.h drilldownchart.h
7 7
General Comments 0
You need to be logged in to leave comments. Login now