main.cpp
167 lines
| 5.2 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r830 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
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
|
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] | ||
Jani Honkonen
|
r829 | class DrilldownChart : public QChart | ||
sauimone
|
r430 | { | ||
Q_OBJECT | ||||
public: | ||||
Jani Honkonen
|
r829 | explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0) : QChart(parent, wFlags), m_currentSeries(0) {} | ||
sauimone
|
r430 | |||
void changeSeries(QSeries* series) | ||||
{ | ||||
if (m_currentSeries) | ||||
removeSeries(m_currentSeries); | ||||
m_currentSeries = series; | ||||
addSeries(series); | ||||
Jani Honkonen
|
r829 | setTitle(series->name()); | ||
sauimone
|
r430 | } | ||
public Q_SLOTS: | ||||
Jani Honkonen
|
r829 | void handleClicked(QBarSet *barset, QString category, Qt::MouseButtons button) | ||
sauimone
|
r430 | { | ||
Tero Ahola
|
r613 | Q_UNUSED(barset) | ||
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; | ||||
Jani Honkonen
|
r829 | DrilldownChart* drilldownChart = new DrilldownChart(); | ||
drilldownChart->setTheme(QChart::ChartThemeBlueIcy); | ||||
drilldownChart->setAnimationOptions(QChart::SeriesAnimations); | ||||
sauimone
|
r430 | |||
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); | ||||
Tero Ahola
|
r733 | seasonSeries->setName("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); | ||
Tero Ahola
|
r733 | weeklySeries->setName(QString("Crop by week - " + month)); | ||
sauimone
|
r430 | } | ||
sauimone
|
r449 | // Use right click signal to implement drilldown | ||
Jani Honkonen
|
r829 | QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons))); | ||
sauimone
|
r430 | } | ||
sauimone
|
r462 | |||
// Enable drilldown from season series using right click. | ||||
Jani Honkonen
|
r829 | QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), drilldownChart, SLOT(handleClicked(QBarSet*,QString,Qt::MouseButtons))); | ||
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); | ||||
Jani Honkonen
|
r829 | foreach (QString week, weeks ) | ||
sauimone
|
r438 | *weeklyCrop << (qrand() % 20); | ||
sauimone
|
r462 | // Get the drilldown series from season series and add crop to it. | ||
Jani Honkonen
|
r829 | seasonSeries->drilldownSeries(month)->appendBarSet(weeklyCrop); | ||
sauimone
|
r449 | seasonSeries->drilldownSeries(month)->setToolTipEnabled(true); | ||
sauimone
|
r438 | *monthlyCrop << weeklyCrop->total(); | ||
} | ||||
Jani Honkonen
|
r829 | seasonSeries->appendBarSet(monthlyCrop); | ||
sauimone
|
r438 | } | ||
sauimone
|
r449 | //! [5] | ||
seasonSeries->setToolTipEnabled(true); | ||||
sauimone
|
r462 | //! [6] | ||
sauimone
|
r449 | // Show season series in initial view | ||
drilldownChart->changeSeries(seasonSeries); | ||||
Jani Honkonen
|
r829 | drilldownChart->setTitle(seasonSeries->name()); | ||
sauimone
|
r462 | //! [6] | ||
sauimone
|
r430 | |||
Michal Klocek
|
r535 | drilldownChart->axisX()->setGridLineVisible(false); | ||
sauimone
|
r430 | |||
Jani Honkonen
|
r829 | QChartView *chartView = new QChartView(drilldownChart); | ||
window.setCentralWidget(chartView); | ||||
sauimone
|
r425 | window.resize(400, 300); | ||
window.show(); | ||||
sauimone
|
r430 | |||
sauimone
|
r425 | return a.exec(); | ||
} | ||||
sauimone
|
r430 | #include "main.moc" | ||