##// END OF EJS Templates
Drilldown example for barcharts continuing. Still some bugs
sauimone -
r430:ec0910b997a4
parent child
Show More
@@ -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
11 int main(int argc, char *argv[])
13 //! [1]
14 class DrilldownBarSeries : public QStackedBarSeries
12 {
15 {
13 QApplication a(argc, argv);
16 Q_OBJECT
14 QMainWindow window;
17 public:
18 DrilldownBarSeries(QStringList categories, QObject* parent=0) : QStackedBarSeries(categories,parent) {}
15
19
16 // TODO:
17 /*
20 /*
18 //! [1]
21 public Q_SLOTS:
19 // Define categories
22 void handleRightClick(QBarSet *barset, QString category)
20 QStringList categories;
23 {
21 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
24 }
25 */
26 };
22 //! [1]
27 //! [1]
23
28
24 //! [2]
29 //! [2]
25 // Create some test sets for chat
30 class DrilldownBarSet : public QBarSet
26 QBarSet *set0 = new QBarSet("Bub");
31 {
27 QBarSet *set1 = new QBarSet("Bob");
32 Q_OBJECT
28 QBarSet *set2 = new QBarSet("Guybrush");
33 public:
29 QBarSet *set3 = new QBarSet("Larry");
34 DrilldownBarSet(QString name, DrilldownBarSeries *parent) : QBarSet(name, parent), mSeries(parent) {}
30 QBarSet *set4 = new QBarSet("Zak");
35
31
36 DrilldownBarSeries* drilldownSeries() { return mSeries; }
32 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
37
33 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
38 private:
34 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
39 DrilldownBarSeries* mSeries;
35 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
40 };
36 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
37 //! [2]
41 //! [2]
38
42
39 //! [3]
43 //! [3]
40 // Create series and add sets to it
44 class DrilldownChart : public QChartView
41 QStackedBarSeries* series = new QStackedBarSeries(categories);
45 {
42
46 Q_OBJECT
43 series->addBarSet(set0);
47 public:
44 series->addBarSet(set1);
48 explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {}
45 series->addBarSet(set2);
49
46 series->addBarSet(set3);
50 void changeSeries(QSeries* series)
47 series->addBarSet(set4);
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 };
48 //! [3]
71 //! [3]
49
72
50 //! [4]
73
51 // Enable tooltip
74 int main(int argc, char *argv[])
52 series->setToolTipEnabled();
75 {
53
76 QApplication a(argc, argv);
54 // Connect clicked signal of set to toggle floating values of set.
77 QMainWindow window;
55 // Note that we leave QBarset "Zak" unconnected here, so clicking on it doesn't toggle values.
78
56 QObject::connect(set0,SIGNAL(clicked(QBarSet*,QString)),set0,SIGNAL(toggleFloatingValues()));
79 DrilldownChart* drilldownChart = new DrilldownChart(&window);
57 QObject::connect(set1,SIGNAL(clicked(QBarSet*,QString)),set1,SIGNAL(toggleFloatingValues()));
80 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
58 QObject::connect(set2,SIGNAL(clicked(QBarSet*,QString)),set2,SIGNAL(toggleFloatingValues()));
81
59 QObject::connect(set3,SIGNAL(clicked(QBarSet*,QString)),set3,SIGNAL(toggleFloatingValues()));
82 //! [1]
60 //! [4]
83 // Define categories
61
84 QStringList months;
62 //! [5]
85 months << "Jun" << "Jul" << "Aug" << "Oct";
63 // Create view for chart and add series to it. Apply theme.
86 QStringList weeks;
64
87 weeks << "week 1" << "week 2" << "week 3" << "week 4";
65 QChartView* chartView = new QChartView(&window);
88 QStringList plants;
66 chartView->addSeries(series);
89 plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo";
67 chartView->setChartTitle("simple stacked barchart");
90 //! [1]
68 chartView->setChartTheme(QChart::ChartThemeIcy);
91
69 //! [5]
92 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
70
93 seasonSeries->setTitle("Crop by month - Season");
71 //! [6]
94
72 chartView->axisX()->setAxisVisible(false);
95 foreach (QString plant, plants) {
73 chartView->axisX()->setGridVisible(false);
96 DrilldownBarSet *seasonCrop = new DrilldownBarSet(plant, seasonSeries);
74 chartView->axisX()->setLabelsVisible(false);
97 foreach(QString month, months) {
75 //! [6]
98 DrilldownBarSeries* monthSeries = new DrilldownBarSeries(weeks, drilldownChart);
76
99 DrilldownBarSet *monthCrop = new DrilldownBarSet(plant, monthSeries);
77 window.setCentralWidget(chartView);
100 foreach(QString week, weeks) {
101 *monthCrop << (qrand() % 20);
102 }
103 monthSeries->addBarSet(monthCrop);
104 *seasonCrop << monthCrop->valueAt(plants.indexOf(plant));
105 }
106
107 // We want floating values!
108 QObject::connect(seasonCrop,SIGNAL(clicked(QString)),seasonCrop,SIGNAL(toggleFloatingValues()));
109 seasonSeries->addBarSet(seasonCrop);
110 }
111
112 QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString)));
113
114 seasonSeries->setToolTipEnabled(true);
115
116 drilldownChart->addSeries(seasonSeries);
117
118 drilldownChart->axisX()->setAxisVisible(false);
119 drilldownChart->axisX()->setGridVisible(false);
120 drilldownChart->axisX()->setLabelsVisible(false);
121
122 window.setCentralWidget(drilldownChart);
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