##// END OF EJS Templates
model delegate for bar series. updated examples
sauimone -
r161:cc2daab2560d
parent child
Show More
@@ -1,88 +1,43
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <QStandardItemModel>
4 4 #include <barchartseries.h>
5 5 #include "chartwidget.h"
6 6
7 7 QTCOMMERCIALCHART_USE_NAMESPACE
8 8
9 9 int main(int argc, char *argv[])
10 10 {
11 11 QApplication a(argc, argv);
12 12 QMainWindow window;
13 13
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);
81 36
82 37 window.setCentralWidget(chartWidget);
83 38 window.resize(400, 300);
84 39 window.show();
85 40
86 41 return a.exec();
87 42 }
88 43
@@ -1,88 +1,43
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <QStandardItemModel>
4 4 #include <percentbarchartseries.h>
5 5 #include "chartwidget.h"
6 6
7 7 QTCOMMERCIALCHART_USE_NAMESPACE
8 8
9 9 int main(int argc, char *argv[])
10 10 {
11 11 QApplication a(argc, argv);
12 12 QMainWindow window;
13 13
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);
81 36
82 37 window.setCentralWidget(chartWidget);
83 38 window.resize(400, 300);
84 39 window.show();
85 40
86 41 return a.exec();
87 42 }
88 43
@@ -1,88 +1,43
1 1 #include <QApplication>
2 2 #include <QMainWindow>
3 3 #include <QStandardItemModel>
4 4 #include <stackedbarchartseries.h>
5 5 #include "chartwidget.h"
6 6
7 7 QTCOMMERCIALCHART_USE_NAMESPACE
8 8
9 9 int main(int argc, char *argv[])
10 10 {
11 11 QApplication a(argc, argv);
12 12 QMainWindow window;
13 13
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);
81 36
82 37 window.setCentralWidget(chartWidget);
83 38 window.resize(400, 300);
84 39 window.show();
85 40
86 41 return a.exec();
87 42 }
88 43
@@ -1,99 +1,149
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 }
15 20 }
16 21
17 void BarChartModel::removeSeries(BarChartSeriesBase& series)
22 int BarChartModel::addData(QList<qreal> data)
18 23 {
19 int i = mSeries.indexOf(&series);
20 if (-1 == i) {
21 return;
24 qDebug() << "BarChartModel::addData" << data.count();
25 DataContainer* c = new DataContainer(data,mRunningId);
26 mDataModel.append(c);
27 mRunningId++;
28 return mRunningId-1;
29 }
30
31 void BarChartModel::removeData(int id)
32 {
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 }
41 55 }
42 56 return count;
43 57 }
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();
60 if (temp < min) {
61 min = temp;
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);
78 if (temp < min) {
79 min = temp;
80 }
62 81 }
63 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();
77 if (temp > max) {
78 max = temp;
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);
98 if (temp > max) {
99 max = temp;
100 }
79 101 }
80 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"
98 148
99 149 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,45 +1,63
1 1 #ifndef BARCHARTMODEL_H
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
13 13 {
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
33 38 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
44 62
45 63 #endif // BARCHARTMODEL_H
@@ -1,30 +1,29
1 1 #ifndef BARCHARTSERIES_H
2 2 #define BARCHARTSERIES_H
3 3
4 4 #include <QList>
5 5 #include <QAbstractItemModel>
6 6 #include "barchartseriesbase.h"
7 7
8 8 class BarGroup;
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 // Container for series
13 13 class QTCOMMERCIALCHART_EXPORT BarChartSeries : public BarChartSeriesBase
14 14 {
15 15 Q_OBJECT
16 16 public:
17 17 BarChartSeries(QObject* parent=0);
18 18
19 19 // from BarChartSeriesBase
20 20 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; }
21 21
22 22 private:
23 23
24 QAbstractItemModel* mModel;
25 24 BarGroup* mBarGroup;
26 25 };
27 26
28 27 QTCOMMERCIALCHART_END_NAMESPACE
29 28
30 29 #endif // BARCHARTSERIES_H
@@ -1,158 +1,187
1 1 #include <limits.h>
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)
15 17 {
16 18 mModel = 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 {
28 32 Q_ASSERT(mModel->rowCount() > 0);
29 33 Q_ASSERT(mModel->columnCount() > 0);
30 34
31 35 // TODO: make min and max members and update them when data changes.
32 36 // This is slower since they are checked every time, even if data is same since previous call.
33 37 int min = INT_MAX;
34 38
35 39 for (int i=0; i <mModel->rowCount(); i++) {
36 40 for(int j=0; j<mModel->columnCount(); j++) {
37 41 int temp = mModel->data(mModel->index(i,j)).toInt();
38 42 if (temp < min) {
39 43 min = temp;
40 44 }
41 45 }
42 46 }
43 47 return min;
44 48 }
45 49
46 50 int BarChartSeriesBase::max()
47 51 {
48 52 Q_ASSERT(mModel->rowCount() > 0);
49 53 Q_ASSERT(mModel->columnCount() > 0);
50 54
51 55 // TODO: make min and max members and update them when data changes.
52 56 // This is slower since they are checked every time, even if data is same since previous call.
53 57 int max = INT_MIN;
54 58
55 59 for (int i=0; i <mModel->rowCount(); i++) {
56 60 for(int j=0; j<mModel->columnCount(); j++) {
57 61 int temp = mModel->data(mModel->index(i,j)).toInt();
58 62 if (temp > max) {
59 63 max = temp;
60 64 }
61 65 }
62 66 }
63 67 return max;
64 68 }
65 69
66 70 int BarChartSeriesBase::maxColumnSum()
67 71 {
68 72 Q_ASSERT(mModel->rowCount() > 0);
69 73 Q_ASSERT(mModel->columnCount() > 0);
70 74
71 75 int max = INT_MIN;
72 76
73 77 for (int col=0; col <mModel->columnCount(); col++) {
74 78 int sum = columnSum(col);
75 79 if (sum > max) {
76 80 max = sum;
77 81 }
78 82 }
79 83 return max;
80 84 }
81 85
82 86 int BarChartSeriesBase::countRows()
83 87 {
84 88 return mModel->rowCount();
85 89 }
86 90
87 91 int BarChartSeriesBase::countColumns()
88 92 {
89 93 return mModel->columnCount();
90 94 }
91 95
92 96 int BarChartSeriesBase::countTotalItems()
93 97 {
94 98 return mModel->rowCount() * mModel->columnCount();
95 99 }
96 100
97 101 int BarChartSeriesBase::valueAt(int row, int column)
98 102 {
99 103 QModelIndex index = mModel->index(row,column);
100 104 return mModel->data(index).toInt();
101 105 }
102 106
103 107 int BarChartSeriesBase::columnSum(int column)
104 108 {
105 109 int sum(0);
106 110 int count = mModel->rowCount();
107 111
108 112 for (int row = 0; row < count; row++) {
109 113 sum += mModel->data(mModel->index(row,column)).toInt();
110 114 }
111 115 return sum;
112 116 }
113 117 */
114 qreal BarChartSeriesBase::min()
118
119 int BarChartSeriesBase::addData(QList<qreal> data)
115 120 {
116 Q_ASSERT(mData != 0);
121 qDebug() << "BarChartSeriesBase::addData";
122 return mModel.addData(data);
123 }
117 124
125 void BarChartSeriesBase::removeData(int id)
126 {
127 mModel.removeData(id);
128 }
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
121 137 for (int i=0; i<count; i++) {
122 138 if (mData->at(i) < min) {
123 139 min = mData->at(i);
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
136 153 for (int i=0; i<count; i++) {
137 154 if (mData->at(i) > max) {
138 155 max = mData->at(i);
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"
157 186
158 187 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,58 +1,52
1 1 #ifndef BARCHARTSERIESBASE_H
2 2 #define BARCHARTSERIESBASE_H
3 3
4 4 #include <QList>
5 5 #include <QAbstractItemModel>
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 {
16 17 Q_OBJECT
17 18 protected:
18 19 BarChartSeriesBase(QObject* parent=0);
19 20
20 21 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
57 51
58 52 #endif // BARCHARTSERIESBASE_H
@@ -1,70 +1,65
1 1 #include "bargroup.h"
2 2 #include "bar_p.h"
3 3 #include "barlabel_p.h"
4 4 #include <QDebug>
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
9 9 BarGroupBase(series,parent)
10 10 {
11 11 mBarDefaultWidth = 10;
12 12 }
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;
35 30 qreal tM = mMax;
36 31 qreal scale = (tH/tM);
37 32
38 33 qreal tC = itemCount+1;
39 34 qreal xStepPerSeries = (tW/tC);
40 35
41 36 // Scaling.
42 37 int itemIndex(0);
43 38 int labelIndex = itemCount * seriesCount;
44 39
45 40 for (int item=0; item < itemCount; item++) {
46 41 qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
47 42 qreal yPos = mHeight;
48 43 for (int series = 0; series < seriesCount; series++) {
49 44 qreal barHeight = mModel.valueAt(series, item) * scale;
50 45 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
51 46
52 47 // TODO: width settable per bar?
53 48 bar->resize(mBarDefaultWidth, barHeight);
54 49 bar->setColor(mColors.at(series));
55 50 bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
56 51 itemIndex++;
57 52 xPos += mBarDefaultWidth;
58 53 }
59 54
60 55 // TODO: Layout for labels, remove magic number
61 56 xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2));
62 57 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
63 58 label->setPos(xPos, mHeight + 20);
64 59 labelIndex++;
65 60 }
66 61
67 62 mLayoutDirty = true;
68 63 }
69 64
70 65 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,133 +1,129
1 1 #include "bargroupbase.h"
2 2 #include "bar_p.h"
3 3 #include "barlabel_p.h"
4 4 #include "separator_p.h"
5 5 #include "barchartseriesbase.h"
6 6 #include <QDebug>
7 7
8 8 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)
24 22 {
25 23 mSeparatorsVisible = visible;
26 24 }
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
43 42 {
44 43 return QRectF(0,0,mWidth,mHeight);
45 44 }
46 45
47 46 void BarGroupBase::themeChanged(ChartTheme *theme)
48 47 {
49 48 mTheme = theme;
50 49 }
51 50
52 51 void BarGroupBase::setBarWidth( int w )
53 52 {
54 53 mBarDefaultWidth = w;
55 54 }
56 55
57 56 int BarGroupBase::addColor( QColor color )
58 57 {
59 58 int colorIndex = mColors.count();
60 59 mColors.append(color);
61 60 return colorIndex;
62 61 }
63 62
64 63 void BarGroupBase::resetColors()
65 64 {
66 65 mColors.clear();
67 66 }
68 67
69 68 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;
81 75 }
82 76
83 77 // Create new graphic items for bars
84 78 int totalItems = mModel.countTotalItems(); // mSeries.countTotalItems();
85 79 for (int i=0; i<totalItems; i++) {
86 80 Bar *bar = new Bar(this);
87 81 childItems().append(bar);
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));
95 89 label->set(text);
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
103 97 childItems().append(sep);
104 98 }
105 99
106 100 // TODO: if (autolayout) { layoutChanged() } or something
107 101 mLayoutDirty = true;
108 102 }
109 103
110 104 //handlers
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)
123 119 {
124 120 mWidth = rect.width();
125 121 mHeight = rect.height();
126 122 layoutChanged();
127 123 mLayoutSet = true;
128 124 setPos(rect.topLeft());
129 125 }
130 126
131 127 #include "moc_bargroupbase.cpp"
132 128
133 129 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,74 +1,71
1 1 #ifndef BARGROUPBASE_H
2 2 #define BARGROUPBASE_H
3 3
4 4 #include "charttheme_p.h"
5 5 #include "chartitem_p.h"
6 6 //#include "barlabel_p.h"
7 7 //#include "bar_p.h"
8 8 #include "barchartseriesbase.h"
9 9 #include "barchartmodel_p.h"
10 10 #include <QGraphicsItem>
11 11
12 12 QTCOMMERCIALCHART_BEGIN_NAMESPACE
13 13
14 14 // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated.
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);
21 20 void setSeparatorsVisible(bool visible = true);
22 21
23 22 public: // From ChartItem
24 23 void setSize(const QSizeF &size){};
25 24 void setPlotDomain(const PlotDomain& data){};
26 25
27 26 // From QGraphicsItem
28 27 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
29 28 QRectF boundingRect() const;
30 29
31 30 // From ChartThemeObserver
32 31 void themeChanged(ChartTheme *theme);
33 32
34 33 // TODO: these may change with layout awarness.
35 34 void setBarWidth( int w );
36 35 int addColor( QColor color );
37 36 void resetColors();
38 37
39 38 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
40 39 virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes
41 40
42 41 protected slots:
43 42 void handleModelChanged(int index);
44 43 void handleDomainChanged(const Domain& domain);
45 44 void handleGeometryChanged(const QRectF& size);
46 45
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;
55 52
56 53 int mHeight; // Layout spesific
57 54 int mWidth;
58 55 int mBarDefaultWidth;
59 56
60 57 bool mLayoutSet; // True, if component has been laid out.
61 58 bool mLayoutDirty;
62 59
63 60 QList<QColor> mColors; // List of colors for series for now
64 61
65 62 ChartTheme* mTheme;
66 63 bool mSeparatorsVisible;
67 64
68 BarChartModel mModel;
65 BarChartModel& mModel;
69 66
70 67 };
71 68
72 69 QTCOMMERCIALCHART_END_NAMESPACE
73 70
74 71 #endif // BARGROUPBASE_H
@@ -1,32 +1,31
1 1 #ifndef PERCENTBARCHARTSERIES_H
2 2 #define PERCENTBARCHARTSERIES_H
3 3
4 4 #include <QList>
5 5 #include <QAbstractItemModel>
6 6 #include "barchartseriesbase.h"
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class PercentBarGroup;
11 11
12 12 class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public BarChartSeriesBase
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 PercentBarChartSeries(QObject* parent=0);
17 17
18 18 // from BarChartSeriesBase
19 19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; }
20 20
21 21 public Q_SLOTS:
22 22
23 23 private:
24 24
25 QAbstractItemModel* mModel;
26 25 PercentBarGroup* mPercentBarGroup;
27 26 };
28 27
29 28 QTCOMMERCIALCHART_END_NAMESPACE
30 29
31 30
32 31 #endif // PERCENTBARCHARTSERIES_H
@@ -1,74 +1,73
1 1 #include "percentbargroup.h"
2 2 #include "bar_p.h"
3 3 #include "barlabel_p.h"
4 4 #include "separator_p.h"
5 5 #include <QDebug>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9
10 10 PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) :
11 11 BarGroupBase(series, parent)
12 12 {
13 13 }
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?
46 45 bar->resize(mBarDefaultWidth, barHeight);
47 46 bar->setColor(mColors.at(row));
48 47 bar->setPos(xPos, yPos-barHeight);
49 48 itemIndex++;
50 49 yPos -= barHeight;
51 50 }
52 51
53 52 // TODO: Layout for labels, remove magic number
54 53 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
55 54 label->setPos(xPos, mHeight + 20);
56 55 labelIndex++;
57 56 xPos += xStep;
58 57 }
59 58
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
74 73 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,44 +1,44
1 1 #include "separator_p.h"
2 2 #include <QDebug>
3 3 #include <QPainter>
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 Separator::Separator(QGraphicsItem *parent)
8 8 : ChartItem(parent)
9 9 {
10 10 }
11 11
12 12 void Separator::setPos(qreal x, qreal y)
13 13 {
14 14 mXpos = x;
15 15 mYpos = y;
16 16 }
17 17
18 18 void Separator::setColor(QColor color)
19 19 {
20 20 mColor = color;
21 21 }
22 22
23 23 void Separator::setSize(const QSizeF &size)
24 24 {
25 25 mWidth = size.width();
26 26 mHeight = size.height();
27 27 }
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);
35 35 }
36 36
37 37 QRectF Separator::boundingRect() const
38 38 {
39 39 QRectF r(mXpos,mYpos,mWidth,mHeight);
40 40 return r;
41 41 }
42 42
43 43
44 44 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,31 +1,30
1 1 #ifndef STACKEDBARCHARTSERIES_H
2 2 #define STACKEDBARCHARTSERIES_H
3 3
4 4 #include <QList>
5 5 #include <QAbstractItemModel>
6 6 #include "barchartseriesbase.h"
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class StackedBarGroup;
11 11
12 12 class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public BarChartSeriesBase
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 StackedBarChartSeries(QObject* parent=0);
17 17
18 18 // from QChartSeries
19 19 virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; }
20 20
21 21 public Q_SLOTS:
22 22
23 23 private:
24 24
25 QAbstractItemModel* mModel;
26 25 StackedBarGroup* mStackedBarGroup;
27 26 };
28 27
29 28 QTCOMMERCIALCHART_END_NAMESPACE
30 29
31 30 #endif // STACKEDBARCHARTSERIES_H
@@ -1,82 +1,82
1 1 #include "stackedbargroup.h"
2 2 #include "bar_p.h"
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
10 10 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
11 11 BarGroupBase(series,parent)
12 12 {
13 13 }
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 }
29 29
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?
51 51 // TODO: how to get color for series(x) from theme?
52 52 // mTheme->themeForSeries();
53 53 bar->resize(mBarDefaultWidth, barHeight);
54 54 bar->setColor(mColors.at(row));
55 55 bar->setPos(xPos, yPos-barHeight);
56 56 itemIndex++;
57 57 yPos -= barHeight;
58 58 }
59 59
60 60 // TODO: Layout for labels, remove magic number
61 61 BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex));
62 62 label->setPos(xPos, mHeight + 20);
63 63 labelIndex++;
64 64 xPos += xStep;
65 65 }
66 66
67 67
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
82 82 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,165 +1,160
1 1 #include "chartdataset_p.h"
2 2 //series
3 3 #include "qlinechartseries.h"
4 4 #include "barchartseries.h"
5 5 #include "stackedbarchartseries.h"
6 6 #include "percentbarchartseries.h"
7 7 #include "qpieseries.h"
8 8 #include "qscatterseries.h"
9 9
10 10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 11
12 12 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent)
13 13 {
14 14 Domain domain;
15 15 m_domains<<domain;
16 16 }
17 17
18 18 ChartDataSet::~ChartDataSet()
19 19 {
20 20 // TODO Auto-generated destructor stub
21 21 }
22 22
23 23 const Domain& ChartDataSet::domain() const
24 24 {
25 25 return m_domains[m_domainIndex];
26 26 }
27 27
28 28 void ChartDataSet::addSeries(QChartSeries* series)
29 29 {
30 30 // TODO: we should check the series not already added
31 31 m_chartSeries << series;
32 32 m_domainIndex = 0;
33 33 m_domains.resize(1);
34 34
35 35 Domain& domain = m_domains[m_domainIndex];
36 36
37 37 switch(series->type())
38 38 {
39 39 case QChartSeries::SeriesTypeLine: {
40 40
41 41 QLineChartSeries* xyseries = static_cast<QLineChartSeries*>(series);
42 42
43 43 for (int i = 0; i < xyseries->count(); i++)
44 44 {
45 45 qreal x = xyseries->x(i);
46 46 qreal y = xyseries->y(i);
47 47 domain.m_minX = qMin(domain.m_minX,x);
48 48 domain.m_minY = qMin(domain.m_minY,y);
49 49 domain.m_maxX = qMax(domain.m_maxX,x);
50 50 domain.m_maxY = qMax(domain.m_maxY,y);
51 51 }
52 52 break;
53 53 }
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
92 87 case QChartSeries::SeriesTypePie: {
93 88 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
94 89 // TODO: domain stuff
95 90 break;
96 91 }
97 92
98 93 case QChartSeries::SeriesTypeScatter: {
99 94 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
100 95 Q_ASSERT(scatterSeries);
101 96 foreach (QPointF point, scatterSeries->data()) {
102 97 domain.m_minX = qMin(domain.m_minX, point.x());
103 98 domain.m_maxX = qMax(domain.m_maxX, point.x());
104 99 domain.m_minY = qMin(domain.m_minY, point.y());
105 100 domain.m_maxY = qMax(domain.m_maxY, point.y());
106 101 }
107 102 break;
108 103 }
109 104
110 105 default: {
111 106 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
112 107 return;
113 108 break;
114 109 }
115 110
116 111 }
117 112
118 113 emit seriesAdded(series);
119 114 emit domainChanged(domain);
120 115 }
121 116
122 117 bool ChartDataSet::nextDomain()
123 118 {
124 119 if (m_domainIndex < m_domains.count() - 1) {
125 120 m_domainIndex++;
126 121 emit domainChanged(m_domains[m_domainIndex]);
127 122 return true;
128 123 }
129 124 else {
130 125 return false;
131 126 }
132 127 }
133 128
134 129 bool ChartDataSet::previousDomain()
135 130 {
136 131 if (m_domainIndex > 0) {
137 132 m_domainIndex--;
138 133 emit domainChanged(m_domains[m_domainIndex]);
139 134 return true;
140 135 }
141 136 else {
142 137 return false;
143 138 }
144 139 }
145 140
146 141 void ChartDataSet::clearDomains()
147 142 {
148 143 if (m_domainIndex > 0) {
149 144 m_domainIndex = 0;
150 145 emit domainChanged(m_domains[m_domainIndex]);
151 146 }
152 147 }
153 148
154 149 void ChartDataSet::addDomain(const Domain& domain)
155 150 {
156 151 m_domains.resize(m_domainIndex + 1);
157 152 m_domains << domain;
158 153 m_domainIndex++;
159 154
160 155 emit domainChanged(domain);
161 156 }
162 157
163 158 #include "moc_chartdataset_p.cpp"
164 159
165 160 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now