##// 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"
@@ -141,20 +141,6 QBrush QBarSet::brush()
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 */
@@ -17,10 +17,15 public:
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
@@ -42,8 +47,6 Q_SIGNALS:
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
@@ -51,7 +54,8 public Q_SLOTS:
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
General Comments 0
You need to be logged in to leave comments. Login now