main.cpp
149 lines
| 4.4 KiB
| text/x-c
|
CppLexer
|
r430 | #include <QtGui/QApplication> | ||
|
r425 | #include <QMainWindow> | ||
|
r430 | #include <qchartglobal.h> | ||
|
r425 | #include <qchartview.h> | ||
#include <qstackedbarseries.h> | ||||
#include <qbarset.h> | ||||
#include <qchartaxis.h> | ||||
#include <QStringList> | ||||
|
r430 | #include <QDebug> | ||
|
r425 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
|
r430 | //! [1] | ||
class DrilldownBarSeries : public QStackedBarSeries | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
|
r449 | DrilldownBarSeries(QStringList categories, QObject *parent = 0) : QStackedBarSeries(categories,parent) {} | ||
|
r438 | |||
|
r462 | void mapDrilldownSeries(QString category, DrilldownBarSeries* drilldownSeries) | ||
|
r430 | { | ||
|
r449 | mDrilldownSeries[category] = drilldownSeries; | ||
|
r430 | } | ||
|
r438 | |||
DrilldownBarSeries* drilldownSeries(QString category) | ||||
|
r449 | { | ||
return mDrilldownSeries[category]; | ||||
} | ||||
public Q_SLOTS: | ||||
|
r430 | |||
private: | ||||
|
r449 | |||
QMap<QString, DrilldownBarSeries*> mDrilldownSeries; | ||||
|
r430 | }; | ||
|
r449 | //! [1] | ||
|
r430 | |||
|
r449 | //! [2] | ||
|
r430 | 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); | ||||
|
r733 | setChartTitle(series->name()); | ||
|
r430 | } | ||
public Q_SLOTS: | ||||
|
r462 | void handleRightClick(QBarSet *barset, QString category) | ||
|
r430 | { | ||
|
r613 | Q_UNUSED(barset) | ||
|
r449 | DrilldownBarSeries* series = static_cast<DrilldownBarSeries*> (sender()); | ||
changeSeries(series->drilldownSeries(category)); | ||||
|
r430 | } | ||
private: | ||||
QSeries* m_currentSeries; | ||||
}; | ||||
|
r449 | //! [2] | ||
|
r430 | |||
|
r425 | int main(int argc, char *argv[]) | ||
{ | ||||
QApplication a(argc, argv); | ||||
QMainWindow window; | ||||
|
r430 | DrilldownChart* drilldownChart = new DrilldownChart(&window); | ||
|
r757 | drilldownChart->setChartTheme(QChart::ChartThemeBlueIcy); | ||
|
r430 | |||
|
r449 | //! [3] | ||
|
r425 | // Define categories | ||
|
r430 | QStringList months; | ||
|
r462 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; | ||
|
r430 | QStringList weeks; | ||
weeks << "week 1" << "week 2" << "week 3" << "week 4"; | ||||
QStringList plants; | ||||
plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; | ||||
|
r449 | //! [3] | ||
|
r438 | //! [4] | ||
|
r449 | // Create drilldown structure | ||
DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); | ||||
|
r733 | seasonSeries->setName("Crop by month - Season"); | ||
|
r425 | |||
|
r449 | // Each month in season series has drilldown series for weekly data | ||
foreach (QString month, months) { | ||||
|
r430 | |||
|
r449 | // Create drilldown series for every week | ||
|
r438 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); | ||
|
r462 | seasonSeries->mapDrilldownSeries(month, weeklySeries); | ||
|
r438 | |||
|
r449 | // Drilling down from weekly data brings us back to season data. | ||
foreach (QString week, weeks) { | ||||
|
r462 | weeklySeries->mapDrilldownSeries(week, seasonSeries); | ||
|
r733 | weeklySeries->setName(QString("Crop by week - " + month)); | ||
|
r430 | } | ||
|
r449 | // Use right click signal to implement drilldown | ||
QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); | ||||
|
r430 | } | ||
|
r462 | |||
// Enable drilldown from season series using right click. | ||||
QObject::connect(seasonSeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); | ||||
|
r449 | //! [4] | ||
|
r430 | |||
|
r449 | //! [5] | ||
// Fill monthly and weekly series with data | ||||
|
r438 | foreach (QString plant, plants) { | ||
|
r449 | QBarSet* monthlyCrop = new QBarSet(plant); | ||
foreach (QString month, months) { | ||||
QBarSet* weeklyCrop = new QBarSet(plant); | ||||
|
r438 | foreach (QString week, weeks ) { | ||
*weeklyCrop << (qrand() % 20); | ||||
} | ||||
|
r462 | // Get the drilldown series from season series and add crop to it. | ||
|
r449 | seasonSeries->drilldownSeries(month)->addBarSet(weeklyCrop); | ||
seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); | ||||
|
r438 | *monthlyCrop << weeklyCrop->total(); | ||
|
r449 | |||
|
r438 | QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); | ||
} | ||||
|
r449 | seasonSeries->addBarSet(monthlyCrop); | ||
|
r438 | QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); | ||
} | ||||
|
r449 | //! [5] | ||
seasonSeries->setToolTipEnabled(true); | ||||
|
r462 | //! [6] | ||
|
r449 | // Show season series in initial view | ||
drilldownChart->changeSeries(seasonSeries); | ||||
|
r733 | drilldownChart->setChartTitle(seasonSeries->name()); | ||
|
r462 | //! [6] | ||
|
r430 | |||
|
r535 | drilldownChart->axisX()->setGridLineVisible(false); | ||
|
r430 | |||
window.setCentralWidget(drilldownChart); | ||||
|
r425 | window.resize(400, 300); | ||
window.show(); | ||||
|
r430 | |||
|
r425 | return a.exec(); | ||
} | ||||
|
r430 | #include "main.moc" | ||