@@ -1,151 +1,171 | |||||
1 | #include <limits.h> |
|
1 | #include <limits.h> | |
2 | #include <QVector> |
|
2 | #include <QVector> | |
3 | #include <QDebug> |
|
3 | #include <QDebug> | |
4 | #include "barchartmodel_p.h" |
|
4 | #include "barchartmodel_p.h" | |
5 | #include "qbarcategory.h" |
|
5 | #include "qbarcategory.h" | |
6 | #include "qbarset.h" |
|
6 | #include "qbarset.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | BarChartModel::BarChartModel(QBarCategory *category, QObject *parent) : |
|
10 | BarChartModel::BarChartModel(QBarCategory *category, QObject *parent) : | |
11 | QObject(parent) |
|
11 | QObject(parent) | |
12 | ,mCategory(category) |
|
12 | ,mCategory(category) | |
13 | { |
|
13 | { | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 | BarChartModel::~BarChartModel() |
|
16 | BarChartModel::~BarChartModel() | |
17 | { |
|
17 | { | |
18 | delete mCategory; |
|
18 | delete mCategory; | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 |
|
21 | |||
22 | QBarCategory& BarChartModel::category() |
|
22 | QBarCategory& BarChartModel::category() | |
23 | { |
|
23 | { | |
24 | return *mCategory; |
|
24 | return *mCategory; | |
25 | } |
|
25 | } | |
26 |
|
26 | |||
27 | void BarChartModel::addBarSet(QBarSet *set) |
|
27 | void BarChartModel::addBarSet(QBarSet *set) | |
28 | { |
|
28 | { | |
29 | mDataModel.append(set); |
|
29 | mDataModel.append(set); | |
30 | } |
|
30 | } | |
31 |
|
31 | |||
32 | void BarChartModel::removeBarSet(QBarSet *set) |
|
32 | void BarChartModel::removeBarSet(QBarSet *set) | |
33 | { |
|
33 | { | |
34 | if (mDataModel.contains(set)) { |
|
34 | if (mDataModel.contains(set)) { | |
35 | mDataModel.removeOne(set); |
|
35 | mDataModel.removeOne(set); | |
36 | delete set; |
|
|||
37 | } |
|
36 | } | |
38 | } |
|
37 | } | |
39 |
|
38 | |||
|
39 | QBarSet* BarChartModel::nextSet(bool getFirst) | |||
|
40 | { | |||
|
41 | if (getFirst) { | |||
|
42 | mCurrentSet = 0; | |||
|
43 | } | |||
|
44 | ||||
|
45 | if ((mDataModel.count() <= 0) || (mDataModel.count() <= mCurrentSet)) { | |||
|
46 | return 0; | |||
|
47 | } | |||
|
48 | ||||
|
49 | QBarSet* set = mDataModel.at(mCurrentSet); | |||
|
50 | mCurrentSet++; | |||
|
51 | return set; | |||
|
52 | } | |||
|
53 | ||||
|
54 | ||||
|
55 | QBarSet& BarChartModel::setAt(int index) | |||
|
56 | { | |||
|
57 | return *mDataModel.at(index); | |||
|
58 | } | |||
|
59 | ||||
40 | int BarChartModel::countSets() |
|
60 | int BarChartModel::countSets() | |
41 | { |
|
61 | { | |
42 | return mDataModel.count(); |
|
62 | return mDataModel.count(); | |
43 | } |
|
63 | } | |
44 |
|
64 | |||
45 | int BarChartModel::countCategories() |
|
65 | int BarChartModel::countCategories() | |
46 | { |
|
66 | { | |
47 | int count(0); |
|
67 | int count(0); | |
48 | for (int i=0; i<mDataModel.count(); i++){ |
|
68 | for (int i=0; i<mDataModel.count(); i++){ | |
49 | // TODO: can we assume that all series have same number of values? If not. then which values are empty? |
|
69 | // TODO: can we assume that all series have same number of values? If not. then which values are empty? | |
50 | int temp = mDataModel.at(i)->count(); |
|
70 | int temp = mDataModel.at(i)->count(); | |
51 | if (temp > count) { |
|
71 | if (temp > count) { | |
52 | count = temp; |
|
72 | count = temp; | |
53 | } |
|
73 | } | |
54 | } |
|
74 | } | |
55 | return count; |
|
75 | return count; | |
56 | } |
|
76 | } | |
57 |
|
77 | |||
58 | int BarChartModel::countTotalItems() |
|
78 | int BarChartModel::countTotalItems() | |
59 | { |
|
79 | { | |
60 | int total = mDataModel.count() * countCategories(); |
|
80 | int total = mDataModel.count() * countCategories(); | |
61 | return total; |
|
81 | return total; | |
62 | } |
|
82 | } | |
63 |
|
83 | |||
64 | qreal BarChartModel::min() |
|
84 | qreal BarChartModel::min() | |
65 | { |
|
85 | { | |
66 | Q_ASSERT(mDataModel.count() > 0); |
|
86 | Q_ASSERT(mDataModel.count() > 0); | |
67 | // TODO: make min and max members and update them when data changes. |
|
87 | // TODO: make min and max members and update them when data changes. | |
68 | // This is slower since they are checked every time, even if data is same since previous call. |
|
88 | // This is slower since they are checked every time, even if data is same since previous call. | |
69 | qreal min = INT_MAX; |
|
89 | qreal min = INT_MAX; | |
70 |
|
90 | |||
71 | for (int i=0; i <mDataModel.count(); i++) { |
|
91 | for (int i=0; i <mDataModel.count(); i++) { | |
72 | int itemCount = mDataModel.at(i)->count(); |
|
92 | int itemCount = mDataModel.at(i)->count(); | |
73 | for (int j=0; j<itemCount; j++) { |
|
93 | for (int j=0; j<itemCount; j++) { | |
74 | qreal temp = mDataModel.at(i)->valueAt(j); |
|
94 | qreal temp = mDataModel.at(i)->valueAt(j); | |
75 | if (temp < min) { |
|
95 | if (temp < min) { | |
76 | min = temp; |
|
96 | min = temp; | |
77 | } |
|
97 | } | |
78 | } |
|
98 | } | |
79 | } |
|
99 | } | |
80 | return min; |
|
100 | return min; | |
81 | } |
|
101 | } | |
82 |
|
102 | |||
83 | qreal BarChartModel::max() |
|
103 | qreal BarChartModel::max() | |
84 | { |
|
104 | { | |
85 | Q_ASSERT(mDataModel.count() > 0); |
|
105 | Q_ASSERT(mDataModel.count() > 0); | |
86 |
|
106 | |||
87 | // TODO: make min and max members and update them when data changes. |
|
107 | // TODO: make min and max members and update them when data changes. | |
88 | // This is slower since they are checked every time, even if data is same since previous call. |
|
108 | // This is slower since they are checked every time, even if data is same since previous call. | |
89 | qreal max = INT_MIN; |
|
109 | qreal max = INT_MIN; | |
90 |
|
110 | |||
91 | for (int i=0; i <mDataModel.count(); i++) { |
|
111 | for (int i=0; i <mDataModel.count(); i++) { | |
92 | int itemCount = mDataModel.at(i)->count(); |
|
112 | int itemCount = mDataModel.at(i)->count(); | |
93 | for (int j=0; j<itemCount; j++) { |
|
113 | for (int j=0; j<itemCount; j++) { | |
94 | qreal temp = mDataModel.at(i)->valueAt(j); |
|
114 | qreal temp = mDataModel.at(i)->valueAt(j); | |
95 | if (temp > max) { |
|
115 | if (temp > max) { | |
96 | max = temp; |
|
116 | max = temp; | |
97 | } |
|
117 | } | |
98 | } |
|
118 | } | |
99 | } |
|
119 | } | |
100 |
|
120 | |||
101 | return max; |
|
121 | return max; | |
102 | } |
|
122 | } | |
103 |
|
123 | |||
104 | qreal BarChartModel::valueAt(int set, int category) |
|
124 | qreal BarChartModel::valueAt(int set, int category) | |
105 | { |
|
125 | { | |
106 | if ((set < 0) || (set >= mDataModel.count())) { |
|
126 | if ((set < 0) || (set >= mDataModel.count())) { | |
107 | // No set, no value. |
|
127 | // No set, no value. | |
108 | return 0; |
|
128 | return 0; | |
109 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { |
|
129 | } else if ((category < 0) || (category >= mDataModel.at(set)->count())) { | |
110 | // No category, no value. |
|
130 | // No category, no value. | |
111 | return 0; |
|
131 | return 0; | |
112 | } |
|
132 | } | |
113 |
|
133 | |||
114 | return mDataModel.at(set)->valueAt(category); |
|
134 | return mDataModel.at(set)->valueAt(category); | |
115 | } |
|
135 | } | |
116 |
|
136 | |||
117 | qreal BarChartModel::categorySum(int column) |
|
137 | qreal BarChartModel::categorySum(int column) | |
118 | { |
|
138 | { | |
119 | qreal sum(0); |
|
139 | qreal sum(0); | |
120 | int count = mDataModel.count(); // Count rows |
|
140 | int count = mDataModel.count(); // Count rows | |
121 |
|
141 | |||
122 | for (int row = 0; row < count; row++) { |
|
142 | for (int row = 0; row < count; row++) { | |
123 | if (column < mDataModel.at(row)->count()) { |
|
143 | if (column < mDataModel.at(row)->count()) { | |
124 | sum += mDataModel.at(row)->valueAt(column); |
|
144 | sum += mDataModel.at(row)->valueAt(column); | |
125 | } |
|
145 | } | |
126 | } |
|
146 | } | |
127 | return sum; |
|
147 | return sum; | |
128 | } |
|
148 | } | |
129 |
|
149 | |||
130 | qreal BarChartModel::maxCategorySum() |
|
150 | qreal BarChartModel::maxCategorySum() | |
131 | { |
|
151 | { | |
132 | qreal max = INT_MIN; |
|
152 | qreal max = INT_MIN; | |
133 | int count = countCategories(); |
|
153 | int count = countCategories(); | |
134 |
|
154 | |||
135 | for (int col=0; col<count; col++) { |
|
155 | for (int col=0; col<count; col++) { | |
136 | qreal sum = categorySum(col); |
|
156 | qreal sum = categorySum(col); | |
137 | if (sum > max) { |
|
157 | if (sum > max) { | |
138 | max = sum; |
|
158 | max = sum; | |
139 | } |
|
159 | } | |
140 | } |
|
160 | } | |
141 | return max; |
|
161 | return max; | |
142 | } |
|
162 | } | |
143 |
|
163 | |||
144 | QString BarChartModel::label(int category) |
|
164 | QString BarChartModel::label(int category) | |
145 | { |
|
165 | { | |
146 | return mCategory->label(category); |
|
166 | return mCategory->label(category); | |
147 | } |
|
167 | } | |
148 |
|
168 | |||
149 | #include "moc_barchartmodel_p.cpp" |
|
169 | #include "moc_barchartmodel_p.cpp" | |
150 |
|
170 | |||
151 | QTCOMMERCIALCHART_END_NAMESPACE |
|
171 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,53 +1,58 | |||||
1 | #ifndef BARCHARTMODEL_H |
|
1 | #ifndef BARCHARTMODEL_H | |
2 | #define BARCHARTMODEL_H |
|
2 | #define BARCHARTMODEL_H | |
3 |
|
3 | |||
4 | #include <QObject> |
|
4 | #include <QObject> | |
5 | #include "qchartglobal.h" |
|
5 | #include "qchartglobal.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | // Model for bar chart. Internal class. |
|
9 | // Model for bar chart. Internal class. | |
10 | // TODO: Implement as QAbstractItemModel? |
|
10 | // TODO: Implement as QAbstractItemModel? | |
11 |
|
11 | |||
12 | class QBarSet; |
|
12 | class QBarSet; | |
13 | class QBarCategory; |
|
13 | class QBarCategory; | |
14 |
|
14 | |||
15 | class BarChartModel : public QObject //, public QAbstractItemModel |
|
15 | class BarChartModel : public QObject //, public QAbstractItemModel | |
16 | { |
|
16 | { | |
17 | Q_OBJECT |
|
17 | Q_OBJECT | |
18 | public: |
|
18 | public: | |
19 | explicit BarChartModel(QBarCategory *category, QObject *parent = 0); |
|
19 | explicit BarChartModel(QBarCategory *category, QObject *parent = 0); | |
20 | ~BarChartModel(); |
|
20 | ~BarChartModel(); | |
21 |
|
21 | |||
22 | QBarCategory& category(); |
|
22 | QBarCategory& category(); | |
23 | void addBarSet(QBarSet *set); |
|
23 | void addBarSet(QBarSet *set); | |
24 | void removeBarSet(QBarSet *set); |
|
24 | void removeBarSet(QBarSet *set); | |
|
25 | QBarSet* nextSet(bool getFirst); | |||
|
26 | ||||
|
27 | QBarSet& setAt(int index); // Internal | |||
25 |
|
28 | |||
26 | int countSets(); // Number of sets in model |
|
29 | int countSets(); // Number of sets in model | |
27 | int countCategories(); // Number of categories |
|
30 | int countCategories(); // Number of categories | |
28 | int countTotalItems(); // Total items in all sets. Includes empty items. |
|
31 | int countTotalItems(); // Total items in all sets. Includes empty items. | |
29 |
|
32 | |||
30 | qreal max(); // Maximum value of all sets |
|
33 | qreal max(); // Maximum value of all sets | |
31 | qreal min(); // Minimum value of all sets |
|
34 | qreal min(); // Minimum value of all sets | |
32 | qreal valueAt(int set, int category); |
|
35 | qreal valueAt(int set, int category); | |
33 |
|
36 | |||
34 | qreal categorySum(int column); |
|
37 | qreal categorySum(int column); | |
35 | qreal maxCategorySum(); // returns maximum sum of sets in all categories. |
|
38 | qreal maxCategorySum(); // returns maximum sum of sets in all categories. | |
36 |
|
39 | |||
37 | QString label(int category); |
|
40 | QString label(int category); | |
38 |
|
41 | |||
39 | signals: |
|
42 | signals: | |
40 | void modelUpdated(); |
|
43 | void modelUpdated(); | |
41 |
|
44 | |||
42 | public slots: |
|
45 | public slots: | |
43 |
|
46 | |||
44 | private: |
|
47 | private: | |
45 |
|
48 | |||
46 | QList<QBarSet*> mDataModel; |
|
49 | QList<QBarSet*> mDataModel; | |
47 | QBarCategory* mCategory; // Owned |
|
50 | QBarCategory* mCategory; // Owned | |
48 |
|
51 | |||
|
52 | int mCurrentSet; | |||
|
53 | ||||
49 | }; |
|
54 | }; | |
50 |
|
55 | |||
51 | QTCOMMERCIALCHART_END_NAMESPACE |
|
56 | QTCOMMERCIALCHART_END_NAMESPACE | |
52 |
|
57 | |||
53 | #endif // BARCHARTMODEL_H |
|
58 | #endif // BARCHARTMODEL_H |
@@ -1,59 +1,69 | |||||
1 | #include <QDebug> |
|
1 | #include <QDebug> | |
2 | #include "barchartseries.h" |
|
2 | #include "barchartseries.h" | |
3 | #include "qbarcategory.h" |
|
3 | #include "qbarcategory.h" | |
4 | #include "qbarset.h" |
|
4 | #include "qbarset.h" | |
5 | #include "barchartmodel_p.h" |
|
5 | #include "barchartmodel_p.h" | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | BarChartSeries::BarChartSeries(QBarCategory *category, QObject *parent) |
|
10 | BarChartSeries::BarChartSeries(QBarCategory *category, QObject *parent) | |
11 | : QChartSeries(parent) |
|
11 | : QChartSeries(parent) | |
12 | ,mModel(new BarChartModel(category, this)) |
|
12 | ,mModel(new BarChartModel(category, this)) | |
13 | { |
|
13 | { | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 | void BarChartSeries::addBarSet(QBarSet *set) |
|
16 | void BarChartSeries::addBarSet(QBarSet *set) | |
17 | { |
|
17 | { | |
18 | mModel->addBarSet(set); |
|
18 | mModel->addBarSet(set); | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | void BarChartSeries::removeBarSet(QBarSet *set) |
|
21 | void BarChartSeries::removeBarSet(QBarSet *set) | |
22 | { |
|
22 | { | |
23 | mModel->removeBarSet(set); |
|
23 | mModel->removeBarSet(set); | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
|
26 | int BarChartSeries::countSets() | |||
|
27 | { | |||
|
28 | return mModel->countSets(); | |||
|
29 | } | |||
|
30 | ||||
|
31 | QBarSet* BarChartSeries::nextSet(bool getFirst) | |||
|
32 | { | |||
|
33 | return mModel->nextSet(getFirst); | |||
|
34 | } | |||
|
35 | ||||
26 | int BarChartSeries::countCategories() |
|
36 | int BarChartSeries::countCategories() | |
27 | { |
|
37 | { | |
28 | return mModel->countCategories(); |
|
38 | return mModel->countCategories(); | |
29 | } |
|
39 | } | |
30 |
|
40 | |||
31 | qreal BarChartSeries::min() |
|
41 | qreal BarChartSeries::min() | |
32 | { |
|
42 | { | |
33 | return mModel->min(); |
|
43 | return mModel->min(); | |
34 | } |
|
44 | } | |
35 |
|
45 | |||
36 | qreal BarChartSeries::max() |
|
46 | qreal BarChartSeries::max() | |
37 | { |
|
47 | { | |
38 | return mModel->max(); |
|
48 | return mModel->max(); | |
39 | } |
|
49 | } | |
40 |
|
50 | |||
41 | qreal BarChartSeries::valueAt(int set, int category) |
|
51 | qreal BarChartSeries::valueAt(int set, int category) | |
42 | { |
|
52 | { | |
43 | return mModel->valueAt(set,category); |
|
53 | return mModel->valueAt(set,category); | |
44 | } |
|
54 | } | |
45 |
|
55 | |||
46 | qreal BarChartSeries::maxCategorySum() |
|
56 | qreal BarChartSeries::maxCategorySum() | |
47 | { |
|
57 | { | |
48 | return mModel->maxCategorySum(); |
|
58 | return mModel->maxCategorySum(); | |
49 | } |
|
59 | } | |
50 |
|
60 | |||
51 | BarChartModel& BarChartSeries::model() |
|
61 | BarChartModel& BarChartSeries::model() | |
52 | { |
|
62 | { | |
53 | return *mModel; |
|
63 | return *mModel; | |
54 | } |
|
64 | } | |
55 |
|
65 | |||
56 |
|
66 | |||
57 | #include "moc_barchartseries.cpp" |
|
67 | #include "moc_barchartseries.cpp" | |
58 |
|
68 | |||
59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
69 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,54 +1,56 | |||||
1 | #ifndef BARCHARTSERIES_H |
|
1 | #ifndef BARCHARTSERIES_H | |
2 | #define BARCHARTSERIES_H |
|
2 | #define BARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 | #include "qchartseries.h" |
|
6 | #include "qchartseries.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class QBarCategory; |
|
10 | class QBarCategory; | |
11 | class QBarSet; |
|
11 | class QBarSet; | |
12 | class BarGroup; |
|
12 | class BarGroup; | |
13 | class BarChartModel; |
|
13 | class BarChartModel; | |
14 |
|
14 | |||
15 | // Container for series |
|
15 | // Container for series | |
16 | class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries |
|
16 | class QTCOMMERCIALCHART_EXPORT BarChartSeries : public QChartSeries | |
17 | { |
|
17 | { | |
18 | Q_OBJECT |
|
18 | Q_OBJECT | |
19 | public: |
|
19 | public: | |
20 | BarChartSeries(QBarCategory *category, QObject* parent=0); |
|
20 | BarChartSeries(QBarCategory *category, QObject* parent=0); | |
21 |
|
21 | |||
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } |
|
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeBar; } | |
23 |
|
23 | |||
24 | void addBarSet(QBarSet *set); // Takes ownership |
|
24 | void addBarSet(QBarSet *set); // Takes ownership of set | |
25 |
void removeBarSet(QBarSet *set); // |
|
25 | void removeBarSet(QBarSet *set); // Removes set, releases ownership. | |
|
26 | int countSets(); | |||
|
27 | QBarSet* nextSet(bool getFirst=false); // Returns first set, if called with true | |||
26 |
|
28 | |||
27 | //TODO: |
|
29 | //TODO: | |
28 | //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series) |
|
30 | //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series) | |
29 |
|
31 | |||
30 | // TODO: Functions below this are not part of api and will be moved |
|
32 | // TODO: Functions below this are not part of api and will be moved | |
31 | // to private implementation, when we start using it (not part of api) |
|
33 | // to private implementation, when we start using it (not part of api) | |
32 | int countCategories(); |
|
34 | int countCategories(); | |
33 | qreal min(); |
|
35 | qreal min(); | |
34 | qreal max(); |
|
36 | qreal max(); | |
35 | qreal valueAt(int set, int category); |
|
37 | qreal valueAt(int set, int category); | |
36 | qreal maxCategorySum(); |
|
38 | qreal maxCategorySum(); | |
37 |
|
39 | |||
38 | BarChartModel& model(); |
|
40 | BarChartModel& model(); | |
39 |
|
41 | |||
40 | signals: |
|
42 | signals: | |
41 | void changed(int index); |
|
43 | void changed(int index); | |
42 |
|
44 | |||
43 | public Q_SLOTS: |
|
45 | public Q_SLOTS: | |
44 |
|
46 | |||
45 | private: |
|
47 | private: | |
46 |
|
48 | |||
47 | BarGroup* mBarGroup; |
|
49 | BarGroup* mBarGroup; | |
48 | BarChartModel* mModel; |
|
50 | BarChartModel* mModel; | |
49 |
|
51 | |||
50 | }; |
|
52 | }; | |
51 |
|
53 | |||
52 | QTCOMMERCIALCHART_END_NAMESPACE |
|
54 | QTCOMMERCIALCHART_END_NAMESPACE | |
53 |
|
55 | |||
54 | #endif // BARCHARTSERIES_H |
|
56 | #endif // BARCHARTSERIES_H |
@@ -1,73 +1,71 | |||||
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 "qbarset.h" | |||
4 | #include <QDebug> |
|
5 | #include <QDebug> | |
5 |
|
6 | |||
6 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
7 |
|
8 | |||
8 | BarGroup::BarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
9 | BarGroup::BarGroup(BarChartModel& model, QGraphicsItem *parent) : | |
9 | BarGroupBase(model,parent) |
|
10 | BarGroupBase(model,parent) | |
10 | { |
|
11 | { | |
11 | mBarDefaultWidth = 5; |
|
12 | mBarDefaultWidth = 5; | |
12 | } |
|
13 | } | |
13 |
|
14 | |||
14 | void BarGroup::layoutChanged() |
|
15 | void BarGroup::layoutChanged() | |
15 | { |
|
16 | { | |
16 | // qDebug() << "BarGroup::layoutChanged"; |
|
17 | // qDebug() << "BarGroup::layoutChanged"; | |
17 | // Scale bars to new layout |
|
18 | // Scale bars to new layout | |
18 | // Layout for bars: |
|
19 | // Layout for bars: | |
19 | if (mModel.countSets() <= 0) { |
|
20 | if (mModel.countSets() <= 0) { | |
20 | qDebug() << "No sets in model!"; |
|
21 | qDebug() << "No sets in model!"; | |
21 | return; |
|
22 | return; | |
22 | } |
|
23 | } | |
23 |
|
24 | |||
24 | if (childItems().count() == 0) { |
|
25 | if (childItems().count() == 0) { | |
25 | qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!"; |
|
26 | qDebug() << "WARNING: BarGroup::layoutChanged called before graphics items are created!"; | |
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 | int itemCount = mModel.countCategories(); |
|
32 | int itemCount = mModel.countCategories(); | |
32 | int setCount = mModel.countSets(); |
|
33 | int setCount = mModel.countSets(); | |
33 |
|
34 | |||
34 | qreal tW = mWidth; |
|
35 | qreal tW = mWidth; | |
35 | qreal tH = mHeight; |
|
36 | qreal tH = mHeight; | |
36 | qreal tM = mModel.max(); |
|
37 | qreal tM = mModel.max(); | |
37 | qreal scale = (tH/tM); |
|
38 | qreal scale = (tH/tM); | |
38 | qreal tC = itemCount+1; |
|
39 | qreal tC = itemCount+1; | |
39 | qreal xStepPerSet = (tW/tC); |
|
40 | qreal xStepPerSet = (tW/tC); | |
40 |
|
41 | |||
41 | // Scaling. |
|
42 | // Scaling. | |
42 | int itemIndex(0); |
|
43 | int itemIndex(0); | |
43 | int labelIndex = itemCount * setCount; |
|
44 | int labelIndex = itemCount * setCount; | |
44 |
|
45 | |||
45 | for (int item=0; item < itemCount; item++) { |
|
46 | for (int item=0; item < itemCount; item++) { | |
46 | qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); |
|
47 | qreal xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); | |
47 | qreal yPos = mHeight; |
|
48 | qreal yPos = mHeight; | |
48 | for (int set = 0; set < setCount; set++) { |
|
49 | for (int set = 0; set < setCount; set++) { | |
49 | qreal barHeight = mModel.valueAt(set, item) * scale; |
|
50 | qreal barHeight = mModel.valueAt(set, item) * scale; | |
50 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
51 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
51 |
|
52 | |||
52 | // TODO: width settable per bar? |
|
53 | // TODO: width settable per bar? | |
53 | bar->resize(mBarDefaultWidth, barHeight); |
|
54 | bar->resize(mBarDefaultWidth, barHeight); | |
54 |
bar->setBrush(m |
|
55 | bar->setBrush(mModel.setAt(set).brush()); | |
55 | // bar->setPen(mModel.barSet(set).pen()); |
|
|||
56 | // bar->setColor(mColors.at(set)); |
|
|||
57 | // bar->setPen(); |
|
|||
58 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight); |
|
56 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + set * mBarDefaultWidth, mHeight); | |
59 | itemIndex++; |
|
57 | itemIndex++; | |
60 | xPos += mBarDefaultWidth; |
|
58 | xPos += mBarDefaultWidth; | |
61 | } |
|
59 | } | |
62 |
|
60 | |||
63 | // TODO: Layout for labels, remove magic number |
|
61 | // TODO: Layout for labels, remove magic number | |
64 | xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); |
|
62 | xPos = xStepPerSet * item + ((tW + mBarDefaultWidth*setCount)/(itemCount*2)); | |
65 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
63 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
66 | label->setPos(xPos, mHeight + 20); |
|
64 | label->setPos(xPos, mHeight + 20); | |
67 | labelIndex++; |
|
65 | labelIndex++; | |
68 | } |
|
66 | } | |
69 |
|
67 | |||
70 | mLayoutDirty = true; |
|
68 | mLayoutDirty = true; | |
71 | } |
|
69 | } | |
72 |
|
70 | |||
73 | QTCOMMERCIALCHART_END_NAMESPACE |
|
71 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,137 +1,121 | |||||
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 <QDebug> |
|
5 | #include <QDebug> | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | BarGroupBase::BarGroupBase(BarChartModel& model, QGraphicsItem *parent) |
|
9 | BarGroupBase::BarGroupBase(BarChartModel& model, QGraphicsItem *parent) | |
10 | : ChartItem(parent) |
|
10 | : ChartItem(parent) | |
11 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready |
|
11 | ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready | |
12 | ,mLayoutSet(false) |
|
12 | ,mLayoutSet(false) | |
13 | ,mLayoutDirty(true) |
|
13 | ,mLayoutDirty(true) | |
14 | ,mSeparatorsVisible(true) |
|
14 | ,mSeparatorsVisible(true) | |
15 | ,mModel(model) |
|
15 | ,mModel(model) | |
16 | { |
|
16 | { | |
17 | dataChanged(); |
|
17 | dataChanged(); | |
18 | } |
|
18 | } | |
19 |
|
19 | |||
20 | void BarGroupBase::setSeparatorsVisible(bool visible) |
|
20 | void BarGroupBase::setSeparatorsVisible(bool visible) | |
21 | { |
|
21 | { | |
22 | mSeparatorsVisible = visible; |
|
22 | mSeparatorsVisible = visible; | |
23 | } |
|
23 | } | |
24 |
|
24 | |||
25 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
25 | void BarGroupBase::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
26 | { |
|
26 | { | |
27 | // qDebug() << "BarGroupBase::paint" << childItems().count(); |
|
27 | // qDebug() << "BarGroupBase::paint" << childItems().count(); | |
28 | if (!mLayoutSet) { |
|
28 | if (!mLayoutSet) { | |
29 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; |
|
29 | qDebug() << "BarGroupBase::paint called without layout set. Aborting."; | |
30 | return; |
|
30 | return; | |
31 | } |
|
31 | } | |
32 | // if (mLayoutDirty) { |
|
32 | // if (mLayoutDirty) { | |
33 | // Layout or data has changed. Need to redraw. |
|
33 | // Layout or data has changed. Need to redraw. | |
34 | foreach(QGraphicsItem* i, childItems()) { |
|
34 | foreach(QGraphicsItem* i, childItems()) { | |
35 | i->paint(painter,option,widget); |
|
35 | i->paint(painter,option,widget); | |
36 | } |
|
36 | } | |
37 | // } |
|
37 | // } | |
38 | } |
|
38 | } | |
39 |
|
39 | |||
40 | QRectF BarGroupBase::boundingRect() const |
|
40 | QRectF BarGroupBase::boundingRect() const | |
41 | { |
|
41 | { | |
42 | return QRectF(0,0,mWidth,mHeight); |
|
42 | return QRectF(0,0,mWidth,mHeight); | |
43 | } |
|
43 | } | |
44 |
|
44 | |||
45 | void BarGroupBase::setBarWidth( int w ) |
|
45 | void BarGroupBase::setBarWidth( int w ) | |
46 | { |
|
46 | { | |
47 | mBarDefaultWidth = w; |
|
47 | mBarDefaultWidth = w; | |
48 | } |
|
48 | } | |
49 | /* |
|
49 | /* | |
50 | int BarGroupBase::addColor( QColor color ) |
|
|||
51 | { |
|
|||
52 | // qDebug() << "BarGroupBase::addColor"; |
|
|||
53 | int colorIndex = mColors.count(); |
|
|||
54 | mColors.append(color); |
|
|||
55 | return colorIndex; |
|
|||
56 | } |
|
|||
57 | */ |
|
|||
58 | /* |
|
|||
59 | void BarGroupBase::resetColors() |
|
|||
60 | { |
|
|||
61 | // qDebug() << "BarGroupBase::resetColors"; |
|
|||
62 | mColors.clear(); |
|
|||
63 | } |
|
|||
64 | */ |
|
|||
65 | void BarGroupBase::resetBrushes() |
|
50 | void BarGroupBase::resetBrushes() | |
66 |
|
|
51 | { | |
67 | mBrushes.clear(); |
|
52 | mBrushes.clear(); | |
68 |
|
|
53 | } | |
69 |
|
|
54 | ||
70 | void BarGroupBase::addBrush(QBrush brush) |
|
55 | void BarGroupBase::addBrush(QBrush brush) | |
71 |
|
|
56 | { | |
72 | mBrushes.append(brush); |
|
57 | mBrushes.append(brush); | |
73 |
|
|
58 | } | |
74 |
|
59 | */ | ||
75 |
|
||||
76 | void BarGroupBase::dataChanged() |
|
60 | void BarGroupBase::dataChanged() | |
77 | { |
|
61 | { | |
78 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? |
|
62 | // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? | |
79 |
|
63 | |||
80 | // Delete old bars |
|
64 | // Delete old bars | |
81 | foreach (QGraphicsItem* item, childItems()) { |
|
65 | foreach (QGraphicsItem* item, childItems()) { | |
82 | delete item; |
|
66 | delete item; | |
83 | } |
|
67 | } | |
84 |
|
68 | |||
85 | // Create new graphic items for bars |
|
69 | // Create new graphic items for bars | |
86 | int totalItems = mModel.countTotalItems(); |
|
70 | int totalItems = mModel.countTotalItems(); | |
87 | for (int i=0; i<totalItems; i++) { |
|
71 | for (int i=0; i<totalItems; i++) { | |
88 | Bar *bar = new Bar(this); |
|
72 | Bar *bar = new Bar(this); | |
89 | childItems().append(bar); |
|
73 | childItems().append(bar); | |
90 | } |
|
74 | } | |
91 |
|
75 | |||
92 | int count = mModel.countCategories(); |
|
76 | int count = mModel.countCategories(); | |
93 | for (int i=0; i<count; i++) { |
|
77 | for (int i=0; i<count; i++) { | |
94 | BarLabel* label = new BarLabel(this); |
|
78 | BarLabel* label = new BarLabel(this); | |
95 | label->set(mModel.label(i)); |
|
79 | label->set(mModel.label(i)); | |
96 | childItems().append(label); |
|
80 | childItems().append(label); | |
97 | } |
|
81 | } | |
98 |
|
82 | |||
99 | count = mModel.countCategories() - 1; // There is one less separator than columns |
|
83 | count = mModel.countCategories() - 1; // There is one less separator than columns | |
100 | for (int i=0; i<count; i++) { |
|
84 | for (int i=0; i<count; i++) { | |
101 | Separator* sep = new Separator(this); |
|
85 | Separator* sep = new Separator(this); | |
102 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme |
|
86 | sep->setColor(QColor(255,0,0,255)); // TODO: color for separations from theme | |
103 | childItems().append(sep); |
|
87 | childItems().append(sep); | |
104 | } |
|
88 | } | |
105 |
|
89 | |||
106 | // TODO: if (autolayout) { layoutChanged() } or something |
|
90 | // TODO: if (autolayout) { layoutChanged() } or something | |
107 | mLayoutDirty = true; |
|
91 | mLayoutDirty = true; | |
108 | } |
|
92 | } | |
109 |
|
93 | |||
110 | //handlers |
|
94 | //handlers | |
111 |
|
95 | |||
112 | void BarGroupBase::handleModelChanged(int index) |
|
96 | void BarGroupBase::handleModelChanged(int index) | |
113 | { |
|
97 | { | |
114 | // qDebug() << "BarGroupBase::handleModelChanged" << index; |
|
98 | // qDebug() << "BarGroupBase::handleModelChanged" << index; | |
115 | dataChanged(); |
|
99 | dataChanged(); | |
116 | } |
|
100 | } | |
117 |
|
101 | |||
118 | void BarGroupBase::handleDomainChanged(const Domain& domain) |
|
102 | void BarGroupBase::handleDomainChanged(const Domain& domain) | |
119 | { |
|
103 | { | |
120 | // qDebug() << "BarGroupBase::handleDomainChanged"; |
|
104 | // qDebug() << "BarGroupBase::handleDomainChanged"; | |
121 | // TODO: Figure out the use case for this. |
|
105 | // TODO: Figure out the use case for this. | |
122 | // Affects the size of visible item, so layout is changed. |
|
106 | // Affects the size of visible item, so layout is changed. | |
123 | // layoutChanged(); |
|
107 | // layoutChanged(); | |
124 | } |
|
108 | } | |
125 |
|
109 | |||
126 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) |
|
110 | void BarGroupBase::handleGeometryChanged(const QRectF& rect) | |
127 | { |
|
111 | { | |
128 | mWidth = rect.width(); |
|
112 | mWidth = rect.width(); | |
129 | mHeight = rect.height(); |
|
113 | mHeight = rect.height(); | |
130 | layoutChanged(); |
|
114 | layoutChanged(); | |
131 | mLayoutSet = true; |
|
115 | mLayoutSet = true; | |
132 | setPos(rect.topLeft()); |
|
116 | setPos(rect.topLeft()); | |
133 | } |
|
117 | } | |
134 |
|
118 | |||
135 | #include "moc_bargroupbase.cpp" |
|
119 | #include "moc_bargroupbase.cpp" | |
136 |
|
120 | |||
137 | QTCOMMERCIALCHART_END_NAMESPACE |
|
121 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,70 +1,68 | |||||
1 | #ifndef BARGROUPBASE_H |
|
1 | #ifndef BARGROUPBASE_H | |
2 | #define BARGROUPBASE_H |
|
2 | #define BARGROUPBASE_H | |
3 |
|
3 | |||
4 | #include "chartitem_p.h" |
|
4 | #include "chartitem_p.h" | |
5 | #include "barchartmodel_p.h" |
|
5 | #include "barchartmodel_p.h" | |
6 | #include <QPen> |
|
6 | #include <QPen> | |
7 | #include <QBrush> |
|
7 | #include <QBrush> | |
8 | #include <QGraphicsItem> |
|
8 | #include <QGraphicsItem> | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. |
|
12 | // Base Class for bar groups. Common implemantation of different groups. Not to be instantiated. | |
13 | class BarGroupBase : public QObject, public ChartItem |
|
13 | class BarGroupBase : public QObject, public ChartItem | |
14 | { |
|
14 | { | |
15 | Q_OBJECT |
|
15 | Q_OBJECT | |
16 | public: |
|
16 | public: | |
17 | BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0); |
|
17 | BarGroupBase(BarChartModel& model, QGraphicsItem *parent = 0); | |
18 | void setSeparatorsVisible(bool visible = true); |
|
18 | void setSeparatorsVisible(bool visible = true); | |
19 |
|
19 | |||
20 | public: |
|
20 | public: | |
21 |
|
21 | |||
22 | // From QGraphicsItem |
|
22 | // From QGraphicsItem | |
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
24 | QRectF boundingRect() const; |
|
24 | QRectF boundingRect() const; | |
25 |
|
25 | |||
26 | // TODO: these may change with layout awarness. |
|
26 | // TODO: these may change with layout awarness. | |
27 | void setBarWidth( int w ); |
|
27 | void setBarWidth( int w ); | |
28 | // int addColor( QColor color ); |
|
|||
29 | // void resetColors(); |
|
|||
30 |
|
28 | |||
31 | void resetBrushes(); |
|
29 | // void resetBrushes(); | |
32 | void addBrush(QBrush brush); |
|
30 | // void addBrush(QBrush brush); | |
33 |
|
31 | |||
34 | void setPen(QPen pen); |
|
32 | void setPen(QPen pen); | |
35 | QPen pen(); |
|
33 | QPen pen(); | |
36 |
|
34 | |||
37 | void setBrush(QBrush brush); |
|
35 | void setBrush(QBrush brush); | |
38 | QBrush brush(); |
|
36 | QBrush brush(); | |
39 |
|
37 | |||
40 |
|
38 | |||
41 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it |
|
39 | // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it | |
42 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
40 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes | |
43 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes |
|
41 | virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes | |
44 |
|
42 | |||
45 | protected slots: |
|
43 | protected slots: | |
46 | void handleModelChanged(int index); |
|
44 | void handleModelChanged(int index); | |
47 | void handleDomainChanged(const Domain& domain); |
|
45 | void handleDomainChanged(const Domain& domain); | |
48 | void handleGeometryChanged(const QRectF& size); |
|
46 | void handleGeometryChanged(const QRectF& size); | |
49 |
|
47 | |||
50 | protected: |
|
48 | protected: | |
51 |
|
49 | |||
52 | // TODO: consider these. |
|
50 | // TODO: consider these. | |
53 | int mHeight; // Layout spesific |
|
51 | int mHeight; // Layout spesific | |
54 | int mWidth; |
|
52 | int mWidth; | |
55 | int mBarDefaultWidth; |
|
53 | int mBarDefaultWidth; | |
56 |
|
54 | |||
57 | bool mLayoutSet; // True, if component has been laid out. |
|
55 | bool mLayoutSet; // True, if component has been laid out. | |
58 | bool mLayoutDirty; |
|
56 | bool mLayoutDirty; | |
59 |
|
57 | |||
60 | QList<QColor> mColors; // List of colors for series for now |
|
58 | QList<QColor> mColors; // List of colors for series for now | |
61 | bool mSeparatorsVisible; |
|
59 | bool mSeparatorsVisible; | |
62 | BarChartModel& mModel; |
|
60 | BarChartModel& mModel; | |
63 |
|
61 | |||
64 | QPen mPen; |
|
62 | QPen mPen; | |
65 | QList<QBrush> mBrushes; |
|
63 | // QList<QBrush> mBrushes; | |
66 | }; |
|
64 | }; | |
67 |
|
65 | |||
68 | QTCOMMERCIALCHART_END_NAMESPACE |
|
66 | QTCOMMERCIALCHART_END_NAMESPACE | |
69 |
|
67 | |||
70 | #endif // BARGROUPBASE_H |
|
68 | #endif // BARGROUPBASE_H |
@@ -1,62 +1,72 | |||||
1 | #include "percentbarchartseries.h" |
|
1 | #include "percentbarchartseries.h" | |
2 |
|
2 | |||
3 | #include <limits.h> |
|
3 | #include <limits.h> | |
4 | #include <QDebug> |
|
4 | #include <QDebug> | |
5 | #include "percentbarchartseries.h" |
|
5 | #include "percentbarchartseries.h" | |
6 | #include "qbarset.h" |
|
6 | #include "qbarset.h" | |
7 | #include "qbarcategory.h" |
|
7 | #include "qbarcategory.h" | |
8 | #include "barchartmodel_p.h" |
|
8 | #include "barchartmodel_p.h" | |
9 |
|
9 | |||
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
11 |
|
11 | |||
12 | PercentBarChartSeries::PercentBarChartSeries(QBarCategory *category, QObject *parent) |
|
12 | PercentBarChartSeries::PercentBarChartSeries(QBarCategory *category, QObject *parent) | |
13 | : QChartSeries(parent) |
|
13 | : QChartSeries(parent) | |
14 | ,mModel(new BarChartModel(category, this)) |
|
14 | ,mModel(new BarChartModel(category, this)) | |
15 | { |
|
15 | { | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | void PercentBarChartSeries::addBarSet(QBarSet *set) |
|
18 | void PercentBarChartSeries::addBarSet(QBarSet *set) | |
19 | { |
|
19 | { | |
20 | mModel->addBarSet(set); |
|
20 | mModel->addBarSet(set); | |
21 | } |
|
21 | } | |
22 |
|
22 | |||
23 | void PercentBarChartSeries::removeBarSet(QBarSet *set) |
|
23 | void PercentBarChartSeries::removeBarSet(QBarSet *set) | |
24 | { |
|
24 | { | |
25 | mModel->removeBarSet(set); |
|
25 | mModel->removeBarSet(set); | |
26 | } |
|
26 | } | |
27 |
|
27 | |||
|
28 | int PercentBarChartSeries::countSets() | |||
|
29 | { | |||
|
30 | return mModel->countSets(); | |||
|
31 | } | |||
|
32 | ||||
|
33 | QBarSet* PercentBarChartSeries::nextSet(bool getFirst) | |||
|
34 | { | |||
|
35 | return mModel->nextSet(getFirst); | |||
|
36 | } | |||
|
37 | ||||
28 | int PercentBarChartSeries::countCategories() |
|
38 | int PercentBarChartSeries::countCategories() | |
29 | { |
|
39 | { | |
30 | return mModel->countCategories(); |
|
40 | return mModel->countCategories(); | |
31 | } |
|
41 | } | |
32 |
|
42 | |||
33 | qreal PercentBarChartSeries::min() |
|
43 | qreal PercentBarChartSeries::min() | |
34 | { |
|
44 | { | |
35 | return mModel->min(); |
|
45 | return mModel->min(); | |
36 | } |
|
46 | } | |
37 |
|
47 | |||
38 | qreal PercentBarChartSeries::max() |
|
48 | qreal PercentBarChartSeries::max() | |
39 | { |
|
49 | { | |
40 | return mModel->max(); |
|
50 | return mModel->max(); | |
41 | } |
|
51 | } | |
42 |
|
52 | |||
43 | qreal PercentBarChartSeries::valueAt(int set, int category) |
|
53 | qreal PercentBarChartSeries::valueAt(int set, int category) | |
44 | { |
|
54 | { | |
45 | return mModel->valueAt(set,category); |
|
55 | return mModel->valueAt(set,category); | |
46 | } |
|
56 | } | |
47 |
|
57 | |||
48 | qreal PercentBarChartSeries::maxCategorySum() |
|
58 | qreal PercentBarChartSeries::maxCategorySum() | |
49 | { |
|
59 | { | |
50 | return mModel->maxCategorySum(); |
|
60 | return mModel->maxCategorySum(); | |
51 | } |
|
61 | } | |
52 |
|
62 | |||
53 | BarChartModel& PercentBarChartSeries::model() |
|
63 | BarChartModel& PercentBarChartSeries::model() | |
54 | { |
|
64 | { | |
55 | return *mModel; |
|
65 | return *mModel; | |
56 | } |
|
66 | } | |
57 |
|
67 | |||
58 |
|
68 | |||
59 | #include "moc_percentbarchartseries.cpp" |
|
69 | #include "moc_percentbarchartseries.cpp" | |
60 |
|
70 | |||
61 | QTCOMMERCIALCHART_END_NAMESPACE |
|
71 | QTCOMMERCIALCHART_END_NAMESPACE | |
62 |
|
72 |
@@ -1,56 +1,58 | |||||
1 | #ifndef PERCENTBARCHARTSERIES_H |
|
1 | #ifndef PERCENTBARCHARTSERIES_H | |
2 | #define PERCENTBARCHARTSERIES_H |
|
2 | #define PERCENTBARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 | #include "qchartseries.h" |
|
6 | #include "qchartseries.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class PercentBarGroup; |
|
10 | class PercentBarGroup; | |
11 | class QBarCategory; |
|
11 | class QBarCategory; | |
12 | class QBarSet; |
|
12 | class QBarSet; | |
13 | class BarChartModel; |
|
13 | class BarChartModel; | |
14 |
|
14 | |||
15 | class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public QChartSeries |
|
15 | class QTCOMMERCIALCHART_EXPORT PercentBarChartSeries : public QChartSeries | |
16 | { |
|
16 | { | |
17 | Q_OBJECT |
|
17 | Q_OBJECT | |
18 | public: |
|
18 | public: | |
19 | PercentBarChartSeries(QBarCategory *category, QObject* parent=0); |
|
19 | PercentBarChartSeries(QBarCategory *category, QObject* parent=0); | |
20 |
|
20 | |||
21 | // from BarChartSeriesBase |
|
21 | // from BarChartSeriesBase | |
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; } |
|
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypePercentBar; } | |
23 |
|
23 | |||
24 | void addBarSet(QBarSet *set); // Takes ownership |
|
24 | void addBarSet(QBarSet *set); // Takes ownership | |
25 | void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned. |
|
25 | void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned. | |
|
26 | int countSets(); | |||
|
27 | QBarSet* nextSet(bool first=false); // Returns first set, if called with true | |||
26 |
|
28 | |||
27 | //TODO: |
|
29 | //TODO: | |
28 | //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series) |
|
30 | //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series) | |
29 |
|
31 | |||
30 | // TODO: Functions below this are not part of api and will be moved |
|
32 | // TODO: Functions below this are not part of api and will be moved | |
31 | // to private implementation, when we start using it (not part of api) |
|
33 | // to private implementation, when we start using it (not part of api) | |
32 | int countCategories(); |
|
34 | int countCategories(); | |
33 | qreal min(); |
|
35 | qreal min(); | |
34 | qreal max(); |
|
36 | qreal max(); | |
35 | qreal valueAt(int set, int category); |
|
37 | qreal valueAt(int set, int category); | |
36 | qreal maxCategorySum(); |
|
38 | qreal maxCategorySum(); | |
37 |
|
39 | |||
38 | BarChartModel& model(); |
|
40 | BarChartModel& model(); | |
39 |
|
41 | |||
40 | signals: |
|
42 | signals: | |
41 | void changed(int index); |
|
43 | void changed(int index); | |
42 |
|
44 | |||
43 | public Q_SLOTS: |
|
45 | public Q_SLOTS: | |
44 |
|
46 | |||
45 |
|
47 | |||
46 | private: |
|
48 | private: | |
47 |
|
49 | |||
48 | PercentBarGroup* mPercentBarGroup; |
|
50 | PercentBarGroup* mPercentBarGroup; | |
49 | BarChartModel* mModel; |
|
51 | BarChartModel* mModel; | |
50 |
|
52 | |||
51 | }; |
|
53 | }; | |
52 |
|
54 | |||
53 | QTCOMMERCIALCHART_END_NAMESPACE |
|
55 | QTCOMMERCIALCHART_END_NAMESPACE | |
54 |
|
56 | |||
55 |
|
57 | |||
56 | #endif // PERCENTBARCHARTSERIES_H |
|
58 | #endif // PERCENTBARCHARTSERIES_H |
@@ -1,81 +1,80 | |||||
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 "qbarset.h" | |||
5 | #include <QDebug> |
|
6 | #include <QDebug> | |
6 |
|
7 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
9 | |||
9 |
|
10 | |||
10 | PercentBarGroup::PercentBarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
11 | PercentBarGroup::PercentBarGroup(BarChartModel& model, QGraphicsItem *parent) : | |
11 | BarGroupBase(model, parent) |
|
12 | BarGroupBase(model, parent) | |
12 | { |
|
13 | { | |
13 | } |
|
14 | } | |
14 |
|
15 | |||
15 | void PercentBarGroup::layoutChanged() |
|
16 | void PercentBarGroup::layoutChanged() | |
16 | { |
|
17 | { | |
17 | // qDebug() << "PercentBarGroup::layoutChanged"; |
|
18 | // qDebug() << "PercentBarGroup::layoutChanged"; | |
18 | // Scale bars to new layout |
|
19 | // Scale bars to new layout | |
19 | // Layout for bars: |
|
20 | // Layout for bars: | |
20 | if (mModel.countSets() <= 0) { |
|
21 | if (mModel.countSets() <= 0) { | |
21 | qDebug() << "No sets in model!"; |
|
22 | qDebug() << "No sets in model!"; | |
22 | // Nothing to do. |
|
23 | // Nothing to do. | |
23 | return; |
|
24 | return; | |
24 | } |
|
25 | } | |
25 |
|
26 | |||
26 | if (childItems().count() == 0) { |
|
27 | if (childItems().count() == 0) { | |
27 | qDebug() << "WARNING: PercentBarGroup::layoutChanged called before graphics items are created!"; |
|
28 | qDebug() << "WARNING: PercentBarGroup::layoutChanged called before graphics items are created!"; | |
28 | return; |
|
29 | return; | |
29 | } |
|
30 | } | |
30 |
|
31 | |||
31 | // TODO: better way to auto-layout |
|
32 | // TODO: better way to auto-layout | |
32 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
33 | // Use reals for accurancy (we might get some compiler warnings... :) | |
33 | int count = mModel.countCategories(); |
|
34 | int count = mModel.countCategories(); | |
34 | int itemIndex(0); |
|
35 | int itemIndex(0); | |
35 | qreal tW = mWidth; |
|
36 | qreal tW = mWidth; | |
36 | qreal tC = count+1; |
|
37 | qreal tC = count+1; | |
37 | qreal xStep = (tW/tC); |
|
38 | qreal xStep = (tW/tC); | |
38 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
39 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
39 | int labelIndex = mModel.countCategories() * mModel.countSets(); |
|
40 | int labelIndex = mModel.countCategories() * mModel.countSets(); | |
40 |
|
41 | |||
41 | for (int category = 0; category < mModel.countCategories(); category++) { |
|
42 | for (int category = 0; category < mModel.countCategories(); category++) { | |
42 | qreal colSum = mModel.categorySum(category); |
|
43 | qreal colSum = mModel.categorySum(category); | |
43 | qreal h = mHeight; |
|
44 | qreal h = mHeight; | |
44 | qreal scale = (h / colSum); |
|
45 | qreal scale = (h / colSum); | |
45 | qreal yPos = h; |
|
46 | qreal yPos = h; | |
46 | for (int set=0; set < mModel.countSets(); set++) { |
|
47 | for (int set=0; set < mModel.countSets(); set++) { | |
47 | qreal barHeight = mModel.valueAt(set, category) * scale; |
|
48 | qreal barHeight = mModel.valueAt(set, category) * scale; | |
48 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
49 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
49 |
|
50 | |||
50 | // TODO: width settable per bar? |
|
51 | // TODO: width settable per bar? | |
51 | bar->resize(mBarDefaultWidth, barHeight); |
|
52 | bar->resize(mBarDefaultWidth, barHeight); | |
52 |
bar->setBrush(m |
|
53 | bar->setBrush(mModel.setAt(set).brush()); | |
53 | // bar->setBrush(mBrush); |
|
|||
54 | // bar->setColor(mColors.at(set)); |
|
|||
55 | bar->setPos(xPos, yPos-barHeight); |
|
54 | bar->setPos(xPos, yPos-barHeight); | |
56 | itemIndex++; |
|
55 | itemIndex++; | |
57 | yPos -= barHeight; |
|
56 | yPos -= barHeight; | |
58 | } |
|
57 | } | |
59 |
|
58 | |||
60 | // TODO: Layout for labels, remove magic number |
|
59 | // TODO: Layout for labels, remove magic number | |
61 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
60 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
62 | label->setPos(xPos, mHeight + 20); |
|
61 | label->setPos(xPos, mHeight + 20); | |
63 | labelIndex++; |
|
62 | labelIndex++; | |
64 | xPos += xStep; |
|
63 | xPos += xStep; | |
65 | } |
|
64 | } | |
66 |
|
65 | |||
67 | // Position separators |
|
66 | // Position separators | |
68 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
67 | 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. |
|
68 | 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 < mModel.countCategories() - 1; s++) { |
|
69 | for (int s=0; s < mModel.countCategories() - 1; s++) { | |
71 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
70 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
72 | sep->setPos(xPos,0); |
|
71 | sep->setPos(xPos,0); | |
73 | sep->setSize(QSizeF(1,mHeight)); |
|
72 | sep->setSize(QSizeF(1,mHeight)); | |
74 | xPos += xStep; |
|
73 | xPos += xStep; | |
75 | separatorIndex++; |
|
74 | separatorIndex++; | |
76 | } |
|
75 | } | |
77 |
|
76 | |||
78 | mLayoutDirty = true; |
|
77 | mLayoutDirty = true; | |
79 | } |
|
78 | } | |
80 |
|
79 | |||
81 | QTCOMMERCIALCHART_END_NAMESPACE |
|
80 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,41 +1,63 | |||||
1 | #include "qbarset.h" |
|
1 | #include "qbarset.h" | |
2 |
|
2 | |||
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
4 |
|
4 | |||
5 | QBarSet::QBarSet() |
|
5 | QBarSet::QBarSet() | |
6 | { |
|
6 | { | |
7 | } |
|
7 | } | |
8 |
|
8 | |||
9 | void QBarSet::setName(QString name) |
|
9 | void QBarSet::setName(QString name) | |
10 | { |
|
10 | { | |
11 | mName = name; |
|
11 | mName = name; | |
12 | } |
|
12 | } | |
13 | QString QBarSet::name() |
|
13 | QString QBarSet::name() | |
14 | { |
|
14 | { | |
15 | return mName; |
|
15 | return mName; | |
16 | } |
|
16 | } | |
17 |
|
17 | |||
18 | QBarSet& QBarSet::operator << (const qreal &value) |
|
18 | QBarSet& QBarSet::operator << (const qreal &value) | |
19 | { |
|
19 | { | |
20 | mValues.append(value); |
|
20 | mValues.append(value); | |
21 | return *this; |
|
21 | return *this; | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | int QBarSet::count() |
|
24 | int QBarSet::count() | |
25 | { |
|
25 | { | |
26 | return mValues.count(); |
|
26 | return mValues.count(); | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
29 | qreal QBarSet::valueAt(int index) |
|
29 | qreal QBarSet::valueAt(int index) | |
30 | { |
|
30 | { | |
31 | return mValues.at(index); |
|
31 | return mValues.at(index); | |
32 | } |
|
32 | } | |
33 |
|
33 | |||
34 | void QBarSet::setValue(int index, qreal value) |
|
34 | void QBarSet::setValue(int index, qreal value) | |
35 | { |
|
35 | { | |
36 | mValues.replace(index,value); |
|
36 | mValues.replace(index,value); | |
37 | } |
|
37 | } | |
38 |
|
38 | |||
|
39 | void QBarSet::setPen(const QPen& pen) | |||
|
40 | { | |||
|
41 | mPen = pen; | |||
|
42 | } | |||
|
43 | ||||
|
44 | const QPen& QBarSet::pen() const | |||
|
45 | { | |||
|
46 | return mPen; | |||
|
47 | } | |||
|
48 | ||||
|
49 | void QBarSet::setBrush(const QBrush& brush) | |||
|
50 | { | |||
|
51 | mBrush = brush; | |||
|
52 | } | |||
|
53 | ||||
|
54 | const QBrush& QBarSet::brush() const | |||
|
55 | { | |||
|
56 | return mBrush; | |||
|
57 | } | |||
|
58 | ||||
|
59 | ||||
|
60 | ||||
39 | //TODO?: |
|
61 | //TODO?: | |
40 | //#include "moc_qbarset.cpp" |
|
62 | //#include "moc_qbarset.cpp" | |
41 | QTCOMMERCIALCHART_END_NAMESPACE |
|
63 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,32 +1,40 | |||||
1 | #ifndef QBARSET_H |
|
1 | #ifndef QBARSET_H | |
2 | #define QBARSET_H |
|
2 | #define QBARSET_H | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include <QPen> |
|
5 | #include <QPen> | |
6 | #include <QBrush> |
|
6 | #include <QBrush> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class QTCOMMERCIALCHART_EXPORT QBarSet // TODO? : public QObject |
|
10 | class QTCOMMERCIALCHART_EXPORT QBarSet // TODO? : public QObject | |
11 | { |
|
11 | { | |
12 | //Q_OBJECT; |
|
12 | //Q_OBJECT; | |
13 | public: |
|
13 | public: | |
14 | QBarSet(); |
|
14 | QBarSet(); | |
15 |
|
15 | |||
16 | void setName(QString name); |
|
16 | void setName(QString name); | |
17 | QString name(); |
|
17 | QString name(); | |
18 | QBarSet& operator << (const qreal &value); // appends new value to set |
|
18 | QBarSet& operator << (const qreal &value); // appends new value to set | |
19 |
|
19 | |||
20 | int count(); // count of values in set |
|
20 | int count(); // count of values in set | |
21 | qreal valueAt(int index); // for modifying individual values |
|
21 | qreal valueAt(int index); // for modifying individual values | |
22 | void setValue(int index, qreal value); // |
|
22 | void setValue(int index, qreal value); // | |
23 |
|
23 | |||
|
24 | void setPen(const QPen& pen); | |||
|
25 | const QPen& pen() const; | |||
|
26 | ||||
|
27 | void setBrush(const QBrush& brush); | |||
|
28 | const QBrush& brush() const; | |||
|
29 | ||||
24 | private: |
|
30 | private: | |
25 |
|
31 | |||
26 | QString mName; |
|
32 | QString mName; | |
27 | QList<qreal> mValues; |
|
33 | QList<qreal> mValues; | |
|
34 | QPen mPen; | |||
|
35 | QBrush mBrush; | |||
28 | }; |
|
36 | }; | |
29 |
|
37 | |||
30 | QTCOMMERCIALCHART_END_NAMESPACE |
|
38 | QTCOMMERCIALCHART_END_NAMESPACE | |
31 |
|
39 | |||
32 | #endif // QBARSET_H |
|
40 | #endif // QBARSET_H |
@@ -1,60 +1,70 | |||||
1 | #include <limits.h> |
|
1 | #include <limits.h> | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include "stackedbarchartseries.h" |
|
3 | #include "stackedbarchartseries.h" | |
4 | #include "qbarcategory.h" |
|
4 | #include "qbarcategory.h" | |
5 | #include "qbarset.h" |
|
5 | #include "qbarset.h" | |
6 | #include "barchartmodel_p.h" |
|
6 | #include "barchartmodel_p.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | StackedBarChartSeries::StackedBarChartSeries(QBarCategory *category, QObject *parent) |
|
10 | StackedBarChartSeries::StackedBarChartSeries(QBarCategory *category, QObject *parent) | |
11 | : QChartSeries(parent) |
|
11 | : QChartSeries(parent) | |
12 | ,mModel(new BarChartModel(category, this)) |
|
12 | ,mModel(new BarChartModel(category, this)) | |
13 | { |
|
13 | { | |
14 | } |
|
14 | } | |
15 |
|
15 | |||
16 | void StackedBarChartSeries::addBarSet(QBarSet *set) |
|
16 | void StackedBarChartSeries::addBarSet(QBarSet *set) | |
17 | { |
|
17 | { | |
18 | mModel->addBarSet(set); |
|
18 | mModel->addBarSet(set); | |
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | void StackedBarChartSeries::removeBarSet(QBarSet *set) |
|
21 | void StackedBarChartSeries::removeBarSet(QBarSet *set) | |
22 | { |
|
22 | { | |
23 | mModel->removeBarSet(set); |
|
23 | mModel->removeBarSet(set); | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
|
26 | int StackedBarChartSeries::countSets() | |||
|
27 | { | |||
|
28 | return mModel->countSets(); | |||
|
29 | } | |||
|
30 | ||||
|
31 | QBarSet* StackedBarChartSeries::nextSet(bool getFirst) | |||
|
32 | { | |||
|
33 | return mModel->nextSet(getFirst); | |||
|
34 | } | |||
|
35 | ||||
26 | int StackedBarChartSeries::countCategories() |
|
36 | int StackedBarChartSeries::countCategories() | |
27 | { |
|
37 | { | |
28 | return mModel->countCategories(); |
|
38 | return mModel->countCategories(); | |
29 | } |
|
39 | } | |
30 |
|
40 | |||
31 | qreal StackedBarChartSeries::min() |
|
41 | qreal StackedBarChartSeries::min() | |
32 | { |
|
42 | { | |
33 | return mModel->min(); |
|
43 | return mModel->min(); | |
34 | } |
|
44 | } | |
35 |
|
45 | |||
36 | qreal StackedBarChartSeries::max() |
|
46 | qreal StackedBarChartSeries::max() | |
37 | { |
|
47 | { | |
38 | return mModel->max(); |
|
48 | return mModel->max(); | |
39 | } |
|
49 | } | |
40 |
|
50 | |||
41 | qreal StackedBarChartSeries::valueAt(int set, int category) |
|
51 | qreal StackedBarChartSeries::valueAt(int set, int category) | |
42 | { |
|
52 | { | |
43 | return mModel->valueAt(set,category); |
|
53 | return mModel->valueAt(set,category); | |
44 | } |
|
54 | } | |
45 |
|
55 | |||
46 | qreal StackedBarChartSeries::maxCategorySum() |
|
56 | qreal StackedBarChartSeries::maxCategorySum() | |
47 | { |
|
57 | { | |
48 | return mModel->maxCategorySum(); |
|
58 | return mModel->maxCategorySum(); | |
49 | } |
|
59 | } | |
50 |
|
60 | |||
51 | BarChartModel& StackedBarChartSeries::model() |
|
61 | BarChartModel& StackedBarChartSeries::model() | |
52 | { |
|
62 | { | |
53 | return *mModel; |
|
63 | return *mModel; | |
54 | } |
|
64 | } | |
55 |
|
65 | |||
56 |
|
66 | |||
57 | #include "moc_stackedbarchartseries.cpp" |
|
67 | #include "moc_stackedbarchartseries.cpp" | |
58 |
|
68 | |||
59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
69 | QTCOMMERCIALCHART_END_NAMESPACE | |
60 |
|
70 |
@@ -1,53 +1,55 | |||||
1 | #ifndef STACKEDBARCHARTSERIES_H |
|
1 | #ifndef STACKEDBARCHARTSERIES_H | |
2 | #define STACKEDBARCHARTSERIES_H |
|
2 | #define STACKEDBARCHARTSERIES_H | |
3 |
|
3 | |||
4 | #include <QList> |
|
4 | #include <QList> | |
5 | #include <QAbstractItemModel> |
|
5 | #include <QAbstractItemModel> | |
6 | #include "qchartseries.h" |
|
6 | #include "qchartseries.h" | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class StackedBarGroup; |
|
10 | class StackedBarGroup; | |
11 | class QBarCategory; |
|
11 | class QBarCategory; | |
12 | class QBarSet; |
|
12 | class QBarSet; | |
13 | class BarChartModel; |
|
13 | class BarChartModel; | |
14 |
|
14 | |||
15 | class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public QChartSeries |
|
15 | class QTCOMMERCIALCHART_EXPORT StackedBarChartSeries : public QChartSeries | |
16 | { |
|
16 | { | |
17 | Q_OBJECT |
|
17 | Q_OBJECT | |
18 | public: |
|
18 | public: | |
19 | StackedBarChartSeries(QBarCategory *category, QObject* parent=0); |
|
19 | StackedBarChartSeries(QBarCategory *category, QObject* parent=0); | |
20 |
|
20 | |||
21 | // from QChartSeries |
|
21 | // from QChartSeries | |
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; } |
|
22 | virtual QChartSeriesType type() const { return QChartSeries::SeriesTypeStackedBar; } | |
23 |
|
23 | |||
24 | void addBarSet(QBarSet *set); // Takes ownership |
|
24 | void addBarSet(QBarSet *set); // Takes ownership | |
25 | void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned. |
|
25 | void removeBarSet(QBarSet *set); // Also deletes the set, if set is owned. | |
|
26 | int countSets(); | |||
|
27 | QBarSet* nextSet(bool first=false); // Returns first set, if called with true | |||
26 |
|
28 | |||
27 | //TODO: |
|
29 | //TODO: | |
28 | //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series) |
|
30 | //QList<QString> legend(); // Returns legend of series (ie. names of all sets in series) | |
29 |
|
31 | |||
30 | // TODO: Functions below this are not part of api and will be moved |
|
32 | // TODO: Functions below this are not part of api and will be moved | |
31 | // to private implementation, when we start using it (not part of api) |
|
33 | // to private implementation, when we start using it (not part of api) | |
32 | int countCategories(); |
|
34 | int countCategories(); | |
33 | qreal min(); |
|
35 | qreal min(); | |
34 | qreal max(); |
|
36 | qreal max(); | |
35 | qreal valueAt(int set, int category); |
|
37 | qreal valueAt(int set, int category); | |
36 | qreal maxCategorySum(); |
|
38 | qreal maxCategorySum(); | |
37 |
|
39 | |||
38 | BarChartModel& model(); |
|
40 | BarChartModel& model(); | |
39 |
|
41 | |||
40 | signals: |
|
42 | signals: | |
41 | void changed(int index); |
|
43 | void changed(int index); | |
42 |
|
44 | |||
43 | public Q_SLOTS: |
|
45 | public Q_SLOTS: | |
44 |
|
46 | |||
45 | private: |
|
47 | private: | |
46 |
|
48 | |||
47 | StackedBarGroup* mStackedBarGroup; |
|
49 | StackedBarGroup* mStackedBarGroup; | |
48 | BarChartModel* mModel; |
|
50 | BarChartModel* mModel; | |
49 | }; |
|
51 | }; | |
50 |
|
52 | |||
51 | QTCOMMERCIALCHART_END_NAMESPACE |
|
53 | QTCOMMERCIALCHART_END_NAMESPACE | |
52 |
|
54 | |||
53 | #endif // STACKEDBARCHARTSERIES_H |
|
55 | #endif // STACKEDBARCHARTSERIES_H |
@@ -1,86 +1,85 | |||||
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 "qbarset.h" | |||
5 | #include <QDebug> |
|
6 | #include <QDebug> | |
6 |
|
7 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
9 | |||
9 | StackedBarGroup::StackedBarGroup(BarChartModel& model, QGraphicsItem *parent) : |
|
10 | StackedBarGroup::StackedBarGroup(BarChartModel& model, QGraphicsItem *parent) : | |
10 | BarGroupBase(model,parent) |
|
11 | BarGroupBase(model,parent) | |
11 | { |
|
12 | { | |
12 | } |
|
13 | } | |
13 |
|
14 | |||
14 | void StackedBarGroup::layoutChanged() |
|
15 | void StackedBarGroup::layoutChanged() | |
15 | { |
|
16 | { | |
16 | // qDebug() << "StackedBarGroup::layoutChanged"; |
|
17 | // qDebug() << "StackedBarGroup::layoutChanged"; | |
17 | // Scale bars to new layout |
|
18 | // Scale bars to new layout | |
18 | // Layout for bars: |
|
19 | // Layout for bars: | |
19 | if (mModel.countSets() <= 0) { |
|
20 | if (mModel.countSets() <= 0) { | |
20 | qDebug() << "No sets in model!"; |
|
21 | qDebug() << "No sets in model!"; | |
21 | // Nothing to do. |
|
22 | // Nothing to do. | |
22 | return; |
|
23 | return; | |
23 | } |
|
24 | } | |
24 |
|
25 | |||
25 | if (mModel.countCategories() == 0) { |
|
26 | if (mModel.countCategories() == 0) { | |
26 | qDebug() << "No categories in model!"; |
|
27 | qDebug() << "No categories in model!"; | |
27 | // Nothing to do |
|
28 | // Nothing to do | |
28 | return; |
|
29 | return; | |
29 | } |
|
30 | } | |
30 |
|
31 | |||
31 | if (childItems().count() == 0) { |
|
32 | if (childItems().count() == 0) { | |
32 | qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!"; |
|
33 | qDebug() << "WARNING: StackedBarGroup::layoutChanged called before graphics items are created!"; | |
33 | return; |
|
34 | return; | |
34 | } |
|
35 | } | |
35 |
|
36 | |||
36 | // TODO: better way to auto-layout |
|
37 | // TODO: better way to auto-layout | |
37 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
38 | // Use reals for accurancy (we might get some compiler warnings... :) | |
38 | // TODO: use temp variable for category count... |
|
39 | // TODO: use temp variable for category count... | |
39 | qreal maxSum = mModel.maxCategorySum(); |
|
40 | qreal maxSum = mModel.maxCategorySum(); | |
40 | qreal h = mHeight; |
|
41 | qreal h = mHeight; | |
41 | qreal scale = (h / maxSum); |
|
42 | qreal scale = (h / maxSum); | |
42 |
|
43 | |||
43 | int itemIndex(0); |
|
44 | int itemIndex(0); | |
44 | qreal tW = mWidth; |
|
45 | qreal tW = mWidth; | |
45 | qreal tC = mModel.countCategories() + 1; |
|
46 | qreal tC = mModel.countCategories() + 1; | |
46 | qreal xStep = (tW/tC); |
|
47 | qreal xStep = (tW/tC); | |
47 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
48 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
48 | int labelIndex = mModel.countSets() * mModel.countCategories(); |
|
49 | int labelIndex = mModel.countSets() * mModel.countCategories(); | |
49 |
|
50 | |||
50 | for (int category = 0; category < mModel.countCategories(); category++) { |
|
51 | for (int category = 0; category < mModel.countCategories(); category++) { | |
51 | qreal yPos = h; |
|
52 | qreal yPos = h; | |
52 | for (int set=0; set < mModel.countSets(); set++) { |
|
53 | for (int set=0; set < mModel.countSets(); set++) { | |
53 | qreal barHeight = mModel.valueAt(set, category) * scale; |
|
54 | qreal barHeight = mModel.valueAt(set, category) * scale; | |
54 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
55 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
55 |
|
56 | |||
56 | bar->resize(mBarDefaultWidth, barHeight); |
|
57 | bar->resize(mBarDefaultWidth, barHeight); | |
57 |
bar->setBrush(m |
|
58 | bar->setBrush(mModel.setAt(set).brush()); | |
58 | // bar->setBrush(mBrush); |
|
|||
59 | // bar->setColor(mColors.at(set)); |
|
|||
60 | bar->setPos(xPos, yPos-barHeight); |
|
59 | bar->setPos(xPos, yPos-barHeight); | |
61 | itemIndex++; |
|
60 | itemIndex++; | |
62 | yPos -= barHeight; |
|
61 | yPos -= barHeight; | |
63 | } |
|
62 | } | |
64 |
|
63 | |||
65 | // TODO: Layout for labels, remove magic number |
|
64 | // TODO: Layout for labels, remove magic number | |
66 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
65 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | |
67 | label->setPos(xPos, mHeight + 20); |
|
66 | label->setPos(xPos, mHeight + 20); | |
68 | labelIndex++; |
|
67 | labelIndex++; | |
69 | xPos += xStep; |
|
68 | xPos += xStep; | |
70 | } |
|
69 | } | |
71 |
|
70 | |||
72 | // Position separators |
|
71 | // Position separators | |
73 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
72 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |
74 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
73 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |
75 | for (int s=0; s < mModel.countCategories() - 1; s++) { |
|
74 | for (int s=0; s < mModel.countCategories() - 1; s++) { | |
76 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
75 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
77 | sep->setPos(xPos,0); |
|
76 | sep->setPos(xPos,0); | |
78 | sep->setSize(QSizeF(1,mHeight)); |
|
77 | sep->setSize(QSizeF(1,mHeight)); | |
79 | xPos += xStep; |
|
78 | xPos += xStep; | |
80 | separatorIndex++; |
|
79 | separatorIndex++; | |
81 | } |
|
80 | } | |
82 |
|
81 | |||
83 | mLayoutDirty = true; |
|
82 | mLayoutDirty = true; | |
84 | } |
|
83 | } | |
85 |
|
84 | |||
86 | QTCOMMERCIALCHART_END_NAMESPACE |
|
85 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,254 +1,231 | |||||
1 | #include "charttheme_p.h" |
|
1 | #include "charttheme_p.h" | |
2 | #include "qchart.h" |
|
2 | #include "qchart.h" | |
3 | #include "qchartaxis.h" |
|
3 | #include "qchartaxis.h" | |
4 |
|
4 | |||
5 |
|
5 | |||
6 | //series |
|
6 | //series | |
|
7 | #include "qbarset.h" | |||
7 | #include "barchartseries.h" |
|
8 | #include "barchartseries.h" | |
8 | #include "stackedbarchartseries.h" |
|
9 | #include "stackedbarchartseries.h" | |
9 | #include "percentbarchartseries.h" |
|
10 | #include "percentbarchartseries.h" | |
10 | #include "qlinechartseries.h" |
|
11 | #include "qlinechartseries.h" | |
11 | #include "qscatterseries.h" |
|
12 | #include "qscatterseries.h" | |
12 | #include "qpieseries.h" |
|
13 | #include "qpieseries.h" | |
13 | #include "qpieslice.h" |
|
14 | #include "qpieslice.h" | |
14 |
|
15 | |||
15 | //items |
|
16 | //items | |
16 | #include "axisitem_p.h" |
|
17 | #include "axisitem_p.h" | |
17 | #include "bargroup.h" |
|
18 | #include "bargroup.h" | |
18 | #include "stackedbargroup.h" |
|
19 | #include "stackedbargroup.h" | |
19 | #include "linechartitem_p.h" |
|
20 | #include "linechartitem_p.h" | |
20 | #include "percentbargroup.h" |
|
21 | #include "percentbargroup.h" | |
21 | #include "scatterpresenter_p.h" |
|
22 | #include "scatterpresenter_p.h" | |
22 | #include "piepresenter.h" |
|
23 | #include "piepresenter.h" | |
23 |
|
24 | |||
24 | //themes |
|
25 | //themes | |
25 | #include "chartthemevanilla_p.h" |
|
26 | #include "chartthemevanilla_p.h" | |
26 | #include "chartthemeicy_p.h" |
|
27 | #include "chartthemeicy_p.h" | |
27 | #include "chartthemegrayscale_p.h" |
|
28 | #include "chartthemegrayscale_p.h" | |
28 | #include "chartthemescientific_p.h" |
|
29 | #include "chartthemescientific_p.h" | |
29 |
|
30 | |||
30 |
|
31 | |||
31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
32 |
|
33 | |||
33 | /* TODO |
|
34 | /* TODO | |
34 | case QChart::ChartThemeUnnamed1: |
|
35 | case QChart::ChartThemeUnnamed1: | |
35 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); |
|
36 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); | |
36 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); |
|
37 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); | |
37 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); |
|
38 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); | |
38 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); |
|
39 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); | |
39 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); |
|
40 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); | |
40 |
|
41 | |||
41 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); |
|
42 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); | |
42 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
43 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); | |
43 | */ |
|
44 | */ | |
44 |
|
45 | |||
45 | ChartTheme::ChartTheme(QChart::ChartTheme id) |
|
46 | ChartTheme::ChartTheme(QChart::ChartTheme id) | |
46 | { |
|
47 | { | |
47 | m_id = id; |
|
48 | m_id = id; | |
48 | m_seriesColor.append(QRgb(0xff000000)); |
|
49 | m_seriesColor.append(QRgb(0xff000000)); | |
49 | m_seriesColor.append(QRgb(0xff707070)); |
|
50 | m_seriesColor.append(QRgb(0xff707070)); | |
50 | m_gradientStartColor = QColor(QRgb(0xffffffff)); |
|
51 | m_gradientStartColor = QColor(QRgb(0xffffffff)); | |
51 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
52 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); | |
52 | } |
|
53 | } | |
53 |
|
54 | |||
54 |
|
55 | |||
55 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
56 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) | |
56 | { |
|
57 | { | |
57 | switch(theme) { |
|
58 | switch(theme) { | |
58 | case QChart::ChartThemeDefault: |
|
59 | case QChart::ChartThemeDefault: | |
59 | return new ChartTheme(); |
|
60 | return new ChartTheme(); | |
60 | case QChart::ChartThemeVanilla: |
|
61 | case QChart::ChartThemeVanilla: | |
61 | return new ChartThemeVanilla(); |
|
62 | return new ChartThemeVanilla(); | |
62 | case QChart::ChartThemeIcy: |
|
63 | case QChart::ChartThemeIcy: | |
63 | return new ChartThemeIcy(); |
|
64 | return new ChartThemeIcy(); | |
64 | case QChart::ChartThemeGrayscale: |
|
65 | case QChart::ChartThemeGrayscale: | |
65 | return new ChartThemeGrayscale(); |
|
66 | return new ChartThemeGrayscale(); | |
66 | case QChart::ChartThemeScientific: |
|
67 | case QChart::ChartThemeScientific: | |
67 | return new ChartThemeScientific(); |
|
68 | return new ChartThemeScientific(); | |
68 | } |
|
69 | } | |
69 | } |
|
70 | } | |
70 |
|
71 | |||
71 | void ChartTheme::decorate(QChart* chart) |
|
72 | void ChartTheme::decorate(QChart* chart) | |
72 | { |
|
73 | { | |
73 | QLinearGradient backgroundGradient; |
|
74 | QLinearGradient backgroundGradient; | |
74 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); |
|
75 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); | |
75 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); |
|
76 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); | |
76 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
77 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); | |
77 | chart->setChartBackgroundBrush(backgroundGradient); |
|
78 | chart->setChartBackgroundBrush(backgroundGradient); | |
78 | } |
|
79 | } | |
79 | //TODO helper to by removed later |
|
80 | //TODO helper to by removed later | |
80 | void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) |
|
81 | void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) | |
81 | { |
|
82 | { | |
82 | switch(series->type()) |
|
83 | switch(series->type()) | |
83 | { |
|
84 | { | |
84 | case QChartSeries::SeriesTypeLine: { |
|
85 | case QChartSeries::SeriesTypeLine: { | |
85 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); |
|
86 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); | |
86 | LineChartItem* i = static_cast<LineChartItem*>(item); |
|
87 | LineChartItem* i = static_cast<LineChartItem*>(item); | |
87 | decorate(i,s,count); |
|
88 | decorate(i,s,count); | |
88 | break; |
|
89 | break; | |
89 | } |
|
90 | } | |
90 | case QChartSeries::SeriesTypeBar: { |
|
91 | case QChartSeries::SeriesTypeBar: { | |
91 | BarChartSeries* b = static_cast<BarChartSeries*>(series); |
|
92 | BarChartSeries* b = static_cast<BarChartSeries*>(series); | |
92 | BarGroup* i = static_cast<BarGroup*>(item); |
|
93 | BarGroup* i = static_cast<BarGroup*>(item); | |
93 | decorate(i,b,count); |
|
94 | decorate(i,b,count); | |
94 | break; |
|
95 | break; | |
95 | } |
|
96 | } | |
96 | case QChartSeries::SeriesTypeStackedBar: { |
|
97 | case QChartSeries::SeriesTypeStackedBar: { | |
97 | StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series); |
|
98 | StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series); | |
98 | StackedBarGroup* i = static_cast<StackedBarGroup*>(item); |
|
99 | StackedBarGroup* i = static_cast<StackedBarGroup*>(item); | |
99 | decorate(i,s,count); |
|
100 | decorate(i,s,count); | |
100 | break; |
|
101 | break; | |
101 | } |
|
102 | } | |
102 | case QChartSeries::SeriesTypePercentBar: { |
|
103 | case QChartSeries::SeriesTypePercentBar: { | |
103 | PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series); |
|
104 | PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series); | |
104 | PercentBarGroup* i = static_cast<PercentBarGroup*>(item); |
|
105 | PercentBarGroup* i = static_cast<PercentBarGroup*>(item); | |
105 | decorate(i,s,count); |
|
106 | decorate(i,s,count); | |
106 | break; |
|
107 | break; | |
107 | } |
|
108 | } | |
108 | case QChartSeries::SeriesTypeScatter: { |
|
109 | case QChartSeries::SeriesTypeScatter: { | |
109 | QScatterSeries* s = qobject_cast<QScatterSeries*>(series); |
|
110 | QScatterSeries* s = qobject_cast<QScatterSeries*>(series); | |
110 | Q_ASSERT(s); |
|
111 | Q_ASSERT(s); | |
111 | ScatterPresenter* i = static_cast<ScatterPresenter*>(item); |
|
112 | ScatterPresenter* i = static_cast<ScatterPresenter*>(item); | |
112 | Q_ASSERT(i); |
|
113 | Q_ASSERT(i); | |
113 | decorate(i, s, count); |
|
114 | decorate(i, s, count); | |
114 | break; |
|
115 | break; | |
115 | } |
|
116 | } | |
116 | case QChartSeries::SeriesTypePie: { |
|
117 | case QChartSeries::SeriesTypePie: { | |
117 | QPieSeries* s = static_cast<QPieSeries*>(series); |
|
118 | QPieSeries* s = static_cast<QPieSeries*>(series); | |
118 | PiePresenter* i = static_cast<PiePresenter*>(item); |
|
119 | PiePresenter* i = static_cast<PiePresenter*>(item); | |
119 | decorate(i,s,count); |
|
120 | decorate(i,s,count); | |
120 | break; |
|
121 | break; | |
121 | } |
|
122 | } | |
122 | default: |
|
123 | default: | |
123 | qDebug()<<"Wrong item to be decorated by theme"; |
|
124 | qDebug()<<"Wrong item to be decorated by theme"; | |
124 | break; |
|
125 | break; | |
125 | } |
|
126 | } | |
126 |
|
127 | |||
127 | } |
|
128 | } | |
128 |
|
129 | |||
129 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) |
|
130 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) | |
130 | { |
|
131 | { | |
131 | QPen pen; |
|
132 | QPen pen; | |
132 | if(pen != series->pen()){ |
|
133 | if(pen != series->pen()){ | |
133 | item->setPen(series->pen()); |
|
134 | item->setPen(series->pen()); | |
134 | return; |
|
135 | return; | |
135 | } |
|
136 | } | |
136 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
137 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); | |
137 | pen.setWidthF(2); |
|
138 | pen.setWidthF(2); | |
138 | item->setPen(pen); |
|
139 | item->setPen(pen); | |
139 | } |
|
140 | } | |
140 |
|
141 | |||
141 | void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) |
|
142 | void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) | |
142 | { |
|
143 | { | |
143 | // TODO: better way to descide series color and remove hard coded colors. |
|
144 | for (int i=0; i<series->countSets(); i++) { | |
144 | item->resetBrushes(); |
|
145 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | |
145 | for (int i=0; i<m_seriesColor.count(); i++) { |
|
|||
146 | QBrush brush(m_seriesColor.at(i)); |
|
|||
147 | item->addBrush(brush); |
|
|||
148 | } |
|
146 | } | |
149 | item->addBrush(QBrush(QColor(255,0,0,128))); |
|
|||
150 | item->addBrush(QBrush(QColor(255,255,0,128))); |
|
|||
151 | item->addBrush(QBrush(QColor(0,255,0,128))); |
|
|||
152 | item->addBrush(QBrush(QColor(0,0,255,128))); |
|
|||
153 | item->addBrush(QBrush(QColor(255,128,0,128))); |
|
|||
154 | } |
|
147 | } | |
155 |
|
148 | |||
156 | void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) |
|
149 | void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) | |
157 | { |
|
150 | { | |
158 | // TODO: better way to descide series color and remove hard coded colors. |
|
151 | for (int i=0; i<series->countSets(); i++) { | |
159 | item->resetBrushes(); |
|
152 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | |
160 | for (int i=0; i<m_seriesColor.count(); i++) { |
|
|||
161 | QBrush brush(m_seriesColor.at(i)); |
|
|||
162 | item->addBrush(brush); |
|
|||
163 | } |
|
153 | } | |
164 | item->addBrush(QBrush(QColor(255,0,0,128))); |
|
|||
165 | item->addBrush(QBrush(QColor(255,255,0,128))); |
|
|||
166 | item->addBrush(QBrush(QColor(0,255,0,128))); |
|
|||
167 | item->addBrush(QBrush(QColor(0,0,255,128))); |
|
|||
168 | item->addBrush(QBrush(QColor(255,128,0,128))); |
|
|||
169 | } |
|
154 | } | |
170 |
|
155 | |||
171 | void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) |
|
156 | void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) | |
172 | { |
|
157 | { | |
173 | // TODO: better way to descide series color and remove hard coded colors. |
|
158 | for (int i=0; i<series->countSets(); i++) { | |
174 | item->resetBrushes(); |
|
159 | series->nextSet(0==i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count()))); | |
175 | for (int i=0; i<m_seriesColor.count(); i++) { |
|
|||
176 | QBrush brush(m_seriesColor.at(i)); |
|
|||
177 | item->addBrush(brush); |
|
|||
178 | } |
|
160 | } | |
179 | item->addBrush(QBrush(QColor(255,0,0,128))); |
|
|||
180 | item->addBrush(QBrush(QColor(255,255,0,128))); |
|
|||
181 | item->addBrush(QBrush(QColor(0,255,0,128))); |
|
|||
182 | item->addBrush(QBrush(QColor(0,0,255,128))); |
|
|||
183 | item->addBrush(QBrush(QColor(255,128,0,128))); |
|
|||
184 | } |
|
161 | } | |
185 |
|
162 | |||
186 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) |
|
163 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) | |
187 | { |
|
164 | { | |
188 | Q_ASSERT(presenter); |
|
165 | Q_ASSERT(presenter); | |
189 | Q_ASSERT(series); |
|
166 | Q_ASSERT(series); | |
190 |
|
167 | |||
191 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); |
|
168 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); | |
192 | // TODO: define alpha in the theme? or in the series? |
|
169 | // TODO: define alpha in the theme? or in the series? | |
193 | color.setAlpha(120); |
|
170 | color.setAlpha(120); | |
194 |
|
171 | |||
195 | QBrush brush(color, Qt::SolidPattern); |
|
172 | QBrush brush(color, Qt::SolidPattern); | |
196 | presenter->m_markerBrush = brush; |
|
173 | presenter->m_markerBrush = brush; | |
197 |
|
174 | |||
198 | QPen pen(brush, 1); |
|
175 | QPen pen(brush, 1); | |
199 | pen.setColor(color); |
|
176 | pen.setColor(color); | |
200 | presenter->m_markerPen = pen; |
|
177 | presenter->m_markerPen = pen; | |
201 | } |
|
178 | } | |
202 |
|
179 | |||
203 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) |
|
180 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) | |
204 | { |
|
181 | { | |
205 | // create a list of slice colors based on current theme |
|
182 | // create a list of slice colors based on current theme | |
206 | int i = 0; |
|
183 | int i = 0; | |
207 | QList<QColor> colors; |
|
184 | QList<QColor> colors; | |
208 | while (colors.count() < series->count()) { |
|
185 | while (colors.count() < series->count()) { | |
209 |
|
186 | |||
210 | // get base color |
|
187 | // get base color | |
211 | QColor c = m_seriesColor[i++]; |
|
188 | QColor c = m_seriesColor[i++]; | |
212 | i = i % m_seriesColor.count(); |
|
189 | i = i % m_seriesColor.count(); | |
213 |
|
190 | |||
214 | // -1 means achromatic color -> cannot manipulate lightness |
|
191 | // -1 means achromatic color -> cannot manipulate lightness | |
215 | // TODO: find a better way to randomize lightness |
|
192 | // TODO: find a better way to randomize lightness | |
216 | if (c.toHsv().hue() == -1) |
|
193 | if (c.toHsv().hue() == -1) | |
217 | qWarning() << "ChartTheme::decorate() warning: achromatic theme color"; |
|
194 | qWarning() << "ChartTheme::decorate() warning: achromatic theme color"; | |
218 |
|
195 | |||
219 | // randomize lightness |
|
196 | // randomize lightness | |
220 | qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter |
|
197 | qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter | |
221 | c = c.lighter(f); |
|
198 | c = c.lighter(f); | |
222 |
|
199 | |||
223 | // find duplicates |
|
200 | // find duplicates | |
224 | bool isUnique = true; |
|
201 | bool isUnique = true; | |
225 | foreach (QColor color, colors) { |
|
202 | foreach (QColor color, colors) { | |
226 | if (c == color) |
|
203 | if (c == color) | |
227 | isUnique = false; |
|
204 | isUnique = false; | |
228 | } |
|
205 | } | |
229 |
|
206 | |||
230 | // add to array if unique |
|
207 | // add to array if unique | |
231 | //if (isUnique) |
|
208 | //if (isUnique) | |
232 | colors << c; |
|
209 | colors << c; | |
233 | } |
|
210 | } | |
234 |
|
211 | |||
235 | // finally update colors |
|
212 | // finally update colors | |
236 | foreach (QPieSlice* s, series->slices()) { |
|
213 | foreach (QPieSlice* s, series->slices()) { | |
237 | s->setPen(QPen(Qt::black)); // TODO: get from theme |
|
214 | s->setPen(QPen(Qt::black)); // TODO: get from theme | |
238 | s->setBrush(colors.takeFirst()); |
|
215 | s->setBrush(colors.takeFirst()); | |
239 | } |
|
216 | } | |
240 | } |
|
217 | } | |
241 |
|
218 | |||
242 |
|
219 | |||
243 | void ChartTheme::decorate(QChartAxis& axis,AxisItem* item) |
|
220 | void ChartTheme::decorate(QChartAxis& axis,AxisItem* item) | |
244 | { |
|
221 | { | |
245 | //TODO: dummy defults for now |
|
222 | //TODO: dummy defults for now | |
246 |
|
223 | |||
247 | axis.setLabelsBrush(Qt::black); |
|
224 | axis.setLabelsBrush(Qt::black); | |
248 | axis.setLabelsPen(Qt::NoPen); |
|
225 | axis.setLabelsPen(Qt::NoPen); | |
249 | axis.setShadesPen(Qt::NoPen); |
|
226 | axis.setShadesPen(Qt::NoPen); | |
250 | axis.setShadesOpacity(0.5); |
|
227 | axis.setShadesOpacity(0.5); | |
251 | item->handleAxisChanged(axis); |
|
228 | item->handleAxisChanged(axis); | |
252 | } |
|
229 | } | |
253 |
|
230 | |||
254 | QTCOMMERCIALCHART_END_NAMESPACE |
|
231 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now