##// END OF EJS Templates
model delegate for bar series. updated examples
sauimone -
r161:cc2daab2560d
parent child
Show More
@@ -14,67 +14,22 int main(int argc, char *argv[])
14 14 BarChartSeries* series0 = new BarChartSeries();
15 15
16 16 // Create some test data to chart
17 QStandardItemModel dataModel(5,10);
18 QModelIndex index;
19 index = dataModel.index(0,0);
20
21 // Series 0
22 dataModel.setData(dataModel.index(0,0),1);
23 dataModel.setData(dataModel.index(0,1),2);
24 dataModel.setData(dataModel.index(0,2),3);
25 dataModel.setData(dataModel.index(0,3),4);
26 dataModel.setData(dataModel.index(0,4),5);
27 dataModel.setData(dataModel.index(0,5),6);
28 dataModel.setData(dataModel.index(0,6),7);
29 dataModel.setData(dataModel.index(0,7),8);
30 dataModel.setData(dataModel.index(0,8),9);
31 dataModel.setData(dataModel.index(0,9),10);
32
33 // Series 1, some other items missing
34 dataModel.setData(dataModel.index(1,0),5);
35 dataModel.setData(dataModel.index(1,3),4);
36 dataModel.setData(dataModel.index(1,5),7);
37 dataModel.setData(dataModel.index(1,6),8);
38 dataModel.setData(dataModel.index(1,8),9);
39 dataModel.setData(dataModel.index(1,9),9);
40
41 // Series 2
42 dataModel.setData(dataModel.index(2,0),3);
43 dataModel.setData(dataModel.index(2,1),5);
44 dataModel.setData(dataModel.index(2,2),8);
45 dataModel.setData(dataModel.index(2,3),13);
46 dataModel.setData(dataModel.index(2,4),8);
47 dataModel.setData(dataModel.index(2,5),5);
48 dataModel.setData(dataModel.index(2,6),3);
49 dataModel.setData(dataModel.index(2,7),2);
50 dataModel.setData(dataModel.index(2,8),1);
51 dataModel.setData(dataModel.index(2,9),1);
52
53 // Series 3
54 dataModel.setData(dataModel.index(3,0),5);
55 dataModel.setData(dataModel.index(3,1),6);
56 dataModel.setData(dataModel.index(3,2),7);
57 dataModel.setData(dataModel.index(3,3),3);
58 dataModel.setData(dataModel.index(3,4),4);
59 dataModel.setData(dataModel.index(3,5),5);
60 dataModel.setData(dataModel.index(3,6),8);
61 dataModel.setData(dataModel.index(3,7),9);
62 dataModel.setData(dataModel.index(3,8),10);
63 dataModel.setData(dataModel.index(3,9),5);
64
65 // Series 4
66 dataModel.setData(dataModel.index(4,0),9);
67 dataModel.setData(dataModel.index(4,1),7);
68 dataModel.setData(dataModel.index(4,2),5);
69 dataModel.setData(dataModel.index(4,3),3);
70 dataModel.setData(dataModel.index(4,4),1);
71 dataModel.setData(dataModel.index(4,5),2);
72 dataModel.setData(dataModel.index(4,6),4);
73 dataModel.setData(dataModel.index(4,7),6);
74 dataModel.setData(dataModel.index(4,8),8);
75 dataModel.setData(dataModel.index(4,9),10);
76
77 series0->setData(&dataModel);
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);
78 33
79 34 ChartWidget* chartWidget = new ChartWidget(&window);
80 35 chartWidget->addSeries(series0);
@@ -14,67 +14,22 int main(int argc, char *argv[])
14 14 PercentBarChartSeries* series0 = new PercentBarChartSeries();
15 15
16 16 // Create some test data to chart
17 QStandardItemModel dataModel(5,10);
18 QModelIndex index;
19 index = dataModel.index(0,0);
20
21 // Series 0
22 dataModel.setData(dataModel.index(0,0),1);
23 dataModel.setData(dataModel.index(0,1),2);
24 dataModel.setData(dataModel.index(0,2),3);
25 dataModel.setData(dataModel.index(0,3),4);
26 dataModel.setData(dataModel.index(0,4),5);
27 dataModel.setData(dataModel.index(0,5),6);
28 dataModel.setData(dataModel.index(0,6),7);
29 dataModel.setData(dataModel.index(0,7),8);
30 dataModel.setData(dataModel.index(0,8),9);
31 dataModel.setData(dataModel.index(0,9),10);
32
33 // Series 1, some other items missing
34 dataModel.setData(dataModel.index(1,0),5);
35 dataModel.setData(dataModel.index(1,3),4);
36 dataModel.setData(dataModel.index(1,5),7);
37 dataModel.setData(dataModel.index(1,6),8);
38 dataModel.setData(dataModel.index(1,8),9);
39 dataModel.setData(dataModel.index(1,9),9);
40
41 // Series 2
42 dataModel.setData(dataModel.index(2,0),3);
43 dataModel.setData(dataModel.index(2,1),5);
44 dataModel.setData(dataModel.index(2,2),8);
45 dataModel.setData(dataModel.index(2,3),13);
46 dataModel.setData(dataModel.index(2,4),8);
47 dataModel.setData(dataModel.index(2,5),5);
48 dataModel.setData(dataModel.index(2,6),3);
49 dataModel.setData(dataModel.index(2,7),2);
50 dataModel.setData(dataModel.index(2,8),1);
51 dataModel.setData(dataModel.index(2,9),1);
52
53 // Series 3
54 dataModel.setData(dataModel.index(3,0),5);
55 dataModel.setData(dataModel.index(3,1),6);
56 dataModel.setData(dataModel.index(3,2),7);
57 dataModel.setData(dataModel.index(3,3),3);
58 dataModel.setData(dataModel.index(3,4),4);
59 dataModel.setData(dataModel.index(3,5),5);
60 dataModel.setData(dataModel.index(3,6),8);
61 dataModel.setData(dataModel.index(3,7),9);
62 dataModel.setData(dataModel.index(3,8),10);
63 dataModel.setData(dataModel.index(3,9),5);
64
65 // Series 4
66 dataModel.setData(dataModel.index(4,0),9);
67 dataModel.setData(dataModel.index(4,1),7);
68 dataModel.setData(dataModel.index(4,2),5);
69 dataModel.setData(dataModel.index(4,3),3);
70 dataModel.setData(dataModel.index(4,4),1);
71 dataModel.setData(dataModel.index(4,5),2);
72 dataModel.setData(dataModel.index(4,6),4);
73 dataModel.setData(dataModel.index(4,7),6);
74 dataModel.setData(dataModel.index(4,8),8);
75 dataModel.setData(dataModel.index(4,9),10);
76
77 series0->setData(&dataModel);
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);
78 33
79 34 ChartWidget* chartWidget = new ChartWidget(&window);
80 35 chartWidget->addSeries(series0);
@@ -14,67 +14,22 int main(int argc, char *argv[])
14 14 StackedBarChartSeries* series0 = new StackedBarChartSeries();
15 15
16 16 // Create some test data to chart
17 QStandardItemModel dataModel(5,10);
18 QModelIndex index;
19 index = dataModel.index(0,0);
20
21 // Series 0
22 dataModel.setData(dataModel.index(0,0),1);
23 dataModel.setData(dataModel.index(0,1),2);
24 dataModel.setData(dataModel.index(0,2),3);
25 dataModel.setData(dataModel.index(0,3),4);
26 dataModel.setData(dataModel.index(0,4),5);
27 dataModel.setData(dataModel.index(0,5),6);
28 dataModel.setData(dataModel.index(0,6),7);
29 dataModel.setData(dataModel.index(0,7),8);
30 dataModel.setData(dataModel.index(0,8),9);
31 dataModel.setData(dataModel.index(0,9),10);
32
33 // Series 1, some other items missing
34 dataModel.setData(dataModel.index(1,0),5);
35 dataModel.setData(dataModel.index(1,3),4);
36 dataModel.setData(dataModel.index(1,5),7);
37 dataModel.setData(dataModel.index(1,6),8);
38 dataModel.setData(dataModel.index(1,8),9);
39 dataModel.setData(dataModel.index(1,9),9);
40
41 // Series 2
42 dataModel.setData(dataModel.index(2,0),3);
43 dataModel.setData(dataModel.index(2,1),5);
44 dataModel.setData(dataModel.index(2,2),8);
45 dataModel.setData(dataModel.index(2,3),13);
46 dataModel.setData(dataModel.index(2,4),8);
47 dataModel.setData(dataModel.index(2,5),5);
48 dataModel.setData(dataModel.index(2,6),3);
49 dataModel.setData(dataModel.index(2,7),2);
50 dataModel.setData(dataModel.index(2,8),1);
51 dataModel.setData(dataModel.index(2,9),1);
52
53 // Series 3
54 dataModel.setData(dataModel.index(3,0),5);
55 dataModel.setData(dataModel.index(3,1),6);
56 dataModel.setData(dataModel.index(3,2),7);
57 dataModel.setData(dataModel.index(3,3),3);
58 dataModel.setData(dataModel.index(3,4),4);
59 dataModel.setData(dataModel.index(3,5),5);
60 dataModel.setData(dataModel.index(3,6),8);
61 dataModel.setData(dataModel.index(3,7),9);
62 dataModel.setData(dataModel.index(3,8),10);
63 dataModel.setData(dataModel.index(3,9),5);
64
65 // Series 4
66 dataModel.setData(dataModel.index(4,0),9);
67 dataModel.setData(dataModel.index(4,1),7);
68 dataModel.setData(dataModel.index(4,2),5);
69 dataModel.setData(dataModel.index(4,3),3);
70 dataModel.setData(dataModel.index(4,4),1);
71 dataModel.setData(dataModel.index(4,5),2);
72 dataModel.setData(dataModel.index(4,6),4);
73 dataModel.setData(dataModel.index(4,7),6);
74 dataModel.setData(dataModel.index(4,8),8);
75 dataModel.setData(dataModel.index(4,9),10);
76
77 series0->setData(&dataModel);
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);
78 33
79 34 ChartWidget* chartWidget = new ChartWidget(&window);
80 35 chartWidget->addSeries(series0);
@@ -1,40 +1,54
1 1 #include <limits.h>
2 #include <QVector>
3 #include <QDebug>
2 4 #include "barchartmodel_p.h"
3 5
4 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
5 7
6 8 BarChartModel::BarChartModel(QObject *parent) :
7 9 QObject(parent)
10 ,mRunningId(1)
8 11 {
9 12 }
10 13
11 void BarChartModel::addSeries(BarChartSeriesBase& series)
14 BarChartModel::~BarChartModel()
12 15 {
13 mSeries.append(&series);
14 emit modelUpdated();
16 qDebug() << "BarChartModel::~BarChartModel";
17 foreach (DataContainer* c, mDataModel) {
18 delete c;
19 }
20 }
21
22 int BarChartModel::addData(QList<qreal> data)
23 {
24 qDebug() << "BarChartModel::addData" << data.count();
25 DataContainer* c = new DataContainer(data,mRunningId);
26 mDataModel.append(c);
27 mRunningId++;
28 return mRunningId-1;
15 29 }
16 30
17 void BarChartModel::removeSeries(BarChartSeriesBase& series)
31 void BarChartModel::removeData(int id)
18 32 {
19 int i = mSeries.indexOf(&series);
20 if (-1 == i) {
21 return;
33 foreach(DataContainer* c, mDataModel) {
34 if (c->mId == id) {
35 mDataModel.removeOne(c);
36 delete c;
37 }
22 38 }
23 mSeries.removeAt(i);
24 emit modelUpdated();
25 39 }
26 40
27 int BarChartModel::countSeries()
41 int BarChartModel::countRows()
28 42 {
29 return mSeries.count();
43 return mDataModel.count();
30 44 }
31 45
32 int BarChartModel::countItemsInSeries()
46 int BarChartModel::countColumns()
33 47 {
34 48 int count(0);
35 for (int i=0; i<mSeries.count(); i++){
49 for (int i=0; i<mDataModel.count(); i++){
36 50 // TODO: can we assume that all series have same number of values? If not. then which values are empty?
37 int temp = mSeries.at(i)->countValues();
51 int temp = mDataModel.at(i)->countColumns();
38 52 if (temp > count) {
39 53 count = temp;
40 54 }
@@ -44,54 +58,90 int BarChartModel::countItemsInSeries()
44 58
45 59 int BarChartModel::countTotalItems()
46 60 {
47 int total = mSeries.count() * countItemsInSeries();
61 int total = mDataModel.count() * countColumns();
62 qDebug() << "BarChartModel::countTotalItems datamodel count" << mDataModel.count();
63 qDebug() << "BarChartModel::countTotalItems countColumns count" << countColumns();
48 64 return total;
49 65 }
50 66
51 67 int BarChartModel::min()
52 68 {
53 Q_ASSERT(mSeries.count() > 0);
69 Q_ASSERT(mDataModel.count() > 0);
54 70 // TODO: make min and max members and update them when data changes.
55 71 // This is slower since they are checked every time, even if data is same since previous call.
56 72 int min = INT_MAX;
57 73
58 for (int i=0; i <mSeries.count(); i++) {
59 int temp = mSeries.at(i)->min();
74 for (int i=0; i <mDataModel.count(); i++) {
75 int itemCount = mDataModel.at(i)->countColumns();
76 for (int j=0; j<itemCount; j++) {
77 int temp = mDataModel.at(i)->valueAt(j);
60 78 if (temp < min) {
61 79 min = temp;
62 80 }
63 81 }
82 }
64 83 return min;
65 84 }
66 85
67 86 int BarChartModel::max()
68 87 {
69 Q_ASSERT(mSeries.count() > 0);
88 Q_ASSERT(mDataModel.count() > 0);
70 89
71 90 // TODO: make min and max members and update them when data changes.
72 91 // This is slower since they are checked every time, even if data is same since previous call.
73 92 int max = INT_MIN;
74 93
75 for (int i=0; i <mSeries.count(); i++) {
76 int temp = mSeries.at(i)->min();
94 for (int i=0; i <mDataModel.count(); i++) {
95 int itemCount = mDataModel.at(i)->countColumns();
96 for (int j=0; j<itemCount; j++) {
97 int temp = mDataModel.at(i)->valueAt(j);
77 98 if (temp > max) {
78 99 max = temp;
79 100 }
80 101 }
102 }
103
81 104 return max;
82 105 }
83 106
84 107 qreal BarChartModel::valueAt(int series, int item)
85 108 {
86 if ((series < 0) || (series >= mSeries.count())) {
109 if ((series < 0) || (series >= mDataModel.count())) {
87 110 // No series, no value.
88 111 return 0;
89 } else if ((item < 0) || (item >= mSeries.at(series)->countValues())) {
112 } else if ((item < 0) || (item >= mDataModel.at(series)->countColumns())) {
90 113 // No item, no value.
91 114 return 0;
92 115 }
93 116
94 return mSeries.at(series)->valueAt(item);
117 return mDataModel.at(series)->valueAt(item);
118 }
119
120 qreal BarChartModel::columnSum(int column)
121 {
122 int sum(0);
123 int count = mDataModel.count(); // Count rows
124
125 for (int row = 0; row < count; row++) {
126 if (column < mDataModel.at(row)->countColumns()) {
127 sum += mDataModel.at(row)->valueAt(column);
128 }
129 }
130 return sum;
131 }
132
133 qreal BarChartModel::maxColumnSum()
134 {
135 int max = INT_MIN;
136 int count = countColumns();
137
138 for (int col=0; col<count; col++) {
139 int sum = columnSum(col);
140 if (sum > max) {
141 max = sum;
142 }
143 }
144 return max;
95 145 }
96 146
97 147 #include "moc_barchartmodel_p.cpp"
@@ -2,11 +2,11
2 2 #define BARCHARTMODEL_H
3 3
4 4 #include <QObject>
5 #include "barchartseries.h"
5 #include "qchartglobal.h"
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 // Model for bar chart.
9 // Model for bar chart. Internal class.
10 10 // TODO: Implement as QAbstractItemModel?
11 11
12 12 class BarChartModel : public QObject //, public QAbstractItemModel
@@ -14,19 +14,24 class BarChartModel : public QObject //, public QAbstractItemModel
14 14 Q_OBJECT
15 15 public:
16 16 explicit BarChartModel(QObject *parent = 0);
17 ~BarChartModel();
17 18
18 // Takes reference, Series are owned by QChart or model?
19 void addSeries(BarChartSeriesBase& series);
20 void removeSeries(BarChartSeriesBase& series);
19 // Adds data to model. returns id.
20 int addData(QList<qreal> data);
21 void removeData(int id);
21 22
22 int countSeries(); // Number of series in model
23 int countItemsInSeries(); // Maximum number of items in series
23 int countRows(); // Number of series in model
24 int countColumns(); // Maximum number of items in series
24 25 int countTotalItems(); // Total items in all series. Includes empty items.
25 26
27 // TODO: qreal these
26 28 int max(); // Maximum value of all series
27 29 int min(); // Minimum value of all series
28 30 qreal valueAt(int series, int item);
29 31
32 qreal columnSum(int column);
33 qreal maxColumnSum(); // returns maximum sum of items in all columns.
34
30 35 signals:
31 36 void modelUpdated();
32 37
@@ -34,10 +39,23 public slots:
34 39
35 40 private:
36 41
37 // Data
38 QList<BarChartSeriesBase*> mSeries;
42 // Little helper class.
43 class DataContainer {
44 public:
45 DataContainer(QList<qreal> data, int id) : mId(id), mData(data) {}
46 int countColumns() { return mData.count(); }
47 qreal valueAt(int item) { return mData.at(item); }
48
49 int mId; // TODO: Is this needed?
50 private:
51 QList<qreal> mData;
52 };
53
54 // Owned. N series. each has a list of values.
55 QList<DataContainer*> mDataModel;
56 int mRunningId;
57 int mMaxColumns; // longest series in datamodel
39 58
40 BarChartModel* mSingle;
41 59 };
42 60
43 61 QTCOMMERCIALCHART_END_NAMESPACE
@@ -21,7 +21,6 public:
21 21
22 22 private:
23 23
24 QAbstractItemModel* mModel;
25 24 BarGroup* mBarGroup;
26 25 };
27 26
@@ -2,13 +2,15
2 2 #include <QDebug>
3 3 #include "barchartseriesbase.h"
4 4 #include "bargroup.h"
5 #include "barchartmodel_p.h"
5 6
6 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 8
8 9 BarChartSeriesBase::BarChartSeriesBase(QObject *parent)
9 10 : QChartSeries(parent)
10 ,mData(0)
11 ,mModel(*(new BarChartModel(this))) // TODO: is this ok?
11 12 {
13 qDebug() << "BarChartSeriesBase::BarChartSeriesBase";
12 14 }
13 15 /*
14 16 bool BarChartSeriesBase::setModel(QAbstractItemModel* model)
@@ -17,11 +19,13 bool BarChartSeriesBase::setModel(QAbstractItemModel* model)
17 19 return true;
18 20 }
19 21 */
22 /*
20 23 bool BarChartSeriesBase::setData(QList<qreal>& data)
21 24 {
22 25 mData = &data;
23 26 return true;
24 27 }
28 */
25 29 /*
26 30 int BarChartSeriesBase::min()
27 31 {
@@ -111,10 +115,22 int BarChartSeriesBase::columnSum(int column)
111 115 return sum;
112 116 }
113 117 */
114 qreal BarChartSeriesBase::min()
118
119 int BarChartSeriesBase::addData(QList<qreal> data)
120 {
121 qDebug() << "BarChartSeriesBase::addData";
122 return mModel.addData(data);
123 }
124
125 void BarChartSeriesBase::removeData(int id)
115 126 {
116 Q_ASSERT(mData != 0);
127 mModel.removeData(id);
128 }
117 129
130 qreal BarChartSeriesBase::min()
131 {
132 return mModel.min();
133 /* Delegated to model
118 134 int count = mData->count();
119 135 int min = INT_MAX;
120 136
@@ -124,12 +140,13 qreal BarChartSeriesBase::min()
124 140 }
125 141 }
126 142 return min;
143 */
127 144 }
128 145
129 146 qreal BarChartSeriesBase::max()
130 147 {
131 Q_ASSERT(mData != 0);
132
148 return mModel.max();
149 /* Delegated to model
133 150 int count = mData->count();
134 151 int max = INT_MIN;
135 152
@@ -139,18 +156,30 qreal BarChartSeriesBase::max()
139 156 }
140 157 }
141 158 return max;
159 */
160 }
161
162 int BarChartSeriesBase::countColumns()
163 {
164 return mModel.countColumns();
165 // return mData->count();
166 }
167
168 qreal BarChartSeriesBase::valueAt(int series, int item)
169 {
170 return mModel.valueAt(series,item);
171 // return mData->at(item);
142 172 }
143 173
144 int BarChartSeriesBase::countValues()
174 qreal BarChartSeriesBase::maxColumnSum()
145 175 {
146 Q_ASSERT(mData != 0);
147 return mData->count();
176 qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel.maxColumnSum();
177 return mModel.maxColumnSum();
148 178 }
149 179
150 qreal BarChartSeriesBase::valueAt(int item)
180 BarChartModel& BarChartSeriesBase::model()
151 181 {
152 Q_ASSERT(mData != 0);
153 return mData->at(item);
182 return mModel;
154 183 }
155 184
156 185 #include "moc_barchartseriesbase.cpp"
@@ -6,10 +6,11
6 6 #include "qchartseries.h"
7 7 #include "qchartglobal.h"
8 8
9 class BarGroupBase;
10
11 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12 10
11 class BarGroupBase;
12 class BarChartModel;
13
13 14 // Container for series
14 15 class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries
15 16 {
@@ -21,36 +22,29 public:
21 22 // from QChartSeries
22 23 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; }
23 24
24 // TODO: Better data model?
25 // virtual bool setModel(QAbstractItemModel* model);
26 virtual bool setData(QList<qreal>& data);
27
28 // Methods to find out minimum and maximum values of data
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.
32
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
37
38 // int columnSum(int column); // TODO: move to model
25 // TODO: << operator for convinience
26 // Returns id for vector.
27 int addData(QList<qreal> data);
28 void removeData(int id);
39 29
40 30 qreal min();
41 31 qreal max();
42 int countValues();
43 qreal valueAt(int item);
32 int countColumns(); // Count items in one series.
33 qreal valueAt(int series, int item);
34 qreal maxColumnSum();
35
36 BarChartModel& model();
37
38 signals:
39 void changed(int index);
44 40
45 41 public Q_SLOTS:
46 42
47 43 private:
48 44
49 QAbstractItemModel* mModel;
45 BarChartModel& mModel;
50 46 BarGroupBase* mBarGroup;
51 47
52 QList<qreal>* mData;
53
54 48 };
55 49
56 50 QTCOMMERCIALCHART_END_NAMESPACE
@@ -13,22 +13,17 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
13 13
14 14 void BarGroup::layoutChanged()
15 15 {
16 qDebug() << "BarGroup::layoutChanged";
16 17 // Scale bars to new layout
17 18 // Layout for bars:
18 /*
19 if (mSeries.countRows() <= 0) {
20 // Nothing to do.
21 return;
22 }
23 */
24 if (mModel.countSeries() <= 0) {
19 if (mModel.countRows() <= 0) {
25 20 return;
26 21 }
27 22
28 23 // TODO: better way to auto-layout?
29 24 // Use reals for accurancy (we might get some compiler warnings... :)
30 int itemCount = mModel.countItemsInSeries();
31 int seriesCount = mModel.countSeries();
25 int itemCount = mModel.countColumns();
26 int seriesCount = mModel.countRows();
32 27
33 28 qreal tW = mWidth;
34 29 qreal tH = mHeight;
@@ -9,15 +9,13 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent)
11 11 : ChartItem(parent)
12 // ,mSeries(series)
13 12 ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
14 13 ,mLayoutSet(false)
15 14 ,mLayoutDirty(true)
16 15 ,mTheme(0)
17 16 ,mSeparatorsVisible(true)
17 ,mModel(series.model())
18 18 {
19 mModel.addSeries(series);
20 dataChanged();
21 19 }
22 20
23 21 void BarGroupBase::setSeparatorsVisible(bool visible)
@@ -27,16 +25,17 void BarGroupBase::setSeparatorsVisible(bool visible)
27 25
28 26 void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
29 27 {
28 qDebug() << "BarGroupBase::paint" << childItems().count();
30 29 if (!mLayoutSet) {
31 30 qDebug() << "BarGroupBase::paint called without layout set. Aborting.";
32 31 return;
33 32 }
34 if (mLayoutDirty) {
33 // if (mLayoutDirty) {
35 34 // Layout or data has changed. Need to redraw.
36 35 foreach(QGraphicsItem* i, childItems()) {
37 36 i->paint(painter,option,widget);
38 37 }
39 }
38 // }
40 39 }
41 40
42 41 QRectF BarGroupBase::boundingRect() const
@@ -70,11 +69,6 void BarGroupBase::dataChanged()
70 69 {
71 70 qDebug() << "BarGroupBase::dataChanged";
72 71
73 // Find out maximum and minimum of all series.
74 // TODO: is this actually needed?
75 // mMax = mModel.max();
76 // mMin = mModel.min();
77
78 72 // Delete old bars
79 73 foreach (QGraphicsItem* item, childItems()) {
80 74 delete item;
@@ -88,7 +82,7 void BarGroupBase::dataChanged()
88 82 }
89 83
90 84 // TODO: labels from series. This creates just some example labels
91 int count = mModel.countItemsInSeries(); // mSeries.countColumns();
85 int count = mModel.countColumns(); // mSeries.countColumns();
92 86 for (int i=0; i<count; i++) {
93 87 BarLabel* label = new BarLabel(this);
94 88 QString text("Label " + QString::number(i));
@@ -96,7 +90,7 void BarGroupBase::dataChanged()
96 90 childItems().append(label);
97 91 }
98 92
99 count = mModel.countItemsInSeries() - 1; // mSeries.countColumns() - 1; // There is one less separator than columns
93 count = mModel.countColumns() - 1; // mSeries.countColumns() - 1; // There is one less separator than columns
100 94 for (int i=0; i<count; i++) {
101 95 Separator* sep = new Separator(this);
102 96 sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme
@@ -111,12 +105,14 void BarGroupBase::dataChanged()
111 105
112 106 void BarGroupBase::handleModelChanged(int index)
113 107 {
114 qDebug() << "BarGroupBase::handleModelChanged";
108 qDebug() << "BarGroupBase::handleModelChanged PUUH" << index;
109 dataChanged();
115 110 }
116 111
117 112 void BarGroupBase::handleDomainChanged(const Domain& domain)
118 113 {
119 qDebug() << "BarGroupBase::handleModelChanged";
114 qDebug() << "BarGroupBase::handleDomainChanged";
115 dataChanged();
120 116 }
121 117
122 118 void BarGroupBase::handleGeometryChanged(const QRectF& rect)
@@ -17,7 +17,6 class BarGroupBase : public QObject, public ChartItem
17 17 Q_OBJECT
18 18 public:
19 19 BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0);
20 // BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0);
21 20 void setSeparatorsVisible(bool visible = true);
22 21
23 22 public: // From ChartItem
@@ -47,8 +46,6 protected slots:
47 46
48 47 protected:
49 48
50 //BarChartSeriesBase& mSeries;
51
52 49 // TODO: consider these.
53 50 int mMin; // Min and max values of data. (updated when data is changed, used when drawing)
54 51 int mMax;
@@ -65,7 +62,7 protected:
65 62 ChartTheme* mTheme;
66 63 bool mSeparatorsVisible;
67 64
68 BarChartModel mModel;
65 BarChartModel& mModel;
69 66
70 67 };
71 68
@@ -22,7 +22,6 public Q_SLOTS:
22 22
23 23 private:
24 24
25 QAbstractItemModel* mModel;
26 25 PercentBarGroup* mPercentBarGroup;
27 26 };
28 27
@@ -14,32 +14,31 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *p
14 14
15 15 void PercentBarGroup::layoutChanged()
16 16 {
17 /*
18 17 // Scale bars to new layout
19 18 // Layout for bars:
20 if (mSeries.countRows() <= 0) {
19 if (mModel.countRows() <= 0) {
21 20 // Nothing to do.
22 21 return;
23 22 }
24 23
25 24 // TODO: better way to auto-layout
26 25 // Use reals for accurancy (we might get some compiler warnings... :)
27 int count = mSeries.countColumns();
26 int count = mModel.countColumns();
28 27 int itemIndex(0);
29 28 qreal tW = mWidth;
30 29 qreal tC = count+1;
31 30 qreal xStep = (tW/tC);
32 31 // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
33 32 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
34 int labelIndex = mSeries.countColumns() * mSeries.countRows();
33 int labelIndex = mModel.countColumns() * mModel.countRows();
35 34
36 for (int column = 0; column < mSeries.countColumns(); column++) {
37 qreal colSum = mSeries.columnSum(column);
35 for (int column = 0; column < mModel.countColumns(); column++) {
36 qreal colSum = mModel.columnSum(column);
38 37 qreal h = mHeight;
39 38 qreal scale = (h / colSum);
40 39 qreal yPos = h;
41 for (int row=0; row < mSeries.countRows(); row++) {
42 qreal barHeight = mSeries.valueAt(row, column) * scale;
40 for (int row=0; row < mModel.countRows(); row++) {
41 qreal barHeight = mModel.valueAt(row, column) * scale;
43 42 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
44 43
45 44 // TODO: width settable per bar?
@@ -60,14 +59,14 void PercentBarGroup::layoutChanged()
60 59 // Position separators
61 60 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
62 61 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
63 for (int s=0; s < mSeries.countColumns() - 1; s++) {
62 for (int s=0; s < mModel.countColumns() - 1; s++) {
64 63 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
65 64 sep->setPos(xPos,0);
66 65 sep->setSize(QSizeF(1,mHeight));
67 66 xPos += xStep;
68 67 separatorIndex++;
69 68 }
70 */
69
71 70 mLayoutDirty = true;
72 71 }
73 72
@@ -28,7 +28,7 void Separator::setSize(const QSizeF &size)
28 28
29 29 void Separator::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
30 30 {
31 qDebug() << "separator::paint" << boundingRect();
31 // qDebug() << "separator::paint" << boundingRect();
32 32 QPen pen(mColor);
33 33 painter->setPen(pen);
34 34 painter->drawLine(mXpos,mYpos,mXpos,mHeight);
@@ -22,7 +22,6 public Q_SLOTS:
22 22
23 23 private:
24 24
25 QAbstractItemModel* mModel;
26 25 StackedBarGroup* mStackedBarGroup;
27 26 };
28 27
@@ -3,7 +3,7
3 3 #include "barlabel_p.h"
4 4 #include "separator_p.h"
5 5 #include <QDebug>
6 #include <QPainter>
6 //#include <QPainter>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
@@ -14,15 +14,15 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *p
14 14
15 15 void StackedBarGroup::layoutChanged()
16 16 {
17 /*
17
18 18 // Scale bars to new layout
19 19 // Layout for bars:
20 if (mSeries.countRows() <= 0) {
20 if (mModel.countRows() <= 0) {
21 21 // Nothing to do.
22 22 return;
23 23 }
24 24
25 if (mSeries.countColumns() == 0) {
25 if (mModel.countColumns() == 0) {
26 26 // Nothing to do
27 27 return;
28 28 }
@@ -30,21 +30,21 void StackedBarGroup::layoutChanged()
30 30 // TODO: better way to auto-layout
31 31 // Use reals for accurancy (we might get some compiler warnings... :)
32 32 // TODO: use temp variable for column count...
33 qreal maxSum = mSeries.maxColumnSum();
33 qreal maxSum = mModel.maxColumnSum();
34 34 qreal h = mHeight;
35 35 qreal scale = (h / maxSum);
36 36
37 37 int itemIndex(0);
38 38 qreal tW = mWidth;
39 qreal tC = mSeries.countColumns() + 1;
39 qreal tC = mModel.countColumns() + 1;
40 40 qreal xStep = (tW/tC);
41 41 qreal xPos = ((tW/tC) - mBarDefaultWidth / 2);
42 int labelIndex = mSeries.countColumns() * mSeries.countRows();
42 int labelIndex = mModel.countRows() * mModel.countColumns();
43 43
44 for (int column = 0; column < mSeries.countColumns(); column++) {
44 for (int column = 0; column < mModel.countColumns(); column++) {
45 45 qreal yPos = h;
46 for (int row=0; row < mSeries.countRows(); row++) {
47 qreal barHeight = mSeries.valueAt(row, column) * scale;
46 for (int row=0; row < mModel.countRows(); row++) {
47 qreal barHeight = mModel.valueAt(row, column) * scale;
48 48 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
49 49
50 50 // TODO: width settable per bar?
@@ -68,14 +68,14 void StackedBarGroup::layoutChanged()
68 68 // Position separators
69 69 int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these?
70 70 xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left.
71 for (int s=0; s < mSeries.countColumns() - 1; s++) {
71 for (int s=0; s < mModel.countColumns() - 1; s++) {
72 72 Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex));
73 73 sep->setPos(xPos,0);
74 74 sep->setSize(QSizeF(1,mHeight));
75 75 xPos += xStep;
76 76 separatorIndex++;
77 77 }
78 */
78
79 79 mLayoutDirty = true;
80 80 }
81 81
@@ -54,38 +54,33 void ChartDataSet::addSeries(QChartSeries* series)
54 54 case QChartSeries::SeriesTypeBar: {
55 55
56 56 BarChartSeries* barSeries = static_cast<BarChartSeries*>(series);
57 qreal x = barSeries->countValues();
57 qreal x = barSeries->countColumns();
58 58 qreal y = barSeries->max();
59 59 domain.m_minX = qMin(domain.m_minX,x);
60 60 domain.m_minY = qMin(domain.m_minY,y);
61 61 domain.m_maxX = qMax(domain.m_maxX,x);
62 62 domain.m_maxY = qMax(domain.m_maxY,y);
63
64 63 }
65 64 break;
66 65 case QChartSeries::SeriesTypeStackedBar: {
67 66
68 67 StackedBarChartSeries* stackedBarSeries = static_cast<StackedBarChartSeries*>(series);
69 /*
70 68 qreal x = stackedBarSeries->countColumns();
71 69 qreal y = stackedBarSeries->maxColumnSum();
72 70 domain.m_minX = qMin(domain.m_minX,x);
73 71 domain.m_minY = qMin(domain.m_minY,y);
74 72 domain.m_maxX = qMax(domain.m_maxX,x);
75 73 domain.m_maxY = qMax(domain.m_maxY,y);
76 */
77 74 }
78 75 break;
79 76 case QChartSeries::SeriesTypePercentBar: {
80 77
81 78 PercentBarChartSeries* percentBarSeries = static_cast<PercentBarChartSeries*>(series);
82 /*
83 79 qreal x = percentBarSeries->countColumns();
84 80 domain.m_minX = qMin(domain.m_minX,x);
85 81 domain.m_minY = 0;
86 82 domain.m_maxX = qMax(domain.m_maxX,x);
87 83 domain.m_maxY = 100;
88 */
89 84 }
90 85 break;
91 86
General Comments 0
You need to be logged in to leave comments. Login now