##// END OF EJS Templates
animations to stacked and percent barchart
sauimone -
r694:08035ff71c15
parent child
Show More
@@ -1,354 +1,252
1 #include "barchartitem_p.h"
1 #include "barchartitem_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "qbarset.h"
4 #include "qbarset.h"
5 #include "qbarseries.h"
5 #include "qbarseries.h"
6 #include "qchart.h"
6 #include "qchart.h"
7 #include "qchartaxis.h"
7 #include "qchartaxis.h"
8 #include "qchartaxiscategories.h"
8 #include "qchartaxiscategories.h"
9 #include "chartpresenter_p.h"
9 #include "chartpresenter_p.h"
10 #include "chartanimator_p.h"
10 #include "chartanimator_p.h"
11 #include "chartdataset_p.h"
11 #include "chartdataset_p.h"
12 #include <QDebug>
12 #include <QDebug>
13 #include <QToolTip>
13 #include <QToolTip>
14
14
15 QTCOMMERCIALCHART_BEGIN_NAMESPACE
15 QTCOMMERCIALCHART_BEGIN_NAMESPACE
16
16
17 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
17 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
18 ChartItem(presenter),
18 ChartItem(presenter),
19 mLayoutSet(false),
19 mLayoutSet(false),
20 mSeries(series)
20 mSeries(series)
21 {
21 {
22 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
22 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
23 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
23 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
24 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
24 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
25 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
25 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
26 setZValue(ChartPresenter::BarSeriesZValue);
26 setZValue(ChartPresenter::BarSeriesZValue);
27 initAxisLabels();
27 initAxisLabels();
28 dataChanged();
28 dataChanged();
29 }
29 }
30
30
31 BarChartItem::~BarChartItem()
31 BarChartItem::~BarChartItem()
32 {
32 {
33 disconnect(this,SLOT(showToolTip(QPoint,QString)));
33 disconnect(this,SLOT(showToolTip(QPoint,QString)));
34 }
34 }
35
35
36 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
36 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
37 {
37 {
38 if (!mLayoutSet) {
38 if (!mLayoutSet) {
39 qDebug() << "BarChartItem::paint called without layout set. Aborting.";
39 qDebug() << "BarChartItem::paint called without layout set. Aborting.";
40 return;
40 return;
41 }
41 }
42 foreach(QGraphicsItem* i, childItems()) {
42 foreach(QGraphicsItem* i, childItems()) {
43 i->paint(painter,option,widget);
43 i->paint(painter,option,widget);
44 }
44 }
45 }
45 }
46
46
47 QRectF BarChartItem::boundingRect() const
47 QRectF BarChartItem::boundingRect() const
48 {
48 {
49 return m_rect;
49 return m_rect;
50 }
50 }
51
51
52 void BarChartItem::dataChanged()
52 void BarChartItem::dataChanged()
53 {
53 {
54 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
54 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
55 // Delete old bars
55 // Delete old bars
56 foreach (QGraphicsItem* item, childItems()) {
56 foreach (QGraphicsItem* item, childItems()) {
57 delete item;
57 delete item;
58 }
58 }
59
59
60 mBars.clear();
60 mBars.clear();
61 mFloatingValues.clear();
61 mFloatingValues.clear();
62 mLayout.clear();
62 mLayout.clear();
63
63
64 // Create new graphic items for bars
64 // Create new graphic items for bars
65 for (int c=0; c<mSeries->categoryCount(); c++) {
65 for (int c=0; c<mSeries->categoryCount(); c++) {
66 QString category = mSeries->categoryName(c);
66 QString category = mSeries->categoryName(c);
67 for (int s=0; s<mSeries->barsetCount(); s++) {
67 for (int s=0; s<mSeries->barsetCount(); s++) {
68 QBarSet *set = mSeries->barsetAt(s);
68 QBarSet *set = mSeries->barsetAt(s);
69 Bar *bar = new Bar(category,this);
69 Bar *bar = new Bar(category,this);
70 childItems().append(bar);
70 childItems().append(bar);
71 mBars.append(bar);
71 mBars.append(bar);
72 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
72 connect(bar,SIGNAL(clicked(QString)),set,SIGNAL(clicked(QString)));
73 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
73 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
74 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
74 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
75 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
75 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
76 mLayout.append(QRectF(0,0,0,0));
76 mLayout.append(QRectF(0,0,0,0));
77 }
77 }
78 }
78 }
79
79
80 // Create floating values
80 // Create floating values
81 for (int category=0; category<mSeries->categoryCount(); category++) {
81 for (int category=0; category<mSeries->categoryCount(); category++) {
82 for (int s=0; s<mSeries->barsetCount(); s++) {
82 for (int s=0; s<mSeries->barsetCount(); s++) {
83 QBarSet *set = mSeries->barsetAt(s);
83 QBarSet *set = mSeries->barsetAt(s);
84 BarValue *value = new BarValue(*set, this);
84 BarValue *value = new BarValue(*set, this);
85 childItems().append(value);
85 childItems().append(value);
86 mFloatingValues.append(value);
86 mFloatingValues.append(value);
87 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
87 connect(set,SIGNAL(toggleFloatingValues()),value,SLOT(toggleVisible()));
88 }
88 }
89 }
89 }
90 }
90 }
91 /*
92 void BarChartItem::layoutChanged()
93 {
94 qDebug() << "Deprecated BarChartItem::layoutChanged called. aborting";
95 return;
96 // Scale bars to new layout
97 // Layout for bars:
98 if (mSeries->barsetCount() <= 0) {
99 qDebug() << "No sets in model!";
100 return;
101 }
102
103 if (childItems().count() == 0) {
104 qDebug() << "WARNING: BarChartitem::layoutChanged called before graphics items are created!";
105 return;
106 }
107
108
109 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
110 qreal categoryCount = mSeries->categoryCount();
111 qreal setCount = mSeries->barsetCount();
112 qreal max = mSeries->max();
113
114 // Domain:
115 if (mDomainMaxY > max) {
116 max = mDomainMaxY;
117 }
118
119 qreal width = geometry().width();
120 qreal height = geometry().height();
121 qreal scale = (height/max);
122 qreal categoryWidth = width/categoryCount;
123 qreal barWidth = categoryWidth / (setCount+1);
124
125 BarLayout layout;
126
127 int itemIndex(0);
128 for (int category=0; category < categoryCount; category++) {
129 qreal xPos = categoryWidth * category + barWidth/2;
130 qreal yPos = height;
131 for (int set = 0; set < setCount; set++) {
132 qreal barHeight = mSeries->valueAt(set,category) * scale;
133 Bar* bar = mBars.at(itemIndex);
134
135 QRectF rect(xPos,yPos-barHeight,mBarWidth,barHeight);
136 layout.insert(bar,rect);
137 // TODO: width settable per bar?
138 bar->setRect(xPos, yPos-barHeight,barWidth, barHeight);
139 bar->setPen(mSeries->barsetAt(set)->pen());
140 bar->setBrush(mSeries->barsetAt(set)->brush());
141
142 // bar->resize(mBarWidth, barHeight);
143 // layout.insert(bar,QSizeF(mBarWidth,barHeight));
144 bar->setPen(mSeries->barsetAt(set)->pen());
145 bar->setBrush(mSeries->barsetAt(set)->brush());
146 // bar->setPos(xPos, yPos-barHeight);
147 itemIndex++;
148 xPos += barWidth;
149 }
150 }
151
152 // Position floating values
153 itemIndex = 0;
154 for (int category=0; category < mSeries->categoryCount(); category++) {
155 qreal xPos = categoryWidth * category + categoryWidth/2 + barWidth;
156 qreal yPos = height;
157 for (int set=0; set < mSeries->barsetCount(); set++) {
158 qreal barHeight = mSeries->valueAt(set,category) * scale;
159 BarValue* value = mFloatingValues.at(itemIndex);
160
161 QBarSet* barSet = mSeries->barsetAt(set);
162 value->resize(100,50); // TODO: proper layout for this.
163 value->setPos(xPos, yPos-barHeight/2);
164 value->setPen(barSet->floatingValuePen());
165
166 if (mSeries->valueAt(set,category) != 0) {
167 value->setValueString(QString::number(mSeries->valueAt(set,category)));
168 } else {
169 value->setValueString(QString(""));
170 }
171
172 itemIndex++;
173 xPos += barWidth;
174 }
175 }
176 // update();
177 }
178 */
179 QVector<QRectF> BarChartItem::calculateLayout()
91 QVector<QRectF> BarChartItem::calculateLayout()
180 {
92 {
181 // layoutChanged();
182 /*
183 BarLayout layout;
184 foreach(Bar* bar, mBars) {
185 layout.insert(bar,bar->boundingRect());
186 }
187 */
188 QVector<QRectF> layout;
93 QVector<QRectF> layout;
189
94
190 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
95 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
191 qreal categoryCount = mSeries->categoryCount();
96 qreal categoryCount = mSeries->categoryCount();
192 qreal setCount = mSeries->barsetCount();
97 qreal setCount = mSeries->barsetCount();
193
98
194 qreal width = geometry().width();
99 qreal width = geometry().width();
195 qreal height = geometry().height();
100 qreal height = geometry().height();
196
101
197 qreal max = mSeries->max();
102 qreal max = mSeries->max();
198
103
199 // Domain:
104 // Domain:
200 if (mDomainMaxY > max) {
105 if (mDomainMaxY > max) {
201 max = mDomainMaxY;
106 max = mDomainMaxY;
202 }
107 }
203
108
204 qreal scale = (height/max);
109 qreal scale = (height/max);
205 qreal categoryWidth = width/categoryCount;
110 qreal categoryWidth = width/categoryCount;
206 qreal barWidth = categoryWidth / (setCount+1);
111 qreal barWidth = categoryWidth / (setCount+1);
207
112
208 int itemIndex(0);
113 int itemIndex(0);
209 for (int category=0; category < categoryCount; category++) {
114 for (int category=0; category < categoryCount; category++) {
210 qreal xPos = categoryWidth * category + barWidth/2;
115 qreal xPos = categoryWidth * category + barWidth/2;
211 qreal yPos = height;
116 qreal yPos = height;
212 for (int set = 0; set < setCount; set++) {
117 for (int set = 0; set < setCount; set++) {
213 qreal barHeight = mSeries->valueAt(set,category) * scale;
118 qreal barHeight = mSeries->valueAt(set,category) * scale;
214 Bar* bar = mBars.at(itemIndex);
119 Bar* bar = mBars.at(itemIndex);
215
120
216 QRectF rect(xPos,yPos-barHeight,barWidth,barHeight);
121 QRectF rect(xPos,yPos-barHeight,barWidth,barHeight);
217 //layout.insert(bar,rect);
218 layout.append(rect);
122 layout.append(rect);
219 // TODO: width settable per bar?
220 // bar->resize(mBarWidth, barHeight);
221 // layout.insert(bar,QSizeF(mBarWidth,barHeight));
222 bar->setPen(mSeries->barsetAt(set)->pen());
123 bar->setPen(mSeries->barsetAt(set)->pen());
223 bar->setBrush(mSeries->barsetAt(set)->brush());
124 bar->setBrush(mSeries->barsetAt(set)->brush());
224 // bar->setPos(xPos, yPos-barHeight);
225 itemIndex++;
125 itemIndex++;
226 xPos += barWidth;
126 xPos += barWidth;
227 }
127 }
228 }
128 }
229
129
230 // Position floating values
130 // Position floating values
231 itemIndex = 0;
131 itemIndex = 0;
232 for (int category=0; category < mSeries->categoryCount(); category++) {
132 for (int category=0; category < mSeries->categoryCount(); category++) {
233 qreal xPos = categoryWidth * category + barWidth;
133 qreal xPos = categoryWidth * category + barWidth;
234 qreal yPos = height;
134 qreal yPos = height;
235 for (int set=0; set < mSeries->barsetCount(); set++) {
135 for (int set=0; set < mSeries->barsetCount(); set++) {
236 qreal barHeight = mSeries->valueAt(set,category) * scale;
136 qreal barHeight = mSeries->valueAt(set,category) * scale;
237 BarValue* value = mFloatingValues.at(itemIndex);
137 BarValue* value = mFloatingValues.at(itemIndex);
238
138
239 QBarSet* barSet = mSeries->barsetAt(set);
139 QBarSet* barSet = mSeries->barsetAt(set);
240 value->resize(100,50); // TODO: proper layout for this.
140 value->resize(100,50); // TODO: proper layout for this.
241 value->setPos(xPos, yPos-barHeight/2);
141 value->setPos(xPos, yPos-barHeight/2);
242 value->setPen(barSet->floatingValuePen());
142 value->setPen(barSet->floatingValuePen());
243
143
244 if (mSeries->valueAt(set,category) != 0) {
144 if (mSeries->valueAt(set,category) != 0) {
245 value->setValueString(QString::number(mSeries->valueAt(set,category)));
145 value->setValueString(QString::number(mSeries->valueAt(set,category)));
246 } else {
146 } else {
247 value->setValueString(QString(""));
147 value->setValueString(QString(""));
248 }
148 }
249
149
250 itemIndex++;
150 itemIndex++;
251 xPos += barWidth;
151 xPos += barWidth;
252 }
152 }
253 }
153 }
254
154
255 return layout;
155 return layout;
256 }
156 }
257
157
258 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
158 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
259 {
159 {
260 if (animator())
160 if (animator())
261 animator()->updateLayout(this, mLayout, layout);
161 animator()->updateLayout(this, mLayout, layout);
262 else
162 else
263 setLayout(layout);
163 setLayout(layout);
264 }
164 }
265
165
266 void BarChartItem::setLayout(const QVector<QRectF> &layout)
166 void BarChartItem::setLayout(const QVector<QRectF> &layout)
267 {
167 {
268 mLayout = layout;
168 mLayout = layout;
269
169
270 for (int i=0; i<mBars.count(); i++) {
170 for (int i=0; i<mBars.count(); i++) {
271 //mBars.at(i)->setSize(layout.at(i).size());
272 //mBars.at(i)->setPos(layout.at(i).topLeft());
273 mBars.at(i)->setRect(layout.at(i));
171 mBars.at(i)->setRect(layout.at(i));
274 }
172 }
275
173
276 update();
174 update();
277 }
175 }
278
176
279 void BarChartItem::initAxisLabels()
177 void BarChartItem::initAxisLabels()
280 {
178 {
281 int count = mSeries->categoryCount();
179 int count = mSeries->categoryCount();
282 if (0 == count) {
180 if (0 == count) {
283 return;
181 return;
284 }
182 }
285
183
286 Domain* domain = presenter()->dataSet()->domain(mSeries);
184 Domain* domain = presenter()->dataSet()->domain(mSeries);
287
185
288 qreal min = 0;
186 qreal min = 0;
289 qreal max = count+1;
187 qreal max = count+1;
290
188
291 domain->setRangeX(min,max,count+1);
189 domain->setRangeX(min,max,count+1);
292 }
190 }
293
191
294 //handlers
192 //handlers
295
193
296 void BarChartItem::handleModelChanged(int index)
194 void BarChartItem::handleModelChanged(int index)
297 {
195 {
298 Q_UNUSED(index)
196 Q_UNUSED(index)
299 dataChanged();
197 dataChanged();
300 }
198 }
301
199
302 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
200 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
303 {
201 {
304 mDomainMinX = minX;
202 mDomainMinX = minX;
305 mDomainMaxX = maxX;
203 mDomainMaxX = maxX;
306 mDomainMinY = minY;
204 mDomainMinY = minY;
307 mDomainMaxY = maxY;
205 mDomainMaxY = maxY;
308 handleLayoutChanged();
206 handleLayoutChanged();
309
207
310 /*
208 /*
311 int count = mSeries->categoryCount();
209 int count = mSeries->categoryCount();
312 if (0 == count) {
210 if (0 == count) {
313 return;
211 return;
314 }
212 }
315
213
316 // Position labels to domain
214 // Position labels to domain
317 qreal min = domain.minX();
215 qreal min = domain.minX();
318 qreal max = domain.maxX();
216 qreal max = domain.maxX();
319 qreal step = (max-min)/count;
217 qreal step = (max-min)/count;
320 QChartAxisCategories& categories = mChart->axisX()->categories();
218 QChartAxisCategories& categories = mChart->axisX()->categories();
321 categories.clear();
219 categories.clear();
322 for (int i=0; i<count; i++) {
220 for (int i=0; i<count; i++) {
323 categories.insert(min,mSeries->categoryName(i));
221 categories.insert(min,mSeries->categoryName(i));
324 min += step;
222 min += step;
325 }
223 }
326 */
224 */
327 }
225 }
328
226
329 void BarChartItem::handleGeometryChanged(const QRectF& rect)
227 void BarChartItem::handleGeometryChanged(const QRectF& rect)
330 {
228 {
331 m_rect=rect;
229 m_rect=rect;
332 handleLayoutChanged();
230 handleLayoutChanged();
333 mLayoutSet = true;
231 mLayoutSet = true;
334 setPos(rect.topLeft());
232 setPos(rect.topLeft());
335 }
233 }
336
234
337 void BarChartItem::handleLayoutChanged()
235 void BarChartItem::handleLayoutChanged()
338 {
236 {
339 qDebug() << "BarChartItem::handleLayoutChanged";
237 qDebug() << "BarChartItem::handleLayoutChanged";
340 QVector<QRectF> layout = calculateLayout();
238 QVector<QRectF> layout = calculateLayout();
341 applyLayout(layout);
239 applyLayout(layout);
342 update();
240 update();
343 }
241 }
344
242
345
243
346 void BarChartItem::showToolTip(QPoint pos, QString tip)
244 void BarChartItem::showToolTip(QPoint pos, QString tip)
347 {
245 {
348 // TODO: cool tooltip instead of default
246 // TODO: cool tooltip instead of default
349 QToolTip::showText(pos,tip);
247 QToolTip::showText(pos,tip);
350 }
248 }
351
249
352 #include "moc_barchartitem_p.cpp"
250 #include "moc_barchartitem_p.cpp"
353
251
354 QTCOMMERCIALCHART_END_NAMESPACE
252 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,78
1 #ifndef BARCHARTITEM_H
1 #ifndef BARCHARTITEM_H
2 #define BARCHARTITEM_H
2 #define BARCHARTITEM_H
3
3
4 #include "chartitem_p.h"
4 #include "chartitem_p.h"
5 #include "qbarseries.h"
5 #include "qbarseries.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 class Bar;
12 class Bar;
13 class BarValue;
13 class BarValue;
14 class QChartAxisCategories;
14 class QChartAxisCategories;
15 class QChart;
15 class QChart;
16
16
17 //typedef QVector<QRectF> BarLayout;
17 //typedef QVector<QRectF> BarLayout;
18
18
19 class BarChartItem : public ChartItem
19 class BarChartItem : public ChartItem
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22 public:
22 public:
23 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
23 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
24 virtual ~BarChartItem();
24 virtual ~BarChartItem();
25
25
26 // Common implemantation of different presenters. Not to be instantiated.
26 // Common implemantation of different presenters. Not to be instantiated.
27 // TODO: combine this with BarPresenter and derive other presenters from it?
27 // TODO: combine this with BarPresenter and derive other presenters from it?
28
28
29 public:
29 public:
30 // From QGraphicsItem
30 // From QGraphicsItem
31 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
31 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
32 QRectF boundingRect() const;
32 QRectF boundingRect() const;
33
33
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 private slots:
36 private slots:
37 //virtual void layoutChanged(); // layout has changed -> need to recalculate bar sizes
38
37
39 public:
38 public:
40 QVector<QRectF> calculateLayout();
39 virtual QVector<QRectF> calculateLayout();
41 void applyLayout(const QVector<QRectF> &layout);
40 void applyLayout(const QVector<QRectF> &layout);
42 void setLayout(const QVector<QRectF> &layout);
41 void setLayout(const QVector<QRectF> &layout);
43 void updateLayout(const QVector<QRectF> &layout);
42 void updateLayout(const QVector<QRectF> &layout);
44
43
45 QRectF geometry() const { return m_rect;}
44 QRectF geometry() const { return m_rect;}
46
45
47 protected:
46 protected:
48 void initAxisLabels();
47 void initAxisLabels();
49
48
50 public slots:
49 public slots:
51 void handleModelChanged(int index);
50 void handleModelChanged(int index);
52 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
51 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
53 void handleGeometryChanged(const QRectF& size);
52 void handleGeometryChanged(const QRectF& size);
54 void handleLayoutChanged();
53 void handleLayoutChanged();
55
54
56 // Internal slots
55 // Internal slots
57 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
56 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
58
57
59 protected:
58 protected:
60
59
61 // TODO: consider these.
60 // TODO: consider these.
62 qreal mDomainMinX;
61 qreal mDomainMinX;
63 qreal mDomainMaxX;
62 qreal mDomainMaxX;
64 qreal mDomainMinY;
63 qreal mDomainMinY;
65 qreal mDomainMaxY;
64 qreal mDomainMaxY;
66
65
67 QRectF m_rect;
66 QRectF m_rect;
68 bool mLayoutSet; // True, if component has been laid out.
67 bool mLayoutSet; // True, if component has been laid out.
69 QVector<QRectF> mLayout;
68 QVector<QRectF> mLayout;
70
69
71 // Not owned.
70 // Not owned.
72 QBarSeries* mSeries;
71 QBarSeries* mSeries;
73 QList<Bar*> mBars;
72 QList<Bar*> mBars;
74 QList<BarValue*> mFloatingValues;
73 QList<BarValue*> mFloatingValues;
75 };
74 };
76
75
77 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
78
77
79 #endif // BARCHARTITEM_H
78 #endif // BARCHARTITEM_H
@@ -1,94 +1,81
1 #include "percentbarchartitem_p.h"
1 #include "percentbarchartitem_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "qbarset.h"
4 #include "qbarset.h"
5 #include <QDebug>
5 #include <QDebug>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter* presenter) :
9 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter* presenter) :
10 BarChartItem(series, presenter)
10 BarChartItem(series, presenter)
11 {
11 {
12 }
12 }
13
13
14 void PercentBarChartItem::layoutChanged()
14 QVector<QRectF> PercentBarChartItem::calculateLayout()
15 {
15 {
16 // Scale bars to new layout
16 QVector<QRectF> layout;
17 // Layout for bars:
18 if (mSeries->barsetCount() <= 0) {
19 qDebug() << "No sets in model!";
20 // Nothing to do.
21 return;
22 }
23
24 if (childItems().count() == 0) {
25 qDebug() << "WARNING: PercentBarChartItem::layoutChanged called before graphics items are created!";
26 return;
27 }
28
17
29 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
18 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
30 qreal width = geometry().width();
19 qreal width = geometry().width();
31 qreal height = geometry().height();
20 qreal height = geometry().height();
32
21
33 qreal categoryCount = mSeries->categoryCount();
22 qreal categoryCount = mSeries->categoryCount();
34 qreal barWidth = width / (mSeries->categoryCount() * 2);
23 qreal barWidth = width / (mSeries->categoryCount() * 2);
35 qreal xStep = width/categoryCount;
24 qreal xStep = width/categoryCount;
36 qreal xPos = xStep/2 - barWidth / 2;
25 qreal xPos = xStep/2 - barWidth / 2;
37
26
38 int itemIndex(0);
27 int itemIndex(0);
39 for (int category = 0; category < categoryCount ; category++) {
28 for (int category = 0; category < categoryCount ; category++) {
40 qreal colSum = mSeries->categorySum(category);
29 qreal colSum = mSeries->categorySum(category);
41 qreal scale = (height / colSum);
30 qreal scale = (height / colSum);
42 qreal yPos = height;
31 qreal yPos = height;
43 for (int set=0; set < mSeries->barsetCount(); set++) {
32 for (int set=0; set < mSeries->barsetCount(); set++) {
44 qreal barHeight = mSeries->valueAt(set, category) * scale;
33 qreal barHeight = mSeries->valueAt(set, category) * scale;
45 Bar* bar = mBars.at(itemIndex);
34 Bar* bar = mBars.at(itemIndex);
46
47 // TODO: width settable per bar?
48
49 bar->setPen(mSeries->barsetAt(set)->pen());
35 bar->setPen(mSeries->barsetAt(set)->pen());
50 bar->setRect(xPos, yPos-barHeight,barWidth, barHeight);
51 bar->setBrush(mSeries->barsetAt(set)->brush());
36 bar->setBrush(mSeries->barsetAt(set)->brush());
52
37 QRectF rect(xPos, yPos-barHeight,barWidth, barHeight);
38 layout.append(rect);
53 itemIndex++;
39 itemIndex++;
54 yPos -= barHeight;
40 yPos -= barHeight;
55 }
41 }
56 xPos += xStep;
42 xPos += xStep;
57 }
43 }
58
44
59 // Position floating values
45 // Position floating values
60 itemIndex = 0;
46 itemIndex = 0;
61 xPos = (width/categoryCount);
47 xPos = (width/categoryCount);
62 for (int category=0; category < mSeries->categoryCount(); category++) {
48 for (int category=0; category < mSeries->categoryCount(); category++) {
63 qreal yPos = height;
49 qreal yPos = height;
64 qreal colSum = mSeries->categorySum(category);
50 qreal colSum = mSeries->categorySum(category);
65 qreal scale = (height / colSum);
51 qreal scale = (height / colSum);
66 for (int set=0; set < mSeries->barsetCount(); set++) {
52 for (int set=0; set < mSeries->barsetCount(); set++) {
67 qreal barHeight = mSeries->valueAt(set,category) * scale;
53 qreal barHeight = mSeries->valueAt(set,category) * scale;
68 BarValue* value = mFloatingValues.at(itemIndex);
54 BarValue* value = mFloatingValues.at(itemIndex);
69
55
70 QBarSet* barSet = mSeries->barsetAt(set);
56 QBarSet* barSet = mSeries->barsetAt(set);
71 value->resize(100,50); // TODO: proper layout for this.
57 value->resize(100,50); // TODO: proper layout for this.
72 value->setPos(xPos, yPos-barHeight/2);
58 value->setPos(xPos, yPos-barHeight/2);
73 value->setPen(barSet->floatingValuePen());
59 value->setPen(barSet->floatingValuePen());
74
60
75 if (mSeries->valueAt(set,category) != 0) {
61 if (mSeries->valueAt(set,category) != 0) {
76 int p = mSeries->percentageAt(set,category) * 100;
62 int p = mSeries->percentageAt(set,category) * 100;
77 QString vString(QString::number(p));
63 QString vString(QString::number(p));
78 vString.truncate(3);
64 vString.truncate(3);
79 vString.append("%");
65 vString.append("%");
80 value->setValueString(vString);
66 value->setValueString(vString);
81 } else {
67 } else {
82 value->setValueString(QString(""));
68 value->setValueString(QString(""));
83 }
69 }
84
70
85 itemIndex++;
71 itemIndex++;
86 yPos -= barHeight;
72 yPos -= barHeight;
87 }
73 }
88 xPos += xStep;
74 xPos += xStep;
89 }
75 }
76 return layout;
90 }
77 }
91
78
92 #include "moc_percentbarchartitem_p.cpp"
79 #include "moc_percentbarchartitem_p.cpp"
93
80
94 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,28 +1,27
1 #ifndef PERCENTBARCHARTITEM_H
1 #ifndef PERCENTBARCHARTITEM_H
2 #define PERCENTBARCHARTITEM_H
2 #define PERCENTBARCHARTITEM_H
3
3
4 #include "barchartitem_p.h"
4 #include "barchartitem_p.h"
5 #include <QGraphicsItem>
5 #include <QGraphicsItem>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 class QBarSeries;
9 class QBarSeries;
10
10
11 class PercentBarChartItem : public BarChartItem
11 class PercentBarChartItem : public BarChartItem
12 {
12 {
13 Q_OBJECT
13 Q_OBJECT
14 public:
14 public:
15 PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter);
15 PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter);
16
16
17 private:
17 private:
18
18 virtual QVector<QRectF> calculateLayout();
19 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
20
19
21 private:
20 private:
22
21
23 // Data
22 // Data
24 };
23 };
25
24
26 QTCOMMERCIALCHART_END_NAMESPACE
25 QTCOMMERCIALCHART_END_NAMESPACE
27
26
28 #endif // PERCENTBARCHARTITEM_H
27 #endif // PERCENTBARCHARTITEM_H
@@ -1,101 +1,84
1 #include "stackedbarchartitem_p.h"
1 #include "stackedbarchartitem_p.h"
2 #include "bar_p.h"
2 #include "bar_p.h"
3 #include "barvalue_p.h"
3 #include "barvalue_p.h"
4 #include "qbarset.h"
4 #include "qbarset.h"
5 #include <QDebug>
5 #include <QDebug>
6
6
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8
8
9 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
9 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
10 BarChartItem(series,presenter)
10 BarChartItem(series,presenter)
11 {
11 {
12 }
12 }
13
13
14 StackedBarChartItem::~StackedBarChartItem()
14 StackedBarChartItem::~StackedBarChartItem()
15 {
15 {
16 }
16 }
17
17
18
18
19 void StackedBarChartItem::layoutChanged()
19 QVector<QRectF> StackedBarChartItem::calculateLayout()
20 {
20 {
21 // Scale bars to new layout
21 QVector<QRectF> layout;
22 // Layout for bars:
23 if (mSeries->barsetCount() <= 0) {
24 qDebug() << "No sets in model!";
25 // Nothing to do.
26 return;
27 }
28
29 if (mSeries->categoryCount() == 0) {
30 qDebug() << "No categories in model!";
31 // Nothing to do
32 return;
33 }
34
35 if (childItems().count() == 0) {
36 qDebug() << "WARNING: StackedBarChartItem::layoutChanged called before graphics items are created!";
37 return;
38 }
39
40 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
22 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
41
23
42 qreal maxSum = mSeries->maxCategorySum();
24 qreal maxSum = mSeries->maxCategorySum();
43 // Domain:
25 // Domain:
44 if (mDomainMaxY > maxSum) {
26 if (mDomainMaxY > maxSum) {
45 maxSum = mDomainMaxY;
27 maxSum = mDomainMaxY;
46 }
28 }
47
29
48 qreal height = geometry().height();
30 qreal height = geometry().height();
49 qreal width = geometry().width();
31 qreal width = geometry().width();
50 qreal scale = (height / mSeries->maxCategorySum());
32 qreal scale = (height / mSeries->maxCategorySum());
51 qreal categotyCount = mSeries->categoryCount();
33 qreal categotyCount = mSeries->categoryCount();
52 qreal barWidth = width / (categotyCount *2);
34 qreal barWidth = width / (categotyCount *2);
53 qreal xStep = width/categotyCount;
35 qreal xStep = width/categotyCount;
54 qreal xPos = xStep/2 - barWidth/2;
36 qreal xPos = xStep/2 - barWidth/2;
55
37
56
57 int itemIndex(0);
38 int itemIndex(0);
58 for (int category = 0; category < categotyCount; category++) {
39 for (int category = 0; category < categotyCount; category++) {
59 qreal yPos = height;
40 qreal yPos = height;
60 for (int set=0; set < mSeries->barsetCount(); set++) {
41 for (int set=0; set < mSeries->barsetCount(); set++) {
61 qreal barHeight = mSeries->valueAt(set, category) * scale;
42 qreal barHeight = mSeries->valueAt(set, category) * scale;
62 Bar* bar = mBars.at(itemIndex);
43 Bar* bar = mBars.at(itemIndex);
63 bar->setPen(mSeries->barsetAt(set)->pen());
44 bar->setPen(mSeries->barsetAt(set)->pen());
64 bar->setBrush(mSeries->barsetAt(set)->brush());
45 bar->setBrush(mSeries->barsetAt(set)->brush());
65 bar->setRect(xPos, yPos-barHeight,barWidth, barHeight);
46 QRectF rect(xPos,yPos-barHeight,barWidth,barHeight);
47 layout.append(rect);
66 itemIndex++;
48 itemIndex++;
67 yPos -= barHeight;
49 yPos -= barHeight;
68 }
50 }
69 xPos += xStep;
51 xPos += xStep;
70 }
52 }
71
53
72 // Position floating values
54 // Position floating values
73 itemIndex = 0;
55 itemIndex = 0;
74 xPos = (width/categotyCount);
56 xPos = (width/categotyCount);
75 for (int category=0; category < mSeries->categoryCount(); category++) {
57 for (int category=0; category < mSeries->categoryCount(); category++) {
76 qreal yPos = height;
58 qreal yPos = height;
77 for (int set=0; set < mSeries->barsetCount(); set++) {
59 for (int set=0; set < mSeries->barsetCount(); set++) {
78 qreal barHeight = mSeries->valueAt(set,category) * scale;
60 qreal barHeight = mSeries->valueAt(set,category) * scale;
79 BarValue* value = mFloatingValues.at(itemIndex);
61 BarValue* value = mFloatingValues.at(itemIndex);
80
62
81 QBarSet* barSet = mSeries->barsetAt(set);
63 QBarSet* barSet = mSeries->barsetAt(set);
82 value->resize(100,50); // TODO: proper layout for this.
64 value->resize(100,50); // TODO: proper layout for this.
83 value->setPos(xPos, yPos-barHeight/2);
65 value->setPos(xPos, yPos-barHeight/2);
84 value->setPen(barSet->floatingValuePen());
66 value->setPen(barSet->floatingValuePen());
85
67
86 if (mSeries->valueAt(set,category) != 0) {
68 if (mSeries->valueAt(set,category) != 0) {
87 value->setValueString(QString::number(mSeries->valueAt(set,category)));
69 value->setValueString(QString::number(mSeries->valueAt(set,category)));
88 } else {
70 } else {
89 value->setValueString(QString(""));
71 value->setValueString(QString(""));
90 }
72 }
91
73
92 itemIndex++;
74 itemIndex++;
93 yPos -= barHeight;
75 yPos -= barHeight;
94 }
76 }
95 xPos += xStep;
77 xPos += xStep;
96 }
78 }
79 return layout;
97 }
80 }
98
81
99 #include "moc_stackedbarchartitem_p.cpp"
82 #include "moc_stackedbarchartitem_p.cpp"
100
83
101 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,28 +1,28
1 #ifndef STACKEDBARCHARTITEM_H
1 #ifndef STACKEDBARCHARTITEM_H
2 #define STACKEDBARCHARTITEM_H
2 #define STACKEDBARCHARTITEM_H
3
3
4 #include "barchartitem_p.h"
4 #include "barchartitem_p.h"
5 #include "qstackedbarseries.h"
5 #include "qstackedbarseries.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class StackedBarChartItem : public BarChartItem
10 class StackedBarChartItem : public BarChartItem
11 {
11 {
12 Q_OBJECT
12 Q_OBJECT
13 public:
13 public:
14 StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter);
14 StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter);
15 ~StackedBarChartItem();
15 ~StackedBarChartItem();
16
16
17 private:
17 private:
18 // From BarChartItem
18 // From BarChartItem
19 void layoutChanged(); // layout has changed -> need to recalculate bar sizes
19 virtual QVector<QRectF> calculateLayout();
20
20
21 private:
21 private:
22
22
23 // Data
23 // Data
24 };
24 };
25
25
26 QTCOMMERCIALCHART_END_NAMESPACE
26 QTCOMMERCIALCHART_END_NAMESPACE
27
27
28 #endif // STACKEDBARCHARTITEM_H
28 #endif // STACKEDBARCHARTITEM_H
General Comments 0
You need to be logged in to leave comments. Login now