main.cpp
160 lines
| 5.1 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> | ||||
#include <QStringList> | ||||
sauimone
|
r430 | #include <QDebug> | ||
sauimone
|
r425 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
sauimone
|
r430 | //! [1] | ||
class DrilldownBarSeries : public QStackedBarSeries | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
sauimone
|
r438 | DrilldownBarSeries(QStringList categories, QObject* parent = 0) : QStackedBarSeries(categories,parent) {} | ||
sauimone
|
r430 | |||
public Q_SLOTS: | ||||
void handleRightClick(QBarSet *barset, QString category) | ||||
{ | ||||
sauimone
|
r438 | qDebug() << "DrilldownBarSeries::handleRightClick" << barset->name() << category; | ||
// mChart->changeSeries(this); | ||||
sauimone
|
r430 | } | ||
sauimone
|
r438 | |||
sauimone
|
r430 | }; | ||
//! [1] | ||||
//! [2] | ||||
class DrilldownBarSet : public QBarSet | ||||
{ | ||||
Q_OBJECT | ||||
public: | ||||
sauimone
|
r438 | DrilldownBarSet(QString name, DrilldownBarSeries* drilldownSeries) : QBarSet(name) , mSeries(drilldownSeries) {} | ||
sauimone
|
r430 | |||
sauimone
|
r438 | DrilldownBarSeries* drilldownSeries(QString category) | ||
{ | ||||
return mSeries; | ||||
} | ||||
sauimone
|
r430 | |||
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: | ||||
sauimone
|
r438 | void handleRightClick(QBarSet *barset, QString category) | ||
sauimone
|
r430 | { | ||
qDebug() << "DrilldownChart::handleRightClick" << barset->name() << category; | ||||
// TODO: continue from here | ||||
sauimone
|
r438 | DrilldownBarSet* drilldownBarSet = static_cast<DrilldownBarSet*>(barset); | ||
changeSeries(drilldownBarSet->drilldownSeries(category)); | ||||
sauimone
|
r430 | } | ||
private: | ||||
QSeries* m_currentSeries; | ||||
}; | ||||
//! [3] | ||||
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
|
r438 | //! [4] | ||
sauimone
|
r425 | // Define categories | ||
sauimone
|
r430 | QStringList months; | ||
sauimone
|
r438 | months << "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
|
r438 | //! [4] | ||
sauimone
|
r425 | |||
sauimone
|
r438 | DrilldownBarSeries* monthlySeries = new DrilldownBarSeries(months, drilldownChart); | ||
monthlySeries->setTitle("Crop by month - Season"); | ||||
sauimone
|
r430 | |||
foreach (QString plant, plants) { | ||||
sauimone
|
r438 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); | ||
DrilldownBarSet* monthlyCrop = new DrilldownBarSet(plant,weeklySeries); | ||||
weeklySeries->setTitle("Crop by week - Month"); | ||||
sauimone
|
r430 | foreach(QString month, months) { | ||
sauimone
|
r438 | DrilldownBarSet* weeklyCrop = new DrilldownBarSet(plant,monthlySeries); | ||
foreach (QString week, weeks ) { | ||||
*weeklyCrop << (qrand() % 20); | ||||
sauimone
|
r430 | } | ||
sauimone
|
r438 | |||
weeklySeries->addBarSet(weeklyCrop); | ||||
weeklySeries->setToolTipEnabled(true); | ||||
*monthlyCrop << weeklyCrop->total(); | ||||
QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); | ||||
QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); | ||||
sauimone
|
r430 | } | ||
sauimone
|
r438 | QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); | ||
monthlySeries->addBarSet(monthlyCrop); | ||||
sauimone
|
r430 | } | ||
sauimone
|
r438 | /* | ||
foreach (QString plant, plants) { | ||||
DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); | ||||
DrilldownBarSet* monthlyCrop = new DrilldownBarSet(plant,weeklySeries); | ||||
weeklySeries->setTitle("Crop by week - Month"); | ||||
foreach(QString month, months) { | ||||
DrilldownBarSet* weeklyCrop = new DrilldownBarSet(plant,monthlySeries); | ||||
foreach (QString week, weeks ) { | ||||
*weeklyCrop << (qrand() % 20); | ||||
} | ||||
weeklySeries->addBarSet(weeklyCrop); | ||||
weeklySeries->setToolTipEnabled(true); | ||||
*monthlyCrop << weeklyCrop->total(); | ||||
QObject::connect(weeklyCrop,SIGNAL(clicked(QString)),weeklyCrop,SIGNAL(toggleFloatingValues())); | ||||
QObject::connect(weeklySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); | ||||
} | ||||
QObject::connect(monthlyCrop,SIGNAL(clicked(QString)),monthlyCrop,SIGNAL(toggleFloatingValues())); | ||||
monthlySeries->addBarSet(monthlyCrop); | ||||
} | ||||
*/ | ||||
QObject::connect(monthlySeries,SIGNAL(rightClicked(QBarSet*,QString)),drilldownChart,SLOT(handleRightClick(QBarSet*,QString))); | ||||
sauimone
|
r430 | |||
sauimone
|
r438 | monthlySeries->setToolTipEnabled(true); | ||
drilldownChart->changeSeries(monthlySeries); | ||||
drilldownChart->setChartTitle(monthlySeries->title()); | ||||
sauimone
|
r430 | |||
drilldownChart->axisX()->setAxisVisible(false); | ||||
drilldownChart->axisX()->setGridVisible(false); | ||||
drilldownChart->axisX()->setLabelsVisible(false); | ||||
window.setCentralWidget(drilldownChart); | ||||
sauimone
|
r425 | window.resize(400, 300); | ||
window.show(); | ||||
sauimone
|
r430 | |||
sauimone
|
r425 | return a.exec(); | ||
} | ||||
sauimone
|
r430 | #include "main.moc" | ||