main.cpp
115 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[]) | ||||
{ | ||||
QApplication a(argc, argv); | ||||
QMainWindow window; | ||||
sauimone
|
r1415 | //! [1] | ||
Jani Honkonen
|
r829 | DrilldownChart* drilldownChart = new DrilldownChart(); | ||
drilldownChart->setTheme(QChart::ChartThemeBlueIcy); | ||||
drilldownChart->setAnimationOptions(QChart::SeriesAnimations); | ||||
sauimone
|
r1415 | //! [1] | ||
sauimone
|
r430 | |||
sauimone
|
r1415 | //! [2] | ||
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
|
r1415 | //! [2] | ||
sauimone
|
r449 | |||
sauimone
|
r1415 | //! [3] | ||
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 | ||
sauimone
|
r1321 | for (int month=0; month < months.count(); month++) { | ||
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. | ||
sauimone
|
r1321 | for (int week=0; week < weeks.count(); week++) { | ||
sauimone
|
r462 | weeklySeries->mapDrilldownSeries(week, seasonSeries); | ||
sauimone
|
r1321 | weeklySeries->setName(QString("Crop by week - " + months.at(month))); | ||
sauimone
|
r430 | } | ||
sauimone
|
r1563 | // Use clicked signal to implement drilldown | ||
QObject::connect(weeklySeries, SIGNAL(clicked(int, QBarSet*)), drilldownChart, SLOT(handleClicked(int, QBarSet*))); | ||||
sauimone
|
r430 | } | ||
sauimone
|
r462 | |||
sauimone
|
r1563 | // Enable drilldown from season series using clicked signal | ||
QObject::connect(seasonSeries, SIGNAL(clicked(int, QBarSet*)), drilldownChart, SLOT(handleClicked(int, QBarSet*))); | ||||
sauimone
|
r1415 | //! [3] | ||
sauimone
|
r430 | |||
sauimone
|
r1415 | //! [4] | ||
sauimone
|
r449 | // Fill monthly and weekly series with data | ||
sauimone
|
r438 | foreach (QString plant, plants) { | ||
sauimone
|
r449 | QBarSet* monthlyCrop = new QBarSet(plant); | ||
sauimone
|
r1321 | for (int month=0; month<months.count(); month++) { | ||
sauimone
|
r449 | QBarSet* weeklyCrop = new QBarSet(plant); | ||
sauimone
|
r1321 | for (int week=0; week<weeks.count(); week++) { | ||
sauimone
|
r438 | *weeklyCrop << (qrand() % 20); | ||
sauimone
|
r1321 | } | ||
sauimone
|
r462 | // Get the drilldown series from season series and add crop to it. | ||
sauimone
|
r1194 | seasonSeries->drilldownSeries(month)->append(weeklyCrop); | ||
sauimone
|
r934 | *monthlyCrop << weeklyCrop->sum(); | ||
sauimone
|
r438 | } | ||
sauimone
|
r1194 | seasonSeries->append(monthlyCrop); | ||
sauimone
|
r438 | } | ||
sauimone
|
r1415 | //! [4] | ||
sauimone
|
r449 | |||
sauimone
|
r1415 | //! [5] | ||
sauimone
|
r449 | // Show season series in initial view | ||
drilldownChart->changeSeries(seasonSeries); | ||||
Jani Honkonen
|
r829 | drilldownChart->setTitle(seasonSeries->name()); | ||
sauimone
|
r1415 | //! [5] | ||
sauimone
|
r430 | |||
sauimone
|
r1415 | //! [6] | ||
Michal Klocek
|
r535 | drilldownChart->axisX()->setGridLineVisible(false); | ||
Michal Klocek
|
r1541 | //TODO: drilldownChart->axisY()->setNiceNumbersEnabled(true); | ||
sauimone
|
r891 | drilldownChart->legend()->setVisible(true); | ||
Tero Ahola
|
r1357 | drilldownChart->legend()->setAlignment(Qt::AlignBottom); | ||
sauimone
|
r1415 | //! [6] | ||
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(); | ||
} | ||||