@@ -1,114 +1,114 | |||||
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 <qpieseries.h> |
|
5 | #include <qpieseries.h> | |
6 | #include <qpieslice.h> |
|
6 | #include <qpieslice.h> | |
7 | #include <QTime> |
|
7 | #include <QTime> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
9 | QTCOMMERCIALCHART_USE_NAMESPACE | |
10 |
|
10 | |||
11 | class DrilldownSlice : public QPieSlice |
|
11 | class DrilldownSlice : public QPieSlice | |
12 | { |
|
12 | { | |
13 | Q_OBJECT |
|
13 | Q_OBJECT | |
14 |
|
14 | |||
15 | public: |
|
15 | public: | |
16 | DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) |
|
16 | DrilldownSlice(qreal value, QString prefix, QSeries* drilldownSeries) | |
17 | :m_drilldownSeries(drilldownSeries), |
|
17 | :m_drilldownSeries(drilldownSeries), | |
18 | m_prefix(prefix) |
|
18 | m_prefix(prefix) | |
19 | { |
|
19 | { | |
20 | setValue(value); |
|
20 | setValue(value); | |
21 | setLabelVisible(true); |
|
21 | setLabelVisible(true); | |
22 | updateLabel(); |
|
22 | updateLabel(); | |
23 | connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); |
|
23 | connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | QSeries* drilldownSeries() const { return m_drilldownSeries; } |
|
26 | QSeries* drilldownSeries() const { return m_drilldownSeries; } | |
27 |
|
27 | |||
28 | public Q_SLOTS: |
|
28 | public Q_SLOTS: | |
29 | void updateLabel() |
|
29 | void updateLabel() | |
30 | { |
|
30 | { | |
31 | QString label = m_prefix; |
|
31 | QString label = m_prefix; | |
32 | label += " " + QString::number(this->value())+ "e ("; |
|
32 | label += " " + QString::number(this->value())+ "e ("; | |
33 | label += QString::number(this->percentage()*100, 'f', 1) + "%)"; |
|
33 | label += QString::number(this->percentage()*100, 'f', 1) + "%)"; | |
34 | setLabel(label); |
|
34 | setLabel(label); | |
35 | } |
|
35 | } | |
36 |
|
36 | |||
37 | private: |
|
37 | private: | |
38 | QSeries* m_drilldownSeries; |
|
38 | QSeries* m_drilldownSeries; | |
39 | QString m_prefix; |
|
39 | QString m_prefix; | |
40 | }; |
|
40 | }; | |
41 |
|
41 | |||
42 | class DrilldownChart : public QChartView |
|
42 | class DrilldownChart : public QChartView | |
43 | { |
|
43 | { | |
44 | Q_OBJECT |
|
44 | Q_OBJECT | |
45 | public: |
|
45 | public: | |
46 | explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {} |
|
46 | explicit DrilldownChart(QWidget *parent = 0):QChartView(parent), m_currentSeries(0) {} | |
47 |
|
47 | |||
48 | void changeSeries(QSeries* series) |
|
48 | void changeSeries(QSeries* series) | |
49 | { |
|
49 | { | |
50 | // NOTE: if the series is owned by the chart it will be deleted |
|
50 | // NOTE: if the series is owned by the chart it will be deleted | |
51 | // here the "window" owns the series... |
|
51 | // here the "window" owns the series... | |
52 | if (m_currentSeries) |
|
52 | if (m_currentSeries) | |
53 | removeSeries(m_currentSeries); |
|
53 | removeSeries(m_currentSeries); | |
54 | m_currentSeries = series; |
|
54 | m_currentSeries = series; | |
55 | addSeries(series); |
|
55 | addSeries(series); | |
56 |
setChartTitle(series-> |
|
56 | setChartTitle(series->name()); | |
57 | } |
|
57 | } | |
58 |
|
58 | |||
59 | public Q_SLOTS: |
|
59 | public Q_SLOTS: | |
60 | void handleSliceClicked(QPieSlice* slice) |
|
60 | void handleSliceClicked(QPieSlice* slice) | |
61 | { |
|
61 | { | |
62 | DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice); |
|
62 | DrilldownSlice* drilldownSlice = static_cast<DrilldownSlice*>(slice); | |
63 | changeSeries(drilldownSlice->drilldownSeries()); |
|
63 | changeSeries(drilldownSlice->drilldownSeries()); | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | private: |
|
66 | private: | |
67 | QSeries* m_currentSeries; |
|
67 | QSeries* m_currentSeries; | |
68 | }; |
|
68 | }; | |
69 |
|
69 | |||
70 | int main(int argc, char *argv[]) |
|
70 | int main(int argc, char *argv[]) | |
71 | { |
|
71 | { | |
72 | QApplication a(argc, argv); |
|
72 | QApplication a(argc, argv); | |
73 |
|
73 | |||
74 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); |
|
74 | qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); | |
75 |
|
75 | |||
76 | QMainWindow window; |
|
76 | QMainWindow window; | |
77 |
|
77 | |||
78 | DrilldownChart* drilldownChart = new DrilldownChart(&window); |
|
78 | DrilldownChart* drilldownChart = new DrilldownChart(&window); | |
79 | drilldownChart->setRenderHint(QPainter::Antialiasing); |
|
79 | drilldownChart->setRenderHint(QPainter::Antialiasing); | |
80 | drilldownChart->setChartTheme(QChart::ChartThemeLight); |
|
80 | drilldownChart->setChartTheme(QChart::ChartThemeLight); | |
81 | drilldownChart->setAnimationOptions(QChart::AllAnimations); |
|
81 | drilldownChart->setAnimationOptions(QChart::AllAnimations); | |
82 |
|
82 | |||
83 | QPieSeries* yearSeries = new QPieSeries(&window); |
|
83 | QPieSeries* yearSeries = new QPieSeries(&window); | |
84 |
yearSeries->set |
|
84 | yearSeries->setName("Sales by year - All"); | |
85 |
|
85 | |||
86 | QList<QString> months; |
|
86 | QList<QString> months; | |
87 | months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; |
|
87 | months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec"; | |
88 | QList<QString> names; |
|
88 | QList<QString> names; | |
89 | names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob"; |
|
89 | names << "Jane" << "John" << "Axel" << "Mary" << "Samantha" << "Bob"; | |
90 |
|
90 | |||
91 | foreach (QString name, names) { |
|
91 | foreach (QString name, names) { | |
92 | QPieSeries* series = new QPieSeries(&window); |
|
92 | QPieSeries* series = new QPieSeries(&window); | |
93 |
series->set |
|
93 | series->setName("Sales by month - " + name); | |
94 |
|
94 | |||
95 | foreach (QString month, months) |
|
95 | foreach (QString month, months) | |
96 | *series << new DrilldownSlice(qrand() % 1000, month, yearSeries); |
|
96 | *series << new DrilldownSlice(qrand() % 1000, month, yearSeries); | |
97 |
|
97 | |||
98 | QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); |
|
98 | QObject::connect(series, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | |
99 |
|
99 | |||
100 | *yearSeries << new DrilldownSlice(series->total(), name, series); |
|
100 | *yearSeries << new DrilldownSlice(series->total(), name, series); | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); |
|
103 | QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*, Qt::MouseButtons)), drilldownChart, SLOT(handleSliceClicked(QPieSlice*))); | |
104 |
|
104 | |||
105 | drilldownChart->changeSeries(yearSeries); |
|
105 | drilldownChart->changeSeries(yearSeries); | |
106 |
|
106 | |||
107 | window.setCentralWidget(drilldownChart); |
|
107 | window.setCentralWidget(drilldownChart); | |
108 | window.resize(800, 600); |
|
108 | window.resize(800, 600); | |
109 | window.show(); |
|
109 | window.show(); | |
110 |
|
110 | |||
111 | return a.exec(); |
|
111 | return a.exec(); | |
112 | } |
|
112 | } | |
113 |
|
113 | |||
114 | #include "main.moc" |
|
114 | #include "main.moc" |
@@ -1,149 +1,149 | |||||
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 QChartView | |
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(QWidget *parent = 0) : QChartView(parent), 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-> |
|
51 | setChartTitle(series->name()); | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | public Q_SLOTS: |
|
54 | public Q_SLOTS: | |
55 | void handleRightClick(QBarSet *barset, QString category) |
|
55 | void handleRightClick(QBarSet *barset, QString category) | |
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(&window); | |
73 | drilldownChart->setChartTheme(QChart::ChartThemeIcy); |
|
73 | drilldownChart->setChartTheme(QChart::ChartThemeIcy); | |
74 |
|
74 | |||
75 | //! [3] |
|
75 | //! [3] | |
76 | // Define categories |
|
76 | // Define categories | |
77 | QStringList months; |
|
77 | QStringList months; | |
78 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; |
|
78 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; | |
79 | QStringList weeks; |
|
79 | QStringList weeks; | |
80 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; |
|
80 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; | |
81 | QStringList plants; |
|
81 | QStringList plants; | |
82 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; |
|
82 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; | |
83 | //! [3] |
|
83 | //! [3] | |
84 |
|
84 | |||
85 | //! [4] |
|
85 | //! [4] | |
86 | // Create drilldown structure |
|
86 | // Create drilldown structure | |
87 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); |
|
87 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); | |
88 |
seasonSeries->set |
|
88 | seasonSeries->setName("Crop by month - Season"); | |
89 |
|
89 | |||
90 | // Each month in season series has drilldown series for weekly data |
|
90 | // Each month in season series has drilldown series for weekly data | |
91 | foreach (QString month, months) { |
|
91 | foreach (QString month, months) { | |
92 |
|
92 | |||
93 | // Create drilldown series for every week |
|
93 | // Create drilldown series for every week | |
94 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); |
|
94 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); | |
95 | seasonSeries->mapDrilldownSeries(month, weeklySeries); |
|
95 | seasonSeries->mapDrilldownSeries(month, weeklySeries); | |
96 |
|
96 | |||
97 | // Drilling down from weekly data brings us back to season data. |
|
97 | // Drilling down from weekly data brings us back to season data. | |
98 | foreach (QString week, weeks) { |
|
98 | foreach (QString week, weeks) { | |
99 | weeklySeries->mapDrilldownSeries(week, seasonSeries); |
|
99 | weeklySeries->mapDrilldownSeries(week, seasonSeries); | |
100 |
weeklySeries->set |
|
100 | weeklySeries->setName(QString("Crop by week - " + month)); | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | // Use right click signal to implement drilldown |
|
103 | // Use right click signal to implement drilldown | |
104 | 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))); | |
105 | } |
|
105 | } | |
106 |
|
106 | |||
107 | // Enable drilldown from season series using right click. |
|
107 | // Enable drilldown from season series using right click. | |
108 | QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); |
|
108 | QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); | |
109 | //! [4] |
|
109 | //! [4] | |
110 |
|
110 | |||
111 | //! [5] |
|
111 | //! [5] | |
112 | // Fill monthly and weekly series with data |
|
112 | // Fill monthly and weekly series with data | |
113 | foreach (QString plant, plants) { |
|
113 | foreach (QString plant, plants) { | |
114 | QBarSet* monthlyCrop = new QBarSet(plant); |
|
114 | QBarSet* monthlyCrop = new QBarSet(plant); | |
115 | foreach (QString month, months) { |
|
115 | foreach (QString month, months) { | |
116 | QBarSet* weeklyCrop = new QBarSet(plant); |
|
116 | QBarSet* weeklyCrop = new QBarSet(plant); | |
117 | foreach (QString week, weeks ) { |
|
117 | foreach (QString week, weeks ) { | |
118 | *weeklyCrop << (qrand() % 20); |
|
118 | *weeklyCrop << (qrand() % 20); | |
119 | } |
|
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)->addBarSet(weeklyCrop); | |
122 | seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); |
|
122 | seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); | |
123 | *monthlyCrop << weeklyCrop->total(); |
|
123 | *monthlyCrop << weeklyCrop->total(); | |
124 |
|
124 | |||
125 | QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); |
|
125 | QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); | |
126 | } |
|
126 | } | |
127 | seasonSeries->addBarSet(monthlyCrop); |
|
127 | seasonSeries->addBarSet(monthlyCrop); | |
128 | QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); |
|
128 | QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); | |
129 | } |
|
129 | } | |
130 | //! [5] |
|
130 | //! [5] | |
131 |
|
131 | |||
132 | seasonSeries->setToolTipEnabled(true); |
|
132 | seasonSeries->setToolTipEnabled(true); | |
133 |
|
133 | |||
134 | //! [6] |
|
134 | //! [6] | |
135 | // Show season series in initial view |
|
135 | // Show season series in initial view | |
136 | drilldownChart->changeSeries(seasonSeries); |
|
136 | drilldownChart->changeSeries(seasonSeries); | |
137 |
drilldownChart->setChartTitle(seasonSeries-> |
|
137 | drilldownChart->setChartTitle(seasonSeries->name()); | |
138 | //! [6] |
|
138 | //! [6] | |
139 |
|
139 | |||
140 | drilldownChart->axisX()->setGridLineVisible(false); |
|
140 | drilldownChart->axisX()->setGridLineVisible(false); | |
141 |
|
141 | |||
142 | window.setCentralWidget(drilldownChart); |
|
142 | window.setCentralWidget(drilldownChart); | |
143 | window.resize(400, 300); |
|
143 | window.resize(400, 300); | |
144 | window.show(); |
|
144 | window.show(); | |
145 |
|
145 | |||
146 | return a.exec(); |
|
146 | return a.exec(); | |
147 | } |
|
147 | } | |
148 |
|
148 | |||
149 | #include "main.moc" |
|
149 | #include "main.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now