From 1723c50daa1e36427b7310b332f6ff0699f3af5c 2012-02-13 15:06:19 From: sauimone Date: 2012-02-13 15:06:19 Subject: [PATCH] proof of concept implementation for barset and barcategory --- diff --git a/example/barchart/main.cpp b/example/barchart/main.cpp index 8e9474d..467c985 100644 --- a/example/barchart/main.cpp +++ b/example/barchart/main.cpp @@ -2,6 +2,8 @@ #include #include #include +#include +#include #include "chartwidget.h" QTCOMMERCIALCHART_USE_NAMESPACE @@ -11,25 +13,29 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); QMainWindow window; - BarChartSeries* series0 = new BarChartSeries(); + QBarCategory category; + category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec"; + + BarChartSeries* series0 = new BarChartSeries(category); + + QBarSet barSet0; + QBarSet barSet1; + QBarSet barSet2; + QBarSet barSet3; + QBarSet barSet4; // Create some test data to chart - QList data0; - data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; - QList data1; - data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; - QList data2; - data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; - QList data3; - data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; - QList data4; - data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; - - series0->addData(data0); - series0->addData(data1); - series0->addData(data2); - series0->addData(data3); - series0->addData(data4); + barSet0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; + barSet1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; + barSet2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; + barSet3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; + barSet4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; + + series0->addBarSet(barSet0); + series0->addBarSet(barSet1); + series0->addBarSet(barSet2); + series0->addBarSet(barSet3); + series0->addBarSet(barSet4); ChartWidget* chartWidget = new ChartWidget(&window); chartWidget->addSeries(series0); diff --git a/example/percentbarchart/main.cpp b/example/percentbarchart/main.cpp index 7c791ff..189a78d 100644 --- a/example/percentbarchart/main.cpp +++ b/example/percentbarchart/main.cpp @@ -31,6 +31,10 @@ int main(int argc, char *argv[]) series0->addData(data3); series0->addData(data4); + QList labels; + labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec"; + series0->setLabels(labels); + ChartWidget* chartWidget = new ChartWidget(&window); chartWidget->addSeries(series0); diff --git a/example/stackedbarchart/main.cpp b/example/stackedbarchart/main.cpp index 66f99ab..ac47280 100644 --- a/example/stackedbarchart/main.cpp +++ b/example/stackedbarchart/main.cpp @@ -31,6 +31,10 @@ int main(int argc, char *argv[]) series0->addData(data3); series0->addData(data4); + QList labels; + labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec"; + series0->setLabels(labels); + ChartWidget* chartWidget = new ChartWidget(&window); chartWidget->addSeries(series0); diff --git a/src/barchart/barchartmodel.cpp b/src/barchart/barchartmodel.cpp index 8620df5..17e7fff 100644 --- a/src/barchart/barchartmodel.cpp +++ b/src/barchart/barchartmodel.cpp @@ -2,12 +2,15 @@ #include #include #include "barchartmodel_p.h" +#include "qbarcategory.h" +#include "qbarset.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarChartModel::BarChartModel(QObject *parent) : +BarChartModel::BarChartModel(QBarCategory &category, QObject *parent) : QObject(parent) ,mRunningId(1) + ,mCategory(category) { } @@ -41,6 +44,19 @@ void BarChartModel::removeData(int id) emit modelUpdated(); } +void BarChartModel::addBarSet(QBarSet &set) +{ + DataContainer* c = new DataContainer(set.mValues,mRunningId); + mDataModel.append(c); + mRunningId++; +} + +void BarChartModel::removeBarSet(QBarSet &set) +{ + // TODO: +} + + int BarChartModel::countRows() { // qDebug() << "BarChartModel::countRows"; diff --git a/src/barchart/barchartmodel_p.h b/src/barchart/barchartmodel_p.h index 2ab132f..e210575 100644 --- a/src/barchart/barchartmodel_p.h +++ b/src/barchart/barchartmodel_p.h @@ -9,17 +9,23 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE // Model for bar chart. Internal class. // TODO: Implement as QAbstractItemModel? +class QBarSet; +class QBarCategory; + class BarChartModel : public QObject //, public QAbstractItemModel { Q_OBJECT public: - explicit BarChartModel(QObject *parent = 0); + explicit BarChartModel(QBarCategory &category, QObject *parent = 0); ~BarChartModel(); - - // Adds data to model. returns id. + + // TODO: remove these after add and remove QBarSet works. int addData(QList data); void removeData(int id); + void addBarSet(QBarSet &set); + void removeBarSet(QBarSet &set); + int countRows(); // Number of series in model int countColumns(); // Maximum number of items in series int countTotalItems(); // Total items in all series. Includes empty items. @@ -54,6 +60,7 @@ private: QList mDataModel; int mRunningId; int mMaxColumns; // longest series in datamodel + QBarCategory& mCategory; }; diff --git a/src/barchart/barchartseries.cpp b/src/barchart/barchartseries.cpp index dfd66f5..dfe68f1 100644 --- a/src/barchart/barchartseries.cpp +++ b/src/barchart/barchartseries.cpp @@ -3,8 +3,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarChartSeries::BarChartSeries(QObject *parent) - : BarChartSeriesBase(parent) +BarChartSeries::BarChartSeries(QBarCategory &category, QObject *parent) + : BarChartSeriesBase(category, parent) { } diff --git a/src/barchart/barchartseries.h b/src/barchart/barchartseries.h index 5e85940..e842e44 100644 --- a/src/barchart/barchartseries.h +++ b/src/barchart/barchartseries.h @@ -14,7 +14,7 @@ class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase { Q_OBJECT public: - BarChartSeries(QObject* parent=0); + BarChartSeries(QBarCategory &category, QObject* parent=0); // from BarChartSeriesBase virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } diff --git a/src/barchart/barchartseriesbase.cpp b/src/barchart/barchartseriesbase.cpp index 66f25d4..6715034 100644 --- a/src/barchart/barchartseriesbase.cpp +++ b/src/barchart/barchartseriesbase.cpp @@ -3,12 +3,13 @@ #include "barchartseriesbase.h" #include "bargroup.h" #include "barchartmodel_p.h" +#include "qbarset.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarChartSeriesBase::BarChartSeriesBase(QObject *parent) +BarChartSeriesBase::BarChartSeriesBase(QBarCategory &category, QObject *parent) : QChartSeries(parent) - ,mModel(new BarChartModel(this)) + ,mModel(new BarChartModel(category, this)) { } @@ -27,6 +28,16 @@ void BarChartSeriesBase::setLabels(QList labels) mLabels = labels; } +void BarChartSeriesBase::addBarSet(QBarSet &set) +{ + mModel->addBarSet(set); +} + +void BarChartSeriesBase::removeBarSet(QBarSet &set) +{ + mModel->removeBarSet(set); +} + qreal BarChartSeriesBase::min() { return mModel->min(); diff --git a/src/barchart/barchartseriesbase.h b/src/barchart/barchartseriesbase.h index bee6ac1..91e269b 100644 --- a/src/barchart/barchartseriesbase.h +++ b/src/barchart/barchartseriesbase.h @@ -10,24 +10,30 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class BarGroupBase; class BarChartModel; +class QBarSet; +class QBarCategory; // Container for series class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries { Q_OBJECT protected: - BarChartSeriesBase(QObject* parent=0); +// BarChartSeriesBase(QObject* parent=0); + BarChartSeriesBase(QBarCategory &category, QObject* parent=0); public: // from QChartSeries virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; } - // TODO: << operator for convinience - // Returns id for vector. + // TODO: These 3 will be removed. int addData(QList data); void removeData(int id); void setLabels(QList labels); + // TODO: Expose these to user in derived class instead of here? Common implementation for all bar charts, but not visible to user + void addBarSet(QBarSet &set); // Bob[1,5,6,2,15,4] first value goes to category 1 etc.. + void removeBarSet(QBarSet &set); // + // These shouldn't be visible to chart series user. However, ChartDataSet needs to access them, and friends are evil. qreal min(); qreal max(); @@ -46,7 +52,6 @@ public Q_SLOTS: private: BarChartModel* mModel; - QList mLabels; }; diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index f95a6ce..d48f1d1 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -13,7 +13,7 @@ BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : void BarGroup::layoutChanged() { - qDebug() << "BarGroup::layoutChanged"; +// qDebug() << "BarGroup::layoutChanged"; // Scale bars to new layout // Layout for bars: if (mModel.countRows() <= 0) { diff --git a/src/barchart/percentbarchartseries.cpp b/src/barchart/percentbarchartseries.cpp index 6581fbe..553f7d4 100644 --- a/src/barchart/percentbarchartseries.cpp +++ b/src/barchart/percentbarchartseries.cpp @@ -6,8 +6,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -PercentBarChartSeries::PercentBarChartSeries(QObject *parent) : - BarChartSeriesBase(parent) +PercentBarChartSeries::PercentBarChartSeries(QBarCategory &category, QObject *parent) : + BarChartSeriesBase(category, parent) { } diff --git a/src/barchart/percentbarchartseries.h b/src/barchart/percentbarchartseries.h index ce2d25c..ff75a49 100644 --- a/src/barchart/percentbarchartseries.h +++ b/src/barchart/percentbarchartseries.h @@ -13,7 +13,7 @@ class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase { Q_OBJECT public: - PercentBarChartSeries(QObject* parent=0); + PercentBarChartSeries(QBarCategory &category, QObject* parent=0); // from BarChartSeriesBase virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; } diff --git a/src/barchart/qbarcategory.cpp b/src/barchart/qbarcategory.cpp new file mode 100644 index 0000000..210a1f8 --- /dev/null +++ b/src/barchart/qbarcategory.cpp @@ -0,0 +1,26 @@ +#include "qbarcategory.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QBarCategory::QBarCategory() +{ +} + +QBarCategory& QBarCategory::operator << (const QString &label) +{ + mList.append(label); + return *this; +} + +int QBarCategory::count() +{ + return mList.count(); +} + +QList& QBarCategory::items() +{ + return mList; +} + + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarcategory.h b/src/barchart/qbarcategory.h new file mode 100644 index 0000000..ae012a0 --- /dev/null +++ b/src/barchart/qbarcategory.h @@ -0,0 +1,27 @@ +#ifndef QBARCATEGORY_H +#define QBARCATEGORY_H + +#include "qchartglobal.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QTCOMMERCIALCHART_EXPORT QBarCategory // : pubclic QObject // TODO: Need for this? +{ +public: + QBarCategory(); + + QBarCategory& operator << (const QString &label); + + // Number of items in category + int count(); + QList& items(); + +public: + + QList mList; + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QBARCATEGORY_H diff --git a/src/barchart/qbarset.cpp b/src/barchart/qbarset.cpp new file mode 100644 index 0000000..d5b285d --- /dev/null +++ b/src/barchart/qbarset.cpp @@ -0,0 +1,20 @@ +#include "qbarset.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +QBarSet::QBarSet() +{ +} + +void QBarSet::setName(QString name) +{ + mName = name; +} + +QBarSet& QBarSet::operator << (const qreal &value) +{ + mValues.append(value); + return *this; +} + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qbarset.h b/src/barchart/qbarset.h new file mode 100644 index 0000000..69f8366 --- /dev/null +++ b/src/barchart/qbarset.h @@ -0,0 +1,29 @@ +#ifndef QBARSET_H +#define QBARSET_H + +#include "qchartglobal.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class QTCOMMERCIALCHART_EXPORT QBarSet // : pubclic QObject // TODO: Need for this? +{ +public: + QBarSet(); + + void setName(QString name); +// void setValues(QList &values); + + // TODO: + QBarSet& operator << (const qreal &value); + + // TODO: Hide these from user of QBarSet. (but data model needs access to these) +public: + + QString mName; + QList mValues; + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // QBARSET_H diff --git a/src/barchart/stackedbarchartseries.cpp b/src/barchart/stackedbarchartseries.cpp index 7a5d542..92bf389 100644 --- a/src/barchart/stackedbarchartseries.cpp +++ b/src/barchart/stackedbarchartseries.cpp @@ -4,8 +4,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -StackedBarChartSeries::StackedBarChartSeries(QObject *parent) : - BarChartSeriesBase(parent) +StackedBarChartSeries::StackedBarChartSeries(QBarCategory &category, QObject *parent) : + BarChartSeriesBase(category, parent) { } diff --git a/src/barchart/stackedbarchartseries.h b/src/barchart/stackedbarchartseries.h index 680c5a4..4e321a4 100644 --- a/src/barchart/stackedbarchartseries.h +++ b/src/barchart/stackedbarchartseries.h @@ -13,7 +13,7 @@ class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase { Q_OBJECT public: - StackedBarChartSeries(QObject* parent=0); + StackedBarChartSeries(QBarCategory &category, QObject* parent=0); // from QChartSeries virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; } diff --git a/src/barchart/stackedbargroup.cpp b/src/barchart/stackedbargroup.cpp index fc858af..c02c1c2 100644 --- a/src/barchart/stackedbargroup.cpp +++ b/src/barchart/stackedbargroup.cpp @@ -13,7 +13,7 @@ StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *p void StackedBarGroup::layoutChanged() { - qDebug() << "StackedBarGroup::layoutChanged"; +// qDebug() << "StackedBarGroup::layoutChanged"; // Scale bars to new layout // Layout for bars: if (mModel.countRows() <= 0) { diff --git a/src/qchart.cpp b/src/qchart.cpp index bd7d45a..39e5916 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -44,15 +44,15 @@ QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type) break; } case QChartSeries::SeriesTypeBar: { - series = new BarChartSeries(this); + //series = new BarChartSeries(this); break; } case QChartSeries::SeriesTypeStackedBar: { - series = new StackedBarChartSeries(this); + //series = new StackedBarChartSeries(this); break; } case QChartSeries::SeriesTypePercentBar: { - series = new PercentBarChartSeries(this); + //series = new PercentBarChartSeries(this); break; } case QChartSeries::SeriesTypeScatter: { diff --git a/src/src.pro b/src/src.pro index 384b67c..f328cc8 100644 --- a/src/src.pro +++ b/src/src.pro @@ -18,6 +18,8 @@ SOURCES += barchart/barchartseries.cpp \ barchart/separator.cpp \ barchart/bargroupbase.cpp \ barchart/barchartseriesbase.cpp \ + barchart/qbarset.cpp \ + barchart/qbarcategory.cpp \ linechart/linechartanimationitem.cpp \ linechart/linechartitem.cpp \ linechart/qlinechartseries.cpp \ @@ -56,6 +58,8 @@ PUBLIC_HEADERS += linechart/qlinechartseries.h \ barchart/percentbargroup.h \ barchart/barchartseriesbase.h \ barchart/bargroupbase.h \ + barchart/qbarset.h \ + barchart/qbarcategory.h \ qchartseries.h \ qscatterseries.h \ qchart.h \ diff --git a/test/chartwidgettest/mainwidget.cpp b/test/chartwidgettest/mainwidget.cpp index 27890f4..92b3097 100644 --- a/test/chartwidgettest/mainwidget.cpp +++ b/test/chartwidgettest/mainwidget.cpp @@ -211,11 +211,11 @@ void MainWidget::addSeries(QString series, QString data) } } else if (data == "Table, 5 series"){ // Create some test data to chart - data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10; - data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0; - data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1; - data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5; - data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10; + data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 5; + data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4; + data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3; + data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2; + data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1; } else { // TODO: check if data has a valid file name Q_ASSERT(false);