##// END OF EJS Templates
Make stackedbarchartdrilldown compile
Jani Honkonen -
r829:f9360d7ac284
parent child
Show More
@@ -1,29 +1,29
1 TEMPLATE = subdirs
1 TEMPLATE = subdirs
2 SUBDIRS += \
2 SUBDIRS += \
3 areachart \
3 areachart \
4 barchart \
4 barchart \
5 #chartview \
5 #chartview \
6 customchart \
6 customchart \
7 #dynamiclinechart \
7 #dynamiclinechart \
8 #ekgchart \
8 #ekgchart \
9 linechart \
9 linechart \
10 #multichart \
10 #multichart \
11 percentbarchart \
11 percentbarchart \
12 piechart \
12 piechart \
13 piechartdrilldown \
13 piechartdrilldown \
14 #presenterchart \
14 #presenterchart \
15 scatterchart \
15 scatterchart \
16 #scatterinteractions \
16 #scatterinteractions \
17 #splinechart \
17 #splinechart \
18 stackedbarchart \
18 stackedbarchart \
19 #stackedbarchartdrilldown \
19 stackedbarchartdrilldown \
20 #tablemodelchart \
20 #tablemodelchart \
21 zoomlinechart
21 zoomlinechart
22
22
23
23
24
24
25
25
26
26
27
27
28
28
29
29
@@ -1,149 +1,147
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 mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries)
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 return mDrilldownSeries[category];
27 return mDrilldownSeries[category];
28 }
28 }
29
29
30 public Q_SLOTS:
30 public Q_SLOTS:
31
31
32 private:
32 private:
33
33
34 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
34 QMap<QString, DrilldownBarSeries*> mDrilldownSeries;
35 };
35 };
36 //! [1]
36 //! [1]
37
37
38 //! [2]
38 //! [2]
39 class DrilldownChart : public QChartView
39 class DrilldownChart : public QChart
40 {
40 {
41 Q_OBJECT
41 Q_OBJECT
42 public:
42 public:
43 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
43 explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0) : QChart(parent, wFlags), m_currentSeries(0) {}
44
44
45 void changeSeries(QSeries* series)
45 void changeSeries(QSeries* series)
46 {
46 {
47 if (m_currentSeries)
47 if (m_currentSeries)
48 removeSeries(m_currentSeries);
48 removeSeries(m_currentSeries);
49 m_currentSeries = series;
49 m_currentSeries = series;
50 addSeries(series);
50 addSeries(series);
51 setChartTitle(series->name());
51 setTitle(series->name());
52 }
52 }
53
53
54 public Q_SLOTS:
54 public Q_SLOTS:
55 void handleRightClick(QBarSet *barset, QString category)
55 void handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button)
56 {
56 {
57 Q_UNUSED(barset)
57 Q_UNUSED(barset)
58 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
58 DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender());
59 changeSeries(series->drilldownSeries(category));
59 changeSeries(series->drilldownSeries(category));
60 }
60 }
61
61
62 private:
62 private:
63 QSeries* m_currentSeries;
63 QSeries* m_currentSeries;
64 };
64 };
65 //! [2]
65 //! [2]
66
66
67 int main(int argc, char *argv[])
67 int main(int argc, char *argv[])
68 {
68 {
69 QApplication a(argc, argv);
69 QApplication a(argc, argv);
70 QMainWindow window;
70 QMainWindow window;
71
71
72 DrilldownChart* drilldownChart = new DrilldownChart(&window);
72 DrilldownChart* drilldownChart = new DrilldownChart();
73 drilldownChart->setChartTheme(QChart::ChartThemeBlueIcy);
73 drilldownChart->setTheme(QChart::ChartThemeBlueIcy);
74 drilldownChart->setAnimationOptions(QChart::SeriesAnimations);
74
75
75 //! [3]
76 //! [3]
76 // Define categories
77 // Define categories
77 QStringList months;
78 QStringList months;
78 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
79 months << "May" << "Jun" << "Jul" << "Aug" << "Sep";
79 QStringList weeks;
80 QStringList weeks;
80 weeks << "week 1" << "week 2" << "week 3" << "week 4";
81 weeks << "week 1" << "week 2" << "week 3" << "week 4";
81 QStringList plants;
82 QStringList plants;
82 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
83 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
83 //! [3]
84 //! [3]
84
85
85 //! [4]
86 //! [4]
86 // Create drilldown structure
87 // Create drilldown structure
87 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
88 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
88 seasonSeries->setName("Crop by month - Season");
89 seasonSeries->setName("Crop by month - Season");
89
90
90 // Each month in season series has drilldown series for weekly data
91 // Each month in season series has drilldown series for weekly data
91 foreach (QString month, months) {
92 foreach (QString month, months) {
92
93
93 // Create drilldown series for every week
94 // Create drilldown series for every week
94 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
95 DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart);
95 seasonSeries->mapDrilldownSeries(month, weeklySeries);
96 seasonSeries->mapDrilldownSeries(month, weeklySeries);
96
97
97 // Drilling down from weekly data brings us back to season data.
98 // Drilling down from weekly data brings us back to season data.
98 foreach (QString week, weeks) {
99 foreach (QString week, weeks) {
99 weeklySeries->mapDrilldownSeries(week, seasonSeries);
100 weeklySeries->mapDrilldownSeries(week, seasonSeries);
100 weeklySeries->setName(QString("Crop by week - " + month));
101 weeklySeries->setName(QString("Crop by week - " + month));
101 }
102 }
102
103
103 // Use right click signal to implement drilldown
104 // Use right click signal to implement drilldown
104 QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
105 QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
105 }
106 }
106
107
107 // Enable drilldown from season series using right click.
108 // Enable drilldown from season series using right click.
108 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
109 QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons)));
109 //! [4]
110 //! [4]
110
111
111 //! [5]
112 //! [5]
112 // Fill monthly and weekly series with data
113 // Fill monthly and weekly series with data
113 foreach (QString plant, plants) {
114 foreach (QString plant, plants) {
114 QBarSet* monthlyCrop = new QBarSet(plant);
115 QBarSet* monthlyCrop = new QBarSet(plant);
115 foreach (QString month, months) {
116 foreach (QString month, months) {
116 QBarSet* weeklyCrop = new QBarSet(plant);
117 QBarSet* weeklyCrop = new QBarSet(plant);
117 foreach (QString week, weeks ) {
118 foreach (QString week, weeks )
118 *weeklyCrop << (qrand() % 20);
119 *weeklyCrop << (qrand() % 20);
119 }
120 // Get the drilldown series from season series and add crop to it.
120 // Get the drilldown series from season series and add crop to it.
121 seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop);
121 seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop);
122 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
122 seasonSeries->drilldownSeries(month)->setToolTipEnabled(true);
123 *monthlyCrop << weeklyCrop->total();
123 *monthlyCrop << weeklyCrop->total();
124
125 QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues()));
126 }
124 }
127 seasonSeries->addBarSet(monthlyCrop);
125 seasonSeries->appendBarSet(monthlyCrop);
128 QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues()));
129 }
126 }
130 //! [5]
127 //! [5]
131
128
132 seasonSeries->setToolTipEnabled(true);
129 seasonSeries->setToolTipEnabled(true);
133
130
134 //! [6]
131 //! [6]
135 // Show season series in initial view
132 // Show season series in initial view
136 drilldownChart->changeSeries(seasonSeries);
133 drilldownChart->changeSeries(seasonSeries);
137 drilldownChart->setChartTitle(seasonSeries->name());
134 drilldownChart->setTitle(seasonSeries->name());
138 //! [6]
135 //! [6]
139
136
140 drilldownChart->axisX()->setGridLineVisible(false);
137 drilldownChart->axisX()->setGridLineVisible(false);
141
138
142 window.setCentralWidget(drilldownChart);
139 QChartView *chartView = new QChartView(drilldownChart);
140 window.setCentralWidget(chartView);
143 window.resize(400, 300);
141 window.resize(400, 300);
144 window.show();
142 window.show();
145
143
146 return a.exec();
144 return a.exec();
147 }
145 }
148
146
149 #include "main.moc"
147 #include "main.moc"
@@ -1,9 +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
6 chartwidget.cpp
6 HEADERS +=
7 HEADERS += \
8 chartwidget.h
9
7
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now