main.cpp
113 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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r2049 | #include <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[]) | ||||
{ | ||||
QApplication a(argc, argv); | ||||
QMainWindow window; | ||||
|
r1415 | //! [1] | ||
|
r829 | DrilldownChart* drilldownChart = new DrilldownChart(); | ||
drilldownChart->setTheme(QChart::ChartThemeBlueIcy); | ||||
drilldownChart->setAnimationOptions(QChart::SeriesAnimations); | ||||
|
r1415 | //! [1] | ||
|
r430 | |||
|
r1415 | //! [2] | ||
|
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"; | ||||
|
r1415 | //! [2] | ||
|
r449 | |||
|
r1415 | //! [3] | ||
|
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 | ||
|
r2098 | for (int month = 0; month < months.count(); month++) { | ||
|
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. | ||
|
r2098 | for (int week = 0; week < weeks.count(); week++) { | ||
|
r462 | weeklySeries->mapDrilldownSeries(week, seasonSeries); | ||
|
r1321 | weeklySeries->setName(QString("Crop by week - " + months.at(month))); | ||
|
r430 | } | ||
|
r1563 | // Use clicked signal to implement drilldown | ||
QObject::connect(weeklySeries, SIGNAL(clicked(int, QBarSet*)), drilldownChart, SLOT(handleClicked(int, QBarSet*))); | ||||
|
r430 | } | ||
|
r462 | |||
|
r1563 | // Enable drilldown from season series using clicked signal | ||
QObject::connect(seasonSeries, SIGNAL(clicked(int, QBarSet*)), drilldownChart, SLOT(handleClicked(int, QBarSet*))); | ||||
|
r1415 | //! [3] | ||
|
r430 | |||
|
r1415 | //! [4] | ||
|
r449 | // Fill monthly and weekly series with data | ||
|
r438 | foreach (QString plant, plants) { | ||
|
r449 | QBarSet* monthlyCrop = new QBarSet(plant); | ||
|
r2098 | for (int month = 0; month < months.count(); month++) { | ||
|
r449 | QBarSet* weeklyCrop = new QBarSet(plant); | ||
|
r2098 | for (int week = 0; week < weeks.count(); week++) | ||
|
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 | } | ||
|
r1415 | //! [4] | ||
|
r449 | |||
|
r1415 | //! [5] | ||
|
r449 | // Show season series in initial view | ||
drilldownChart->changeSeries(seasonSeries); | ||||
|
r829 | drilldownChart->setTitle(seasonSeries->name()); | ||
|
r1415 | //! [5] | ||
|
r430 | |||
|
r1415 | //! [6] | ||
|
r535 | drilldownChart->axisX()->setGridLineVisible(false); | ||
|
r891 | drilldownChart->legend()->setVisible(true); | ||
|
r1357 | drilldownChart->legend()->setAlignment(Qt::AlignBottom); | ||
|
r1415 | //! [6] | ||
|
r430 | |||
|
r829 | QChartView *chartView = new QChartView(drilldownChart); | ||
window.setCentralWidget(chartView); | ||||
|
r425 | window.resize(400, 300); | ||
window.show(); | ||||
|
r430 | |||
|
r425 | return a.exec(); | ||
} | ||||