##// 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 ** 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 <QStackedBarSeries>
25 #include <QBarSet>
24 #include <QBarSet>
26 #include <QLegend>
25 #include <QLegend>
26 #include "drilldownseries.h"
27 #include "drilldownchart.h"
27
28
28 QTCOMMERCIALCHART_USE_NAMESPACE
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 int main(int argc, char *argv[])
31 int main(int argc, char *argv[])
92 {
32 {
93 QApplication a(argc, argv);
33 QApplication a(argc, argv);
94 QMainWindow window;
34 QMainWindow window;
95
35
96 DrilldownChart* drilldownChart = new DrilldownChart();
36 DrilldownChart* drilldownChart = new DrilldownChart();
97 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
37 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
98 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
38 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
99
39
100 //! [3]
40 //! [3]
101 // Define categories
41 // Define categories
102 QStringList months;
42 QStringList months;
103 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
43 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
104 QStringList weeks;
44 QStringList weeks;
105 weeks << "week 1" << "week 2" << "week 3" << "week 4";
45 weeks << "week 1" << "week 2" << "week 3" << "week 4";
106 QStringList plants;
46 QStringList plants;
107 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
47 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
108 //! [3]
48 //! [3]
109
49
110 //! [4]
50 //! [4]
111 // Create drilldown structure
51 // Create drilldown structure
112 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
52 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
113 seasonSeries->setName("Crop by month - Season");
53 seasonSeries->setName("Crop by month - Season");
114
54
115 // Each month in season series has drilldown series for weekly data
55 // Each month in season series has drilldown series for weekly data
116 foreach (QString month, months) {
56 foreach (QString month, months) {
117
57
118 // Create drilldown series for every week
58 // Create drilldown series for every week
119 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
59 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
120 seasonSeries->mapDrilldownSeries(month, weeklySeries);
60 seasonSeries->mapDrilldownSeries(month, weeklySeries);
121
61
122 // Drilling down from weekly data brings us back to season data.
62 // Drilling down from weekly data brings us back to season data.
123 foreach (QString week, weeks) {
63 foreach (QString week, weeks) {
124 weeklySeries->mapDrilldownSeries(week, seasonSeries);
64 weeklySeries->mapDrilldownSeries(week, seasonSeries);
125 weeklySeries->setName(QString("Crop by week - " + month));
65 weeklySeries->setName(QString("Crop by week - " + month));
126 }
66 }
127
67
128 // Use right click signal to implement drilldown
68 // Use right click signal to implement drilldown
129 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
69 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
130 }
70 }
131
71
132 // Enable drilldown from season series using right click.
72 // Enable drilldown from season series using right click.
133 QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
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 // Fill monthly and weekly series with data
77 // Fill monthly and weekly series with data
138 foreach (QString plant, plants) {
78 foreach (QString plant, plants) {
139 QBarSet* monthlyCrop = new QBarSet(plant);
79 QBarSet* monthlyCrop = new QBarSet(plant);
140 foreach (QString month, months) {
80 foreach (QString month, months) {
141 QBarSet* weeklyCrop = new QBarSet(plant);
81 QBarSet* weeklyCrop = new QBarSet(plant);
142 foreach (QString week, weeks )
82 foreach (QString week, weeks )
143 *weeklyCrop << (qrand() % 20);
83 *weeklyCrop << (qrand() % 20);
144 // Get the drilldown series from season series and add crop to it.
84 // Get the drilldown series from season series and add crop to it.
145 seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop);
85 seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop);
146 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
86 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
147 *monthlyCrop << weeklyCrop->total();
87 *monthlyCrop << weeklyCrop->total();
148 }
88 }
149 seasonSeries->appendBarSet(monthlyCrop);
89 seasonSeries->appendBarSet(monthlyCrop);
150 }
90 }
151 //! [5]
91 //! [5]
152
153 seasonSeries->setToolTipEnabled(true);
154
92
155 //! [6]
93 //! [6]
156 // Show season series in initial view
94 // Show season series in initial view
157 drilldownChart->changeSeries(seasonSeries);
95 drilldownChart->changeSeries(seasonSeries);
158 drilldownChart->setTitle(seasonSeries->name());
96 drilldownChart->setTitle(seasonSeries->name());
159 //! [6]
97 //! [6]
160
98
99 //! [7]
161 drilldownChart->axisX()->setGridLineVisible(false);
100 drilldownChart->axisX()->setGridLineVisible(false);
101 drilldownChart->axisY()->setNiceNumbers(true);
162 drilldownChart->legend()->setVisible(true);
102 drilldownChart->legend()->setVisible(true);
163 drilldownChart->legend()->setAlignmnent(QLegend::AlignmentBottom);
103 drilldownChart->legend()->setAlignmnent(QLegend::AlignmentBottom);
104 //! [7]
164
105
165 QChartView *chartView = new QChartView(drilldownChart);
106 QChartView *chartView = new QChartView(drilldownChart);
166 window.setCentralWidget(chartView);
107 window.setCentralWidget(chartView);
167 window.resize(400, 300);
108 window.resize(400, 300);
168 window.show();
109 window.show();
169
110
170 return a.exec();
111 return a.exec();
171 }
112 }
172
113
173 #include "main.moc"
@@ -1,7 +1,7
1 !include( ../examples.pri ) {
1 !include( ../examples.pri ) {
2 error( "Couldn't find the examples.pri file!" )
2 error( "Couldn't find the examples.pri file!" )
3 }
3 }
4 TARGET = stackedbarchartdrilldown
4 TARGET = stackedbarchartdrilldown
5 SOURCES += main.cpp
5 SOURCES += main.cpp drilldownseries.cpp drilldownchart.cpp
6 HEADERS +=
6 HEADERS += drilldownseries.h drilldownchart.h
7
7
General Comments 0
You need to be logged in to leave comments. Login now