@@ -1,187 +1,58 | |||
|
1 | 1 | #include <limits.h> |
|
2 | 2 | #include <QDebug> |
|
3 | 3 | #include "barchartseriesbase.h" |
|
4 | 4 | #include "bargroup.h" |
|
5 | 5 | #include "barchartmodel_p.h" |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | BarChartSeriesBase::BarChartSeriesBase(QObject *parent) |
|
10 | 10 | : QChartSeries(parent) |
|
11 | 11 | ,mModel(*(new BarChartModel(this))) // TODO: is this ok? |
|
12 | 12 | { |
|
13 | qDebug() << "BarChartSeriesBase::BarChartSeriesBase"; | |
|
14 | 13 | } |
|
15 | /* | |
|
16 | bool BarChartSeriesBase::setModel(QAbstractItemModel* model) | |
|
17 | { | |
|
18 | mModel = model; | |
|
19 | return true; | |
|
20 | } | |
|
21 | */ | |
|
22 | /* | |
|
23 | bool BarChartSeriesBase::setData(QList<qreal>& data) | |
|
24 | { | |
|
25 | mData = &data; | |
|
26 | return true; | |
|
27 | } | |
|
28 | */ | |
|
29 | /* | |
|
30 | int BarChartSeriesBase::min() | |
|
31 | { | |
|
32 | Q_ASSERT(mModel->rowCount() > 0); | |
|
33 | Q_ASSERT(mModel->columnCount() > 0); | |
|
34 | ||
|
35 | // TODO: make min and max members and update them when data changes. | |
|
36 | // This is slower since they are checked every time, even if data is same since previous call. | |
|
37 | int min = INT_MAX; | |
|
38 | ||
|
39 | for (int i=0; i <mModel->rowCount(); i++) { | |
|
40 | for(int j=0; j<mModel->columnCount(); j++) { | |
|
41 | int temp = mModel->data(mModel->index(i,j)).toInt(); | |
|
42 | if (temp < min) { | |
|
43 | min = temp; | |
|
44 | } | |
|
45 | } | |
|
46 | } | |
|
47 | return min; | |
|
48 | } | |
|
49 | ||
|
50 | int BarChartSeriesBase::max() | |
|
51 | { | |
|
52 | Q_ASSERT(mModel->rowCount() > 0); | |
|
53 | Q_ASSERT(mModel->columnCount() > 0); | |
|
54 | ||
|
55 | // TODO: make min and max members and update them when data changes. | |
|
56 | // This is slower since they are checked every time, even if data is same since previous call. | |
|
57 | int max = INT_MIN; | |
|
58 | ||
|
59 | for (int i=0; i <mModel->rowCount(); i++) { | |
|
60 | for(int j=0; j<mModel->columnCount(); j++) { | |
|
61 | int temp = mModel->data(mModel->index(i,j)).toInt(); | |
|
62 | if (temp > max) { | |
|
63 | max = temp; | |
|
64 | } | |
|
65 | } | |
|
66 | } | |
|
67 | return max; | |
|
68 | } | |
|
69 | ||
|
70 | int BarChartSeriesBase::maxColumnSum() | |
|
71 | { | |
|
72 | Q_ASSERT(mModel->rowCount() > 0); | |
|
73 | Q_ASSERT(mModel->columnCount() > 0); | |
|
74 | ||
|
75 | int max = INT_MIN; | |
|
76 | ||
|
77 | for (int col=0; col <mModel->columnCount(); col++) { | |
|
78 | int sum = columnSum(col); | |
|
79 | if (sum > max) { | |
|
80 | max = sum; | |
|
81 | } | |
|
82 | } | |
|
83 | return max; | |
|
84 | } | |
|
85 | ||
|
86 | int BarChartSeriesBase::countRows() | |
|
87 | { | |
|
88 | return mModel->rowCount(); | |
|
89 | } | |
|
90 | ||
|
91 | int BarChartSeriesBase::countColumns() | |
|
92 | { | |
|
93 | return mModel->columnCount(); | |
|
94 | } | |
|
95 | ||
|
96 | int BarChartSeriesBase::countTotalItems() | |
|
97 | { | |
|
98 | return mModel->rowCount() * mModel->columnCount(); | |
|
99 | } | |
|
100 | ||
|
101 | int BarChartSeriesBase::valueAt(int row, int column) | |
|
102 | { | |
|
103 | QModelIndex index = mModel->index(row,column); | |
|
104 | return mModel->data(index).toInt(); | |
|
105 | } | |
|
106 | ||
|
107 | int BarChartSeriesBase::columnSum(int column) | |
|
108 | { | |
|
109 | int sum(0); | |
|
110 | int count = mModel->rowCount(); | |
|
111 | ||
|
112 | for (int row = 0; row < count; row++) { | |
|
113 | sum += mModel->data(mModel->index(row,column)).toInt(); | |
|
114 | } | |
|
115 | return sum; | |
|
116 | } | |
|
117 | */ | |
|
118 | 14 | |
|
119 | 15 | int BarChartSeriesBase::addData(QList<qreal> data) |
|
120 | 16 | { |
|
121 | qDebug() << "BarChartSeriesBase::addData"; | |
|
122 | 17 | return mModel.addData(data); |
|
123 | 18 | } |
|
124 | 19 | |
|
125 | 20 | void BarChartSeriesBase::removeData(int id) |
|
126 | 21 | { |
|
127 | 22 | mModel.removeData(id); |
|
128 | 23 | } |
|
129 | 24 | |
|
130 | 25 | qreal BarChartSeriesBase::min() |
|
131 | 26 | { |
|
132 | 27 | return mModel.min(); |
|
133 | /* Delegated to model | |
|
134 | int count = mData->count(); | |
|
135 | int min = INT_MAX; | |
|
136 | ||
|
137 | for (int i=0; i<count; i++) { | |
|
138 | if (mData->at(i) < min) { | |
|
139 | min = mData->at(i); | |
|
140 | } | |
|
141 | } | |
|
142 | return min; | |
|
143 | */ | |
|
144 | 28 | } |
|
145 | 29 | |
|
146 | 30 | qreal BarChartSeriesBase::max() |
|
147 | 31 | { |
|
148 | 32 | return mModel.max(); |
|
149 | /* Delegated to model | |
|
150 | int count = mData->count(); | |
|
151 | int max = INT_MIN; | |
|
152 | ||
|
153 | for (int i=0; i<count; i++) { | |
|
154 | if (mData->at(i) > max) { | |
|
155 | max = mData->at(i); | |
|
156 | } | |
|
157 | } | |
|
158 | return max; | |
|
159 | */ | |
|
160 | 33 | } |
|
161 | 34 | |
|
162 | 35 | int BarChartSeriesBase::countColumns() |
|
163 | 36 | { |
|
164 | 37 | return mModel.countColumns(); |
|
165 | // return mData->count(); | |
|
166 | 38 | } |
|
167 | 39 | |
|
168 | 40 | qreal BarChartSeriesBase::valueAt(int series, int item) |
|
169 | 41 | { |
|
170 | 42 | return mModel.valueAt(series,item); |
|
171 | // return mData->at(item); | |
|
172 | 43 | } |
|
173 | 44 | |
|
174 | 45 | qreal BarChartSeriesBase::maxColumnSum() |
|
175 | 46 | { |
|
176 | 47 | qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel.maxColumnSum(); |
|
177 | 48 | return mModel.maxColumnSum(); |
|
178 | 49 | } |
|
179 | 50 | |
|
180 | 51 | BarChartModel& BarChartSeriesBase::model() |
|
181 | 52 | { |
|
182 | 53 | return mModel; |
|
183 | 54 | } |
|
184 | 55 | |
|
185 | 56 | #include "moc_barchartseriesbase.cpp" |
|
186 | 57 | |
|
187 | 58 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,37 +1,36 | |||
|
1 | 1 | #include "barlabel_p.h" |
|
2 | 2 | #include <QPainter> |
|
3 | 3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
4 | 4 | |
|
5 | 5 | BarLabel::BarLabel(QGraphicsItem* parent) : ChartItem(parent) |
|
6 | 6 | { |
|
7 | 7 | } |
|
8 | 8 | |
|
9 | ||
|
10 | 9 | void BarLabel::set(QString label) |
|
11 | 10 | { |
|
12 | 11 | mLabel = label; |
|
13 | 12 | } |
|
14 | 13 | |
|
15 | 14 | void BarLabel::setPos(qreal x, qreal y) |
|
16 | 15 | { |
|
17 | 16 | mXpos = x; |
|
18 | 17 | mYpos = y; |
|
19 | 18 | } |
|
20 | 19 | |
|
21 | 20 | void BarLabel::setSize(const QSizeF &size) |
|
22 | 21 | { |
|
23 | 22 | mSize = size; |
|
24 | 23 | } |
|
25 | 24 | |
|
26 | 25 | void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
27 | 26 | { |
|
28 | 27 | painter->drawText(boundingRect(),mLabel); |
|
29 | 28 | } |
|
30 | 29 | |
|
31 | 30 | QRectF BarLabel::boundingRect() const |
|
32 | 31 | { |
|
33 | 32 | QRectF r(mXpos, mYpos, mXpos + mSize.width(), mYpos + mSize.height()); |
|
34 | 33 | return r; |
|
35 | 34 | } |
|
36 | 35 | |
|
37 | 36 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,82 +1,81 | |||
|
1 | 1 | #include "stackedbargroup.h" |
|
2 | 2 | #include "bar_p.h" |
|
3 | 3 | #include "barlabel_p.h" |
|
4 | 4 | #include "separator_p.h" |
|
5 | 5 | #include <QDebug> |
|
6 | //#include <QPainter> | |
|
7 | 6 | |
|
8 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | 8 | |
|
10 | 9 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : |
|
11 | 10 | BarGroupBase(series,parent) |
|
12 | 11 | { |
|
13 | 12 | } |
|
14 | 13 | |
|
15 | 14 | void StackedBarGroup::layoutChanged() |
|
16 | 15 | { |
|
17 | 16 | |
|
18 | 17 | // Scale bars to new layout |
|
19 | 18 | // Layout for bars: |
|
20 | 19 | if (mModel.countRows() <= 0) { |
|
21 | 20 | // Nothing to do. |
|
22 | 21 | return; |
|
23 | 22 | } |
|
24 | 23 | |
|
25 | 24 | if (mModel.countColumns() == 0) { |
|
26 | 25 | // Nothing to do |
|
27 | 26 | return; |
|
28 | 27 | } |
|
29 | 28 | |
|
30 | 29 | // TODO: better way to auto-layout |
|
31 | 30 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
32 | 31 | // TODO: use temp variable for column count... |
|
33 | 32 | qreal maxSum = mModel.maxColumnSum(); |
|
34 | 33 | qreal h = mHeight; |
|
35 | 34 | qreal scale = (h / maxSum); |
|
36 | 35 | |
|
37 | 36 | int itemIndex(0); |
|
38 | 37 | qreal tW = mWidth; |
|
39 | 38 | qreal tC = mModel.countColumns() + 1; |
|
40 | 39 | qreal xStep = (tW/tC); |
|
41 | 40 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
42 | 41 | int labelIndex = mModel.countRows() * mModel.countColumns(); |
|
43 | 42 | |
|
44 | 43 | for (int column = 0; column < mModel.countColumns(); column++) { |
|
45 | 44 | qreal yPos = h; |
|
46 | 45 | for (int row=0; row < mModel.countRows(); row++) { |
|
47 | 46 | qreal barHeight = mModel.valueAt(row, column) * scale; |
|
48 | 47 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
49 | 48 | |
|
50 | 49 | // TODO: width settable per bar? |
|
51 | // TODO: how to get color for series(x) from theme? | |
|
50 | // TODO: theme stuff | |
|
52 | 51 | // mTheme->themeForSeries(); |
|
53 | 52 | bar->resize(mBarDefaultWidth, barHeight); |
|
54 | 53 | bar->setColor(mColors.at(row)); |
|
55 | 54 | bar->setPos(xPos, yPos-barHeight); |
|
56 | 55 | itemIndex++; |
|
57 | 56 | yPos -= barHeight; |
|
58 | 57 | } |
|
59 | 58 | |
|
60 | 59 | // TODO: Layout for labels, remove magic number |
|
61 | 60 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); |
|
62 | 61 | label->setPos(xPos, mHeight + 20); |
|
63 | 62 | labelIndex++; |
|
64 | 63 | xPos += xStep; |
|
65 | 64 | } |
|
66 | 65 | |
|
67 | 66 | |
|
68 | 67 | // Position separators |
|
69 | 68 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
70 | 69 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
71 | 70 | for (int s=0; s < mModel.countColumns() - 1; s++) { |
|
72 | 71 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
73 | 72 | sep->setPos(xPos,0); |
|
74 | 73 | sep->setSize(QSizeF(1,mHeight)); |
|
75 | 74 | xPos += xStep; |
|
76 | 75 | separatorIndex++; |
|
77 | 76 | } |
|
78 | 77 | |
|
79 | 78 | mLayoutDirty = true; |
|
80 | 79 | } |
|
81 | 80 | |
|
82 | 81 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now