##// END OF EJS Templates
simple text item for barvalue
sauimone -
r811:04debc5ee52b
parent child
Show More
@@ -1,236 +1,235
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "barchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "barvalue_p.h"
24 24 #include "qbarset.h"
25 25 #include "qbarseries.h"
26 26 #include "qchart.h"
27 27 #include "qchartaxis.h"
28 28 #include "qchartaxiscategories.h"
29 29 #include "chartpresenter_p.h"
30 30 #include "chartanimator_p.h"
31 31 #include "chartdataset_p.h"
32 32 #include <QDebug>
33 33 #include <QToolTip>
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36
37 37 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
38 38 ChartItem(presenter),
39 39 m_layoutSet(false),
40 40 m_series(series)
41 41 {
42 42 connect(series, SIGNAL(showToolTip(QPoint,QString)), this, SLOT(showToolTip(QPoint,QString)));
43 43 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
44 44 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
45 45 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
46 46 setZValue(ChartPresenter::BarSeriesZValue);
47 47 dataChanged();
48 48 }
49 49
50 50 BarChartItem::~BarChartItem()
51 51 {
52 52 disconnect(this,SLOT(showToolTip(QPoint,QString)));
53 53 }
54 54
55 55 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
56 56 {
57 57 if (!m_layoutSet) {
58 58 qWarning() << "BarChartItem::paint called without layout set. Aborting.";
59 59 return;
60 60 }
61 61
62 62 foreach(QGraphicsItem* i, childItems())
63 63 i->paint(painter,option,widget);
64 64 }
65 65
66 66 QRectF BarChartItem::boundingRect() const
67 67 {
68 68 return m_rect;
69 69 }
70 70
71 71 void BarChartItem::dataChanged()
72 72 {
73 73 // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them?
74 74 // Delete old bars
75 75 foreach (QGraphicsItem *item, childItems())
76 76 delete item;
77 77
78 78 m_bars.clear();
79 79 m_floatingValues.clear();
80 80 m_layout.clear();
81 81
82 82 // Create new graphic items for bars
83 83 for (int c = 0; c < m_series->categoryCount(); c++) {
84 84 QString category = m_series->categoryName(c);
85 85 for (int s = 0; s < m_series->barsetCount(); s++) {
86 86 QBarSet *set = m_series->barsetAt(s);
87 87 Bar *bar = new Bar(category,this);
88 88 childItems().append(bar);
89 89 m_bars.append(bar);
90 90 connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString)));
91 91 connect(bar, SIGNAL(rightClicked(QString)), set, SIGNAL(rightClicked(QString)));
92 92 connect(bar, SIGNAL(hoverEntered(QPoint)), set, SLOT(barHoverEnterEvent(QPoint)));
93 93 connect(bar, SIGNAL(hoverLeaved()), set, SLOT(barHoverLeaveEvent()));
94 94 m_layout.append(QRectF(0, 0, 0, 0));
95 95 }
96 96 }
97 97
98 98 // Create floating values
99 99 for (int category = 0; category < m_series->categoryCount(); category++) {
100 100 for (int s = 0; s < m_series->barsetCount(); s++) {
101 101 QBarSet *set = m_series->barsetAt(s);
102 102 BarValue *value = new BarValue(*set, this);
103 103 childItems().append(value);
104 104 m_floatingValues.append(value);
105 105 connect(set, SIGNAL(toggleFloatingValues()), value, SLOT(toggleVisible()));
106 106 }
107 107 }
108 108 }
109 109
110 110 QVector<QRectF> BarChartItem::calculateLayout()
111 111 {
112 112 QVector<QRectF> layout;
113 113
114 114 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
115 115 qreal categoryCount = m_series->categoryCount();
116 116 qreal setCount = m_series->barsetCount();
117 117
118 118 qreal width = geometry().width();
119 119 qreal height = geometry().height();
120 120
121 121 qreal max = m_series->max();
122 122
123 123 // Domain:
124 124 if (m_domainMaxY > max) {
125 125 max = m_domainMaxY;
126 126 }
127 127
128 128 qreal scale = (height / max);
129 129 qreal categoryWidth = width / categoryCount;
130 130 qreal barWidth = categoryWidth / (setCount+1);
131 131
132 132 int itemIndex(0);
133 133 for (int category = 0; category < categoryCount; category++) {
134 134 qreal xPos = categoryWidth * category + barWidth / 2;
135 135 qreal yPos = height;
136 136 for (int set = 0; set < setCount; set++) {
137 137 qreal barHeight = m_series->valueAt(set, category) * scale;
138 138 Bar* bar = m_bars.at(itemIndex);
139 139
140 140 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
141 141 layout.append(rect);
142 142 bar->setPen(m_series->barsetAt(set)->pen());
143 143 bar->setBrush(m_series->barsetAt(set)->brush());
144 144 itemIndex++;
145 145 xPos += barWidth;
146 146 }
147 147 }
148 148
149 149 // Position floating values
150 150 itemIndex = 0;
151 151 for (int category = 0; category < m_series->categoryCount(); category++) {
152 152 qreal xPos = categoryWidth * category + barWidth;
153 153 qreal yPos = height;
154 154 for (int set=0; set < m_series->barsetCount(); set++) {
155 155 qreal barHeight = m_series->valueAt(set, category) * scale;
156 156 BarValue* value = m_floatingValues.at(itemIndex);
157 157
158 158 QBarSet* barSet = m_series->barsetAt(set);
159 value->resize(100, 50); // TODO: proper layout for this.
160 value->setPos(xPos, yPos-barHeight / 2);
161 value->setPen(barSet->floatingValuePen());
162 159
163 160 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
164 161 value->setText(QString::number(m_series->valueAt(set, category)));
165 162 } else {
166 163 value->setText(QString(""));
167 164 }
168 165
166 value->setPos(xPos, yPos-barHeight / 2);
167 value->setPen(barSet->floatingValuePen());
168
169 169 itemIndex++;
170 170 xPos += barWidth;
171 171 }
172 172 }
173 173
174 174 return layout;
175 175 }
176 176
177 177 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
178 178 {
179 179 if (animator())
180 180 animator()->updateLayout(this, m_layout, layout);
181 181 else
182 182 setLayout(layout);
183 183 }
184 184
185 185 void BarChartItem::setLayout(const QVector<QRectF> &layout)
186 186 {
187 187 m_layout = layout;
188 188
189 189 for (int i=0; i < m_bars.count(); i++)
190 190 m_bars.at(i)->setRect(layout.at(i));
191 191
192 192 update();
193 193 }
194 194
195 195 //handlers
196 196
197 197 void BarChartItem::handleModelChanged(int index)
198 198 {
199 199 Q_UNUSED(index)
200 200 dataChanged();
201 201 }
202 202
203 203 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
204 204 {
205 205 m_domainMinX = minX;
206 206 m_domainMaxX = maxX;
207 207 m_domainMinY = minY;
208 208 m_domainMaxY = maxY;
209 209 handleLayoutChanged();
210 210 }
211 211
212 212 void BarChartItem::handleGeometryChanged(const QRectF &rect)
213 213 {
214 214 m_rect = rect;
215 215 handleLayoutChanged();
216 216 m_layoutSet = true;
217 217 setPos(rect.topLeft());
218 218 }
219 219
220 220 void BarChartItem::handleLayoutChanged()
221 221 {
222 222 QVector<QRectF> layout = calculateLayout();
223 223 applyLayout(layout);
224 224 update();
225 225 }
226 226
227
228 227 void BarChartItem::showToolTip(QPoint pos, QString tip)
229 228 {
230 229 // TODO: cool tooltip instead of default
231 230 QToolTip::showText(pos, tip);
232 231 }
233 232
234 233 #include "moc_barchartitem_p.cpp"
235 234
236 235 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,93 +1,92
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef BARCHARTITEM_H
22 22 #define BARCHARTITEM_H
23 23
24 24 #include "chartitem_p.h"
25 25 #include "qbarseries.h"
26 26 #include <QPen>
27 27 #include <QBrush>
28 #include <QGraphicsItem>
29 28
30 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 30
32 31 class Bar;
33 32 class BarValue;
34 33 class QChartAxisCategories;
35 34 class QChart;
36 35
37 36 //typedef QVector<QRectF> BarLayout;
38 37
39 38 class BarChartItem : public ChartItem
40 39 {
41 40 Q_OBJECT
42 41 public:
43 42 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
44 43 virtual ~BarChartItem();
45 44
46 45 // Common implemantation of different presenters. Not to be instantiated.
47 46 // TODO: combine this with BarPresenter and derive other presenters from it?
48 47
49 48 public:
50 49 // From QGraphicsItem
51 50 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
52 51 QRectF boundingRect() const;
53 52
54 53 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
55 54 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
56 55
57 56 virtual QVector<QRectF> calculateLayout();
58 57 void applyLayout(const QVector<QRectF> &layout);
59 58 void setLayout(const QVector<QRectF> &layout);
60 59 void updateLayout(const QVector<QRectF> &layout);
61 60
62 61 QRectF geometry() const { return m_rect;}
63 62
64 63 public Q_SLOTS:
65 64 void handleModelChanged(int index);
66 65 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
67 66 void handleGeometryChanged(const QRectF &size);
68 67 void handleLayoutChanged();
69 68
70 69 // Internal slots
71 70 void showToolTip(QPoint pos, QString tip); // shows tooltip (if enabled)
72 71
73 72 protected:
74 73
75 74 // TODO: consider these.
76 75 qreal m_domainMinX;
77 76 qreal m_domainMaxX;
78 77 qreal m_domainMinY;
79 78 qreal m_domainMaxY;
80 79
81 80 QRectF m_rect;
82 81 bool m_layoutSet; // True, if component has been laid out.
83 82 QVector<QRectF> m_layout;
84 83
85 84 // Not owned.
86 85 QBarSeries *m_series;
87 86 QList<Bar *> m_bars;
88 87 QList<BarValue *> m_floatingValues;
89 88 };
90 89
91 90 QTCOMMERCIALCHART_END_NAMESPACE
92 91
93 92 #endif // BARCHARTITEM_H
@@ -1,93 +1,88
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "barvalue_p.h"
22 22 #include <QPainter>
23 23 #include <QPen>
24 #include <QGraphicsSimpleTextItem>
24 25
25 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 27
27 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent)
28 : QGraphicsObject(parent),
28 BarValue::BarValue(QBarSet &set, QGraphicsItem *parent) : QGraphicsObject(parent),
29 29 m_barSet(set),
30 m_xPos(0),
31 m_yPos(0),
32 m_width(0),
33 m_height(0)
30 m_textItem(new QGraphicsSimpleTextItem(this))
34 31 {
35 32 setVisible(false);
36 33 }
37 34
38 35 void BarValue::setText(QString str)
39 36 {
40 m_valueString = str;
37 m_textItem->setText(str);
41 38 }
42 39
43 40 QString BarValue::text() const
44 41 {
45 return m_valueString;
42 return m_textItem->text();
46 43 }
47 44
48 45 void BarValue::setPen(const QPen &pen)
49 46 {
50 m_pen = pen;
47 m_textItem->setPen(pen);
51 48 }
52 49
53 50 QPen BarValue::pen() const
54 51 {
55 return m_pen;
52 return m_textItem->pen();
56 53 }
57 54
58 void BarValue::resize(qreal w, qreal h)
55 void BarValue::setFont(const QFont &font)
59 56 {
60 m_width = w;
61 m_height = h;
57 m_textItem->setFont(font);
58 }
59
60 QFont BarValue::font() const
61 {
62 return m_textItem->font();
62 63 }
63 64
64 65 void BarValue::setPos(qreal x, qreal y)
65 66 {
66 m_xPos = x;
67 m_yPos = y;
67 m_textItem->setPos(x,y);
68 68 }
69 69
70 70 void BarValue::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
71 71 {
72 72 Q_UNUSED(option)
73 73 Q_UNUSED(widget)
74
75 if (isVisible()) {
76 painter->setPen(m_pen);
77 painter->drawText(boundingRect(), m_valueString);
78 }
79 74 }
80 75
81 76 QRectF BarValue::boundingRect() const
82 77 {
83 QRectF r(m_xPos, m_yPos, m_width, m_height);
84 return r;
78 return m_textItem->boundingRect();
85 79 }
86 80
87 81 void BarValue::toggleVisible()
88 82 {
83 qDebug() << "toggle visible";
89 84 setVisible(!isVisible());
90 85 }
91 86
92 87 #include "moc_barvalue_p.cpp"
93 88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,70 +1,67
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef BARVALUE_P_H
22 22 #define BARVALUE_P_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include <QGraphicsObject>
26 26 #include <QPen>
27 class QGraphicsSimpleTextItem;
27 28
28 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 30
30 31 class QBarSet;
31 32
32 33 // Visual class for floating bar values
33 34 // By default these are not visible.
34 35 class BarValue : public QGraphicsObject
35 36 {
36 37 Q_OBJECT
37 38 public:
38 39 BarValue(QBarSet &set, QGraphicsItem *parent = 0);
39 40
40 41 void setText(QString str);
41 42 QString text() const;
42 43
43 44 void setPen(const QPen &pen);
44 45 QPen pen() const;
45 46
46 void resize(qreal w, qreal h);
47 void setFont(const QFont &font);
48 QFont font() const;
49
47 50 void setPos(qreal x, qreal y);
48 51
49 52 // From QGraphicsItem
50 53 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
51 54 QRectF boundingRect() const;
52 55
53 56 public Q_SLOTS:
54 57 void toggleVisible();
55 58
56 59 private:
57 60
58 61 QBarSet &m_barSet;
59 QPen m_pen;
60 QString m_valueString;
61
62 qreal m_xPos;
63 qreal m_yPos;
64 qreal m_width;
65 qreal m_height;
62 QGraphicsSimpleTextItem *m_textItem;
66 63 };
67 64
68 65 QTCOMMERCIALCHART_END_NAMESPACE
69 66
70 67 #endif // BARVALUE_P_H
@@ -1,101 +1,101
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "percentbarchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "barvalue_p.h"
24 24 #include "qbarset.h"
25 25 #include <QDebug>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 30 BarChartItem(series, presenter)
31 31 {
32 32 }
33 33
34 34 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 35 {
36 36 QVector<QRectF> layout;
37 37
38 38 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
39 39 qreal width = geometry().width();
40 40 qreal height = geometry().height();
41 41
42 42 qreal categoryCount = m_series->categoryCount();
43 43 qreal barWidth = width / (m_series->categoryCount() * 2);
44 44 qreal xStep = width / categoryCount;
45 45 qreal xPos = xStep / 2 - barWidth / 2;
46 46
47 47 int itemIndex(0);
48 48 for (int category = 0; category < categoryCount; category++) {
49 49 qreal colSum = m_series->categorySum(category);
50 50 qreal scale = (height / colSum);
51 51 qreal yPos = height;
52 52 for (int set=0; set < m_series->barsetCount(); set++) {
53 53 qreal barHeight = m_series->valueAt(set, category) * scale;
54 54 Bar* bar = m_bars.at(itemIndex);
55 55 bar->setPen(m_series->barsetAt(set)->pen());
56 56 bar->setBrush(m_series->barsetAt(set)->brush());
57 57 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
58 58 layout.append(rect);
59 59 itemIndex++;
60 60 yPos -= barHeight;
61 61 }
62 62 xPos += xStep;
63 63 }
64 64
65 65 // Position floating values
66 66 itemIndex = 0;
67 67 xPos = (width/categoryCount);
68 68 for (int category=0; category < m_series->categoryCount(); category++) {
69 69 qreal yPos = height;
70 70 qreal colSum = m_series->categorySum(category);
71 71 qreal scale = (height / colSum);
72 72 for (int set=0; set < m_series->barsetCount(); set++) {
73 73 qreal barHeight = m_series->valueAt(set,category) * scale;
74 74 BarValue* value = m_floatingValues.at(itemIndex);
75 75
76 76 QBarSet* barSet = m_series->barsetAt(set);
77 value->resize(100, 50); // TODO: proper layout for this.
78 value->setPos(xPos, yPos-barHeight / 2);
79 value->setPen(barSet->floatingValuePen());
80 77
81 78 if (!qFuzzyIsNull(m_series->valueAt(set,category))) {
82 79 int p = m_series->percentageAt(set,category) * 100;
83 80 QString vString(QString::number(p));
84 81 vString.truncate(3);
85 82 vString.append("%");
86 83 value->setText(vString);
87 84 } else {
88 85 value->setText(QString(""));
89 86 }
90 87
88 value->setPos(xPos, yPos-barHeight / 2);
89 value->setPen(barSet->floatingValuePen());
90
91 91 itemIndex++;
92 92 yPos -= barHeight;
93 93 }
94 94 xPos += xStep;
95 95 }
96 96 return layout;
97 97 }
98 98
99 99 #include "moc_percentbarchartitem_p.cpp"
100 100
101 101 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,103 +1,103
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "stackedbarchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "barvalue_p.h"
24 24 #include "qbarset.h"
25 25 #include <QDebug>
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 30 BarChartItem(series, presenter)
31 31 {
32 32 }
33 33
34 34 StackedBarChartItem::~StackedBarChartItem()
35 35 {
36 36 }
37 37
38 38 QVector<QRectF> StackedBarChartItem::calculateLayout()
39 39 {
40 40 QVector<QRectF> layout;
41 41 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
42 42
43 43 qreal maxSum = m_series->maxCategorySum();
44 44 // Domain:
45 45 if (m_domainMaxY > maxSum) {
46 46 maxSum = m_domainMaxY;
47 47 }
48 48
49 49 qreal height = geometry().height();
50 50 qreal width = geometry().width();
51 51 qreal scale = (height / m_series->maxCategorySum());
52 52 qreal categotyCount = m_series->categoryCount();
53 53 qreal barWidth = width / (categotyCount * 2);
54 54 qreal xStep = width / categotyCount;
55 55 qreal xPos = xStep / 2 - barWidth / 2;
56 56
57 57 int itemIndex(0);
58 58 for (int category = 0; category < categotyCount; category++) {
59 59 qreal yPos = height;
60 60 for (int set=0; set < m_series->barsetCount(); set++) {
61 61 qreal barHeight = m_series->valueAt(set, category) * scale;
62 62 Bar* bar = m_bars.at(itemIndex);
63 63 bar->setPen(m_series->barsetAt(set)->pen());
64 64 bar->setBrush(m_series->barsetAt(set)->brush());
65 65 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
66 66 layout.append(rect);
67 67 itemIndex++;
68 68 yPos -= barHeight;
69 69 }
70 70 xPos += xStep;
71 71 }
72 72
73 73 // Position floating values
74 74 itemIndex = 0;
75 75 xPos = (width/categotyCount);
76 76 for (int category=0; category < m_series->categoryCount(); category++) {
77 77 qreal yPos = height;
78 78 for (int set=0; set < m_series->barsetCount(); set++) {
79 79 qreal barHeight = m_series->valueAt(set, category) * scale;
80 80 BarValue* value = m_floatingValues.at(itemIndex);
81 81
82 82 QBarSet* barSet = m_series->barsetAt(set);
83 value->resize(100, 50); // TODO: proper layout for this.
84 value->setPos(xPos, yPos-barHeight / 2);
85 value->setPen(barSet->floatingValuePen());
86 83
87 84 if (!qFuzzyIsNull(m_series->valueAt(set, category))) {
88 85 value->setText(QString::number(m_series->valueAt(set,category)));
89 86 } else {
90 87 value->setText(QString(""));
91 88 }
92 89
90 value->setPos(xPos, yPos-barHeight / 2);
91 value->setPen(barSet->floatingValuePen());
92
93 93 itemIndex++;
94 94 yPos -= barHeight;
95 95 }
96 96 xPos += xStep;
97 97 }
98 98 return layout;
99 99 }
100 100
101 101 #include "moc_stackedbarchartitem_p.cpp"
102 102
103 103 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now