##// END OF EJS Templates
model prototyping for bar chart
sauimone -
r159:e1dfbbf1ecba
parent child
Show More
@@ -0,0 +1,99
1 #include <limits.h>
2 #include "barchartmodel_p.h"
3
4 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5
6 BarChartModel::BarChartModel(QObject *parent) :
7 QObject(parent)
8 {
9 }
10
11 void BarChartModel::addSeries(BarChartSeriesBase& series)
12 {
13 mSeries.append(&series);
14 emit modelUpdated();
15 }
16
17 void BarChartModel::removeSeries(BarChartSeriesBase& series)
18 {
19 int i = mSeries.indexOf(&series);
20 if (-1 == i) {
21 return;
22 }
23 mSeries.removeAt(i);
24 emit modelUpdated();
25 }
26
27 int BarChartModel::countSeries()
28 {
29 return mSeries.count();
30 }
31
32 int BarChartModel::countItemsInSeries()
33 {
34 int count(0);
35 for (int i=0; i<mSeries.count(); i++){
36 // TODO: can we assume that all series have same number of items? If not. then which items are empty.
37 int temp = mSeries.at(i)->countItems();
38 if (temp > count) {
39 count = temp;
40 }
41 }
42 return count;
43 }
44
45 int BarChartModel::countTotalItems()
46 {
47 int total = mSeries.count() * countItemsInSeries();
48 return total;
49 }
50
51 int BarChartModel::min()
52 {
53 Q_ASSERT(mSeries.count() > 0);
54 // TODO: make min and max members and update them when data changes.
55 // This is slower since they are checked every time, even if data is same since previous call.
56 int min = INT_MAX;
57
58 for (int i=0; i <mSeries.count(); i++) {
59 int temp = mSeries.at(i)->min();
60 if (temp < min) {
61 min = temp;
62 }
63 }
64 return min;
65 }
66
67 int BarChartModel::max()
68 {
69 Q_ASSERT(mSeries.count() > 0);
70
71 // TODO: make min and max members and update them when data changes.
72 // This is slower since they are checked every time, even if data is same since previous call.
73 int max = INT_MIN;
74
75 for (int i=0; i <mSeries.count(); i++) {
76 int temp = mSeries.at(i)->min();
77 if (temp > max) {
78 max = temp;
79 }
80 }
81 return max;
82 }
83
84 qreal BarChartModel::valueAt(int series, int item)
85 {
86 if ((series < 0) || (series >= mSeries.count())) {
87 // No series, no value.
88 return 0;
89 } else if ((item < 0) || (item >= mSeries.at(series)->countItems())) {
90 // No item, no value.
91 return 0;
92 }
93
94 return mSeries.at(series)->valueAt(item);
95 }
96
97 #include "moc_barchartmodel_p.cpp"
98
99 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,45
1 #ifndef BARCHARTMODEL_H
2 #define BARCHARTMODEL_H
3
4 #include <QObject>
5 #include "barchartseries.h"
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
9 // Model for bar chart.
10 // TODO: Implement as QAbstractItemModel?
11
12 class BarChartModel : public QObject //, public QAbstractItemModel
13 {
14 Q_OBJECT
15 public:
16 explicit BarChartModel(QObject *parent = 0);
17
18 // Takes reference, Series are owned by QChart or model?
19 void addSeries(BarChartSeriesBase& series);
20 void removeSeries(BarChartSeriesBase& series);
21
22 int countSeries(); // Number of series in model
23 int countItemsInSeries(); // Maximum number of items in series
24 int countTotalItems(); // Total items in all series. Includes empty items.
25
26 int max(); // Maximum value of all series
27 int min(); // Minimum value of all series
28 qreal valueAt(int series, int item);
29
30 signals:
31 void modelUpdated();
32
33 public slots:
34
35 private:
36
37 // Data
38 QList<BarChartSeriesBase*> mSeries;
39
40 BarChartModel* mSingle;
41 };
42
43 QTCOMMERCIALCHART_END_NAMESPACE
44
45 #endif // BARCHARTMODEL_H
@@ -7,15 +7,22 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
9 9 : QChartSeries(parent)
10 ,mData(0)
10 11 {
11 12 }
12
13 bool BarChartSeriesBase::setData(QAbstractItemModel* model)
13 /*
14 bool BarChartSeriesBase::setModel(QAbstractItemModel* model)
14 15 {
15 16 mModel = model;
16 17 return true;
17 18 }
18
19 */
20 bool BarChartSeriesBase::setData(QList<qreal>& data)
21 {
22 mData = &data;
23 return true;
24 }
25 /*
19 26 int BarChartSeriesBase::min()
20 27 {
21 28 Q_ASSERT(mModel->rowCount() > 0);
@@ -103,6 +110,48 int BarChartSeriesBase::columnSum(int column)
103 110 }
104 111 return sum;
105 112 }
113 */
114 qreal BarChartSeriesBase::min()
115 {
116 Q_ASSERT(mData != 0);
117
118 int count = mData->count();
119 int min = INT_MAX;
120
121 for (int i=0; i<count; i++) {
122 if (mData->at(i) < min) {
123 min = mData->at(i);
124 }
125 }
126 return min;
127 }
128
129 qreal BarChartSeriesBase::max()
130 {
131 Q_ASSERT(mData != 0);
132
133 int count = mData->count();
134 int max = INT_MIN;
135
136 for (int i=0; i<count; i++) {
137 if (mData->at(i) > max) {
138 max = mData->at(i);
139 }
140 }
141 return max;
142 }
143
144 int BarChartSeriesBase::countItems()
145 {
146 Q_ASSERT(mData != 0);
147 return mData->count();
148 }
149
150 qreal BarChartSeriesBase::valueAt(int item)
151 {
152 Q_ASSERT(mData != 0);
153 return mData->at(item);
154 }
106 155
107 156 #include "moc_barchartseriesbase.cpp"
108 157
@@ -22,19 +22,25 public:
22 22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
23 23
24 24 // TODO: Better data model?
25 virtual bool setData(QAbstractItemModel* model);
25 // virtual bool setModel(QAbstractItemModel* model);
26 virtual bool setData(QList<qreal>& data);
26 27
27 28 // Methods to find out minimum and maximum values of data
28 int min();
29 int max();
30 int maxColumnSum(); // returns maximum sum of items in all columns.
29 // int min(); // TODO: remove
30 // int max(); // TODO: remove
31 // int maxColumnSum(); // TODO: move to model. returns maximum sum of items in all columns.
31 32
32 int countRows();
33 int countColumns(); // Count items in one series.
34 int countTotalItems();
35 int valueAt(int row, int column);
33 // int countRows(); // TODO: remove.
34 // int countColumns(); // TODO: remove. Count items in one series.
35 // int countTotalItems(); // TODO: move to model
36 // int valueAt(int row, int column); // TODO: move to model
36 37
37 int columnSum(int column);
38 // int columnSum(int column); // TODO: move to model
39
40 qreal min();
41 qreal max();
42 int countItems();
43 qreal valueAt(int item);
38 44
39 45 public Q_SLOTS:
40 46
@@ -42,6 +48,9 private:
42 48
43 49 QAbstractItemModel* mModel;
44 50 BarGroupBase* mBarGroup;
51
52 QList<qreal>* mData;
53
45 54 };
46 55
47 56 QTCOMMERCIALCHART_END_NAMESPACE
@@ -15,45 +15,50 void BarGroup::layoutChanged()
15 15 {
16 16 // Scale bars to new layout
17 17 // Layout for bars:
18 /*
18 19 if (mSeries.countRows() <= 0) {
19 20 // Nothing to do.
20 21 return;
21 22 }
23 */
24 if (mModel.countSeries() <= 0) {
25 return;
26 }
22 27
23 28 // TODO: better way to auto-layout?
24 29 // Use reals for accurancy (we might get some compiler warnings... :)
25 int columnCount = mSeries.countColumns();
26 int rowCount = mSeries.countRows();
30 int itemCount = mModel.countItemsInSeries();
31 int seriesCount = mModel.countSeries();
27 32
28 33 qreal tW = mWidth;
29 34 qreal tH = mHeight;
30 35 qreal tM = mMax;
31 36 qreal scale = (tH/tM);
32 37
33 qreal tC = columnCount+1;
38 qreal tC = itemCount+1;
34 39 qreal xStepPerSeries = (tW/tC);
35 40
36 41 // Scaling.
37 42 int itemIndex(0);
38 int labelIndex = mSeries.countColumns() * mSeries.countRows();
43 int labelIndex = itemCount * seriesCount;
39 44
40 for (int column=0; column < columnCount; column++) {
41 qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
45 for (int item=0; item < itemCount; item++) {
46 qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
42 47 qreal yPos = mHeight;
43 for (int row = 0; row < rowCount; row++) {
44 qreal barHeight = mSeries.valueAt(row, column) * scale;
48 for (int series = 0; series < seriesCount; series++) {
49 qreal barHeight = mModel.valueAt(series, item) * scale;
45 50 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
46 51
47 52 // TODO: width settable per bar?
48 53 bar->resize(mBarDefaultWidth, barHeight);
49 bar->setColor(mColors.at(row));
54 bar->setColor(mColors.at(series));
50 55 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
51 56 itemIndex++;
52 57 xPos += mBarDefaultWidth;
53 58 }
54 59
55 60 // TODO: Layout for labels, remove magic number
56 xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
61 xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
57 62 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
58 63 label->setPos(xPos, mHeight + 20);
59 64 labelIndex++;
@@ -9,13 +9,14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent)
11 11 : ChartItem(parent)
12 ,mSeries(series)
12 // ,mSeries(series)
13 13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 14 ,mLayoutSet(false)
15 15 ,mLayoutDirty(true)
16 16 ,mTheme(0)
17 17 ,mSeparatorsVisible(true)
18 18 {
19 mModel.addSeries(series);
19 20 dataChanged();
20 21 }
21 22
@@ -69,9 +70,10 void BarGroupBase::dataChanged()
69 70 {
70 71 qDebug() << "BarGroupBase::dataChanged";
71 72
72 // Find out maximum and minimum of all series
73 mMax = mSeries.max();
74 mMin = mSeries.min();
73 // Find out maximum and minimum of all series.
74 // TODO: is this actually needed?
75 // mMax = mModel.max();
76 // mMin = mModel.min();
75 77
76 78 // Delete old bars
77 79 foreach (QGraphicsItem* item, childItems()) {
@@ -79,14 +81,14 void BarGroupBase::dataChanged()
79 81 }
80 82
81 83 // Create new graphic items for bars
82 int totalItems = mSeries.countTotalItems();
84 int totalItems = mModel.countTotalItems(); // mSeries.countTotalItems();
83 85 for (int i=0; i<totalItems; i++) {
84 86 Bar *bar = new Bar(this);
85 87 childItems().append(bar);
86 88 }
87 89
88 90 // TODO: labels from series. This creates just some example labels
89 int count = mSeries.countColumns();
91 int count = mModel.countItemsInSeries(); // mSeries.countColumns();
90 92 for (int i=0; i<count; i++) {
91 93 BarLabel* label = new BarLabel(this);
92 94 QString text("Label " + QString::number(i));
@@ -94,7 +96,7 void BarGroupBase::dataChanged()
94 96 childItems().append(label);
95 97 }
96 98
97 count = mSeries.countColumns() - 1; // There is one less separator than columns
99 count = mModel.countItemsInSeries() - 1; // mSeries.countColumns() - 1; // There is one less separator than columns
98 100 for (int i=0; i<count; i++) {
99 101 Separator* sep = new Separator(this);
100 102 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
@@ -6,17 +6,18
6 6 //#include "barlabel_p.h"
7 7 //#include "bar_p.h"
8 8 #include "barchartseriesbase.h"
9 #include "barchartmodel_p.h"
9 10 #include <QGraphicsItem>
10 11
11 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 13
13 14 // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated.
14
15 15 class BarGroupBase : public QObject, public ChartItem
16 16 {
17 17 Q_OBJECT
18 18 public:
19 19 BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0);
20 // BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0);
20 21 void setSeparatorsVisible(bool visible = true);
21 22
22 23 public: // From ChartItem
@@ -46,9 +47,10 protected slots:
46 47
47 48 protected:
48 49
49 BarChartSeriesBase& mSeries;
50 //BarChartSeriesBase& mSeries;
50 51
51 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
52 // TODO: consider these.
53 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
52 54 int mMax;
53 55
54 56 int mHeight; // Layout spesific
@@ -63,6 +65,8 protected:
63 65 ChartTheme* mTheme;
64 66 bool mSeparatorsVisible;
65 67
68 BarChartModel mModel;
69
66 70 };
67 71
68 72 QTCOMMERCIALCHART_END_NAMESPACE
@@ -14,6 +14,7 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *p
14 14
15 15 void PercentBarGroup::layoutChanged()
16 16 {
17 /*
17 18 // Scale bars to new layout
18 19 // Layout for bars:
19 20 if (mSeries.countRows() <= 0) {
@@ -66,7 +67,7 void PercentBarGroup::layoutChanged()
66 67 xPos += xStep;
67 68 separatorIndex++;
68 69 }
69
70 */
70 71 mLayoutDirty = true;
71 72 }
72 73
@@ -14,6 +14,7 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *p
14 14
15 15 void StackedBarGroup::layoutChanged()
16 16 {
17 /*
17 18 // Scale bars to new layout
18 19 // Layout for bars:
19 20 if (mSeries.countRows() <= 0) {
@@ -74,7 +75,7 void StackedBarGroup::layoutChanged()
74 75 xPos += xStep;
75 76 separatorIndex++;
76 77 }
77
78 */
78 79 mLayoutDirty = true;
79 80 }
80 81
@@ -14,12 +14,13 SOURCES += barchart/barchartseries.cpp \
14 14 barchart/percentbarchartseries.cpp \
15 15 barchart/percentbargroup.cpp \
16 16 barchart/barlabel.cpp \
17 linechart/linechartanimationitem.cpp \
18 linechart/linechartitem.cpp \
19 linechart/qlinechartseries.cpp \
17 barchart/barchartmodel.cpp \
20 18 barchart/separator.cpp \
21 19 barchart/bargroupbase.cpp \
22 20 barchart/barchartseriesbase.cpp \
21 linechart/linechartanimationitem.cpp \
22 linechart/linechartitem.cpp \
23 linechart/qlinechartseries.cpp \
23 24 qscatterseries.cpp \
24 25 #scatterpresentation.cpp \
25 26 qchart.cpp \
@@ -37,6 +38,7 PRIVATE_HEADERS += linechart/linechartitem_p.h \
37 38 barchart/barlabel_p.h \
38 39 barchart/bar_p.h \
39 40 barchart/separator_p.h \
41 barchart/barchartmodel_p.h \
40 42 qscatterseries_p.h \
41 43 #scatterpresentation.h \
42 44 axisitem_p.h \
General Comments 0
You need to be logged in to leave comments. Login now