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