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