diff options
Diffstat (limited to 'examples/charts/stackedbarchartdrilldown')
6 files changed, 319 insertions, 0 deletions
diff --git a/examples/charts/stackedbarchartdrilldown/drilldownchart.cpp b/examples/charts/stackedbarchartdrilldown/drilldownchart.cpp new file mode 100644 index 00000000..1acfa172 --- /dev/null +++ b/examples/charts/stackedbarchartdrilldown/drilldownchart.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include "drilldownchart.h" +#include <QBarCategoryAxis> + +QT_CHARTS_USE_NAMESPACE + +DrilldownChart::DrilldownChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) + : QChart(QChart::ChartTypeCartesian, parent, wFlags), + m_currentSeries(0) +{ +} + +void DrilldownChart::changeSeries(DrilldownBarSeries *series) +{ + if (m_currentSeries) { + removeSeries(m_currentSeries); + } + + m_currentSeries = series; + + // Reset axis + QBarCategoryAxis *axis = new QBarCategoryAxis(); + axis->append(m_currentSeries->categories()); + + addSeries(series); + + createDefaultAxes(); + setAxisX(axis, series); + axisY()->setTitleText("Crops"); + + setTitle(series->name()); +} + +void DrilldownChart::handleClicked(int index, QBarSet *barset) +{ + Q_UNUSED(barset) + DrilldownBarSeries *series = static_cast<DrilldownBarSeries *>(sender()); + changeSeries(series->drilldownSeries(index)); +} + +#include "moc_drilldownchart.cpp" diff --git a/examples/charts/stackedbarchartdrilldown/drilldownchart.h b/examples/charts/stackedbarchartdrilldown/drilldownchart.h new file mode 100644 index 00000000..c75216f7 --- /dev/null +++ b/examples/charts/stackedbarchartdrilldown/drilldownchart.h @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#ifndef DRILLDOWNCHART_H +#define DRILLDOWNCHART_H + +#include <QChart> +#include "drilldownseries.h" + +QT_CHARTS_USE_NAMESPACE + +//! [1] +class DrilldownChart : public QChart +{ + Q_OBJECT +public: + explicit DrilldownChart(QGraphicsItem *parent = 0, Qt::WindowFlags wFlags = 0); + + void changeSeries(DrilldownBarSeries *series); + +public Q_SLOTS: + void handleClicked(int index, QBarSet *barset); + +private: + DrilldownBarSeries *m_currentSeries; +}; +//! [1] + +#endif // DRILLDOWNCHART_H diff --git a/examples/charts/stackedbarchartdrilldown/drilldownseries.cpp b/examples/charts/stackedbarchartdrilldown/drilldownseries.cpp new file mode 100644 index 00000000..87d70b5a --- /dev/null +++ b/examples/charts/stackedbarchartdrilldown/drilldownseries.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include "drilldownseries.h" + +QT_CHARTS_USE_NAMESPACE + +DrilldownBarSeries::DrilldownBarSeries(QStringList categories, QObject *parent) + : QStackedBarSeries(parent) +{ + m_categories = categories; +} + +void DrilldownBarSeries::mapDrilldownSeries(int index, DrilldownBarSeries *drilldownSeries) +{ + m_DrilldownSeries[index] = drilldownSeries; +} + +DrilldownBarSeries *DrilldownBarSeries::drilldownSeries(int index) +{ + return m_DrilldownSeries[index]; +} + +QStringList DrilldownBarSeries::categories() +{ + return m_categories; +} + +#include "moc_drilldownseries.cpp" diff --git a/examples/charts/stackedbarchartdrilldown/drilldownseries.h b/examples/charts/stackedbarchartdrilldown/drilldownseries.h new file mode 100644 index 00000000..5a340839 --- /dev/null +++ b/examples/charts/stackedbarchartdrilldown/drilldownseries.h @@ -0,0 +1,48 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#ifndef DRILLDOWNSERIES_H +#define DRILLDOWNSERIES_H + +#include <QStackedBarSeries> +#include <QMap> + +QT_CHARTS_USE_NAMESPACE + +//! [1] +class DrilldownBarSeries : public QStackedBarSeries +{ + Q_OBJECT +public: + DrilldownBarSeries(QStringList categories, QObject *parent = 0); + + void mapDrilldownSeries(int index, DrilldownBarSeries *drilldownSeries); + + DrilldownBarSeries *drilldownSeries(int index); + + QStringList categories(); + +private: + QMap<int, DrilldownBarSeries *> m_DrilldownSeries; + QStringList m_categories; +}; +//! [1] + +#endif // DRILLDOWNSERIES_H diff --git a/examples/charts/stackedbarchartdrilldown/main.cpp b/examples/charts/stackedbarchartdrilldown/main.cpp new file mode 100644 index 00000000..6a46520d --- /dev/null +++ b/examples/charts/stackedbarchartdrilldown/main.cpp @@ -0,0 +1,113 @@ +/**************************************************************************** +** +** Copyright (C) 2014 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 Enterprise Charts Add-on. +** +** $QT_BEGIN_LICENSE$ +** Licensees holding valid Qt Enterprise licenses may use this file in +** accordance with the Qt Enterprise 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$ +** +****************************************************************************/ + +#include <QApplication> +#include <QMainWindow> +#include <QChartView> +#include <QBarSet> +#include <QLegend> +#include "drilldownseries.h" +#include "drilldownchart.h" + +QT_CHARTS_USE_NAMESPACE + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + QMainWindow window; + +//! [1] + DrilldownChart *drilldownChart = new DrilldownChart(); + drilldownChart->setTheme(QChart::ChartThemeBlueIcy); + drilldownChart->setAnimationOptions(QChart::SeriesAnimations); +//! [1] + +//! [2] + // Define categories + QStringList months; + months << "May" << "Jun" << "Jul" << "Aug" << "Sep"; + QStringList weeks; + weeks << "week 1" << "week 2" << "week 3" << "week 4"; + QStringList plants; + plants << "Habanero" << "Lemon Drop" << "Starfish" << "Aji Amarillo"; +//! [2] + +//! [3] + // Create drilldown structure + DrilldownBarSeries *seasonSeries = new DrilldownBarSeries(months, drilldownChart); + seasonSeries->setName("Crop by month - Season"); + + // Each month in season series has drilldown series for weekly data + for (int month = 0; month < months.count(); month++) { + + // Create drilldown series for every week + DrilldownBarSeries *weeklySeries = new DrilldownBarSeries(weeks, drilldownChart); + seasonSeries->mapDrilldownSeries(month, weeklySeries); + + // Drilling down from weekly data brings us back to season data. + for (int week = 0; week < weeks.count(); week++) { + weeklySeries->mapDrilldownSeries(week, seasonSeries); + weeklySeries->setName(QString("Crop by week - " + months.at(month))); + } + + // Use clicked signal to implement drilldown + QObject::connect(weeklySeries, SIGNAL(clicked(int,QBarSet*)), drilldownChart, SLOT(handleClicked(int,QBarSet*))); + } + + // Enable drilldown from season series using clicked signal + QObject::connect(seasonSeries, SIGNAL(clicked(int,QBarSet*)), drilldownChart, SLOT(handleClicked(int,QBarSet*))); +//! [3] + +//! [4] + // Fill monthly and weekly series with data + foreach (QString plant, plants) { + QBarSet *monthlyCrop = new QBarSet(plant); + for (int month = 0; month < months.count(); month++) { + QBarSet *weeklyCrop = new QBarSet(plant); + for (int week = 0; week < weeks.count(); week++) + *weeklyCrop << (qrand() % 20); + // Get the drilldown series from season series and add crop to it. + seasonSeries->drilldownSeries(month)->append(weeklyCrop); + *monthlyCrop << weeklyCrop->sum(); + } + seasonSeries->append(monthlyCrop); + } +//! [4] + +//! [5] + // Show season series in initial view + drilldownChart->changeSeries(seasonSeries); + drilldownChart->setTitle(seasonSeries->name()); +//! [5] + +//! [6] + drilldownChart->axisX()->setGridLineVisible(false); + drilldownChart->legend()->setVisible(true); + drilldownChart->legend()->setAlignment(Qt::AlignBottom); +//! [6] + + QChartView *chartView = new QChartView(drilldownChart); + window.setCentralWidget(chartView); + window.resize(480, 300); + window.show(); + + return a.exec(); +} + diff --git a/examples/charts/stackedbarchartdrilldown/stackedbarchartdrilldown.pro b/examples/charts/stackedbarchartdrilldown/stackedbarchartdrilldown.pro new file mode 100644 index 00000000..c10066f4 --- /dev/null +++ b/examples/charts/stackedbarchartdrilldown/stackedbarchartdrilldown.pro @@ -0,0 +1,6 @@ +!include( ../examples.pri ) { + error( "Couldn't find the examples.pri file!" ) +} +TARGET = stackedbarchartdrilldown +SOURCES += main.cpp drilldownseries.cpp drilldownchart.cpp +HEADERS += drilldownseries.h drilldownchart.h |