##// END OF EJS Templates
proof of concept implementation for barset and barcategory
sauimone -
r169:1723c50daa1e
parent child
Show More
@@ -0,0 +1,26
1 #include "qbarcategory.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QBarCategory::QBarCategory()
6 {
7 }
8
9 QBarCategory& QBarCategory::operator << (const QString &label)
10 {
11 mList.append(label);
12 return *this;
13 }
14
15 int QBarCategory::count()
16 {
17 return mList.count();
18 }
19
20 QList<QString>& QBarCategory::items()
21 {
22 return mList;
23 }
24
25
26 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,27
1 #ifndef QBARCATEGORY_H
2 #define QBARCATEGORY_H
3
4 #include "qchartglobal.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class QTCOMMERCIALCHART_EXPORT QBarCategory // : pubclic QObject // TODO: Need for this?
9 {
10 public:
11 QBarCategory();
12
13 QBarCategory& operator << (const QString &label);
14
15 // Number of items in category
16 int count();
17 QList<QString>& items();
18
19 public:
20
21 QList<QString> mList;
22
23 };
24
25 QTCOMMERCIALCHART_END_NAMESPACE
26
27 #endif // QBARCATEGORY_H
@@ -0,0 +1,20
1 #include "qbarset.h"
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
5 QBarSet::QBarSet()
6 {
7 }
8
9 void QBarSet::setName(QString name)
10 {
11 mName = name;
12 }
13
14 QBarSet& QBarSet::operator << (const qreal &value)
15 {
16 mValues.append(value);
17 return *this;
18 }
19
20 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,29
1 #ifndef QBARSET_H
2 #define QBARSET_H
3
4 #include "qchartglobal.h"
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 class QTCOMMERCIALCHART_EXPORT QBarSet // : pubclic QObject // TODO: Need for this?
9 {
10 public:
11 QBarSet();
12
13 void setName(QString name);
14 // void setValues(QList<qreal> &values);
15
16 // TODO:
17 QBarSet& operator << (const qreal &value);
18
19 // TODO: Hide these from user of QBarSet. (but data model needs access to these)
20 public:
21
22 QString mName;
23 QList<qreal> mValues;
24
25 };
26
27 QTCOMMERCIALCHART_END_NAMESPACE
28
29 #endif // QBARSET_H
@@ -2,6 +2,8
2 2 #include <QMainWindow>
3 3 #include <QStandardItemModel>
4 4 #include <barchartseries.h>
5 #include <qbarcategory.h>
6 #include <qbarset.h>
5 7 #include "chartwidget.h"
6 8
7 9 QTCOMMERCIALCHART_USE_NAMESPACE
@@ -11,25 +13,29 int main(int argc, char *argv[])
11 13 QApplication a(argc, argv);
12 14 QMainWindow window;
13 15
14 BarChartSeries* series0 = new BarChartSeries();
16 QBarCategory category;
17 category << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
18
19 BarChartSeries* series0 = new BarChartSeries(category);
20
21 QBarSet barSet0;
22 QBarSet barSet1;
23 QBarSet barSet2;
24 QBarSet barSet3;
25 QBarSet barSet4;
15 26
16 27 // Create some test data to chart
17 QList<qreal> data0;
18 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
19 QList<qreal> data1;
20 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
21 QList<qreal> data2;
22 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
23 QList<qreal> data3;
24 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
25 QList<qreal> data4;
26 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
27
28 series0->addData(data0);
29 series0->addData(data1);
30 series0->addData(data2);
31 series0->addData(data3);
32 series0->addData(data4);
28 barSet0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
29 barSet1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
30 barSet2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
31 barSet3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
32 barSet4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
33
34 series0->addBarSet(barSet0);
35 series0->addBarSet(barSet1);
36 series0->addBarSet(barSet2);
37 series0->addBarSet(barSet3);
38 series0->addBarSet(barSet4);
33 39
34 40 ChartWidget* chartWidget = new ChartWidget(&window);
35 41 chartWidget->addSeries(series0);
@@ -31,6 +31,10 int main(int argc, char *argv[])
31 31 series0->addData(data3);
32 32 series0->addData(data4);
33 33
34 QList<QString> labels;
35 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
36 series0->setLabels(labels);
37
34 38 ChartWidget* chartWidget = new ChartWidget(&window);
35 39 chartWidget->addSeries(series0);
36 40
@@ -31,6 +31,10 int main(int argc, char *argv[])
31 31 series0->addData(data3);
32 32 series0->addData(data4);
33 33
34 QList<QString> labels;
35 labels << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "June" << "Jul" << "Aug" << "Sep" << "Nov" << "Dec";
36 series0->setLabels(labels);
37
34 38 ChartWidget* chartWidget = new ChartWidget(&window);
35 39 chartWidget->addSeries(series0);
36 40
@@ -2,12 +2,15
2 2 #include <QVector>
3 3 #include <QDebug>
4 4 #include "barchartmodel_p.h"
5 #include "qbarcategory.h"
6 #include "qbarset.h"
5 7
6 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 9
8 BarChartModel::BarChartModel(QObject *parent) :
10 BarChartModel::BarChartModel(QBarCategory &category, QObject *parent) :
9 11 QObject(parent)
10 12 ,mRunningId(1)
13 ,mCategory(category)
11 14 {
12 15 }
13 16
@@ -41,6 +44,19 void BarChartModel::removeData(int id)
41 44 emit modelUpdated();
42 45 }
43 46
47 void BarChartModel::addBarSet(QBarSet &set)
48 {
49 DataContainer* c = new DataContainer(set.mValues,mRunningId);
50 mDataModel.append(c);
51 mRunningId++;
52 }
53
54 void BarChartModel::removeBarSet(QBarSet &set)
55 {
56 // TODO:
57 }
58
59
44 60 int BarChartModel::countRows()
45 61 {
46 62 // qDebug() << "BarChartModel::countRows";
@@ -9,17 +9,23 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9 // Model for bar chart. Internal class.
10 10 // TODO: Implement as QAbstractItemModel?
11 11
12 class QBarSet;
13 class QBarCategory;
14
12 15 class BarChartModel : public QObject //, public QAbstractItemModel
13 16 {
14 17 Q_OBJECT
15 18 public:
16 explicit BarChartModel(QObject *parent = 0);
19 explicit BarChartModel(QBarCategory &category, QObject *parent = 0);
17 20 ~BarChartModel();
18 21
19 // Adds data to model. returns id.
22 // TODO: remove these after add and remove QBarSet works.
20 23 int addData(QList<qreal> data);
21 24 void removeData(int id);
22 25
26 void addBarSet(QBarSet &set);
27 void removeBarSet(QBarSet &set);
28
23 29 int countRows(); // Number of series in model
24 30 int countColumns(); // Maximum number of items in series
25 31 int countTotalItems(); // Total items in all series. Includes empty items.
@@ -54,6 +60,7 private:
54 60 QList<DataContainer*> mDataModel;
55 61 int mRunningId;
56 62 int mMaxColumns; // longest series in datamodel
63 QBarCategory& mCategory;
57 64
58 65 };
59 66
@@ -3,8 +3,8
3 3
4 4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 5
6 BarChartSeries::BarChartSeries(QObject *parent)
7 : BarChartSeriesBase(parent)
6 BarChartSeries::BarChartSeries(QBarCategory &category, QObject *parent)
7 : BarChartSeriesBase(category, parent)
8 8 {
9 9 }
10 10
@@ -14,7 +14,7 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase
14 14 {
15 15 Q_OBJECT
16 16 public:
17 BarChartSeries(QObject* parent=0);
17 BarChartSeries(QBarCategory &category, QObject* parent=0);
18 18
19 19 // from BarChartSeriesBase
20 20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
@@ -3,12 +3,13
3 3 #include "barchartseriesbase.h"
4 4 #include "bargroup.h"
5 5 #include "barchartmodel_p.h"
6 #include "qbarset.h"
6 7
7 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 9
9 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
10 BarChartSeriesBase::BarChartSeriesBase(QBarCategory &category, QObject *parent)
10 11 : QChartSeries(parent)
11 ,mModel(new BarChartModel(this))
12 ,mModel(new BarChartModel(category, this))
12 13 {
13 14 }
14 15
@@ -27,6 +28,16 void BarChartSeriesBase::setLabels(QList<QString> labels)
27 28 mLabels = labels;
28 29 }
29 30
31 void BarChartSeriesBase::addBarSet(QBarSet &set)
32 {
33 mModel->addBarSet(set);
34 }
35
36 void BarChartSeriesBase::removeBarSet(QBarSet &set)
37 {
38 mModel->removeBarSet(set);
39 }
40
30 41 qreal BarChartSeriesBase::min()
31 42 {
32 43 return mModel->min();
@@ -10,24 +10,30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class BarGroupBase;
12 12 class BarChartModel;
13 class QBarSet;
14 class QBarCategory;
13 15
14 16 // Container for series
15 17 class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries
16 18 {
17 19 Q_OBJECT
18 20 protected:
19 BarChartSeriesBase(QObject* parent=0);
21 // BarChartSeriesBase(QObject* parent=0);
22 BarChartSeriesBase(QBarCategory &category, QObject* parent=0);
20 23
21 24 public:
22 25 // from QChartSeries
23 26 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
24 27
25 // TODO: << operator for convinience
26 // Returns id for vector.
28 // TODO: These 3 will be removed.
27 29 int addData(QList<qreal> data);
28 30 void removeData(int id);
29 31 void setLabels(QList<QString> labels);
30 32
33 // TODO: Expose these to user in derived class instead of here? Common implementation for all bar charts, but not visible to user
34 void addBarSet(QBarSet &set); // Bob[1,5,6,2,15,4] first value goes to category 1 etc..
35 void removeBarSet(QBarSet &set); //
36
31 37 // These shouldn't be visible to chart series user. However, ChartDataSet needs to access them, and friends are evil.
32 38 qreal min();
33 39 qreal max();
@@ -46,7 +52,6 public Q_SLOTS:
46 52 private:
47 53
48 54 BarChartModel* mModel;
49
50 55 QList<QString> mLabels;
51 56 };
52 57
@@ -13,7 +13,7 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
13 13
14 14 void BarGroup::layoutChanged()
15 15 {
16 qDebug() << "BarGroup::layoutChanged";
16 // qDebug() << "BarGroup::layoutChanged";
17 17 // Scale bars to new layout
18 18 // Layout for bars:
19 19 if (mModel.countRows() <= 0) {
@@ -6,8 +6,8
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 PercentBarChartSeries::PercentBarChartSeries(QObject *parent) :
10 BarChartSeriesBase(parent)
9 PercentBarChartSeries::PercentBarChartSeries(QBarCategory &category, QObject *parent) :
10 BarChartSeriesBase(category, parent)
11 11 {
12 12 }
13 13
@@ -13,7 +13,7 class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase
13 13 {
14 14 Q_OBJECT
15 15 public:
16 PercentBarChartSeries(QObject* parent=0);
16 PercentBarChartSeries(QBarCategory &category, QObject* parent=0);
17 17
18 18 // from BarChartSeriesBase
19 19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
@@ -4,8 +4,8
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 StackedBarChartSeries::StackedBarChartSeries(QObject *parent) :
8 BarChartSeriesBase(parent)
7 StackedBarChartSeries::StackedBarChartSeries(QBarCategory &category, QObject *parent) :
8 BarChartSeriesBase(category, parent)
9 9 {
10 10 }
11 11
@@ -13,7 +13,7 class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase
13 13 {
14 14 Q_OBJECT
15 15 public:
16 StackedBarChartSeries(QObject* parent=0);
16 StackedBarChartSeries(QBarCategory &category, QObject* parent=0);
17 17
18 18 // from QChartSeries
19 19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
@@ -13,7 +13,7 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *p
13 13
14 14 void StackedBarGroup::layoutChanged()
15 15 {
16 qDebug() << "StackedBarGroup::layoutChanged";
16 // qDebug() << "StackedBarGroup::layoutChanged";
17 17 // Scale bars to new layout
18 18 // Layout for bars:
19 19 if (mModel.countRows() <= 0) {
@@ -44,15 +44,15 QChartSeries* QChart::createSeries(QChartSeries::QChartSeriesType type)
44 44 break;
45 45 }
46 46 case QChartSeries::SeriesTypeBar: {
47 series = new BarChartSeries(this);
47 //series = new BarChartSeries(this);
48 48 break;
49 49 }
50 50 case QChartSeries::SeriesTypeStackedBar: {
51 series = new StackedBarChartSeries(this);
51 //series = new StackedBarChartSeries(this);
52 52 break;
53 53 }
54 54 case QChartSeries::SeriesTypePercentBar: {
55 series = new PercentBarChartSeries(this);
55 //series = new PercentBarChartSeries(this);
56 56 break;
57 57 }
58 58 case QChartSeries::SeriesTypeScatter: {
@@ -18,6 +18,8 SOURCES += barchart/barchartseries.cpp \
18 18 barchart/separator.cpp \
19 19 barchart/bargroupbase.cpp \
20 20 barchart/barchartseriesbase.cpp \
21 barchart/qbarset.cpp \
22 barchart/qbarcategory.cpp \
21 23 linechart/linechartanimationitem.cpp \
22 24 linechart/linechartitem.cpp \
23 25 linechart/qlinechartseries.cpp \
@@ -56,6 +58,8 PUBLIC_HEADERS += linechart/qlinechartseries.h \
56 58 barchart/percentbargroup.h \
57 59 barchart/barchartseriesbase.h \
58 60 barchart/bargroupbase.h \
61 barchart/qbarset.h \
62 barchart/qbarcategory.h \
59 63 qchartseries.h \
60 64 qscatterseries.h \
61 65 qchart.h \
@@ -211,11 +211,11 void MainWidget::addSeries(QString series, QString data)
211 211 }
212 212 } else if (data == "Table, 5 series"){
213 213 // Create some test data to chart
214 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10;
215 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0;
216 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1;
217 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5;
218 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10;
214 data0 << 1 << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 5;
215 data1 << 5 << 0 << 0 << 4 << 0 << 7 << 8 << 9 << 9 << 0 << 4;
216 data2 << 3 << 5 << 8 << 13 << 8 << 5 << 3 << 2 << 1 << 1 << 3;
217 data3 << 5 << 6 << 7 << 3 << 4 << 5 << 8 << 9 << 10 << 5 << 2;
218 data4 << 9 << 7 << 5 << 3 << 1 << 2 << 4 << 6 << 8 << 10 << 1;
219 219 } else {
220 220 // TODO: check if data has a valid file name
221 221 Q_ASSERT(false);
General Comments 0
You need to be logged in to leave comments. Login now