main.cpp
116 lines
| 3.9 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> | ||
Jani Honkonen
|
r838 | #include <QChartView> | ||
#include <QBarSet> | ||||
sauimone
|
r891 | #include <QLegend> | ||
sauimone
|
r903 | #include "drilldownseries.h" | ||
#include "drilldownchart.h" | ||||
sauimone
|
r425 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
int main(int argc, char *argv[]) | ||||
{ | ||||
Jani Honkonen
|
r919 | //! [1] | ||
sauimone
|
r425 | QApplication a(argc, argv); | ||
QMainWindow window; | ||||
Jani Honkonen
|
r919 | //! [1] | ||
sauimone
|
r425 | |||
Jani Honkonen
|
r919 | //! [2] | ||
Jani Honkonen
|
r829 | DrilldownChart* drilldownChart = new DrilldownChart(); | ||
drilldownChart->setTheme(QChart::ChartThemeBlueIcy); | ||||
drilldownChart->setAnimationOptions(QChart::SeriesAnimations); | ||||
Jani Honkonen
|
r919 | //! [2] | ||
sauimone
|
r430 | |||
sauimone
|
r903 | //! [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
|
r903 | //! [3] | ||
sauimone
|
r449 | |||
sauimone
|
r903 | //! [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
|
r903 | //! [4] | ||
sauimone
|
r430 | |||
sauimone
|
r903 | //! [5] | ||
sauimone
|
r449 | // 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
|
r934 | *monthlyCrop << weeklyCrop->sum(); | ||
sauimone
|
r438 | } | ||
Jani Honkonen
|
r829 | seasonSeries->appendBarSet(monthlyCrop); | ||
sauimone
|
r438 | } | ||
sauimone
|
r903 | //! [5] | ||
sauimone
|
r449 | |||
sauimone
|
r903 | //! [6] | ||
sauimone
|
r449 | // Show season series in initial view | ||
drilldownChart->changeSeries(seasonSeries); | ||||
Jani Honkonen
|
r829 | drilldownChart->setTitle(seasonSeries->name()); | ||
sauimone
|
r903 | //! [6] | ||
sauimone
|
r430 | |||
sauimone
|
r903 | //! [7] | ||
Michal Klocek
|
r535 | drilldownChart->axisX()->setGridLineVisible(false); | ||
Tero Ahola
|
r987 | drilldownChart->axisY()->setNiceNumbersEnabled(true); | ||
sauimone
|
r891 | drilldownChart->legend()->setVisible(true); | ||
Jani Honkonen
|
r907 | drilldownChart->legend()->setAlignment(QLegend::AlignmentBottom); | ||
sauimone
|
r903 | //! [7] | ||
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(); | ||
} | ||||