@@ -1,83 +1,129 | |||||
1 | #include <QApplication> |
|
1 | #include <QtGui/QApplication> | |
2 | #include <QMainWindow> |
|
2 | #include <QMainWindow> | |
|
3 | #include <qchartglobal.h> | |||
3 | #include <qchartview.h> |
|
4 | #include <qchartview.h> | |
4 | #include <qstackedbarseries.h> |
|
5 | #include <qstackedbarseries.h> | |
5 | #include <qbarset.h> |
|
6 | #include <qbarset.h> | |
6 | #include <qchartaxis.h> |
|
7 | #include <qchartaxis.h> | |
7 | #include <QStringList> |
|
8 | #include <QStringList> | |
|
9 | #include <QDebug> | |||
8 |
|
10 | |||
9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | QTCOMMERCIALCHART_USE_NAMESPACE | |
10 |
|
12 | |||
|
13 | //! [1] | |||
|
14 | class DrilldownBarSeries : public QStackedBarSeries | |||
|
15 | { | |||
|
16 | Q_OBJECT | |||
|
17 | public: | |||
|
18 | DrilldownBarSeries(QStringList categories, QObject* parent=0) : QStackedBarSeries(categories,parent) {} | |||
|
19 | ||||
|
20 | /* | |||
|
21 | public Q_SLOTS: | |||
|
22 | void handleRightClick(QBarSet *barset, QString category) | |||
|
23 | { | |||
|
24 | } | |||
|
25 | */ | |||
|
26 | }; | |||
|
27 | //! [1] | |||
|
28 | ||||
|
29 | //! [2] | |||
|
30 | class DrilldownBarSet : public QBarSet | |||
|
31 | { | |||
|
32 | Q_OBJECT | |||
|
33 | public: | |||
|
34 | DrilldownBarSet(QString name, DrilldownBarSeries *parent) : QBarSet(name, parent), mSeries(parent) {} | |||
|
35 | ||||
|
36 | DrilldownBarSeries* drilldownSeries() { return mSeries; } | |||
|
37 | ||||
|
38 | private: | |||
|
39 | DrilldownBarSeries* mSeries; | |||
|
40 | }; | |||
|
41 | //! [2] | |||
|
42 | ||||
|
43 | //! [3] | |||
|
44 | class DrilldownChart : public QChartView | |||
|
45 | { | |||
|
46 | Q_OBJECT | |||
|
47 | public: | |||
|
48 | explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {} | |||
|
49 | ||||
|
50 | void changeSeries(QSeries* series) | |||
|
51 | { | |||
|
52 | if (m_currentSeries) | |||
|
53 | removeSeries(m_currentSeries); | |||
|
54 | m_currentSeries = series; | |||
|
55 | addSeries(series); | |||
|
56 | setChartTitle(series->title()); | |||
|
57 | } | |||
|
58 | ||||
|
59 | public Q_SLOTS: | |||
|
60 | void handleRightClick(QBarSet* barset, QString category) | |||
|
61 | { | |||
|
62 | qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category; | |||
|
63 | // TODO: continue from here | |||
|
64 | // DrilldownBarSet* drilldownBarSet = static_cast<DrilldownBarSet*>(barset); | |||
|
65 | // changeSeries(drilldownBarSet->drilldownSeries()); | |||
|
66 | } | |||
|
67 | ||||
|
68 | private: | |||
|
69 | QSeries* m_currentSeries; | |||
|
70 | }; | |||
|
71 | //! [3] | |||
|
72 | ||||
|
73 | ||||
11 | int main(int argc, char *argv[]) |
|
74 | int main(int argc, char *argv[]) | |
12 | { |
|
75 | { | |
13 | QApplication a(argc, argv); |
|
76 | QApplication a(argc, argv); | |
14 | QMainWindow window; |
|
77 | QMainWindow window; | |
15 |
|
78 | |||
16 | // TODO: |
|
79 | DrilldownChart* drilldownChart = new DrilldownChart(&window); | |
17 | /* |
|
80 | drilldownChart->setChartTheme(QChart::ChartThemeIcy); | |
|
81 | ||||
18 | //! [1] |
|
82 | //! [1] | |
19 | // Define categories |
|
83 | // Define categories | |
20 |
QStringList |
|
84 | QStringList months; | |
21 | categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; |
|
85 | months << "Jun" << "Jul" << "Aug" << "Oct"; | |
|
86 | QStringList weeks; | |||
|
87 | weeks << "week 1" << "week 2" << "week 3" << "week 4"; | |||
|
88 | QStringList plants; | |||
|
89 | plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; | |||
22 | //! [1] |
|
90 | //! [1] | |
23 |
|
91 | |||
24 | //! [2] |
|
92 | DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); | |
25 | // Create some test sets for chat |
|
93 | seasonSeries->setTitle("Crop by month - Season"); | |
26 | QBarSet *set0 = new QBarSet("Bub"); |
|
94 | ||
27 | QBarSet *set1 = new QBarSet("Bob"); |
|
95 | foreach (QString plant, plants) { | |
28 | QBarSet *set2 = new QBarSet("Guybrush"); |
|
96 | DrilldownBarSet *seasonCrop = new DrilldownBarSet(plant, seasonSeries); | |
29 | QBarSet *set3 = new QBarSet("Larry"); |
|
97 | foreach(QString month, months) { | |
30 | QBarSet *set4 = new QBarSet("Zak"); |
|
98 | DrilldownBarSeries* monthSeries = new DrilldownBarSeries(weeks, drilldownChart); | |
31 |
|
99 | DrilldownBarSet *monthCrop = new DrilldownBarSet(plant, monthSeries); | ||
32 | *set0 << 1 << 2 << 3 << 4 << 5 << 6; |
|
100 | foreach(QString week, weeks) { | |
33 | *set1 << 5 << 0 << 0 << 4 << 0 << 7; |
|
101 | *monthCrop << (qrand() % 20); | |
34 | *set2 << 3 << 5 << 8 << 13 << 8 << 5; |
|
102 | } | |
35 | *set3 << 5 << 6 << 7 << 3 << 4 << 5; |
|
103 | monthSeries->addBarSet(monthCrop); | |
36 | *set4 << 9 << 7 << 5 << 3 << 1 << 2; |
|
104 | *seasonCrop << monthCrop->valueAt(plants.indexOf(plant)); | |
37 | //! [2] |
|
105 | } | |
38 |
|
106 | |||
39 | //! [3] |
|
107 | // We want floating values! | |
40 | // Create series and add sets to it |
|
108 | QObject::connect(seasonCrop,SIGNAL(clicked(QString)),seasonCrop,SIGNAL(toggleFloatingValues())); | |
41 | QStackedBarSeries* series = new QStackedBarSeries(categories); |
|
109 | seasonSeries->addBarSet(seasonCrop); | |
42 |
|
110 | } | ||
43 | series->addBarSet(set0); |
|
111 | ||
44 | series->addBarSet(set1); |
|
112 | QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); | |
45 | series->addBarSet(set2); |
|
113 | ||
46 | series->addBarSet(set3); |
|
114 | seasonSeries->setToolTipEnabled(true); | |
47 | series->addBarSet(set4); |
|
115 | ||
48 | //! [3] |
|
116 | drilldownChart->addSeries(seasonSeries); | |
49 |
|
117 | |||
50 | //! [4] |
|
118 | drilldownChart->axisX()->setAxisVisible(false); | |
51 | // Enable tooltip |
|
119 | drilldownChart->axisX()->setGridVisible(false); | |
52 | series->setToolTipEnabled(); |
|
120 | drilldownChart->axisX()->setLabelsVisible(false); | |
53 |
|
121 | |||
54 | // Connect clicked signal of set to toggle floating values of set. |
|
122 | window.setCentralWidget(drilldownChart); | |
55 | // Note that we leave QBarset "Zak" unconnected here, so clicking on it doesn't toggle values. |
|
|||
56 | QObject::connect(set0,SIGNAL(clicked(QBarSet*,QString)),set0,SIGNAL(toggleFloatingValues())); |
|
|||
57 | QObject::connect(set1,SIGNAL(clicked(QBarSet*,QString)),set1,SIGNAL(toggleFloatingValues())); |
|
|||
58 | QObject::connect(set2,SIGNAL(clicked(QBarSet*,QString)),set2,SIGNAL(toggleFloatingValues())); |
|
|||
59 | QObject::connect(set3,SIGNAL(clicked(QBarSet*,QString)),set3,SIGNAL(toggleFloatingValues())); |
|
|||
60 | //! [4] |
|
|||
61 |
|
||||
62 | //! [5] |
|
|||
63 | // Create view for chart and add series to it. Apply theme. |
|
|||
64 |
|
||||
65 | QChartView* chartView = new QChartView(&window); |
|
|||
66 | chartView->addSeries(series); |
|
|||
67 | chartView->setChartTitle("simple stacked barchart"); |
|
|||
68 | chartView->setChartTheme(QChart::ChartThemeIcy); |
|
|||
69 | //! [5] |
|
|||
70 |
|
||||
71 | //! [6] |
|
|||
72 | chartView->axisX()->setAxisVisible(false); |
|
|||
73 | chartView->axisX()->setGridVisible(false); |
|
|||
74 | chartView->axisX()->setLabelsVisible(false); |
|
|||
75 | //! [6] |
|
|||
76 |
|
||||
77 | window.setCentralWidget(chartView); |
|
|||
78 | window.resize(400, 300); |
|
123 | window.resize(400, 300); | |
79 | window.show(); |
|
124 | window.show(); | |
80 | */ |
|
125 | ||
81 | return a.exec(); |
|
126 | return a.exec(); | |
82 | } |
|
127 | } | |
83 |
|
128 | |||
|
129 | #include "main.moc" |
@@ -141,20 +141,6 QBrush QBarSet::brush() | |||||
141 | return mBrush; |
|
141 | return mBrush; | |
142 | } |
|
142 | } | |
143 |
|
143 | |||
144 | /* |
|
|||
145 | void QBarSet::barClickedEvent(QString category) |
|
|||
146 | { |
|
|||
147 | // Some bar of this set has been clicked |
|
|||
148 | // TODO: What happens then? |
|
|||
149 | emit clicked(category); // Notify that set has been clicked |
|
|||
150 | } |
|
|||
151 |
|
||||
152 | void QBarSet::barRightClickedEvent(QString category) |
|
|||
153 | { |
|
|||
154 | emit rightClicked(category); |
|
|||
155 | } |
|
|||
156 | */ |
|
|||
157 |
|
||||
158 | /*! |
|
144 | /*! | |
159 | \internal \a pos |
|
145 | \internal \a pos | |
160 | */ |
|
146 | */ |
@@ -17,10 +17,15 public: | |||||
17 | QString name(); |
|
17 | QString name(); | |
18 | QBarSet& operator << (const qreal &value); // appends new value to set |
|
18 | QBarSet& operator << (const qreal &value); // appends new value to set | |
19 |
|
19 | |||
|
20 | // TODO: remove indices eventually. Use as internal? | |||
20 | int count(); // count of values in set |
|
21 | int count(); // count of values in set | |
21 | qreal valueAt(int index); // for modifying individual values |
|
22 | qreal valueAt(int index); // for modifying individual values | |
22 | void setValue(int index, qreal value); // setter for individual value |
|
23 | void setValue(int index, qreal value); // setter for individual value | |
23 |
|
24 | |||
|
25 | // TODO: | |||
|
26 | //qreal value(QString category); | |||
|
27 | //void setValue(QString category, qreal value); | |||
|
28 | ||||
24 | void setPen(QPen pen); |
|
29 | void setPen(QPen pen); | |
25 | QPen pen(); |
|
30 | QPen pen(); | |
26 |
|
31 | |||
@@ -42,8 +47,6 Q_SIGNALS: | |||||
42 | public Q_SLOTS: |
|
47 | public Q_SLOTS: | |
43 | // These are for internal communication |
|
48 | // These are for internal communication | |
44 | // TODO: TO PIMPL ---> |
|
49 | // TODO: TO PIMPL ---> | |
45 | // void barClickedEvent(QString category); |
|
|||
46 | // void barRightClickedEvent(QString category); |
|
|||
47 | void barHoverEnterEvent(QPoint pos); |
|
50 | void barHoverEnterEvent(QPoint pos); | |
48 | void barHoverLeaveEvent(); |
|
51 | void barHoverLeaveEvent(); | |
49 | // <--- TO PIMPL |
|
52 | // <--- TO PIMPL | |
@@ -51,7 +54,8 public Q_SLOTS: | |||||
51 | private: |
|
54 | private: | |
52 |
|
55 | |||
53 | QString mName; |
|
56 | QString mName; | |
54 | QList<qreal> mValues; |
|
57 | QList<qreal> mValues; // TODO: replace with map (category, value) | |
|
58 | QMap<QString,qreal> mMappedValues; | |||
55 | QPen mPen; |
|
59 | QPen mPen; | |
56 | QBrush mBrush; |
|
60 | QBrush mBrush; | |
57 |
|
61 |
General Comments 0
You need to be logged in to leave comments.
Login now