##// 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 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
8 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
9 : QChartSeries(parent)
9 : QChartSeries(parent)
10 ,mData(0)
10 {
11 {
11 }
12 }
12
13 /*
13 bool BarChartSeriesBase::setData(QAbstractItemModel* model)
14 bool BarChartSeriesBase::setModel(QAbstractItemModel* model)
14 {
15 {
15 mModel = model;
16 mModel = model;
16 return true;
17 return true;
17 }
18 }
18
19 */
20 bool BarChartSeriesBase::setData(QList<qreal>& data)
21 {
22 mData = &data;
23 return true;
24 }
25 /*
19 int BarChartSeriesBase::min()
26 int BarChartSeriesBase::min()
20 {
27 {
21 Q_ASSERT(mModel->rowCount() > 0);
28 Q_ASSERT(mModel->rowCount() > 0);
@@ -103,6 +110,48 int BarChartSeriesBase::columnSum(int column)
103 }
110 }
104 return sum;
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 #include "moc_barchartseriesbase.cpp"
156 #include "moc_barchartseriesbase.cpp"
108
157
@@ -22,19 +22,25 public:
22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
22 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
23
23
24 // TODO: Better data model?
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 // Methods to find out minimum and maximum values of data
28 // Methods to find out minimum and maximum values of data
28 int min();
29 // int min(); // TODO: remove
29 int max();
30 // int max(); // TODO: remove
30 int maxColumnSum(); // returns maximum sum of items in all columns.
31 // int maxColumnSum(); // TODO: move to model. returns maximum sum of items in all columns.
31
32
32 int countRows();
33 // int countRows(); // TODO: remove.
33 int countColumns(); // Count items in one series.
34 // int countColumns(); // TODO: remove. Count items in one series.
34 int countTotalItems();
35 // int countTotalItems(); // TODO: move to model
35 int valueAt(int row, int column);
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 public Q_SLOTS:
45 public Q_SLOTS:
40
46
@@ -42,6 +48,9 private:
42
48
43 QAbstractItemModel* mModel;
49 QAbstractItemModel* mModel;
44 BarGroupBase* mBarGroup;
50 BarGroupBase* mBarGroup;
51
52 QList<qreal>* mData;
53
45 };
54 };
46
55
47 QTCOMMERCIALCHART_END_NAMESPACE
56 QTCOMMERCIALCHART_END_NAMESPACE
@@ -15,45 +15,50 void BarGroup::layoutChanged()
15 {
15 {
16 // Scale bars to new layout
16 // Scale bars to new layout
17 // Layout for bars:
17 // Layout for bars:
18 /*
18 if (mSeries.countRows() <= 0) {
19 if (mSeries.countRows() <= 0) {
19 // Nothing to do.
20 // Nothing to do.
20 return;
21 return;
21 }
22 }
23 */
24 if (mModel.countSeries() <= 0) {
25 return;
26 }
22
27
23 // TODO: better way to auto-layout?
28 // TODO: better way to auto-layout?
24 // Use reals for accurancy (we might get some compiler warnings... :)
29 // Use reals for accurancy (we might get some compiler warnings... :)
25 int columnCount = mSeries.countColumns();
30 int itemCount = mModel.countItemsInSeries();
26 int rowCount = mSeries.countRows();
31 int seriesCount = mModel.countSeries();
27
32
28 qreal tW = mWidth;
33 qreal tW = mWidth;
29 qreal tH = mHeight;
34 qreal tH = mHeight;
30 qreal tM = mMax;
35 qreal tM = mMax;
31 qreal scale = (tH/tM);
36 qreal scale = (tH/tM);
32
37
33 qreal tC = columnCount+1;
38 qreal tC = itemCount+1;
34 qreal xStepPerSeries = (tW/tC);
39 qreal xStepPerSeries = (tW/tC);
35
40
36 // Scaling.
41 // Scaling.
37 int itemIndex(0);
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++) {
45 for (int item=0; item < itemCount; item++) {
41 qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
46 qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
42 qreal yPos = mHeight;
47 qreal yPos = mHeight;
43 for (int row = 0; row < rowCount; row++) {
48 for (int series = 0; series < seriesCount; series++) {
44 qreal barHeight = mSeries.valueAt(row, column) * scale;
49 qreal barHeight = mModel.valueAt(series, item) * scale;
45 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
50 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
46
51
47 // TODO: width settable per bar?
52 // TODO: width settable per bar?
48 bar->resize(mBarDefaultWidth, barHeight);
53 bar->resize(mBarDefaultWidth, barHeight);
49 bar->setColor(mColors.at(row));
54 bar->setColor(mColors.at(series));
50 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
55 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
51 itemIndex++;
56 itemIndex++;
52 xPos += mBarDefaultWidth;
57 xPos += mBarDefaultWidth;
53 }
58 }
54
59
55 // TODO: Layout for labels, remove magic number
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 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
62 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
58 label->setPos(xPos, mHeight + 20);
63 label->setPos(xPos, mHeight + 20);
59 labelIndex++;
64 labelIndex++;
@@ -9,13 +9,14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent)
10 BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent)
11 : ChartItem(parent)
11 : ChartItem(parent)
12 ,mSeries(series)
12 // ,mSeries(series)
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
13 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 ,mLayoutSet(false)
14 ,mLayoutSet(false)
15 ,mLayoutDirty(true)
15 ,mLayoutDirty(true)
16 ,mTheme(0)
16 ,mTheme(0)
17 ,mSeparatorsVisible(true)
17 ,mSeparatorsVisible(true)
18 {
18 {
19 mModel.addSeries(series);
19 dataChanged();
20 dataChanged();
20 }
21 }
21
22
@@ -69,9 +70,10 void BarGroupBase::dataChanged()
69 {
70 {
70 qDebug() << "BarGroupBase::dataChanged";
71 qDebug() << "BarGroupBase::dataChanged";
71
72
72 // Find out maximum and minimum of all series
73 // Find out maximum and minimum of all series.
73 mMax = mSeries.max();
74 // TODO: is this actually needed?
74 mMin = mSeries.min();
75 // mMax = mModel.max();
76 // mMin = mModel.min();
75
77
76 // Delete old bars
78 // Delete old bars
77 foreach (QGraphicsItem* item, childItems()) {
79 foreach (QGraphicsItem* item, childItems()) {
@@ -79,14 +81,14 void BarGroupBase::dataChanged()
79 }
81 }
80
82
81 // Create new graphic items for bars
83 // Create new graphic items for bars
82 int totalItems = mSeries.countTotalItems();
84 int totalItems = mModel.countTotalItems(); // mSeries.countTotalItems();
83 for (int i=0; i<totalItems; i++) {
85 for (int i=0; i<totalItems; i++) {
84 Bar *bar = new Bar(this);
86 Bar *bar = new Bar(this);
85 childItems().append(bar);
87 childItems().append(bar);
86 }
88 }
87
89
88 // TODO: labels from series. This creates just some example labels
90 // TODO: labels from series. This creates just some example labels
89 int count = mSeries.countColumns();
91 int count = mModel.countItemsInSeries(); // mSeries.countColumns();
90 for (int i=0; i<count; i++) {
92 for (int i=0; i<count; i++) {
91 BarLabel* label = new BarLabel(this);
93 BarLabel* label = new BarLabel(this);
92 QString text("Label " + QString::number(i));
94 QString text("Label " + QString::number(i));
@@ -94,7 +96,7 void BarGroupBase::dataChanged()
94 childItems().append(label);
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 for (int i=0; i<count; i++) {
100 for (int i=0; i<count; i++) {
99 Separator* sep = new Separator(this);
101 Separator* sep = new Separator(this);
100 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
102 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
@@ -6,17 +6,18
6 //#include "barlabel_p.h"
6 //#include "barlabel_p.h"
7 //#include "bar_p.h"
7 //#include "bar_p.h"
8 #include "barchartseriesbase.h"
8 #include "barchartseriesbase.h"
9 #include "barchartmodel_p.h"
9 #include <QGraphicsItem>
10 #include <QGraphicsItem>
10
11
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
13
13 // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated.
14 // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated.
14
15 class BarGroupBase : public QObject, public ChartItem
15 class BarGroupBase : public QObject, public ChartItem
16 {
16 {
17 Q_OBJECT
17 Q_OBJECT
18 public:
18 public:
19 BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0);
19 BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0);
20 // BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0);
20 void setSeparatorsVisible(bool visible = true);
21 void setSeparatorsVisible(bool visible = true);
21
22
22 public: // From ChartItem
23 public: // From ChartItem
@@ -46,9 +47,10 protected slots:
46
47
47 protected:
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 int mMax;
54 int mMax;
53
55
54 int mHeight; // Layout spesific
56 int mHeight; // Layout spesific
@@ -63,6 +65,8 protected:
63 ChartTheme* mTheme;
65 ChartTheme* mTheme;
64 bool mSeparatorsVisible;
66 bool mSeparatorsVisible;
65
67
68 BarChartModel mModel;
69
66 };
70 };
67
71
68 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
@@ -14,6 +14,7 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *p
14
14
15 void PercentBarGroup::layoutChanged()
15 void PercentBarGroup::layoutChanged()
16 {
16 {
17 /*
17 // Scale bars to new layout
18 // Scale bars to new layout
18 // Layout for bars:
19 // Layout for bars:
19 if (mSeries.countRows() <= 0) {
20 if (mSeries.countRows() <= 0) {
@@ -66,7 +67,7 void PercentBarGroup::layoutChanged()
66 xPos += xStep;
67 xPos += xStep;
67 separatorIndex++;
68 separatorIndex++;
68 }
69 }
69
70 */
70 mLayoutDirty = true;
71 mLayoutDirty = true;
71 }
72 }
72
73
@@ -14,6 +14,7 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *p
14
14
15 void StackedBarGroup::layoutChanged()
15 void StackedBarGroup::layoutChanged()
16 {
16 {
17 /*
17 // Scale bars to new layout
18 // Scale bars to new layout
18 // Layout for bars:
19 // Layout for bars:
19 if (mSeries.countRows() <= 0) {
20 if (mSeries.countRows() <= 0) {
@@ -74,7 +75,7 void StackedBarGroup::layoutChanged()
74 xPos += xStep;
75 xPos += xStep;
75 separatorIndex++;
76 separatorIndex++;
76 }
77 }
77
78 */
78 mLayoutDirty = true;
79 mLayoutDirty = true;
79 }
80 }
80
81
@@ -14,12 +14,13 SOURCES += barchart/barchartseries.cpp \
14 barchart/percentbarchartseries.cpp \
14 barchart/percentbarchartseries.cpp \
15 barchart/percentbargroup.cpp \
15 barchart/percentbargroup.cpp \
16 barchart/barlabel.cpp \
16 barchart/barlabel.cpp \
17 linechart/linechartanimationitem.cpp \
17 barchart/barchartmodel.cpp \
18 linechart/linechartitem.cpp \
19 linechart/qlinechartseries.cpp \
20 barchart/separator.cpp \
18 barchart/separator.cpp \
21 barchart/bargroupbase.cpp \
19 barchart/bargroupbase.cpp \
22 barchart/barchartseriesbase.cpp \
20 barchart/barchartseriesbase.cpp \
21 linechart/linechartanimationitem.cpp \
22 linechart/linechartitem.cpp \
23 linechart/qlinechartseries.cpp \
23 qscatterseries.cpp \
24 qscatterseries.cpp \
24 #scatterpresentation.cpp \
25 #scatterpresentation.cpp \
25 qchart.cpp \
26 qchart.cpp \
@@ -37,6 +38,7 PRIVATE_HEADERS += linechart/linechartitem_p.h \
37 barchart/barlabel_p.h \
38 barchart/barlabel_p.h \
38 barchart/bar_p.h \
39 barchart/bar_p.h \
39 barchart/separator_p.h \
40 barchart/separator_p.h \
41 barchart/barchartmodel_p.h \
40 qscatterseries_p.h \
42 qscatterseries_p.h \
41 #scatterpresentation.h \
43 #scatterpresentation.h \
42 axisitem_p.h \
44 axisitem_p.h \
General Comments 0
You need to be logged in to leave comments. Login now