@@ -1,187 +1,58 | |||||
1 | #include <limits.h> |
|
1 | #include <limits.h> | |
2 | #include <QDebug> |
|
2 | #include <QDebug> | |
3 | #include "barchartseriesbase.h" |
|
3 | #include "barchartseriesbase.h" | |
4 | #include "bargroup.h" |
|
4 | #include "bargroup.h" | |
5 | #include "barchartmodel_p.h" |
|
5 | #include "barchartmodel_p.h" | |
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | BarChartSeriesBase::BarChartSeriesBase(QObject *parent) |
|
9 | BarChartSeriesBase::BarChartSeriesBase(QObject *parent) | |
10 | : QChartSeries(parent) |
|
10 | : QChartSeries(parent) | |
11 | ,mModel(*(new BarChartModel(this))) // TODO: is this ok? |
|
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 | int BarChartSeriesBase::addData(QList<qreal> data) |
|
15 | int BarChartSeriesBase::addData(QList<qreal> data) | |
120 | { |
|
16 | { | |
121 | qDebug() << "BarChartSeriesBase::addData"; |
|
|||
122 | return mModel.addData(data); |
|
17 | return mModel.addData(data); | |
123 | } |
|
18 | } | |
124 |
|
19 | |||
125 | void BarChartSeriesBase::removeData(int id) |
|
20 | void BarChartSeriesBase::removeData(int id) | |
126 | { |
|
21 | { | |
127 | mModel.removeData(id); |
|
22 | mModel.removeData(id); | |
128 | } |
|
23 | } | |
129 |
|
24 | |||
130 | qreal BarChartSeriesBase::min() |
|
25 | qreal BarChartSeriesBase::min() | |
131 | { |
|
26 | { | |
132 | return mModel.min(); |
|
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 | qreal BarChartSeriesBase::max() |
|
30 | qreal BarChartSeriesBase::max() | |
147 | { |
|
31 | { | |
148 | return mModel.max(); |
|
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 | int BarChartSeriesBase::countColumns() |
|
35 | int BarChartSeriesBase::countColumns() | |
163 | { |
|
36 | { | |
164 | return mModel.countColumns(); |
|
37 | return mModel.countColumns(); | |
165 | // return mData->count(); |
|
|||
166 | } |
|
38 | } | |
167 |
|
39 | |||
168 | qreal BarChartSeriesBase::valueAt(int series, int item) |
|
40 | qreal BarChartSeriesBase::valueAt(int series, int item) | |
169 | { |
|
41 | { | |
170 | return mModel.valueAt(series,item); |
|
42 | return mModel.valueAt(series,item); | |
171 | // return mData->at(item); |
|
|||
172 | } |
|
43 | } | |
173 |
|
44 | |||
174 | qreal BarChartSeriesBase::maxColumnSum() |
|
45 | qreal BarChartSeriesBase::maxColumnSum() | |
175 | { |
|
46 | { | |
176 | qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel.maxColumnSum(); |
|
47 | qDebug() << "BarChartSeriesBase::maxColumnSum" << mModel.maxColumnSum(); | |
177 | return mModel.maxColumnSum(); |
|
48 | return mModel.maxColumnSum(); | |
178 | } |
|
49 | } | |
179 |
|
50 | |||
180 | BarChartModel& BarChartSeriesBase::model() |
|
51 | BarChartModel& BarChartSeriesBase::model() | |
181 | { |
|
52 | { | |
182 | return mModel; |
|
53 | return mModel; | |
183 | } |
|
54 | } | |
184 |
|
55 | |||
185 | #include "moc_barchartseriesbase.cpp" |
|
56 | #include "moc_barchartseriesbase.cpp" | |
186 |
|
57 | |||
187 | QTCOMMERCIALCHART_END_NAMESPACE |
|
58 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,37 +1,36 | |||||
1 | #include "barlabel_p.h" |
|
1 | #include "barlabel_p.h" | |
2 | #include <QPainter> |
|
2 | #include <QPainter> | |
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
4 |
|
4 | |||
5 | BarLabel::BarLabel(QGraphicsItem* parent) : ChartItem(parent) |
|
5 | BarLabel::BarLabel(QGraphicsItem* parent) : ChartItem(parent) | |
6 | { |
|
6 | { | |
7 | } |
|
7 | } | |
8 |
|
8 | |||
9 |
|
||||
10 | void BarLabel::set(QString label) |
|
9 | void BarLabel::set(QString label) | |
11 | { |
|
10 | { | |
12 | mLabel = label; |
|
11 | mLabel = label; | |
13 | } |
|
12 | } | |
14 |
|
13 | |||
15 | void BarLabel::setPos(qreal x, qreal y) |
|
14 | void BarLabel::setPos(qreal x, qreal y) | |
16 | { |
|
15 | { | |
17 | mXpos = x; |
|
16 | mXpos = x; | |
18 | mYpos = y; |
|
17 | mYpos = y; | |
19 | } |
|
18 | } | |
20 |
|
19 | |||
21 | void BarLabel::setSize(const QSizeF &size) |
|
20 | void BarLabel::setSize(const QSizeF &size) | |
22 | { |
|
21 | { | |
23 | mSize = size; |
|
22 | mSize = size; | |
24 | } |
|
23 | } | |
25 |
|
24 | |||
26 | void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
25 | void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
27 | { |
|
26 | { | |
28 | painter->drawText(boundingRect(),mLabel); |
|
27 | painter->drawText(boundingRect(),mLabel); | |
29 | } |
|
28 | } | |
30 |
|
29 | |||
31 | QRectF BarLabel::boundingRect() const |
|
30 | QRectF BarLabel::boundingRect() const | |
32 | { |
|
31 | { | |
33 | QRectF r(mXpos, mYpos, mXpos + mSize.width(), mYpos + mSize.height()); |
|
32 | QRectF r(mXpos, mYpos, mXpos + mSize.width(), mYpos + mSize.height()); | |
34 | return r; |
|
33 | return r; | |
35 | } |
|
34 | } | |
36 |
|
35 | |||
37 | QTCOMMERCIALCHART_END_NAMESPACE |
|
36 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,82 +1,81 | |||||
1 | #include "stackedbargroup.h" |
|
1 | #include "stackedbargroup.h" | |
2 | #include "bar_p.h" |
|
2 | #include "bar_p.h" | |
3 | #include "barlabel_p.h" |
|
3 | #include "barlabel_p.h" | |
4 | #include "separator_p.h" |
|
4 | #include "separator_p.h" | |
5 | #include <QDebug> |
|
5 | #include <QDebug> | |
6 | //#include <QPainter> |
|
|||
7 |
|
6 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
8 | |||
10 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : |
|
9 | StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) : | |
11 | BarGroupBase(series,parent) |
|
10 | BarGroupBase(series,parent) | |
12 | { |
|
11 | { | |
13 | } |
|
12 | } | |
14 |
|
13 | |||
15 | void StackedBarGroup::layoutChanged() |
|
14 | void StackedBarGroup::layoutChanged() | |
16 | { |
|
15 | { | |
17 |
|
16 | |||
18 | // Scale bars to new layout |
|
17 | // Scale bars to new layout | |
19 | // Layout for bars: |
|
18 | // Layout for bars: | |
20 | if (mModel.countRows() <= 0) { |
|
19 | if (mModel.countRows() <= 0) { | |
21 | // Nothing to do. |
|
20 | // Nothing to do. | |
22 | return; |
|
21 | return; | |
23 | } |
|
22 | } | |
24 |
|
23 | |||
25 | if (mModel.countColumns() == 0) { |
|
24 | if (mModel.countColumns() == 0) { | |
26 | // Nothing to do |
|
25 | // Nothing to do | |
27 | return; |
|
26 | return; | |
28 | } |
|
27 | } | |
29 |
|
28 | |||
30 | // TODO: better way to auto-layout |
|
29 | // TODO: better way to auto-layout | |
31 | // Use reals for accurancy (we might get some compiler warnings... :) |
|
30 | // Use reals for accurancy (we might get some compiler warnings... :) | |
32 | // TODO: use temp variable for column count... |
|
31 | // TODO: use temp variable for column count... | |
33 | qreal maxSum = mModel.maxColumnSum(); |
|
32 | qreal maxSum = mModel.maxColumnSum(); | |
34 | qreal h = mHeight; |
|
33 | qreal h = mHeight; | |
35 | qreal scale = (h / maxSum); |
|
34 | qreal scale = (h / maxSum); | |
36 |
|
35 | |||
37 | int itemIndex(0); |
|
36 | int itemIndex(0); | |
38 | qreal tW = mWidth; |
|
37 | qreal tW = mWidth; | |
39 | qreal tC = mModel.countColumns() + 1; |
|
38 | qreal tC = mModel.countColumns() + 1; | |
40 | qreal xStep = (tW/tC); |
|
39 | qreal xStep = (tW/tC); | |
41 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); |
|
40 | qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); | |
42 | int labelIndex = mModel.countRows() * mModel.countColumns(); |
|
41 | int labelIndex = mModel.countRows() * mModel.countColumns(); | |
43 |
|
42 | |||
44 | for (int column = 0; column < mModel.countColumns(); column++) { |
|
43 | for (int column = 0; column < mModel.countColumns(); column++) { | |
45 | qreal yPos = h; |
|
44 | qreal yPos = h; | |
46 | for (int row=0; row < mModel.countRows(); row++) { |
|
45 | for (int row=0; row < mModel.countRows(); row++) { | |
47 | qreal barHeight = mModel.valueAt(row, column) * scale; |
|
46 | qreal barHeight = mModel.valueAt(row, column) * scale; | |
48 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); |
|
47 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | |
49 |
|
48 | |||
50 | // TODO: width settable per bar? |
|
49 | // TODO: width settable per bar? | |
51 | // TODO: how to get color for series(x) from theme? |
|
50 | // TODO: theme stuff | |
52 | // mTheme->themeForSeries(); |
|
51 | // mTheme->themeForSeries(); | |
53 | bar->resize(mBarDefaultWidth, barHeight); |
|
52 | bar->resize(mBarDefaultWidth, barHeight); | |
54 | bar->setColor(mColors.at(row)); |
|
53 | bar->setColor(mColors.at(row)); | |
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 |
|
66 | |||
68 | // Position separators |
|
67 | // Position separators | |
69 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? |
|
68 | int separatorIndex = labelIndex; // Separators are after labels in childItems(). TODO: better way to store these? | |
70 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. |
|
69 | xPos = xStep + xStep/2; // Initial position is between first and second group. ie one and half steps from left. | |
71 | for (int s=0; s < mModel.countColumns() - 1; s++) { |
|
70 | for (int s=0; s < mModel.countColumns() - 1; s++) { | |
72 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); |
|
71 | Separator* sep = reinterpret_cast<Separator*> (childItems().at(separatorIndex)); | |
73 | sep->setPos(xPos,0); |
|
72 | sep->setPos(xPos,0); | |
74 | sep->setSize(QSizeF(1,mHeight)); |
|
73 | sep->setSize(QSizeF(1,mHeight)); | |
75 | xPos += xStep; |
|
74 | xPos += xStep; | |
76 | separatorIndex++; |
|
75 | separatorIndex++; | |
77 | } |
|
76 | } | |
78 |
|
77 | |||
79 | mLayoutDirty = true; |
|
78 | mLayoutDirty = true; | |
80 | } |
|
79 | } | |
81 |
|
80 | |||
82 | QTCOMMERCIALCHART_END_NAMESPACE |
|
81 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now