From d89198258a9395d380001c365b411ee1179022b1 2012-01-24 13:40:37 From: sauimone Date: 2012-01-24 13:40:37 Subject: [PATCH] Refactored series creation. --- diff --git a/src/barchart/barchartseries.cpp b/src/barchart/barchartseries.cpp index d57e179..89b1e60 100644 --- a/src/barchart/barchartseries.cpp +++ b/src/barchart/barchartseries.cpp @@ -1,14 +1,21 @@ #include "barchartseries.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarChartSeries::BarChartSeries(QList data, QObject *parent) +BarChartSeries::BarChartSeries(QObject *parent) : QChartSeries(parent) - , mData(data) { } +bool BarChartSeries::setData(QList data) +{ + mData = data; + return true; +} + int BarChartSeries::min() { + Q_ASSERT(mData.count() > 0); + // TODO: make min and max members and update them when data changes. // This is slower since they are checked every time, even if data is same since previous call. int min = mData.at(0); @@ -23,6 +30,8 @@ int BarChartSeries::min() int BarChartSeries::max() { + Q_ASSERT(mData.count() > 0); + int max = mData.at(0); for (int i=0; i data, QObject* parent=0); + BarChartSeries(QObject* parent=0); // from QChartSeries - static QChartSeries* create(QObject* parent = 0 ); virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } + virtual bool setData(QList data); + virtual bool setData(QList data) {return false;} + virtual bool setData(QList x, QList y) {return false;} + + // Methods to find out minimum and maximum values of data int min(); int max(); diff --git a/src/qchartseries.cpp b/src/qchartseries.cpp new file mode 100644 index 0000000..a087b8d --- /dev/null +++ b/src/qchartseries.cpp @@ -0,0 +1,32 @@ +#include "qchartglobal.h" +#include "qchartseries.h" + +#include "barchartseries.h" +#include "qxychartseries.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QChartSeries* QChartSeries::create(QChartSeriesType type, QObject* parent) +{ + // TODO: Other types + switch (type) { + case QChartSeries::SeriesTypeLine: { + QXYChartSeries* s = QXYChartSeries::create(parent); // TODO: do we need create method for derived implementations? + return s; + } + case QChartSeries::SeriesTypePie: { + return 0; + } + case QChartSeries::SeriesTypeScatter: { + return 0; + } + case QChartSeries::SeriesTypeBar: { + BarChartSeries* s = new BarChartSeries(parent); + return s; + } + default: + return 0; + } +} + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/qchartseries.h b/src/qchartseries.h index b7cf198..71e2037 100644 --- a/src/qchartseries.h +++ b/src/qchartseries.h @@ -8,7 +8,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QTCOMMERCIALCHART_EXPORT QChartSeries : public QObject { - + //TODO: + //Q_OBJECT public: enum QChartSeriesType { SeriesTypeLine = 0, @@ -26,7 +27,8 @@ public: virtual ~QChartSeries(){}; // Factory method - static QChartSeries* create(QObject* parent = 0 ){ return 0;} + static QChartSeries* create(QChartSeriesType type, QObject* parent = 0 ); + // Pure virtual virtual QChartSeriesType type() const = 0; diff --git a/src/src.pro b/src/src.pro index 3fc88db..56f75ae 100644 --- a/src/src.pro +++ b/src/src.pro @@ -25,7 +25,8 @@ SOURCES += \ axis.cpp \ qchartwidget.cpp \ pieslice.cpp \ - qchartview.cpp + qchartview.cpp \ + qchartseries.cpp PRIVATE_HEADERS += \ xylinechart/xylinechartitem_p.h \ diff --git a/src/xylinechart/qxychartseries.h b/src/xylinechart/qxychartseries.h index 0efd29c..2c61ba1 100644 --- a/src/xylinechart/qxychartseries.h +++ b/src/xylinechart/qxychartseries.h @@ -10,6 +10,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QTCOMMERCIALCHART_EXPORT QXYChartSeries : public QChartSeries { + //TODO: +// Q_OBJECT private: QXYChartSeries(QObject* parent=0); public: diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 2b1e48e..f73ff63 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -3,6 +3,7 @@ #include "qchartseries.h" #include "qpieseries.h" #include +#include #include #include #include @@ -165,6 +166,22 @@ void MainWidget::addSeries(QString series, QString data) // TODO } + // BarChart + if (series == "Bar") { + qDebug() << "Bar chart series"; + QChartSeries* barSeries = QChartSeries::create(QChartSeries::SeriesTypeBar, this); + QList barData; + barData << 1; + barData << 12; + barData << 5; + barData << 8; + barData << 17; + barData << 9; + barSeries->setData(barData); + m_chartWidget->addSeries(barSeries); + + } + setCurrentSeries(newSeries); }