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