diff --git a/demos/chartviewer/charts.cpp b/demos/chartviewer/charts.cpp new file mode 100644 index 0000000..602dde7 --- /dev/null +++ b/demos/chartviewer/charts.cpp @@ -0,0 +1,29 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "charts.h" + +Chart::Chart(){}; +Chart::~Chart(){}; + +void Chart::initialize() +{ + Charts::addChart(this); +} diff --git a/demos/chartviewer/charts.h b/demos/chartviewer/charts.h new file mode 100644 index 0000000..399bd08 --- /dev/null +++ b/demos/chartviewer/charts.h @@ -0,0 +1,86 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + + +#ifndef CHARTS_H +#define CHARTS_H +#include "window.h" +#include +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE +class QChart; +QTCOMMERCIALCHART_END_NAMESPACE + +QTCOMMERCIALCHART_USE_NAMESPACE + +class Chart +{ +public: + Chart(); + + virtual ~Chart(); + + virtual void initialize(); + virtual QChart* createChart(const DataTable& table) = 0; + virtual QString name() = 0; + virtual QString category() = 0; + virtual QString subCategory() = 0; + +}; + +namespace Charts +{ + +typedef QList ChartList; + +inline ChartList& chartList() +{ + static ChartList list; + return list; +} + +inline bool findChart(Chart* chart) +{ + ChartList& list = chartList(); + if (list.contains(chart)) { + return true; + } + foreach (Chart* item, list) { + if (item->name() == chart->name() && item->category() == chart->category() && item->subCategory() == chart->subCategory()) { + return true; + } + } + return false; +} + +inline void addChart(Chart* chart) +{ + ChartList& list = chartList(); + if (!findChart(chart)) { + list.append(chart); + } +} +} + +#define DECLARE_CHART(chartName) static chartName t; + +#endif diff --git a/demos/chartviewer/charts/areachart.cpp b/demos/chartviewer/charts/areachart.cpp new file mode 100644 index 0000000..c571fe7 --- /dev/null +++ b/demos/chartviewer/charts/areachart.cpp @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qareaseries.h" +#include "qlineseries.h" + +class AreaChart: public Chart +{ +public: + + AreaChart(){ + initialize(); + } + + QString name() { return QObject::tr("AreaChart"); } + QString category() { return QObject::tr("XYSeries"); } + QString subCategory() { return QString::null; } + + QChart* createChart(const DataTable& table) + { + + QChart *chart = new QChart(); + chart->setTitle("Area chart"); + + // The lower series initialized to zero values + QLineSeries *lowerSeries = 0; + QString name("Series "); + int nameIndex = 0; + for (int i(0); i < table.count(); i++) { + QLineSeries *upperSeries = new QLineSeries(chart); + for (int j(0); j < table[i].count(); j++) { + Data data = table[i].at(j); + if (lowerSeries) { + const QList& points = lowerSeries->points(); + upperSeries->append(QPointF(j, points[i].y() + data.first.y())); + } + else + upperSeries->append(QPointF(j, data.first.y())); + } + QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries); + area->setName(name + QString::number(nameIndex)); + nameIndex++; + chart->addSeries(area); + chart->createDefaultAxes(); + lowerSeries = upperSeries; + } + + return chart; + + } + +}; + +DECLARE_CHART(AreaChart) + diff --git a/demos/chartviewer/charts/barchart.cpp b/demos/chartviewer/charts/barchart.cpp new file mode 100644 index 0000000..1bbb4bd --- /dev/null +++ b/demos/chartviewer/charts/barchart.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qstackedbarseries.h" +#include "qbarset.h" + +class BarChart: public Chart +{ +public: + + BarChart(){ + initialize(); + } + + QString name() { return QObject::tr("VerticalStackedBarChart"); } + QString category() { return QObject::tr("BarSeries"); } + QString subCategory() { return QObject::tr("Vertical"); } + + QChart* createChart(const DataTable& table) + { + + QChart* chart = new QChart(); + + chart->setTitle("Bar chart"); + + QStackedBarSeries* series = new QStackedBarSeries(chart); + for (int i(0); i < table.count(); i++) { + QBarSet *set = new QBarSet("Bar set " + QString::number(i)); + foreach (Data data, table[i]) + *set << data.first.y(); + series->append(set); + } + chart->addSeries(series); + chart->createDefaultAxes(); + return chart; + } + +}; + +DECLARE_CHART(BarChart) + diff --git a/demos/chartviewer/charts/charts.pri b/demos/chartviewer/charts/charts.pri new file mode 100644 index 0000000..1f98762 --- /dev/null +++ b/demos/chartviewer/charts/charts.pri @@ -0,0 +1,4 @@ +INCLUDEPATH += $$PWD +DEPENDPATH += $$PWD + +SOURCES+= linechart.cpp scatterchart.cpp splinechart.cpp piechart.cpp barchart.cpp areachart.cpp \ No newline at end of file diff --git a/demos/chartviewer/charts/linechart.cpp b/demos/chartviewer/charts/linechart.cpp new file mode 100644 index 0000000..464be25 --- /dev/null +++ b/demos/chartviewer/charts/linechart.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qlineseries.h" + +class LineChart: public Chart +{ +public: + + LineChart(){ + initialize(); + } + + QString name() { return QObject::tr("LineChart"); } + QString category() { return QObject::tr("XYSeries"); } + QString subCategory() { return QString::null; } + + QChart* createChart(const DataTable& table) { + + QChart* chart = new QChart(); + chart->setTitle("Line chart"); + + QString name("Series "); + int nameIndex = 0; + foreach (DataList list, table) { + QLineSeries *series = new QLineSeries(chart); + foreach (Data data, list) + series->append(data.first); + series->setName(name + QString::number(nameIndex)); + nameIndex++; + chart->addSeries(series); + } + + chart->createDefaultAxes(); + + return chart; + } + +}; + +DECLARE_CHART(LineChart) + diff --git a/demos/chartviewer/charts/piechart.cpp b/demos/chartviewer/charts/piechart.cpp new file mode 100644 index 0000000..d8def3d --- /dev/null +++ b/demos/chartviewer/charts/piechart.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qpieseries.h" + +class PieChart: public Chart +{ +public: + + PieChart(){ + initialize(); + } + + QString name() { return QObject::tr("PieChart"); } + QString category() { return QString::null; } + QString subCategory() { return QString::null; } + + QChart* createChart(const DataTable& table) + { + QChart* chart = new QChart(); + chart->setTitle("Pie chart"); + + qreal pieSize = 1.0 / table.count(); + for (int i = 0; i < table.count(); i++) { + QPieSeries *series = new QPieSeries(chart); + foreach (Data data, table[i]) { + QPieSlice *slice = series->append(data.second, data.first.y()); + if (data == table[i].first()) { + slice->setLabelVisible(); + slice->setExploded(); + } + } + qreal hPos = (pieSize / 2) + (i / (qreal) table.count()); + series->setPieSize(pieSize); + series->setHorizontalPosition(hPos); + series->setVerticalPosition(0.5); + chart->addSeries(series); + } + return chart; + } + +}; + +DECLARE_CHART(PieChart) + diff --git a/demos/chartviewer/charts/scatterchart.cpp b/demos/chartviewer/charts/scatterchart.cpp new file mode 100644 index 0000000..c17e9b4 --- /dev/null +++ b/demos/chartviewer/charts/scatterchart.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qscatterseries.h" + +class ScatterChart: public Chart +{ +public: + + ScatterChart(){ + initialize(); + } + + QString name() { return QObject::tr("ScatterChart"); } + QString category() { return QObject::tr("XYSeries"); } + QString subCategory() { return QString::null; } + + QChart* createChart(const DataTable& table) + { + + QChart* chart = new QChart(); + chart->setTitle("Scatter chart"); + QString name("Series "); + int nameIndex = 0; + foreach (DataList list, table) { + QScatterSeries *series = new QScatterSeries(chart); + foreach (Data data, list) + series->append(data.first); + series->setName(name + QString::number(nameIndex)); + nameIndex++; + chart->addSeries(series); + } + chart->createDefaultAxes(); + + return chart; + } + +}; + +DECLARE_CHART(ScatterChart) diff --git a/demos/chartviewer/charts/splinechart.cpp b/demos/chartviewer/charts/splinechart.cpp new file mode 100644 index 0000000..aad1266 --- /dev/null +++ b/demos/chartviewer/charts/splinechart.cpp @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "charts.h" +#include "qchart.h" +#include "qsplineseries.h" + +class SplineChart: public Chart +{ +public: + + SplineChart(){ + initialize(); + } + + QString name() { return QObject::tr("SplineChart"); } + QString category() { return QObject::tr("XYSeries"); } + QString subCategory() { return QString::null; } + + QChart* createChart(const DataTable& table) + { + + QChart* chart = new QChart(); + + chart->setTitle("Spline chart"); + QString name("Series "); + int nameIndex = 0; + foreach (DataList list, table) { + QSplineSeries *series = new QSplineSeries(chart); + foreach (Data data, list) + series->append(data.first); + series->setName(name + QString::number(nameIndex)); + nameIndex++; + chart->addSeries(series); + } + chart->createDefaultAxes(); + return chart; + } + +}; + +DECLARE_CHART(SplineChart) + diff --git a/demos/chartviewer/chartviewer.pro b/demos/chartviewer/chartviewer.pro index 3965fce..3eed466 100644 --- a/demos/chartviewer/chartviewer.pro +++ b/demos/chartviewer/chartviewer.pro @@ -1,5 +1,7 @@ !include( ../demos.pri ):error( "Couldn't find the demos.pri file!" ) +include(charts/charts.pri) TARGET = chartviewer QT += opengl -SOURCES = main.cpp window.cpp view.cpp -HEADERS = window.h view.h \ No newline at end of file +SOURCES += main.cpp window.cpp view.cpp charts.cpp +HEADERS += window.h view.h charts.h + diff --git a/demos/chartviewer/window.cpp b/demos/chartviewer/window.cpp index 7f94a94..9c7a1f8 100644 --- a/demos/chartviewer/window.cpp +++ b/demos/chartviewer/window.cpp @@ -20,6 +20,7 @@ #include "window.h" #include "view.h" +#include "charts.h" #include #include @@ -100,31 +101,16 @@ Window::Window(QWidget* parent) : widget->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); } - QChart* chart; - chart = createAreaChart(); - baseLayout->addItem(chart, 0, 0); - m_chartList << chart; + Charts::ChartList list = Charts::chartList(); - chart = createBarChart(m_valueCount); - baseLayout->addItem(chart, 0, 1); - m_chartList << chart; - - chart = createLineChart(); - baseLayout->addItem(chart, 0, 2); - m_chartList << chart; - - chart = createPieChart(); - baseLayout->addItem(chart, 1, 0); - m_chartList << chart; - - chart = createSplineChart(); - baseLayout->addItem(chart, 1, 1); - m_chartList << chart; - - chart = createScatterChart(); - baseLayout->addItem(chart, 1, 2); - m_chartList << chart; + for(int i = 0 ; i < 6 ; ++i) + { + if(!(icreateChart(m_dataTable); + baseLayout->addItem(chart, i/3, i%3); + m_chartList << chart; + } m_form = new QGraphicsWidget(); m_form->setLayout(baseLayout); @@ -238,147 +224,6 @@ QComboBox* Window::createLegendBox() const return legendComboBox; } -QChart* Window::createAreaChart() const -{ - QChart *chart = new QChart(); -// chart->axisX()->setNiceNumbersEnabled(true); -// chart->axisY()->setNiceNumbersEnabled(true); - chart->setTitle("Area chart"); - - // The lower series initialized to zero values - QLineSeries *lowerSeries = 0; - QString name("Series "); - int nameIndex = 0; - for (int i(0); i < m_dataTable.count(); i++) { - QLineSeries *upperSeries = new QLineSeries(chart); - for (int j(0); j < m_dataTable[i].count(); j++) { - Data data = m_dataTable[i].at(j); - if (lowerSeries) { - const QList& points = lowerSeries->points(); - upperSeries->append(QPointF(j, points[i].y() + data.first.y())); - } - else - upperSeries->append(QPointF(j, data.first.y())); - } - QAreaSeries *area = new QAreaSeries(upperSeries, lowerSeries); - area->setName(name + QString::number(nameIndex)); - nameIndex++; - chart->addSeries(area); - chart->createDefaultAxes(); - lowerSeries = upperSeries; - } - - return chart; -} - -QChart* Window::createBarChart(int valueCount) const -{ - Q_UNUSED(valueCount); - QChart* chart = new QChart(); - //TODO: chart->axisX()->setNiceNumbersEnabled(true); - //TODO: chart->axisY()->setNiceNumbersEnabled(true); - chart->setTitle("Bar chart"); - - QStackedBarSeries* series = new QStackedBarSeries(chart); - for (int i(0); i < m_dataTable.count(); i++) { - QBarSet *set = new QBarSet("Bar set " + QString::number(i)); - foreach (Data data, m_dataTable[i]) - *set << data.first.y(); - series->append(set); - } - chart->addSeries(series); - chart->createDefaultAxes(); - - return chart; -} - -QChart* Window::createLineChart() const -{ - QChart* chart = new QChart(); - //TODO: chart->axisX()->setNiceNumbersEnabled(true); - //TODO: chart->axisY()->setNiceNumbersEnabled(true); - chart->setTitle("Line chart"); - - QString name("Series "); - int nameIndex = 0; - foreach (DataList list, m_dataTable) { - QLineSeries *series = new QLineSeries(chart); - foreach (Data data, list) - series->append(data.first); - series->setName(name + QString::number(nameIndex)); - nameIndex++; - chart->addSeries(series); - } - chart->createDefaultAxes(); - - return chart; -} - -QChart* Window::createPieChart() const -{ - QChart* chart = new QChart(); - chart->setTitle("Pie chart"); - - qreal pieSize = 1.0 / m_dataTable.count(); - for (int i = 0; i < m_dataTable.count(); i++) { - QPieSeries *series = new QPieSeries(chart); - foreach (Data data, m_dataTable[i]) { - QPieSlice *slice = series->append(data.second, data.first.y()); - if (data == m_dataTable[i].first()) { - slice->setLabelVisible(); - slice->setExploded(); - } - } - qreal hPos = (pieSize / 2) + (i / (qreal) m_dataTable.count()); - series->setPieSize(pieSize); - series->setHorizontalPosition(hPos); - series->setVerticalPosition(0.5); - chart->addSeries(series); - } - - return chart; -} - -QChart* Window::createSplineChart() const -{ - QChart* chart = new QChart(); - //TODO: chart->axisX()->setNiceNumbersEnabled(true); - //TODO: chart->axisY()->setNiceNumbersEnabled(true); - chart->setTitle("Spline chart"); - QString name("Series "); - int nameIndex = 0; - foreach (DataList list, m_dataTable) { - QSplineSeries *series = new QSplineSeries(chart); - foreach (Data data, list) - series->append(data.first); - series->setName(name + QString::number(nameIndex)); - nameIndex++; - chart->addSeries(series); - } - chart->createDefaultAxes(); - return chart; -} - -QChart* Window::createScatterChart() const -{ - QChart* chart = new QChart(); - //TODO: chart->axisX()->setNiceNumbersEnabled(true); - //TODO: chart->axisY()->setNiceNumbersEnabled(true); - chart->setTitle("Scatter chart"); - QString name("Series "); - int nameIndex = 0; - foreach (DataList list, m_dataTable) { - QScatterSeries *series = new QScatterSeries(chart); - foreach (Data data, list) - series->append(data.first); - series->setName(name + QString::number(nameIndex)); - nameIndex++; - chart->addSeries(series); - } - chart->createDefaultAxes(); - return chart; -} - void Window::updateUI() { bool opengl = m_openGLCheckBox->isChecked(); @@ -437,7 +282,7 @@ void Window::updateUI() QChart::AnimationOptions options( m_animatedComboBox->itemData(m_animatedComboBox->currentIndex()).toInt()); - if (m_chartList.at(0)->animationOptions() != options) { + if (!m_chartList.isEmpty() && m_chartList.at(0)->animationOptions() != options) { foreach (QChart *chart, m_chartList) chart->setAnimationOptions(options); } diff --git a/demos/chartviewer/window.h b/demos/chartviewer/window.h index ff83712..2f8ffba 100644 --- a/demos/chartviewer/window.h +++ b/demos/chartviewer/window.h @@ -58,12 +58,6 @@ private: QComboBox* createAnimationBox() const; QComboBox* createLegendBox() const; void connectSignals(); - QChart* createAreaChart() const; - QChart* createBarChart(int valueCount) const; - QChart* createPieChart() const; - QChart* createLineChart() const; - QChart* createSplineChart() const; - QChart* createScatterChart() const; void createProxyWidgets(); protected: