##// 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
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 categories;
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"
@@ -1,177 +1,163
1 #include "qbarset.h"
1 #include "qbarset.h"
2 #include <QDebug>
2 #include <QDebug>
3 #include <QToolTip>
3 #include <QToolTip>
4
4
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6
6
7 /*!
7 /*!
8 \class QBarSet
8 \class QBarSet
9 \brief part of QtCommercial chart API.
9 \brief part of QtCommercial chart API.
10
10
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
11 QBarSet represents one set of bars. Set of bars contains one data value for each category.
12 First value of set is assumed to belong to first category, second to second category and so on.
12 First value of set is assumed to belong to first category, second to second category and so on.
13 If set has fewer values than there are categories, then the missing values are assumed to be
13 If set has fewer values than there are categories, then the missing values are assumed to be
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
14 at the end of set. For missing values in middle of a set, numerical value of zero is used.
15
15
16 \mainclass
16 \mainclass
17
17
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
18 \sa QBarSeries, QStackedBarSeries, QPercentBarSeries
19 */
19 */
20
20
21 /*!
21 /*!
22 \fn void QBarSet::clicked(QString category)
22 \fn void QBarSet::clicked(QString category)
23 \brief signals that set has been clicked
23 \brief signals that set has been clicked
24 Parameter \a category describes on which category was clicked
24 Parameter \a category describes on which category was clicked
25 */
25 */
26
26
27 /*!
27 /*!
28 \fn void QBarSet::rightClicked(QString category)
28 \fn void QBarSet::rightClicked(QString category)
29 \brief signals that set has been clicked with right mouse button
29 \brief signals that set has been clicked with right mouse button
30 Parameter \a category describes on which category was clicked
30 Parameter \a category describes on which category was clicked
31 */
31 */
32
32
33 /*!
33 /*!
34 \fn void QBarSet::hoverEnter(QPoint pos)
34 \fn void QBarSet::hoverEnter(QPoint pos)
35 \brief signals that mouse has entered over the set at position \a pos.
35 \brief signals that mouse has entered over the set at position \a pos.
36 */
36 */
37
37
38 /*!
38 /*!
39 \fn void QBarSet::hoverLeave()
39 \fn void QBarSet::hoverLeave()
40 \brief signals that mouse has left from the set.
40 \brief signals that mouse has left from the set.
41 */
41 */
42
42
43 /*!
43 /*!
44 \fn void QBarSet::toggleFloatingValues()
44 \fn void QBarSet::toggleFloatingValues()
45 \brief \internal
45 \brief \internal
46 */
46 */
47
47
48 /*!
48 /*!
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
49 \fn void QBarSet::showToolTip(QPoint pos, QString tip)
50 \brief \internal \a pos \a tip
50 \brief \internal \a pos \a tip
51 */
51 */
52
52
53
53
54 /*!
54 /*!
55 Constructs QBarSet with a name of \a name and with parent of \a parent
55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 */
56 */
57 QBarSet::QBarSet(QString name, QObject *parent)
57 QBarSet::QBarSet(QString name, QObject *parent)
58 : QObject(parent)
58 : QObject(parent)
59 ,mName(name)
59 ,mName(name)
60 {
60 {
61 }
61 }
62
62
63 /*!
63 /*!
64 Sets new \a name for set.
64 Sets new \a name for set.
65 */
65 */
66 void QBarSet::setName(QString name)
66 void QBarSet::setName(QString name)
67 {
67 {
68 mName = name;
68 mName = name;
69 }
69 }
70
70
71 /*!
71 /*!
72 Returns name of the set.
72 Returns name of the set.
73 */
73 */
74 QString QBarSet::name()
74 QString QBarSet::name()
75 {
75 {
76 return mName;
76 return mName;
77 }
77 }
78
78
79 /*!
79 /*!
80 Appends new value \a value to the end of set.
80 Appends new value \a value to the end of set.
81 */
81 */
82 QBarSet& QBarSet::operator << (const qreal &value)
82 QBarSet& QBarSet::operator << (const qreal &value)
83 {
83 {
84 mValues.append(value);
84 mValues.append(value);
85 return *this;
85 return *this;
86 }
86 }
87
87
88 /*!
88 /*!
89 Returns count of values in set.
89 Returns count of values in set.
90 */
90 */
91 int QBarSet::count()
91 int QBarSet::count()
92 {
92 {
93 return mValues.count();
93 return mValues.count();
94 }
94 }
95
95
96 /*!
96 /*!
97 Returns value of set indexed by \a index
97 Returns value of set indexed by \a index
98 */
98 */
99 qreal QBarSet::valueAt(int index)
99 qreal QBarSet::valueAt(int index)
100 {
100 {
101 return mValues.at(index);
101 return mValues.at(index);
102 }
102 }
103
103
104 /*!
104 /*!
105 Sets a new value \a value to set, indexed by \a index
105 Sets a new value \a value to set, indexed by \a index
106 */
106 */
107 void QBarSet::setValue(int index, qreal value)
107 void QBarSet::setValue(int index, qreal value)
108 {
108 {
109 mValues.replace(index,value);
109 mValues.replace(index,value);
110 }
110 }
111
111
112 /*!
112 /*!
113 Sets pen for set. Bars of this set are drawn using \a pen
113 Sets pen for set. Bars of this set are drawn using \a pen
114 */
114 */
115 void QBarSet::setPen(QPen pen)
115 void QBarSet::setPen(QPen pen)
116 {
116 {
117 mPen = pen;
117 mPen = pen;
118 }
118 }
119
119
120 /*!
120 /*!
121 Returns pen of the set.
121 Returns pen of the set.
122 */
122 */
123 QPen QBarSet::pen()
123 QPen QBarSet::pen()
124 {
124 {
125 return mPen;
125 return mPen;
126 }
126 }
127
127
128 /*!
128 /*!
129 Sets brush for the set. Bars of this set are drawn using \a brush
129 Sets brush for the set. Bars of this set are drawn using \a brush
130 */
130 */
131 void QBarSet::setBrush(QBrush brush)
131 void QBarSet::setBrush(QBrush brush)
132 {
132 {
133 mBrush = brush;
133 mBrush = brush;
134 }
134 }
135
135
136 /*!
136 /*!
137 Returns brush of the set.
137 Returns brush of the set.
138 */
138 */
139 QBrush QBarSet::brush()
139 QBrush QBarSet::brush()
140 {
140 {
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 */
161 void QBarSet::barHoverEnterEvent(QPoint pos)
147 void QBarSet::barHoverEnterEvent(QPoint pos)
162 {
148 {
163 emit showToolTip(pos, mName);
149 emit showToolTip(pos, mName);
164 emit hoverEnter(pos);
150 emit hoverEnter(pos);
165 }
151 }
166
152
167 /*!
153 /*!
168 \internal
154 \internal
169 */
155 */
170 void QBarSet::barHoverLeaveEvent()
156 void QBarSet::barHoverLeaveEvent()
171 {
157 {
172 // Emit signal to user of charts
158 // Emit signal to user of charts
173 emit hoverLeave();
159 emit hoverLeave();
174 }
160 }
175
161
176 #include "moc_qbarset.cpp"
162 #include "moc_qbarset.cpp"
177 QTCOMMERCIALCHART_END_NAMESPACE
163 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,62 +1,66
1 #ifndef QBARSET_H
1 #ifndef QBARSET_H
2 #define QBARSET_H
2 #define QBARSET_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <QPen>
5 #include <QPen>
6 #include <QBrush>
6 #include <QBrush>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
10 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 QBarSet(QString name, QObject *parent = 0);
14 QBarSet(QString name, QObject *parent = 0);
15
15
16 void setName(QString name);
16 void setName(QString name);
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
27 void setBrush(QBrush brush);
32 void setBrush(QBrush brush);
28 QBrush brush();
33 QBrush brush();
29
34
30 Q_SIGNALS:
35 Q_SIGNALS:
31 void clicked(QString category); // Clicked and hover signals exposed to user
36 void clicked(QString category); // Clicked and hover signals exposed to user
32 void rightClicked(QString category);
37 void rightClicked(QString category);
33 void toggleFloatingValues();
38 void toggleFloatingValues();
34
39
35 // TODO: Expose this to user or not?
40 // TODO: Expose this to user or not?
36 // TODO: TO PIMPL --->
41 // TODO: TO PIMPL --->
37 void hoverEnter(QPoint pos);
42 void hoverEnter(QPoint pos);
38 void hoverLeave();
43 void hoverLeave();
39 void showToolTip(QPoint pos, QString tip); // Private signal
44 void showToolTip(QPoint pos, QString tip); // Private signal
40 // <--- TO PIMPL
45 // <--- TO PIMPL
41
46
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
50
53
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
58 };
62 };
59
63
60 QTCOMMERCIALCHART_END_NAMESPACE
64 QTCOMMERCIALCHART_END_NAMESPACE
61
65
62 #endif // QBARSET_H
66 #endif // QBARSET_H
General Comments 0
You need to be logged in to leave comments. Login now