##// 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 2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 4 #include <qchartview.h>
4 5 #include <qstackedbarseries.h>
5 6 #include <qbarset.h>
6 7 #include <qchartaxis.h>
7 8 #include <QStringList>
9 #include <QDebug>
8 10
9 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 74 int main(int argc, char *argv[])
12 75 {
13 76 QApplication a(argc, argv);
14 77 QMainWindow window;
15 78
16 // TODO:
17 /*
79 DrilldownChart* drilldownChart = new DrilldownChart(&window);
80 drilldownChart->setChartTheme(QChart::ChartThemeIcy);
81
18 82 //! [1]
19 83 // Define categories
20 QStringList categories;
21 categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
84 QStringList months;
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 90 //! [1]
23 91
24 //! [2]
25 // Create some test sets for chat
26 QBarSet *set0 = new QBarSet("Bub");
27 QBarSet *set1 = new QBarSet("Bob");
28 QBarSet *set2 = new QBarSet("Guybrush");
29 QBarSet *set3 = new QBarSet("Larry");
30 QBarSet *set4 = new QBarSet("Zak");
31
32 *set0 << 1 << 2 << 3 << 4 << 5 << 6;
33 *set1 << 5 << 0 << 0 << 4 << 0 << 7;
34 *set2 << 3 << 5 << 8 << 13 << 8 << 5;
35 *set3 << 5 << 6 << 7 << 3 << 4 << 5;
36 *set4 << 9 << 7 << 5 << 3 << 1 << 2;
37 //! [2]
38
39 //! [3]
40 // Create series and add sets to it
41 QStackedBarSeries* series = new QStackedBarSeries(categories);
42
43 series->addBarSet(set0);
44 series->addBarSet(set1);
45 series->addBarSet(set2);
46 series->addBarSet(set3);
47 series->addBarSet(set4);
48 //! [3]
49
50 //! [4]
51 // Enable tooltip
52 series->setToolTipEnabled();
53
54 // Connect clicked signal of set to toggle floating values of set.
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);
92 DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart);
93 seasonSeries->setTitle("Crop by month - Season");
94
95 foreach (QString plant, plants) {
96 DrilldownBarSet *seasonCrop = new DrilldownBarSet(plant, seasonSeries);
97 foreach(QString month, months) {
98 DrilldownBarSeries* monthSeries = new DrilldownBarSeries(weeks, drilldownChart);
99 DrilldownBarSet *monthCrop = new DrilldownBarSet(plant, monthSeries);
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 123 window.resize(400, 300);
79 124 window.show();
80 */
125
81 126 return a.exec();
82 127 }
83 128
129 #include "main.moc"
@@ -1,177 +1,163
1 1 #include "qbarset.h"
2 2 #include <QDebug>
3 3 #include <QToolTip>
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 /*!
8 8 \class QBarSet
9 9 \brief part of QtCommercial chart API.
10 10
11 11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
12 12 First value of set is assumed to belong to first category, second to second category and so on.
13 13 If set has fewer values than there are categories, then the missing values are assumed to be
14 14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
15 15
16 16 \mainclass
17 17
18 18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
19 19 */
20 20
21 21 /*!
22 22 \fn void QBarSet::clicked(QString category)
23 23 \brief signals that set has been clicked
24 24 Parameter \a category describes on which category was clicked
25 25 */
26 26
27 27 /*!
28 28 \fn void QBarSet::rightClicked(QString category)
29 29 \brief signals that set has been clicked with right mouse button
30 30 Parameter \a category describes on which category was clicked
31 31 */
32 32
33 33 /*!
34 34 \fn void QBarSet::hoverEnter(QPoint pos)
35 35 \brief signals that mouse has entered over the set at position \a pos.
36 36 */
37 37
38 38 /*!
39 39 \fn void QBarSet::hoverLeave()
40 40 \brief signals that mouse has left from the set.
41 41 */
42 42
43 43 /*!
44 44 \fn void QBarSet::toggleFloatingValues()
45 45 \brief \internal
46 46 */
47 47
48 48 /*!
49 49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
50 50 \brief \internal \a pos \a tip
51 51 */
52 52
53 53
54 54 /*!
55 55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 56 */
57 57 QBarSet::QBarSet(QString name, QObject *parent)
58 58 : QObject(parent)
59 59 ,mName(name)
60 60 {
61 61 }
62 62
63 63 /*!
64 64 Sets new \a name for set.
65 65 */
66 66 void QBarSet::setName(QString name)
67 67 {
68 68 mName = name;
69 69 }
70 70
71 71 /*!
72 72 Returns name of the set.
73 73 */
74 74 QString QBarSet::name()
75 75 {
76 76 return mName;
77 77 }
78 78
79 79 /*!
80 80 Appends new value \a value to the end of set.
81 81 */
82 82 QBarSet& QBarSet::operator << (const qreal &value)
83 83 {
84 84 mValues.append(value);
85 85 return *this;
86 86 }
87 87
88 88 /*!
89 89 Returns count of values in set.
90 90 */
91 91 int QBarSet::count()
92 92 {
93 93 return mValues.count();
94 94 }
95 95
96 96 /*!
97 97 Returns value of set indexed by \a index
98 98 */
99 99 qreal QBarSet::valueAt(int index)
100 100 {
101 101 return mValues.at(index);
102 102 }
103 103
104 104 /*!
105 105 Sets a new value \a value to set, indexed by \a index
106 106 */
107 107 void QBarSet::setValue(int index, qreal value)
108 108 {
109 109 mValues.replace(index,value);
110 110 }
111 111
112 112 /*!
113 113 Sets pen for set. Bars of this set are drawn using \a pen
114 114 */
115 115 void QBarSet::setPen(QPen pen)
116 116 {
117 117 mPen = pen;
118 118 }
119 119
120 120 /*!
121 121 Returns pen of the set.
122 122 */
123 123 QPen QBarSet::pen()
124 124 {
125 125 return mPen;
126 126 }
127 127
128 128 /*!
129 129 Sets brush for the set. Bars of this set are drawn using \a brush
130 130 */
131 131 void QBarSet::setBrush(QBrush brush)
132 132 {
133 133 mBrush = brush;
134 134 }
135 135
136 136 /*!
137 137 Returns brush of the set.
138 138 */
139 139 QBrush QBarSet::brush()
140 140 {
141 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 145 \internal \a pos
160 146 */
161 147 void QBarSet::barHoverEnterEvent(QPoint pos)
162 148 {
163 149 emit showToolTip(pos, mName);
164 150 emit hoverEnter(pos);
165 151 }
166 152
167 153 /*!
168 154 \internal
169 155 */
170 156 void QBarSet::barHoverLeaveEvent()
171 157 {
172 158 // Emit signal to user of charts
173 159 emit hoverLeave();
174 160 }
175 161
176 162 #include "moc_qbarset.cpp"
177 163 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,62 +1,66
1 1 #ifndef QBARSET_H
2 2 #define QBARSET_H
3 3
4 4 #include <qchartglobal.h>
5 5 #include <QPen>
6 6 #include <QBrush>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 11 {
12 12 Q_OBJECT
13 13 public:
14 14 QBarSet(QString name, QObject *parent = 0);
15 15
16 16 void setName(QString name);
17 17 QString name();
18 18 QBarSet& operator << (const qreal &value); // appends new value to set
19 19
20 // TODO: remove indices eventually. Use as internal?
20 21 int count(); // count of values in set
21 22 qreal valueAt(int index); // for modifying individual values
22 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 29 void setPen(QPen pen);
25 30 QPen pen();
26 31
27 32 void setBrush(QBrush brush);
28 33 QBrush brush();
29 34
30 35 Q_SIGNALS:
31 36 void clicked(QString category); // Clicked and hover signals exposed to user
32 37 void rightClicked(QString category);
33 38 void toggleFloatingValues();
34 39
35 40 // TODO: Expose this to user or not?
36 41 // TODO: TO PIMPL --->
37 42 void hoverEnter(QPoint pos);
38 43 void hoverLeave();
39 44 void showToolTip(QPoint pos, QString tip); // Private signal
40 45 // <--- TO PIMPL
41 46
42 47 public Q_SLOTS:
43 48 // These are for internal communication
44 49 // TODO: TO PIMPL --->
45 // void barClickedEvent(QString category);
46 // void barRightClickedEvent(QString category);
47 50 void barHoverEnterEvent(QPoint pos);
48 51 void barHoverLeaveEvent();
49 52 // <--- TO PIMPL
50 53
51 54 private:
52 55
53 56 QString mName;
54 QList<qreal> mValues;
57 QList<qreal> mValues; // TODO: replace with map (category, value)
58 QMap<QString,qreal> mMappedValues;
55 59 QPen mPen;
56 60 QBrush mBrush;
57 61
58 62 };
59 63
60 64 QTCOMMERCIALCHART_END_NAMESPACE
61 65
62 66 #endif // QBARSET_H
General Comments 0
You need to be logged in to leave comments. Login now