From ec0910b997a49ed8750edfeb111a7e2d845f9e0b 2012-03-01 15:36:00 From: sauimone Date: 2012-03-01 15:36:00 Subject: [PATCH] Drilldown example for barcharts continuing. Still some bugs --- diff --git a/example/stackedbarchartdrilldown/main.cpp b/example/stackedbarchartdrilldown/main.cpp index 2b2247b..9b37297 100644 --- a/example/stackedbarchartdrilldown/main.cpp +++ b/example/stackedbarchartdrilldown/main.cpp @@ -1,83 +1,129 @@ -#include +#include #include +#include #include #include #include #include #include +#include QTCOMMERCIALCHART_USE_NAMESPACE +//! [1] +class DrilldownBarSeries : public QStackedBarSeries +{ + Q_OBJECT +public: + DrilldownBarSeries(QStringList categories, QObject* parent=0) : QStackedBarSeries(categories,parent) {} + +/* +public Q_SLOTS: + void handleRightClick(QBarSet *barset, QString category) + { + } +*/ +}; +//! [1] + +//! [2] +class DrilldownBarSet : public QBarSet +{ + Q_OBJECT +public: + DrilldownBarSet(QString name, DrilldownBarSeries *parent) : QBarSet(name, parent), mSeries(parent) {} + + DrilldownBarSeries* drilldownSeries() { return mSeries; } + +private: + DrilldownBarSeries* mSeries; +}; +//! [2] + +//! [3] +class DrilldownChart : public QChartView +{ + Q_OBJECT +public: + explicit DrilldownChart(QWidget *parent = 0) : QChartView(parent), m_currentSeries(0) {} + + void changeSeries(QSeries* series) + { + if (m_currentSeries) + removeSeries(m_currentSeries); + m_currentSeries = series; + addSeries(series); + setChartTitle(series->title()); + } + +public Q_SLOTS: + void handleRightClick(QBarSet* barset, QString category) + { + qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category; + // TODO: continue from here +// DrilldownBarSet* drilldownBarSet = static_cast(barset); +// changeSeries(drilldownBarSet->drilldownSeries()); + } + +private: + QSeries* m_currentSeries; +}; +//! [3] + + int main(int argc, char *argv[]) { QApplication a(argc, argv); QMainWindow window; - // TODO: -/* + DrilldownChart* drilldownChart = new DrilldownChart(&window); + drilldownChart->setChartTheme(QChart::ChartThemeIcy); + //! [1] // Define categories - QStringList categories; - categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; + QStringList months; + months << "Jun" << "Jul" << "Aug" << "Oct"; + QStringList weeks; + weeks << "week 1" << "week 2" << "week 3" << "week 4"; + QStringList plants; + plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; //! [1] - //! [2] - // Create some test sets for chat - QBarSet *set0 = new QBarSet("Bub"); - QBarSet *set1 = new QBarSet("Bob"); - QBarSet *set2 = new QBarSet("Guybrush"); - QBarSet *set3 = new QBarSet("Larry"); - QBarSet *set4 = new QBarSet("Zak"); - - *set0 << 1 << 2 << 3 << 4 << 5 << 6; - *set1 << 5 << 0 << 0 << 4 << 0 << 7; - *set2 << 3 << 5 << 8 << 13 << 8 << 5; - *set3 << 5 << 6 << 7 << 3 << 4 << 5; - *set4 << 9 << 7 << 5 << 3 << 1 << 2; - //! [2] - - //! [3] - // Create series and add sets to it - QStackedBarSeries* series = new QStackedBarSeries(categories); - - series->addBarSet(set0); - series->addBarSet(set1); - series->addBarSet(set2); - series->addBarSet(set3); - series->addBarSet(set4); - //! [3] - - //! [4] - // Enable tooltip - series->setToolTipEnabled(); - - // Connect clicked signal of set to toggle floating values of set. - // Note that we leave QBarset "Zak" unconnected here, so clicking on it doesn't toggle values. - QObject::connect(set0,SIGNAL(clicked(QBarSet*,QString)),set0,SIGNAL(toggleFloatingValues())); - QObject::connect(set1,SIGNAL(clicked(QBarSet*,QString)),set1,SIGNAL(toggleFloatingValues())); - QObject::connect(set2,SIGNAL(clicked(QBarSet*,QString)),set2,SIGNAL(toggleFloatingValues())); - QObject::connect(set3,SIGNAL(clicked(QBarSet*,QString)),set3,SIGNAL(toggleFloatingValues())); - //! [4] - - //! [5] - // Create view for chart and add series to it. Apply theme. - - QChartView* chartView = new QChartView(&window); - chartView->addSeries(series); - chartView->setChartTitle("simple stacked barchart"); - chartView->setChartTheme(QChart::ChartThemeIcy); - //! [5] - - //! [6] - chartView->axisX()->setAxisVisible(false); - chartView->axisX()->setGridVisible(false); - chartView->axisX()->setLabelsVisible(false); - //! [6] - - window.setCentralWidget(chartView); + DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); + seasonSeries->setTitle("Crop by month - Season"); + + foreach (QString plant, plants) { + DrilldownBarSet *seasonCrop = new DrilldownBarSet(plant, seasonSeries); + foreach(QString month, months) { + DrilldownBarSeries* monthSeries = new DrilldownBarSeries(weeks, drilldownChart); + DrilldownBarSet *monthCrop = new DrilldownBarSet(plant, monthSeries); + foreach(QString week, weeks) { + *monthCrop << (qrand() % 20); + } + monthSeries->addBarSet(monthCrop); + *seasonCrop << monthCrop->valueAt(plants.indexOf(plant)); + } + + // We want floating values! + QObject::connect(seasonCrop,SIGNAL(clicked(QString)),seasonCrop,SIGNAL(toggleFloatingValues())); + seasonSeries->addBarSet(seasonCrop); + } + + QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); + + seasonSeries->setToolTipEnabled(true); + + drilldownChart->addSeries(seasonSeries); + + drilldownChart->axisX()->setAxisVisible(false); + drilldownChart->axisX()->setGridVisible(false); + drilldownChart->axisX()->setLabelsVisible(false); + + window.setCentralWidget(drilldownChart); window.resize(400, 300); window.show(); -*/ + return a.exec(); } +#include "main.moc" diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp index 14fb989..68b7641 100644 --- a/src/barchart/qbarset.cpp +++ b/src/barchart/qbarset.cpp @@ -141,20 +141,6 @@ QBrush QBarSet::brush() return mBrush; } -/* -void QBarSet::barClickedEvent(QString category) -{ - // Some bar of this set has been clicked - // TODO: What happens then? - emit clicked(category); // Notify that set has been clicked -} - -void QBarSet::barRightClickedEvent(QString category) -{ - emit rightClicked(category); -} -*/ - /*! \internal \a pos */ diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h index 12b116e..ec24add 100644 --- a/src/barchart/qbarset.h +++ b/src/barchart/qbarset.h @@ -17,10 +17,15 @@ public: QString name(); QBarSet& operator << (const qreal &value); // appends new value to set + // TODO: remove indices eventually. Use as internal? int count(); // count of values in set qreal valueAt(int index); // for modifying individual values void setValue(int index, qreal value); // setter for individual value + // TODO: + //qreal value(QString category); + //void setValue(QString category, qreal value); + void setPen(QPen pen); QPen pen(); @@ -42,8 +47,6 @@ Q_SIGNALS: public Q_SLOTS: // These are for internal communication // TODO: TO PIMPL ---> -// void barClickedEvent(QString category); -// void barRightClickedEvent(QString category); void barHoverEnterEvent(QPoint pos); void barHoverLeaveEvent(); // <--- TO PIMPL @@ -51,7 +54,8 @@ public Q_SLOTS: private: QString mName; - QList mValues; + QList mValues; // TODO: replace with map (category, value) + QMap mMappedValues; QPen mPen; QBrush mBrush;