@@ -0,0 +1,99 | |||||
|
1 | #include <limits.h> | |||
|
2 | #include "barchartmodel_p.h" | |||
|
3 | ||||
|
4 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
5 | ||||
|
6 | BarChartModel::BarChartModel(QObject *parent) : | |||
|
7 | QObject(parent) | |||
|
8 | { | |||
|
9 | } | |||
|
10 | ||||
|
11 | void BarChartModel::addSeries(BarChartSeriesBase& series) | |||
|
12 | { | |||
|
13 | mSeries.append(&series); | |||
|
14 | emit modelUpdated(); | |||
|
15 | } | |||
|
16 | ||||
|
17 | void BarChartModel::removeSeries(BarChartSeriesBase& series) | |||
|
18 | { | |||
|
19 | int i = mSeries.indexOf(&series); | |||
|
20 | if (-1 == i) { | |||
|
21 | return; | |||
|
22 | } | |||
|
23 | mSeries.removeAt(i); | |||
|
24 | emit modelUpdated(); | |||
|
25 | } | |||
|
26 | ||||
|
27 | int BarChartModel::countSeries() | |||
|
28 | { | |||
|
29 | return mSeries.count(); | |||
|
30 | } | |||
|
31 | ||||
|
32 | int BarChartModel::countItemsInSeries() | |||
|
33 | { | |||
|
34 | int count(0); | |||
|
35 | for (int i=0; i<mSeries.count(); i++){ | |||
|
36 | // TODO: can we assume that all series have same number of items? If not. then which items are empty. | |||
|
37 | int temp = mSeries.at(i)->countItems(); | |||
|
38 | if (temp > count) { | |||
|
39 | count = temp; | |||
|
40 | } | |||
|
41 | } | |||
|
42 | return count; | |||
|
43 | } | |||
|
44 | ||||
|
45 | int BarChartModel::countTotalItems() | |||
|
46 | { | |||
|
47 | int total = mSeries.count() * countItemsInSeries(); | |||
|
48 | return total; | |||
|
49 | } | |||
|
50 | ||||
|
51 | int BarChartModel::min() | |||
|
52 | { | |||
|
53 | Q_ASSERT(mSeries.count() > 0); | |||
|
54 | // TODO: make min and max members and update them when data changes. | |||
|
55 | // This is slower since they are checked every time, even if data is same since previous call. | |||
|
56 | int min = INT_MAX; | |||
|
57 | ||||
|
58 | for (int i=0; i <mSeries.count(); i++) { | |||
|
59 | int temp = mSeries.at(i)->min(); | |||
|
60 | if (temp < min) { | |||
|
61 | min = temp; | |||
|
62 | } | |||
|
63 | } | |||
|
64 | return min; | |||
|
65 | } | |||
|
66 | ||||
|
67 | int BarChartModel::max() | |||
|
68 | { | |||
|
69 | Q_ASSERT(mSeries.count() > 0); | |||
|
70 | ||||
|
71 | // TODO: make min and max members and update them when data changes. | |||
|
72 | // This is slower since they are checked every time, even if data is same since previous call. | |||
|
73 | int max = INT_MIN; | |||
|
74 | ||||
|
75 | for (int i=0; i <mSeries.count(); i++) { | |||
|
76 | int temp = mSeries.at(i)->min(); | |||
|
77 | if (temp > max) { | |||
|
78 | max = temp; | |||
|
79 | } | |||
|
80 | } | |||
|
81 | return max; | |||
|
82 | } | |||
|
83 | ||||
|
84 | qreal BarChartModel::valueAt(int series, int item) | |||
|
85 | { | |||
|
86 | if ((series < 0) || (series >= mSeries.count())) { | |||
|
87 | // No series, no value. | |||
|
88 | return 0; | |||
|
89 | } else if ((item < 0) || (item >= mSeries.at(series)->countItems())) { | |||
|
90 | // No item, no value. | |||
|
91 | return 0; | |||
|
92 | } | |||
|
93 | ||||
|
94 | return mSeries.at(series)->valueAt(item); | |||
|
95 | } | |||
|
96 | ||||
|
97 | #include "moc_barchartmodel_p.cpp" | |||
|
98 | ||||
|
99 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -0,0 +1,45 | |||||
|
1 | #ifndef BARCHARTMODEL_H | |||
|
2 | #define BARCHARTMODEL_H | |||
|
3 | ||||
|
4 | #include <QObject> | |||
|
5 | #include "barchartseries.h" | |||
|
6 | ||||
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |||
|
8 | ||||
|
9 | // Model for bar chart. | |||
|
10 | // TODO: Implement as QAbstractItemModel? | |||
|
11 | ||||
|
12 | class BarChartModel : public QObject //, public QAbstractItemModel | |||
|
13 | { | |||
|
14 | Q_OBJECT | |||
|
15 | public: | |||
|
16 | explicit BarChartModel(QObject *parent = 0); | |||
|
17 | ||||
|
18 | // Takes reference, Series are owned by QChart or model? | |||
|
19 | void addSeries(BarChartSeriesBase& series); | |||
|
20 | void removeSeries(BarChartSeriesBase& series); | |||
|
21 | ||||
|
22 | int countSeries(); // Number of series in model | |||
|
23 | int countItemsInSeries(); // Maximum number of items in series | |||
|
24 | int countTotalItems(); // Total items in all series. Includes empty items. | |||
|
25 | ||||
|
26 | int max(); // Maximum value of all series | |||
|
27 | int min(); // Minimum value of all series | |||
|
28 | qreal valueAt(int series, int item); | |||
|
29 | ||||
|
30 | signals: | |||
|
31 | void modelUpdated(); | |||
|
32 | ||||
|
33 | public slots: | |||
|
34 | ||||
|
35 | private: | |||
|
36 | ||||
|
37 | // Data | |||
|
38 | QList<BarChartSeriesBase*> mSeries; | |||
|
39 | ||||
|
40 | BarChartModel* mSingle; | |||
|
41 | }; | |||
|
42 | ||||
|
43 | QTCOMMERCIALCHART_END_NAMESPACE | |||
|
44 | ||||
|
45 | #endif // BARCHARTMODEL_H |
@@ -1,109 +1,158 | |||||
1 | #include <limits.h> |
|
1 | #include <limits.h> | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include "barchartseriesbase.h" |
|
3 | #include "barchartseriesbase.h" | |
4 | #include "bargroup.h" |
|
4 | #include "bargroup.h" | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | BarChartSeriesBase::BarChartSeriesBase(QObject *parent) |
|
8 | BarChartSeriesBase::BarChartSeriesBase(QObject *parent) | |
9 | : QChartSeries(parent) |
|
9 | : QChartSeries(parent) | |
|
10 | ,mData(0) | |||
10 | { |
|
11 | { | |
11 | } |
|
12 | } | |
12 |
|
13 | /* | ||
13 |
bool BarChartSeriesBase::set |
|
14 | bool BarChartSeriesBase::setModel(QAbstractItemModel* model) | |
14 |
|
|
15 | { | |
15 | mModel = model; |
|
16 | mModel = model; | |
16 | return true; |
|
17 | return true; | |
17 |
|
|
18 | } | |
18 |
|
19 | */ | ||
|
20 | bool BarChartSeriesBase::setData(QList<qreal>& data) | |||
|
21 | { | |||
|
22 | mData = &data; | |||
|
23 | return true; | |||
|
24 | } | |||
|
25 | /* | |||
19 | int BarChartSeriesBase::min() |
|
26 | int BarChartSeriesBase::min() | |
20 |
|
|
27 | { | |
21 | Q_ASSERT(mModel->rowCount() > 0); |
|
28 | Q_ASSERT(mModel->rowCount() > 0); | |
22 | Q_ASSERT(mModel->columnCount() > 0); |
|
29 | Q_ASSERT(mModel->columnCount() > 0); | |
23 |
|
|
30 | ||
24 | // TODO: make min and max members and update them when data changes. |
|
31 | // TODO: make min and max members and update them when data changes. | |
25 | // This is slower since they are checked every time, even if data is same since previous call. |
|
32 | // This is slower since they are checked every time, even if data is same since previous call. | |
26 | int min = INT_MAX; |
|
33 | int min = INT_MAX; | |
27 |
|
|
34 | ||
28 | for (int i=0; i <mModel->rowCount(); i++) { |
|
35 | for (int i=0; i <mModel->rowCount(); i++) { | |
29 | for(int j=0; j<mModel->columnCount(); j++) { |
|
36 | for(int j=0; j<mModel->columnCount(); j++) { | |
30 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
37 | int temp = mModel->data(mModel->index(i,j)).toInt(); | |
31 | if (temp < min) { |
|
38 | if (temp < min) { | |
32 | min = temp; |
|
39 | min = temp; | |
33 | } |
|
40 | } | |
34 | } |
|
41 | } | |
35 | } |
|
42 | } | |
36 | return min; |
|
43 | return min; | |
37 |
|
|
44 | } | |
38 |
|
|
45 | ||
39 | int BarChartSeriesBase::max() |
|
46 | int BarChartSeriesBase::max() | |
40 |
|
|
47 | { | |
41 | Q_ASSERT(mModel->rowCount() > 0); |
|
48 | Q_ASSERT(mModel->rowCount() > 0); | |
42 | Q_ASSERT(mModel->columnCount() > 0); |
|
49 | Q_ASSERT(mModel->columnCount() > 0); | |
43 |
|
|
50 | ||
44 | // TODO: make min and max members and update them when data changes. |
|
51 | // TODO: make min and max members and update them when data changes. | |
45 | // This is slower since they are checked every time, even if data is same since previous call. |
|
52 | // This is slower since they are checked every time, even if data is same since previous call. | |
46 | int max = INT_MIN; |
|
53 | int max = INT_MIN; | |
47 |
|
|
54 | ||
48 | for (int i=0; i <mModel->rowCount(); i++) { |
|
55 | for (int i=0; i <mModel->rowCount(); i++) { | |
49 | for(int j=0; j<mModel->columnCount(); j++) { |
|
56 | for(int j=0; j<mModel->columnCount(); j++) { | |
50 | int temp = mModel->data(mModel->index(i,j)).toInt(); |
|
57 | int temp = mModel->data(mModel->index(i,j)).toInt(); | |
51 | if (temp > max) { |
|
58 | if (temp > max) { | |
52 | max = temp; |
|
59 | max = temp; | |
53 | } |
|
60 | } | |
54 | } |
|
61 | } | |
55 | } |
|
62 | } | |
56 | return max; |
|
63 | return max; | |
57 |
|
|
64 | } | |
58 |
|
|
65 | ||
59 | int BarChartSeriesBase::maxColumnSum() |
|
66 | int BarChartSeriesBase::maxColumnSum() | |
60 |
|
|
67 | { | |
61 | Q_ASSERT(mModel->rowCount() > 0); |
|
68 | Q_ASSERT(mModel->rowCount() > 0); | |
62 | Q_ASSERT(mModel->columnCount() > 0); |
|
69 | Q_ASSERT(mModel->columnCount() > 0); | |
63 |
|
|
70 | ||
64 | int max = INT_MIN; |
|
71 | int max = INT_MIN; | |
65 |
|
|
72 | ||
66 | for (int col=0; col <mModel->columnCount(); col++) { |
|
73 | for (int col=0; col <mModel->columnCount(); col++) { | |
67 | int sum = columnSum(col); |
|
74 | int sum = columnSum(col); | |
68 | if (sum > max) { |
|
75 | if (sum > max) { | |
69 | max = sum; |
|
76 | max = sum; | |
70 | } |
|
77 | } | |
71 | } |
|
78 | } | |
72 | return max; |
|
79 | return max; | |
73 |
|
|
80 | } | |
74 |
|
|
81 | ||
75 | int BarChartSeriesBase::countRows() |
|
82 | int BarChartSeriesBase::countRows() | |
76 |
|
|
83 | { | |
77 | return mModel->rowCount(); |
|
84 | return mModel->rowCount(); | |
78 |
|
|
85 | } | |
79 |
|
|
86 | ||
80 | int BarChartSeriesBase::countColumns() |
|
87 | int BarChartSeriesBase::countColumns() | |
81 |
|
|
88 | { | |
82 | return mModel->columnCount(); |
|
89 | return mModel->columnCount(); | |
83 |
|
|
90 | } | |
84 |
|
|
91 | ||
85 | int BarChartSeriesBase::countTotalItems() |
|
92 | int BarChartSeriesBase::countTotalItems() | |
86 |
|
|
93 | { | |
87 | return mModel->rowCount() * mModel->columnCount(); |
|
94 | return mModel->rowCount() * mModel->columnCount(); | |
88 |
|
|
95 | } | |
89 |
|
|
96 | ||
90 | int BarChartSeriesBase::valueAt(int row, int column) |
|
97 | int BarChartSeriesBase::valueAt(int row, int column) | |
91 |
|
|
98 | { | |
92 | QModelIndex index = mModel->index(row,column); |
|
99 | QModelIndex index = mModel->index(row,column); | |
93 | return mModel->data(index).toInt(); |
|
100 | return mModel->data(index).toInt(); | |
94 |
|
|
101 | } | |
95 |
|
|
102 | ||
96 | int BarChartSeriesBase::columnSum(int column) |
|
103 | int BarChartSeriesBase::columnSum(int column) | |
97 |
|
|
104 | { | |
98 | int sum(0); |
|
105 | int sum(0); | |
99 | int count = mModel->rowCount(); |
|
106 | int count = mModel->rowCount(); | |
100 |
|
|
107 | ||
101 | for (int row = 0; row < count; row++) { |
|
108 | for (int row = 0; row < count; row++) { | |
102 | sum += mModel->data(mModel->index(row,column)).toInt(); |
|
109 | sum += mModel->data(mModel->index(row,column)).toInt(); | |
103 | } |
|
110 | } | |
104 | return sum; |
|
111 | return sum; | |
105 |
|
|
112 | } | |
|
113 | */ | |||
|
114 | qreal BarChartSeriesBase::min() | |||
|
115 | { | |||
|
116 | Q_ASSERT(mData != 0); | |||
|
117 | ||||
|
118 | int count = mData->count(); | |||
|
119 | int min = INT_MAX; | |||
|
120 | ||||
|
121 | for (int i=0; i<count; i++) { | |||
|
122 | if (mData->at(i) < min) { | |||
|
123 | min = mData->at(i); | |||
|
124 | } | |||
|
125 | } | |||
|
126 | return min; | |||
|
127 | } | |||
|
128 | ||||
|
129 | qreal BarChartSeriesBase::max() | |||
|
130 | { | |||
|
131 | Q_ASSERT(mData != 0); | |||
|
132 | ||||
|
133 | int count = mData->count(); | |||
|
134 | int max = INT_MIN; | |||
|
135 | ||||
|
136 | for (int i=0; i<count; i++) { | |||
|
137 | if (mData->at(i) > max) { | |||
|
138 | max = mData->at(i); | |||
|
139 | } | |||
|
140 | } | |||
|
141 | return max; | |||
|
142 | } | |||
|
143 | ||||
|
144 | int BarChartSeriesBase::countItems() | |||
|
145 | { | |||
|
146 | Q_ASSERT(mData != 0); | |||
|
147 | return mData->count(); | |||
|
148 | } | |||
|
149 | ||||
|
150 | qreal BarChartSeriesBase::valueAt(int item) | |||
|
151 | { | |||
|
152 | Q_ASSERT(mData != 0); | |||
|
153 | return mData->at(item); | |||
|
154 | } | |||
106 |
|
155 | |||
107 | #include "moc_barchartseriesbase.cpp" |
|
156 | #include "moc_barchartseriesbase.cpp" | |
108 |
|
157 | |||
109 | QTCOMMERCIALCHART_END_NAMESPACE |
|
158 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,49 +1,58 | |||||
1 | #ifndef BARCHARTSERIESBASE_H |
|
1 | #ifndef BARCHARTSERIESBASE_H | |
2 | #define BARCHARTSERIESBASE_H |
|
2 | #define BARCHARTSERIESBASE_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 | #include "qchartseries.h" |
|
6 | #include "qchartseries.h" | |
7 | #include "qchartglobal.h" |
|
7 | #include "qchartglobal.h" | |
8 |
|
8 | |||
9 | class BarGroupBase; |
|
9 | class BarGroupBase; | |
10 |
|
10 | |||
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
12 |
|
12 | |||
13 | // Container for series |
|
13 | // Container for series | |
14 | class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries |
|
14 | class QTCOMMERCIALCHART_EXPORT BarChartSeriesBase : public QChartSeries | |
15 | { |
|
15 | { | |
16 | Q_OBJECT |
|
16 | Q_OBJECT | |
17 | protected: |
|
17 | protected: | |
18 | BarChartSeriesBase(QObject* parent=0); |
|
18 | BarChartSeriesBase(QObject* parent=0); | |
19 |
|
19 | |||
20 | public: |
|
20 | public: | |
21 | // from QChartSeries |
|
21 | // from QChartSeries | |
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; } |
|
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeInvalid; } | |
23 |
|
23 | |||
24 | // TODO: Better data model? |
|
24 | // TODO: Better data model? | |
25 |
virtual bool set |
|
25 | // virtual bool setModel(QAbstractItemModel* model); | |
|
26 | virtual bool setData(QList<qreal>& data); | |||
26 |
|
27 | |||
27 | // Methods to find out minimum and maximum values of data |
|
28 | // Methods to find out minimum and maximum values of data | |
28 | int min(); |
|
29 | // int min(); // TODO: remove | |
29 | int max(); |
|
30 | // int max(); // TODO: remove | |
30 |
int maxColumnSum(); |
|
31 | // int maxColumnSum(); // TODO: move to model. returns maximum sum of items in all columns. | |
31 |
|
32 | |||
32 | int countRows(); |
|
33 | // int countRows(); // TODO: remove. | |
33 | int countColumns(); // Count items in one series. |
|
34 | // int countColumns(); // TODO: remove. Count items in one series. | |
34 | int countTotalItems(); |
|
35 | // int countTotalItems(); // TODO: move to model | |
35 | int valueAt(int row, int column); |
|
36 | // int valueAt(int row, int column); // TODO: move to model | |
36 |
|
37 | |||
37 | int columnSum(int column); |
|
38 | // int columnSum(int column); // TODO: move to model | |
|
39 | ||||
|
40 | qreal min(); | |||
|
41 | qreal max(); | |||
|
42 | int countItems(); | |||
|
43 | qreal valueAt(int item); | |||
38 |
|
44 | |||
39 | public Q_SLOTS: |
|
45 | public Q_SLOTS: | |
40 |
|
46 | |||
41 | private: |
|
47 | private: | |
42 |
|
48 | |||
43 | QAbstractItemModel* mModel; |
|
49 | QAbstractItemModel* mModel; | |
44 | BarGroupBase* mBarGroup; |
|
50 | BarGroupBase* mBarGroup; | |
|
51 | ||||
|
52 | QList<qreal>* mData; | |||
|
53 | ||||
45 | }; |
|
54 | }; | |
46 |
|
55 | |||
47 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | QTCOMMERCIALCHART_END_NAMESPACE | |
48 |
|
57 | |||
49 | #endif // BARCHARTSERIESBASE_H |
|
58 | #endif // BARCHARTSERIESBASE_H |
@@ -1,65 +1,70 | |||||
1 | #include "bargroup.h" |
|
1 | #include "bargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include <QDebug> |
|
4 | #include <QDebug> | |
5 |
|
5 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
7 | |||
8 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : |
|
8 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | |
9 | BarGroupBase(series,parent) |
|
9 | BarGroupBase(series,parent) | |
10 | { |
|
10 | { | |
11 | mBarDefaultWidth = 10; |
|
11 | mBarDefaultWidth = 10; | |
12 | } |
|
12 | } | |
13 |
|
13 | |||
14 | void BarGroup::layoutChanged() |
|
14 | void BarGroup::layoutChanged() | |
15 | { |
|
15 | { | |
16 | // Scale bars to new layout |
|
16 | // Scale bars to new layout | |
17 | // Layout for bars: |
|
17 | // Layout for bars: | |
|
18 | /* | |||
18 | if (mSeries.countRows() <= 0) { |
|
19 | if (mSeries.countRows() <= 0) { | |
19 | // Nothing to do. |
|
20 | // Nothing to do. | |
20 | return; |
|
21 | return; | |
21 | } |
|
22 | } | |
|
23 | */ | |||
|
24 | if (mModel.countSeries() <= 0) { | |||
|
25 | return; | |||
|
26 | } | |||
22 |
|
27 | |||
23 | // TODO: better way to auto-layout? |
|
28 | // TODO: better way to auto-layout? | |
24 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
29 | // Use reals for accurancy (we might get some compiler warnings... :) | |
25 | int columnCount = mSeries.countColumns(); |
|
30 | int itemCount = mModel.countItemsInSeries(); | |
26 |
int |
|
31 | int seriesCount = mModel.countSeries(); | |
27 |
|
32 | |||
28 | qreal tW = mWidth; |
|
33 | qreal tW = mWidth; | |
29 | qreal tH = mHeight; |
|
34 | qreal tH = mHeight; | |
30 | qreal tM = mMax; |
|
35 | qreal tM = mMax; | |
31 | qreal scale = (tH/tM); |
|
36 | qreal scale = (tH/tM); | |
32 |
|
37 | |||
33 |
qreal tC = |
|
38 | qreal tC = itemCount+1; | |
34 | qreal xStepPerSeries = (tW/tC); |
|
39 | qreal xStepPerSeries = (tW/tC); | |
35 |
|
40 | |||
36 | // Scaling. |
|
41 | // Scaling. | |
37 | int itemIndex(0); |
|
42 | int itemIndex(0); | |
38 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
43 | int labelIndex = itemCount * seriesCount; | |
39 |
|
44 | |||
40 |
for (int |
|
45 | for (int item=0; item < itemCount; item++) { | |
41 |
qreal xPos = xStepPerSeries * |
|
46 | qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); | |
42 | qreal yPos = mHeight; |
|
47 | qreal yPos = mHeight; | |
43 |
for (int |
|
48 | for (int series = 0; series < seriesCount; series++) { | |
44 |
qreal barHeight = m |
|
49 | qreal barHeight = mModel.valueAt(series, item) * scale; | |
45 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
50 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
46 |
|
51 | |||
47 | // TODO: width settable per bar? |
|
52 | // TODO: width settable per bar? | |
48 | bar->resize(mBarDefaultWidth, barHeight); |
|
53 | bar->resize(mBarDefaultWidth, barHeight); | |
49 |
bar->setColor(mColors.at( |
|
54 | bar->setColor(mColors.at(series)); | |
50 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); |
|
55 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | |
51 | itemIndex++; |
|
56 | itemIndex++; | |
52 | xPos += mBarDefaultWidth; |
|
57 | xPos += mBarDefaultWidth; | |
53 | } |
|
58 | } | |
54 |
|
59 | |||
55 | // TODO: Layout for labels, remove magic number |
|
60 | // TODO: Layout for labels, remove magic number | |
56 |
xPos = xStepPerSeries * |
|
61 | xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); | |
57 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
62 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
58 | label->setPos(xPos, mHeight + 20); |
|
63 | label->setPos(xPos, mHeight + 20); | |
59 | labelIndex++; |
|
64 | labelIndex++; | |
60 | } |
|
65 | } | |
61 |
|
66 | |||
62 | mLayoutDirty = true; |
|
67 | mLayoutDirty = true; | |
63 | } |
|
68 | } | |
64 |
|
69 | |||
65 | QTCOMMERCIALCHART_END_NAMESPACE |
|
70 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,131 +1,133 | |||||
1 | #include "bargroupbase.h" |
|
1 | #include "bargroupbase.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include "barchartseriesbase.h" |
|
5 | #include "barchartseriesbase.h" | |
6 | #include <QDebug> |
|
6 | #include <QDebug> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent) |
|
10 | BarGroupBase::BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent) | |
11 | : ChartItem(parent) |
|
11 | : ChartItem(parent) | |
12 | ,mSeries(series) |
|
12 | // ,mSeries(series) | |
13 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
13 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |
14 | ,mLayoutSet(false) |
|
14 | ,mLayoutSet(false) | |
15 | ,mLayoutDirty(true) |
|
15 | ,mLayoutDirty(true) | |
16 | ,mTheme(0) |
|
16 | ,mTheme(0) | |
17 | ,mSeparatorsVisible(true) |
|
17 | ,mSeparatorsVisible(true) | |
18 | { |
|
18 | { | |
|
19 | mModel.addSeries(series); | |||
19 | dataChanged(); |
|
20 | dataChanged(); | |
20 | } |
|
21 | } | |
21 |
|
22 | |||
22 | void BarGroupBase::setSeparatorsVisible(bool visible) |
|
23 | void BarGroupBase::setSeparatorsVisible(bool visible) | |
23 | { |
|
24 | { | |
24 | mSeparatorsVisible = visible; |
|
25 | mSeparatorsVisible = visible; | |
25 | } |
|
26 | } | |
26 |
|
27 | |||
27 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
28 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
28 | { |
|
29 | { | |
29 | if (!mLayoutSet) { |
|
30 | if (!mLayoutSet) { | |
30 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; |
|
31 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; | |
31 | return; |
|
32 | return; | |
32 | } |
|
33 | } | |
33 | if (mLayoutDirty) { |
|
34 | if (mLayoutDirty) { | |
34 | // Layout or data has changed. Need to redraw. |
|
35 | // Layout or data has changed. Need to redraw. | |
35 | foreach(QGraphicsItem* i, childItems()) { |
|
36 | foreach(QGraphicsItem* i, childItems()) { | |
36 | i->paint(painter,option,widget); |
|
37 | i->paint(painter,option,widget); | |
37 | } |
|
38 | } | |
38 | } |
|
39 | } | |
39 | } |
|
40 | } | |
40 |
|
41 | |||
41 | QRectF BarGroupBase::boundingRect() const |
|
42 | QRectF BarGroupBase::boundingRect() const | |
42 | { |
|
43 | { | |
43 | return QRectF(0,0,mWidth,mHeight); |
|
44 | return QRectF(0,0,mWidth,mHeight); | |
44 | } |
|
45 | } | |
45 |
|
46 | |||
46 | void BarGroupBase::themeChanged(ChartTheme *theme) |
|
47 | void BarGroupBase::themeChanged(ChartTheme *theme) | |
47 | { |
|
48 | { | |
48 | mTheme = theme; |
|
49 | mTheme = theme; | |
49 | } |
|
50 | } | |
50 |
|
51 | |||
51 | void BarGroupBase::setBarWidth( int w ) |
|
52 | void BarGroupBase::setBarWidth( int w ) | |
52 | { |
|
53 | { | |
53 | mBarDefaultWidth = w; |
|
54 | mBarDefaultWidth = w; | |
54 | } |
|
55 | } | |
55 |
|
56 | |||
56 | int BarGroupBase::addColor( QColor color ) |
|
57 | int BarGroupBase::addColor( QColor color ) | |
57 | { |
|
58 | { | |
58 | int colorIndex = mColors.count(); |
|
59 | int colorIndex = mColors.count(); | |
59 | mColors.append(color); |
|
60 | mColors.append(color); | |
60 | return colorIndex; |
|
61 | return colorIndex; | |
61 | } |
|
62 | } | |
62 |
|
63 | |||
63 | void BarGroupBase::resetColors() |
|
64 | void BarGroupBase::resetColors() | |
64 | { |
|
65 | { | |
65 | mColors.clear(); |
|
66 | mColors.clear(); | |
66 | } |
|
67 | } | |
67 |
|
68 | |||
68 | void BarGroupBase::dataChanged() |
|
69 | void BarGroupBase::dataChanged() | |
69 | { |
|
70 | { | |
70 | qDebug() << "BarGroupBase::dataChanged"; |
|
71 | qDebug() << "BarGroupBase::dataChanged"; | |
71 |
|
72 | |||
72 | // Find out maximum and minimum of all series |
|
73 | // Find out maximum and minimum of all series. | |
73 | mMax = mSeries.max(); |
|
74 | // TODO: is this actually needed? | |
74 |
mM |
|
75 | // mMax = mModel.max(); | |
|
76 | // mMin = mModel.min(); | |||
75 |
|
77 | |||
76 | // Delete old bars |
|
78 | // Delete old bars | |
77 | foreach (QGraphicsItem* item, childItems()) { |
|
79 | foreach (QGraphicsItem* item, childItems()) { | |
78 | delete item; |
|
80 | delete item; | |
79 | } |
|
81 | } | |
80 |
|
82 | |||
81 | // Create new graphic items for bars |
|
83 | // Create new graphic items for bars | |
82 |
int totalItems = m |
|
84 | int totalItems = mModel.countTotalItems(); // mSeries.countTotalItems(); | |
83 | for (int i=0; i<totalItems; i++) { |
|
85 | for (int i=0; i<totalItems; i++) { | |
84 | Bar *bar = new Bar(this); |
|
86 | Bar *bar = new Bar(this); | |
85 | childItems().append(bar); |
|
87 | childItems().append(bar); | |
86 | } |
|
88 | } | |
87 |
|
89 | |||
88 | // TODO: labels from series. This creates just some example labels |
|
90 | // TODO: labels from series. This creates just some example labels | |
89 |
int count = m |
|
91 | int count = mModel.countItemsInSeries(); // mSeries.countColumns(); | |
90 | for (int i=0; i<count; i++) { |
|
92 | for (int i=0; i<count; i++) { | |
91 | BarLabel* label = new BarLabel(this); |
|
93 | BarLabel* label = new BarLabel(this); | |
92 | QString text("Label " + QString::number(i)); |
|
94 | QString text("Label " + QString::number(i)); | |
93 | label->set(text); |
|
95 | label->set(text); | |
94 | childItems().append(label); |
|
96 | childItems().append(label); | |
95 | } |
|
97 | } | |
96 |
|
98 | |||
97 |
count = m |
|
99 | count = mModel.countItemsInSeries() - 1; // mSeries.countColumns() - 1; // There is one less separator than columns | |
98 | for (int i=0; i<count; i++) { |
|
100 | for (int i=0; i<count; i++) { | |
99 | Separator* sep = new Separator(this); |
|
101 | Separator* sep = new Separator(this); | |
100 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme |
|
102 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme | |
101 | childItems().append(sep); |
|
103 | childItems().append(sep); | |
102 | } |
|
104 | } | |
103 |
|
105 | |||
104 | // TODO: if (autolayout) { layoutChanged() } or something |
|
106 | // TODO: if (autolayout) { layoutChanged() } or something | |
105 | mLayoutDirty = true; |
|
107 | mLayoutDirty = true; | |
106 | } |
|
108 | } | |
107 |
|
109 | |||
108 | //handlers |
|
110 | //handlers | |
109 |
|
111 | |||
110 | void BarGroupBase::handleModelChanged(int index) |
|
112 | void BarGroupBase::handleModelChanged(int index) | |
111 | { |
|
113 | { | |
112 | qDebug() << "BarGroupBase::handleModelChanged"; |
|
114 | qDebug() << "BarGroupBase::handleModelChanged"; | |
113 | } |
|
115 | } | |
114 |
|
116 | |||
115 | void BarGroupBase::handleDomainChanged(const Domain& domain) |
|
117 | void BarGroupBase::handleDomainChanged(const Domain& domain) | |
116 | { |
|
118 | { | |
117 | qDebug() << "BarGroupBase::handleModelChanged"; |
|
119 | qDebug() << "BarGroupBase::handleModelChanged"; | |
118 | } |
|
120 | } | |
119 |
|
121 | |||
120 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) |
|
122 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) | |
121 | { |
|
123 | { | |
122 | mWidth = rect.width(); |
|
124 | mWidth = rect.width(); | |
123 | mHeight = rect.height(); |
|
125 | mHeight = rect.height(); | |
124 | layoutChanged(); |
|
126 | layoutChanged(); | |
125 | mLayoutSet = true; |
|
127 | mLayoutSet = true; | |
126 | setPos(rect.topLeft()); |
|
128 | setPos(rect.topLeft()); | |
127 | } |
|
129 | } | |
128 |
|
130 | |||
129 | #include "moc_bargroupbase.cpp" |
|
131 | #include "moc_bargroupbase.cpp" | |
130 |
|
132 | |||
131 | QTCOMMERCIALCHART_END_NAMESPACE |
|
133 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,70 +1,74 | |||||
1 | #ifndef BARGROUPBASE_H |
|
1 | #ifndef BARGROUPBASE_H | |
2 | #define BARGROUPBASE_H |
|
2 | #define BARGROUPBASE_H | |
3 |
|
3 | |||
4 | #include "charttheme_p.h" |
|
4 | #include "charttheme_p.h" | |
5 | #include "chartitem_p.h" |
|
5 | #include "chartitem_p.h" | |
6 | //#include "barlabel_p.h" |
|
6 | //#include "barlabel_p.h" | |
7 | //#include "bar_p.h" |
|
7 | //#include "bar_p.h" | |
8 | #include "barchartseriesbase.h" |
|
8 | #include "barchartseriesbase.h" | |
|
9 | #include "barchartmodel_p.h" | |||
9 | #include <QGraphicsItem> |
|
10 | #include <QGraphicsItem> | |
10 |
|
11 | |||
11 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
12 |
|
13 | |||
13 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. |
|
14 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. | |
14 |
|
||||
15 | class BarGroupBase : public QObject, public ChartItem |
|
15 | class BarGroupBase : public QObject, public ChartItem | |
16 | { |
|
16 | { | |
17 | Q_OBJECT |
|
17 | Q_OBJECT | |
18 | public: |
|
18 | public: | |
19 | BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0); |
|
19 | BarGroupBase(BarChartSeriesBase& series, QGraphicsItem *parent = 0); | |
|
20 | // BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0); | |||
20 | void setSeparatorsVisible(bool visible = true); |
|
21 | void setSeparatorsVisible(bool visible = true); | |
21 |
|
22 | |||
22 | public: // From ChartItem |
|
23 | public: // From ChartItem | |
23 | void setSize(const QSizeF &size){}; |
|
24 | void setSize(const QSizeF &size){}; | |
24 | void setPlotDomain(const PlotDomain& data){}; |
|
25 | void setPlotDomain(const PlotDomain& data){}; | |
25 |
|
26 | |||
26 | // From QGraphicsItem |
|
27 | // From QGraphicsItem | |
27 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
28 | QRectF boundingRect() const; |
|
29 | QRectF boundingRect() const; | |
29 |
|
30 | |||
30 | // From ChartThemeObserver |
|
31 | // From ChartThemeObserver | |
31 | void themeChanged(ChartTheme *theme); |
|
32 | void themeChanged(ChartTheme *theme); | |
32 |
|
33 | |||
33 | // TODO: these may change with layout awarness. |
|
34 | // TODO: these may change with layout awarness. | |
34 | void setBarWidth( int w ); |
|
35 | void setBarWidth( int w ); | |
35 | int addColor( QColor color ); |
|
36 | int addColor( QColor color ); | |
36 | void resetColors(); |
|
37 | void resetColors(); | |
37 |
|
38 | |||
38 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
39 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
39 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes |
|
40 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes | |
40 |
|
41 | |||
41 | protected slots: |
|
42 | protected slots: | |
42 | void handleModelChanged(int index); |
|
43 | void handleModelChanged(int index); | |
43 | void handleDomainChanged(const Domain& domain); |
|
44 | void handleDomainChanged(const Domain& domain); | |
44 | void handleGeometryChanged(const QRectF& size); |
|
45 | void handleGeometryChanged(const QRectF& size); | |
45 |
|
46 | |||
46 |
|
47 | |||
47 | protected: |
|
48 | protected: | |
48 |
|
49 | |||
49 | BarChartSeriesBase& mSeries; |
|
50 | //BarChartSeriesBase& mSeries; | |
50 |
|
51 | |||
51 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) |
|
52 | // TODO: consider these. | |
|
53 | int mMin; // Min and max values of data. (updated when data is changed, used when drawing) | |||
52 | int mMax; |
|
54 | int mMax; | |
53 |
|
55 | |||
54 | int mHeight; // Layout spesific |
|
56 | int mHeight; // Layout spesific | |
55 | int mWidth; |
|
57 | int mWidth; | |
56 | int mBarDefaultWidth; |
|
58 | int mBarDefaultWidth; | |
57 |
|
59 | |||
58 | bool mLayoutSet; // True, if component has been laid out. |
|
60 | bool mLayoutSet; // True, if component has been laid out. | |
59 | bool mLayoutDirty; |
|
61 | bool mLayoutDirty; | |
60 |
|
62 | |||
61 | QList<QColor> mColors; // List of colors for series for now |
|
63 | QList<QColor> mColors; // List of colors for series for now | |
62 |
|
64 | |||
63 | ChartTheme* mTheme; |
|
65 | ChartTheme* mTheme; | |
64 | bool mSeparatorsVisible; |
|
66 | bool mSeparatorsVisible; | |
65 |
|
67 | |||
|
68 | BarChartModel mModel; | |||
|
69 | ||||
66 | }; |
|
70 | }; | |
67 |
|
71 | |||
68 | QTCOMMERCIALCHART_END_NAMESPACE |
|
72 | QTCOMMERCIALCHART_END_NAMESPACE | |
69 |
|
73 | |||
70 | #endif // BARGROUPBASE_H |
|
74 | #endif // BARGROUPBASE_H |
@@ -1,73 +1,74 | |||||
1 | #include "percentbargroup.h" |
|
1 | #include "percentbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
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 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : |
|
10 | PercentBarGroup::PercentBarGroup(PercentBarChartSeries& series, QGraphicsItem *parent) : | |
11 | BarGroupBase(series, parent) |
|
11 | BarGroupBase(series, parent) | |
12 | { |
|
12 | { | |
13 | } |
|
13 | } | |
14 |
|
14 | |||
15 | void PercentBarGroup::layoutChanged() |
|
15 | void PercentBarGroup::layoutChanged() | |
16 | { |
|
16 | { | |
|
17 | /* | |||
17 | // Scale bars to new layout |
|
18 | // Scale bars to new layout | |
18 | // Layout for bars: |
|
19 | // Layout for bars: | |
19 | if (mSeries.countRows() <= 0) { |
|
20 | if (mSeries.countRows() <= 0) { | |
20 | // Nothing to do. |
|
21 | // Nothing to do. | |
21 | return; |
|
22 | return; | |
22 | } |
|
23 | } | |
23 |
|
|
24 | ||
24 | // TODO: better way to auto-layout |
|
25 | // TODO: better way to auto-layout | |
25 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
26 | // Use reals for accurancy (we might get some compiler warnings... :) | |
26 | int count = mSeries.countColumns(); |
|
27 | int count = mSeries.countColumns(); | |
27 | int itemIndex(0); |
|
28 | int itemIndex(0); | |
28 | qreal tW = mWidth; |
|
29 | qreal tW = mWidth; | |
29 | qreal tC = count+1; |
|
30 | qreal tC = count+1; | |
30 | qreal xStep = (tW/tC); |
|
31 | qreal xStep = (tW/tC); | |
31 |
|
|
32 | // qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); | |
32 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
33 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
33 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
34 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); | |
34 |
|
|
35 | ||
35 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
36 | for (int column = 0; column < mSeries.countColumns(); column++) { | |
36 | qreal colSum = mSeries.columnSum(column); |
|
37 | qreal colSum = mSeries.columnSum(column); | |
37 | qreal h = mHeight; |
|
38 | qreal h = mHeight; | |
38 | qreal scale = (h / colSum); |
|
39 | qreal scale = (h / colSum); | |
39 | qreal yPos = h; |
|
40 | qreal yPos = h; | |
40 | for (int row=0; row < mSeries.countRows(); row++) { |
|
41 | for (int row=0; row < mSeries.countRows(); row++) { | |
41 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
42 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
42 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
43 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
43 |
|
|
44 | ||
44 | // TODO: width settable per bar? |
|
45 | // TODO: width settable per bar? | |
45 | bar->resize(mBarDefaultWidth, barHeight); |
|
46 | bar->resize(mBarDefaultWidth, barHeight); | |
46 | bar->setColor(mColors.at(row)); |
|
47 | bar->setColor(mColors.at(row)); | |
47 | bar->setPos(xPos, yPos-barHeight); |
|
48 | bar->setPos(xPos, yPos-barHeight); | |
48 | itemIndex++; |
|
49 | itemIndex++; | |
49 | yPos -= barHeight; |
|
50 | yPos -= barHeight; | |
50 | } |
|
51 | } | |
51 |
|
|
52 | ||
52 | // TODO: Layout for labels, remove magic number |
|
53 | // TODO: Layout for labels, remove magic number | |
53 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
54 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
54 | label->setPos(xPos, mHeight + 20); |
|
55 | label->setPos(xPos, mHeight + 20); | |
55 | labelIndex++; |
|
56 | labelIndex++; | |
56 | xPos += xStep; |
|
57 | xPos += xStep; | |
57 | } |
|
58 | } | |
58 |
|
|
59 | ||
59 | // Position separators |
|
60 | // Position separators | |
60 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
61 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |
61 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
62 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |
62 | for (int s=0; s < mSeries.countColumns() - 1; s++) { |
|
63 | for (int s=0; s < mSeries.countColumns() - 1; s++) { | |
63 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
64 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
64 | sep->setPos(xPos,0); |
|
65 | sep->setPos(xPos,0); | |
65 | sep->setSize(QSizeF(1,mHeight)); |
|
66 | sep->setSize(QSizeF(1,mHeight)); | |
66 | xPos += xStep; |
|
67 | xPos += xStep; | |
67 | separatorIndex++; |
|
68 | separatorIndex++; | |
68 | } |
|
69 | } | |
69 |
|
70 | */ | ||
70 | mLayoutDirty = true; |
|
71 | mLayoutDirty = true; | |
71 | } |
|
72 | } | |
72 |
|
73 | |||
73 | QTCOMMERCIALCHART_END_NAMESPACE |
|
74 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,81 +1,82 | |||||
1 | #include "stackedbargroup.h" |
|
1 | #include "stackedbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
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 | |||
10 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : |
|
10 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : | |
11 | BarGroupBase(series,parent) |
|
11 | BarGroupBase(series,parent) | |
12 | { |
|
12 | { | |
13 | } |
|
13 | } | |
14 |
|
14 | |||
15 | void StackedBarGroup::layoutChanged() |
|
15 | void StackedBarGroup::layoutChanged() | |
16 | { |
|
16 | { | |
|
17 | /* | |||
17 | // Scale bars to new layout |
|
18 | // Scale bars to new layout | |
18 | // Layout for bars: |
|
19 | // Layout for bars: | |
19 | if (mSeries.countRows() <= 0) { |
|
20 | if (mSeries.countRows() <= 0) { | |
20 | // Nothing to do. |
|
21 | // Nothing to do. | |
21 | return; |
|
22 | return; | |
22 | } |
|
23 | } | |
23 |
|
|
24 | ||
24 | if (mSeries.countColumns() == 0) { |
|
25 | if (mSeries.countColumns() == 0) { | |
25 | // Nothing to do |
|
26 | // Nothing to do | |
26 | return; |
|
27 | return; | |
27 | } |
|
28 | } | |
28 |
|
|
29 | ||
29 | // TODO: better way to auto-layout |
|
30 | // TODO: better way to auto-layout | |
30 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
31 | // Use reals for accurancy (we might get some compiler warnings... :) | |
31 | // TODO: use temp variable for column count... |
|
32 | // TODO: use temp variable for column count... | |
32 | qreal maxSum = mSeries.maxColumnSum(); |
|
33 | qreal maxSum = mSeries.maxColumnSum(); | |
33 | qreal h = mHeight; |
|
34 | qreal h = mHeight; | |
34 | qreal scale = (h / maxSum); |
|
35 | qreal scale = (h / maxSum); | |
35 |
|
|
36 | ||
36 | int itemIndex(0); |
|
37 | int itemIndex(0); | |
37 | qreal tW = mWidth; |
|
38 | qreal tW = mWidth; | |
38 | qreal tC = mSeries.countColumns() + 1; |
|
39 | qreal tC = mSeries.countColumns() + 1; | |
39 | qreal xStep = (tW/tC); |
|
40 | qreal xStep = (tW/tC); | |
40 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
41 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
41 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); |
|
42 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); | |
42 |
|
|
43 | ||
43 | for (int column = 0; column < mSeries.countColumns(); column++) { |
|
44 | for (int column = 0; column < mSeries.countColumns(); column++) { | |
44 | qreal yPos = h; |
|
45 | qreal yPos = h; | |
45 | for (int row=0; row < mSeries.countRows(); row++) { |
|
46 | for (int row=0; row < mSeries.countRows(); row++) { | |
46 | qreal barHeight = mSeries.valueAt(row, column) * scale; |
|
47 | qreal barHeight = mSeries.valueAt(row, column) * scale; | |
47 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
48 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
48 |
|
|
49 | ||
49 | // TODO: width settable per bar? |
|
50 | // TODO: width settable per bar? | |
50 | // TODO: how to get color for series(x) from theme? |
|
51 | // TODO: how to get color for series(x) from theme? | |
51 |
|
|
52 | // mTheme->themeForSeries(); | |
52 | bar->resize(mBarDefaultWidth, barHeight); |
|
53 | bar->resize(mBarDefaultWidth, barHeight); | |
53 | bar->setColor(mColors.at(row)); |
|
54 | bar->setColor(mColors.at(row)); | |
54 | bar->setPos(xPos, yPos-barHeight); |
|
55 | bar->setPos(xPos, yPos-barHeight); | |
55 | itemIndex++; |
|
56 | itemIndex++; | |
56 | yPos -= barHeight; |
|
57 | yPos -= barHeight; | |
57 | } |
|
58 | } | |
58 |
|
|
59 | ||
59 | // TODO: Layout for labels, remove magic number |
|
60 | // TODO: Layout for labels, remove magic number | |
60 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
61 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
61 | label->setPos(xPos, mHeight + 20); |
|
62 | label->setPos(xPos, mHeight + 20); | |
62 | labelIndex++; |
|
63 | labelIndex++; | |
63 | xPos += xStep; |
|
64 | xPos += xStep; | |
64 | } |
|
65 | } | |
65 |
|
|
66 | ||
66 |
|
|
67 | ||
67 | // Position separators |
|
68 | // Position separators | |
68 | 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? | |
69 | 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. | |
70 | for (int s=0; s < mSeries.countColumns() - 1; s++) { |
|
71 | for (int s=0; s < mSeries.countColumns() - 1; s++) { | |
71 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
72 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
72 | sep->setPos(xPos,0); |
|
73 | sep->setPos(xPos,0); | |
73 | sep->setSize(QSizeF(1,mHeight)); |
|
74 | sep->setSize(QSizeF(1,mHeight)); | |
74 | xPos += xStep; |
|
75 | xPos += xStep; | |
75 | separatorIndex++; |
|
76 | separatorIndex++; | |
76 | } |
|
77 | } | |
77 |
|
78 | */ | ||
78 | mLayoutDirty = true; |
|
79 | mLayoutDirty = true; | |
79 | } |
|
80 | } | |
80 |
|
81 | |||
81 | QTCOMMERCIALCHART_END_NAMESPACE |
|
82 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,112 +1,114 | |||||
1 | !include( ../common.pri ):error( Couldn't find the common.pri file! ) |
|
1 | !include( ../common.pri ):error( Couldn't find the common.pri file! ) | |
2 | TARGET = QtCommercialChart |
|
2 | TARGET = QtCommercialChart | |
3 | DESTDIR = $$CHART_BUILD_LIB_DIR |
|
3 | DESTDIR = $$CHART_BUILD_LIB_DIR | |
4 | TEMPLATE = lib |
|
4 | TEMPLATE = lib | |
5 | QT += core \ |
|
5 | QT += core \ | |
6 | gui |
|
6 | gui | |
7 | CONFIG += debug_and_release |
|
7 | CONFIG += debug_and_release | |
8 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd |
|
8 | CONFIG(debug, debug|release):TARGET = QtCommercialChartd | |
9 | SOURCES += barchart/barchartseries.cpp \ |
|
9 | SOURCES += barchart/barchartseries.cpp \ | |
10 | barchart/bargroup.cpp \ |
|
10 | barchart/bargroup.cpp \ | |
11 | barchart/bar.cpp \ |
|
11 | barchart/bar.cpp \ | |
12 | barchart/stackedbarchartseries.cpp \ |
|
12 | barchart/stackedbarchartseries.cpp \ | |
13 | barchart/stackedbargroup.cpp \ |
|
13 | barchart/stackedbargroup.cpp \ | |
14 | barchart/percentbarchartseries.cpp \ |
|
14 | barchart/percentbarchartseries.cpp \ | |
15 | barchart/percentbargroup.cpp \ |
|
15 | barchart/percentbargroup.cpp \ | |
16 | barchart/barlabel.cpp \ |
|
16 | barchart/barlabel.cpp \ | |
17 |
|
|
17 | barchart/barchartmodel.cpp \ | |
18 | linechart/linechartitem.cpp \ |
|
|||
19 | linechart/qlinechartseries.cpp \ |
|
|||
20 | barchart/separator.cpp \ |
|
18 | barchart/separator.cpp \ | |
21 | barchart/bargroupbase.cpp \ |
|
19 | barchart/bargroupbase.cpp \ | |
22 | barchart/barchartseriesbase.cpp \ |
|
20 | barchart/barchartseriesbase.cpp \ | |
|
21 | linechart/linechartanimationitem.cpp \ | |||
|
22 | linechart/linechartitem.cpp \ | |||
|
23 | linechart/qlinechartseries.cpp \ | |||
23 | qscatterseries.cpp \ |
|
24 | qscatterseries.cpp \ | |
24 | #scatterpresentation.cpp \ |
|
25 | #scatterpresentation.cpp \ | |
25 | qchart.cpp \ |
|
26 | qchart.cpp \ | |
26 | axisitem.cpp \ |
|
27 | axisitem.cpp \ | |
27 | qchartview.cpp \ |
|
28 | qchartview.cpp \ | |
28 | qchartseries.cpp \ |
|
29 | qchartseries.cpp \ | |
29 | qchartaxis.cpp \ |
|
30 | qchartaxis.cpp \ | |
30 | charttheme.cpp \ |
|
31 | charttheme.cpp \ | |
31 | chartdataset.cpp \ |
|
32 | chartdataset.cpp \ | |
32 | chartpresenter.cpp \ |
|
33 | chartpresenter.cpp \ | |
33 | domain.cpp \ |
|
34 | domain.cpp \ | |
34 | scatterpresenter.cpp |
|
35 | scatterpresenter.cpp | |
35 | PRIVATE_HEADERS += linechart/linechartitem_p.h \ |
|
36 | PRIVATE_HEADERS += linechart/linechartitem_p.h \ | |
36 | linechart/linechartanimationitem_p.h \ |
|
37 | linechart/linechartanimationitem_p.h \ | |
37 | barchart/barlabel_p.h \ |
|
38 | barchart/barlabel_p.h \ | |
38 | barchart/bar_p.h \ |
|
39 | barchart/bar_p.h \ | |
39 | barchart/separator_p.h \ |
|
40 | barchart/separator_p.h \ | |
|
41 | barchart/barchartmodel_p.h \ | |||
40 | qscatterseries_p.h \ |
|
42 | qscatterseries_p.h \ | |
41 | #scatterpresentation.h \ |
|
43 | #scatterpresentation.h \ | |
42 | axisitem_p.h \ |
|
44 | axisitem_p.h \ | |
43 | chartitem_p.h \ |
|
45 | chartitem_p.h \ | |
44 | charttheme_p.h \ |
|
46 | charttheme_p.h \ | |
45 | chartdataset_p.h \ |
|
47 | chartdataset_p.h \ | |
46 | chartpresenter_p.h \ |
|
48 | chartpresenter_p.h \ | |
47 | domain_p.h |
|
49 | domain_p.h | |
48 | PUBLIC_HEADERS += linechart/qlinechartseries.h \ |
|
50 | PUBLIC_HEADERS += linechart/qlinechartseries.h \ | |
49 | barchart/barchartseries.h \ |
|
51 | barchart/barchartseries.h \ | |
50 | barchart/bargroup.h \ |
|
52 | barchart/bargroup.h \ | |
51 | barchart/stackedbarchartseries.h \ |
|
53 | barchart/stackedbarchartseries.h \ | |
52 | barchart/stackedbargroup.h \ |
|
54 | barchart/stackedbargroup.h \ | |
53 | barchart/percentbarchartseries.h \ |
|
55 | barchart/percentbarchartseries.h \ | |
54 | barchart/percentbargroup.h \ |
|
56 | barchart/percentbargroup.h \ | |
55 | barchart/barchartseriesbase.h \ |
|
57 | barchart/barchartseriesbase.h \ | |
56 | barchart/bargroupbase.h \ |
|
58 | barchart/bargroupbase.h \ | |
57 | qchartseries.h \ |
|
59 | qchartseries.h \ | |
58 | qscatterseries.h \ |
|
60 | qscatterseries.h \ | |
59 | qchart.h \ |
|
61 | qchart.h \ | |
60 | qchartglobal.h \ |
|
62 | qchartglobal.h \ | |
61 | qchartview.h \ |
|
63 | qchartview.h \ | |
62 | qchartaxis.h |
|
64 | qchartaxis.h | |
63 |
|
65 | |||
64 | include(piechart/piechart.pri) |
|
66 | include(piechart/piechart.pri) | |
65 |
|
67 | |||
66 | THEMES += themes/chartthemeicy_p.h \ |
|
68 | THEMES += themes/chartthemeicy_p.h \ | |
67 | themes/chartthemegrayscale_p.h \ |
|
69 | themes/chartthemegrayscale_p.h \ | |
68 | themes/chartthemescientific_p.h \ |
|
70 | themes/chartthemescientific_p.h \ | |
69 | themes/chartthemevanilla_p.h |
|
71 | themes/chartthemevanilla_p.h | |
70 | HEADERS += $$PUBLIC_HEADERS \ |
|
72 | HEADERS += $$PUBLIC_HEADERS \ | |
71 | scatterpresenter.h |
|
73 | scatterpresenter.h | |
72 | HEADERS += $$PRIVATE_HEADERS |
|
74 | HEADERS += $$PRIVATE_HEADERS | |
73 | HEADERS += $$THEMES |
|
75 | HEADERS += $$THEMES | |
74 | INCLUDEPATH += linechart \ |
|
76 | INCLUDEPATH += linechart \ | |
75 | barchart \ |
|
77 | barchart \ | |
76 | themes \ |
|
78 | themes \ | |
77 | . |
|
79 | . | |
78 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib |
|
80 | OBJECTS_DIR = $$CHART_BUILD_DIR/lib | |
79 | MOC_DIR = $$CHART_BUILD_DIR/lib |
|
81 | MOC_DIR = $$CHART_BUILD_DIR/lib | |
80 | UI_DIR = $$CHART_BUILD_DIR/lib |
|
82 | UI_DIR = $$CHART_BUILD_DIR/lib | |
81 | RCC_DIR = $$CHART_BUILD_DIR/lib |
|
83 | RCC_DIR = $$CHART_BUILD_DIR/lib | |
82 | DEFINES += QTCOMMERCIALCHART_LIBRARY |
|
84 | DEFINES += QTCOMMERCIALCHART_LIBRARY | |
83 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart |
|
85 | public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart | |
84 | public_headers.files = $$PUBLIC_HEADERS |
|
86 | public_headers.files = $$PUBLIC_HEADERS | |
85 | target.path = $$[QT_INSTALL_LIBS] |
|
87 | target.path = $$[QT_INSTALL_LIBS] | |
86 | INSTALLS += target \ |
|
88 | INSTALLS += target \ | |
87 | public_headers |
|
89 | public_headers | |
88 | install_build_headers.name = bild_headers |
|
90 | install_build_headers.name = bild_headers | |
89 | install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h |
|
91 | install_build_headers.output = $$CHART_BUILD_HEADER_DIR/${QMAKE_FILE_BASE}.h | |
90 | install_build_headers.input = PUBLIC_HEADERS |
|
92 | install_build_headers.input = PUBLIC_HEADERS | |
91 | install_build_headers.commands = $$QMAKE_COPY \ |
|
93 | install_build_headers.commands = $$QMAKE_COPY \ | |
92 | ${QMAKE_FILE_NAME} \ |
|
94 | ${QMAKE_FILE_NAME} \ | |
93 | $$CHART_BUILD_HEADER_DIR |
|
95 | $$CHART_BUILD_HEADER_DIR | |
94 | install_build_headers.CONFIG += target_predeps \ |
|
96 | install_build_headers.CONFIG += target_predeps \ | |
95 | no_link |
|
97 | no_link | |
96 | QMAKE_EXTRA_COMPILERS += install_build_headers |
|
98 | QMAKE_EXTRA_COMPILERS += install_build_headers | |
97 | chartversion.target = qchartversion_p.h |
|
99 | chartversion.target = qchartversion_p.h | |
98 | chartversion.commands = @echo \ |
|
100 | chartversion.commands = @echo \ | |
99 | "build_time" \ |
|
101 | "build_time" \ | |
100 | > \ |
|
102 | > \ | |
101 | $$chartversion.target; |
|
103 | $$chartversion.target; | |
102 | chartversion.depends = $$HEADERS \ |
|
104 | chartversion.depends = $$HEADERS \ | |
103 | $$SOURCES |
|
105 | $$SOURCES | |
104 | PRE_TARGETDEPS += qchartversion_p.h |
|
106 | PRE_TARGETDEPS += qchartversion_p.h | |
105 | QMAKE_CLEAN += qchartversion_p.h |
|
107 | QMAKE_CLEAN += qchartversion_p.h | |
106 | QMAKE_EXTRA_TARGETS += chartversion |
|
108 | QMAKE_EXTRA_TARGETS += chartversion | |
107 | unix:QMAKE_DISTCLEAN += -r \ |
|
109 | unix:QMAKE_DISTCLEAN += -r \ | |
108 | $$CHART_BUILD_HEADER_DIR \ |
|
110 | $$CHART_BUILD_HEADER_DIR \ | |
109 | $$CHART_BUILD_LIB_DIR |
|
111 | $$CHART_BUILD_LIB_DIR | |
110 | win32:QMAKE_DISTCLEAN += /Q \ |
|
112 | win32:QMAKE_DISTCLEAN += /Q \ | |
111 | $$CHART_BUILD_HEADER_DIR \ |
|
113 | $$CHART_BUILD_HEADER_DIR \ | |
112 | $$CHART_BUILD_LIB_DIR |
|
114 | $$CHART_BUILD_LIB_DIR |
General Comments 0
You need to be logged in to leave comments.
Login now