main.cpp
116 lines
| 3.8 KiB
| text/x-c
|
CppLexer
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r430 | #include <QtGui/QApplication> | ||
|
r425 | #include <QMainWindow> | ||
|
r838 | #include <QChartView> | ||
#include <QBarSet> | ||||
|
r891 | #include <QLegend> | ||
|
r903 | #include "drilldownseries.h" | ||
#include "drilldownchart.h" | ||||
|
r425 | |||
QTCOMMERCIALCHART_USE_NAMESPACE | ||||
int main(int argc, char *argv[]) | ||||
{ | ||||
|
r919 | //! [1] | ||
|
r425 | QApplication a(argc, argv); | ||
QMainWindow window; | ||||
|
r919 | //! [1] | ||
|
r425 | |||
|
r919 | //! [2] | ||
|
r829 | DrilldownChart* drilldownChart = new DrilldownChart(); | ||
drilldownChart->setTheme(QChart::ChartThemeBlueIcy); | ||||
drilldownChart->setAnimationOptions(QChart::SeriesAnimations); | ||||
|
r919 | //! [2] | ||
|
r430 | |||
|
r903 | //! [3] | ||
|
r425 | // Define categories | ||
|
r430 | QStringList months; | ||
|
r462 | months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; | ||
|
r430 | QStringList weeks; | ||
weeks << "week 1" << "week 2" << "week 3" << "week 4"; | ||||
QStringList plants; | ||||
plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; | ||||
|
r903 | //! [3] | ||
|
r449 | |||
|
r903 | //! [4] | ||
|
r449 | // Create drilldown structure | ||
DrilldownBarSeries* seasonSeries = new DrilldownBarSeries(months, drilldownChart); | ||||
|
r733 | seasonSeries->setName("Crop by month - Season"); | ||
|
r425 | |||
|
r449 | // Each month in season series has drilldown series for weekly data | ||
foreach (QString month, months) { | ||||
|
r430 | |||
|
r449 | // Create drilldown series for every week | ||
|
r438 | DrilldownBarSeries* weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); | ||
|
r462 | seasonSeries->mapDrilldownSeries(month, weeklySeries); | ||
|
r438 | |||
|
r449 | // Drilling down from weekly data brings us back to season data. | ||
foreach (QString week, weeks) { | ||||
|
r462 | weeklySeries->mapDrilldownSeries(week, seasonSeries); | ||
|
r733 | weeklySeries->setName(QString("Crop by week - " + month)); | ||
|
r430 | } | ||
|
r449 | // Use right click signal to implement drilldown | ||
|
r1008 | QObject::connect(weeklySeries, SIGNAL(clicked(QBarSet*,QString)), drilldownChart, SLOT(handleClicked(QBarSet*,QString))); | ||
|
r430 | } | ||
|
r462 | |||
// Enable drilldown from season series using right click. | ||||
|
r1008 | QObject::connect(seasonSeries, SIGNAL(clicked(QBarSet*,QString)), drilldownChart, SLOT(handleClicked(QBarSet*,QString))); | ||
|
r903 | //! [4] | ||
|
r430 | |||
|
r903 | //! [5] | ||
|
r449 | // Fill monthly and weekly series with data | ||
|
r438 | foreach (QString plant, plants) { | ||
|
r449 | QBarSet* monthlyCrop = new QBarSet(plant); | ||
foreach (QString month, months) { | ||||
QBarSet* weeklyCrop = new QBarSet(plant); | ||||
|
r829 | foreach (QString week, weeks ) | ||
|
r438 | *weeklyCrop << (qrand() % 20); | ||
|
r462 | // Get the drilldown series from season series and add crop to it. | ||
|
r1194 | seasonSeries->drilldownSeries(month)->append(weeklyCrop); | ||
|
r934 | *monthlyCrop << weeklyCrop->sum(); | ||
|
r438 | } | ||
|
r1194 | seasonSeries->append(monthlyCrop); | ||
|
r438 | } | ||
|
r903 | //! [5] | ||
|
r449 | |||
|
r903 | //! [6] | ||
|
r449 | // Show season series in initial view | ||
drilldownChart->changeSeries(seasonSeries); | ||||
|
r829 | drilldownChart->setTitle(seasonSeries->name()); | ||
|
r903 | //! [6] | ||
|
r430 | |||
|
r903 | //! [7] | ||
|
r535 | drilldownChart->axisX()->setGridLineVisible(false); | ||
|
r987 | drilldownChart->axisY()->setNiceNumbersEnabled(true); | ||
|
r891 | drilldownChart->legend()->setVisible(true); | ||
|
r907 | drilldownChart->legend()->setAlignment(QLegend::AlignmentBottom); | ||
|
r903 | //! [7] | ||
|
r430 | |||
|
r829 | QChartView *chartView = new QChartView(drilldownChart); | ||
window.setCentralWidget(chartView); | ||||
|
r425 | window.resize(400, 300); | ||
window.show(); | ||||
|
r430 | |||
|
r425 | return a.exec(); | ||
} | ||||