##// END OF EJS Templates
Fixed a bug with QBarSet::setLabelBrush
Tero Ahola -
r1306:f8d084165244
parent child
Show More
@@ -1,215 +1,216
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "barchartitem_p.h"
21 #include "barchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qbarseries_p.h"
26 #include "qbarseries_p.h"
27 #include "qchart.h"
27 #include "qchart.h"
28 #include "chartpresenter_p.h"
28 #include "chartpresenter_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30 #include "chartdataset_p.h"
30 #include "chartdataset_p.h"
31 #include <QPainter>
31 #include <QPainter>
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
36 ChartItem(presenter),
36 ChartItem(presenter),
37 m_series(series)
37 m_series(series)
38 {
38 {
39 setFlag(ItemClipsChildrenToShape);
39 setFlag(ItemClipsChildrenToShape);
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
41 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
41 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
42 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool)));
42 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool)));
43 setZValue(ChartPresenter::BarSeriesZValue);
43 setZValue(ChartPresenter::BarSeriesZValue);
44 dataChanged();
44 dataChanged();
45 }
45 }
46
46
47 BarChartItem::~BarChartItem()
47 BarChartItem::~BarChartItem()
48 {
48 {
49 }
49 }
50
50
51 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
51 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 {
52 {
53 Q_UNUSED(painter);
53 Q_UNUSED(painter);
54 Q_UNUSED(option);
54 Q_UNUSED(option);
55 Q_UNUSED(widget);
55 Q_UNUSED(widget);
56 }
56 }
57
57
58 QRectF BarChartItem::boundingRect() const
58 QRectF BarChartItem::boundingRect() const
59 {
59 {
60 return m_rect;
60 return m_rect;
61 }
61 }
62
62
63 void BarChartItem::dataChanged()
63 void BarChartItem::dataChanged()
64 {
64 {
65 foreach(QGraphicsItem *item, childItems()) {
65 foreach(QGraphicsItem *item, childItems()) {
66 delete item;
66 delete item;
67 }
67 }
68
68
69 m_bars.clear();
69 m_bars.clear();
70 m_labels.clear();
70 m_labels.clear();
71 m_layout.clear();
71 m_layout.clear();
72
72
73 bool labelsVisible = m_series->isLabelsVisible();
73 bool labelsVisible = m_series->isLabelsVisible();
74
74
75 // Create new graphic items for bars
75 // Create new graphic items for bars
76 for (int c = 0; c < m_series->categoryCount(); c++) {
76 for (int c = 0; c < m_series->categoryCount(); c++) {
77 QString category = m_series->d_func()->categoryName(c);
77 QString category = m_series->d_func()->categoryName(c);
78 for (int s = 0; s < m_series->barsetCount(); s++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
79 QBarSet *set = m_series->d_func()->barsetAt(s);
79 QBarSet *set = m_series->d_func()->barsetAt(s);
80
80
81 // Bars
81 // Bars
82 Bar *bar = new Bar(set,category,this);
82 Bar *bar = new Bar(set,category,this);
83 m_bars.append(bar);
83 m_bars.append(bar);
84 connect(bar, SIGNAL(clicked(QBarSet*,QString)), m_series, SIGNAL(clicked(QBarSet*,QString)));
84 connect(bar, SIGNAL(clicked(QBarSet*,QString)), m_series, SIGNAL(clicked(QBarSet*,QString)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
86 m_layout.append(QRectF(0, 0, 0, 0));
86 m_layout.append(QRectF(0, 0, 0, 0));
87
87
88 // Labels
88 // Labels
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
90 label->setVisible(labelsVisible);
90 label->setVisible(labelsVisible);
91 m_labels.append(label);
91 m_labels.append(label);
92 }
92 }
93 }
93 }
94
94
95 // TODO: Is this the right place to call it?
95 // TODO: Is this the right place to call it?
96 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
96 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
97 }
97 }
98
98
99 QVector<QRectF> BarChartItem::calculateLayout()
99 QVector<QRectF> BarChartItem::calculateLayout()
100 {
100 {
101 QVector<QRectF> layout;
101 QVector<QRectF> layout;
102
102
103 // Use temporary qreals for accurancy
103 // Use temporary qreals for accurancy
104 qreal categoryCount = m_series->categoryCount();
104 qreal categoryCount = m_series->categoryCount();
105 qreal setCount = m_series->barsetCount();
105 qreal setCount = m_series->barsetCount();
106 bool barsVisible = m_series->isVisible();
106 bool barsVisible = m_series->isVisible();
107
107
108 // Domain:
108 // Domain:
109 qreal width = geometry().width();
109 qreal width = geometry().width();
110 qreal height = geometry().height();
110 qreal height = geometry().height();
111 qreal rangeY = m_domainMaxY - m_domainMinY;
111 qreal rangeY = m_domainMaxY - m_domainMinY;
112 qreal rangeX = m_domainMaxX - m_domainMinX;
112 qreal rangeX = m_domainMaxX - m_domainMinX;
113 qreal scaleY = (height / rangeY);
113 qreal scaleY = (height / rangeY);
114 qreal scaleX = (width / rangeX);
114 qreal scaleX = (width / rangeX);
115 qreal categoryWidth = width / categoryCount;
115 qreal categoryWidth = width / categoryCount;
116 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
116 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
117
117
118 int itemIndex(0);
118 int itemIndex(0);
119 for (int category = 0; category < categoryCount; category++) {
119 for (int category = 0; category < categoryCount; category++) {
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
121 for (int set = 0; set < setCount; set++) {
121 for (int set = 0; set < setCount; set++) {
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
124 qreal barHeight = barSet->at(category).y() * scaleY;
124 qreal barHeight = barSet->at(category).y() * scaleY;
125
125
126 Bar* bar = m_bars.at(itemIndex);
126 Bar* bar = m_bars.at(itemIndex);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
128
128
129 layout.append(rect);
129 layout.append(rect);
130 bar->setPen(barSet->pen());
130 bar->setPen(barSet->pen());
131 bar->setBrush(barSet->brush());
131 bar->setBrush(barSet->brush());
132 bar->setVisible(barsVisible);
132 bar->setVisible(barsVisible);
133
133
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
135
135
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
137 label->setText(QString::number(barSet->at(category).y()));
137 label->setText(QString::number(barSet->at(category).y()));
138 } else {
138 } else {
139 label->setText(QString(""));
139 label->setText(QString(""));
140 }
140 }
141
141
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
144 label->setFont(barSet->labelFont());
144 label->setFont(barSet->labelFont());
145 label->setBrush(barSet->labelBrush());
145
146
146 itemIndex++;
147 itemIndex++;
147 }
148 }
148 }
149 }
149
150
150 return layout;
151 return layout;
151 }
152 }
152
153
153 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
154 {
155 {
155 if (animator())
156 if (animator())
156 animator()->updateLayout(this, m_layout, layout);
157 animator()->updateLayout(this, m_layout, layout);
157 else
158 else
158 setLayout(layout);
159 setLayout(layout);
159 }
160 }
160
161
161 void BarChartItem::setLayout(const QVector<QRectF> &layout)
162 void BarChartItem::setLayout(const QVector<QRectF> &layout)
162 {
163 {
163 m_layout = layout;
164 m_layout = layout;
164
165
165 for (int i=0; i < m_bars.count(); i++)
166 for (int i=0; i < m_bars.count(); i++)
166 m_bars.at(i)->setRect(layout.at(i));
167 m_bars.at(i)->setRect(layout.at(i));
167
168
168 update();
169 update();
169 }
170 }
170 //handlers
171 //handlers
171
172
172 void BarChartItem::handleModelChanged()
173 void BarChartItem::handleModelChanged()
173 {
174 {
174 // dataChanged();
175 // dataChanged();
175 presenter()->resetAllElements();
176 presenter()->resetAllElements();
176 }
177 }
177
178
178 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
179 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
179 {
180 {
180 m_domainMinX = minX;
181 m_domainMinX = minX;
181 m_domainMaxX = maxX;
182 m_domainMaxX = maxX;
182 m_domainMinY = minY;
183 m_domainMinY = minY;
183 m_domainMaxY = maxY;
184 m_domainMaxY = maxY;
184 handleLayoutChanged();
185 handleLayoutChanged();
185 }
186 }
186
187
187 void BarChartItem::handleGeometryChanged(const QRectF &rect)
188 void BarChartItem::handleGeometryChanged(const QRectF &rect)
188 {
189 {
189 prepareGeometryChange();
190 prepareGeometryChange();
190 m_rect = rect;
191 m_rect = rect;
191 handleLayoutChanged();
192 handleLayoutChanged();
192 }
193 }
193
194
194 void BarChartItem::handleLayoutChanged()
195 void BarChartItem::handleLayoutChanged()
195 {
196 {
196 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
197 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
197 // rect size zero.
198 // rect size zero.
198 return;
199 return;
199 }
200 }
200 QVector<QRectF> layout = calculateLayout();
201 QVector<QRectF> layout = calculateLayout();
201 applyLayout(layout);
202 applyLayout(layout);
202 update();
203 update();
203 }
204 }
204
205
205 void BarChartItem::labelsVisibleChanged(bool visible)
206 void BarChartItem::labelsVisibleChanged(bool visible)
206 {
207 {
207 foreach (QGraphicsSimpleTextItem* label, m_labels) {
208 foreach (QGraphicsSimpleTextItem* label, m_labels) {
208 label->setVisible(visible);
209 label->setVisible(visible);
209 }
210 }
210 update();
211 update();
211 }
212 }
212
213
213 #include "moc_barchartitem_p.cpp"
214 #include "moc_barchartitem_p.cpp"
214
215
215 QTCOMMERCIALCHART_END_NAMESPACE
216 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,89 +1,90
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "groupedbarchartitem_p.h"
21 #include "groupedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> GroupedBarChartItem::calculateLayout()
34 QVector<QRectF> GroupedBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accurancy
38 // Use temporary qreals for accurancy
39 qreal categoryCount = m_series->categoryCount();
39 qreal categoryCount = m_series->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->barsetCount();
41
41
42 // Domain:
42 // Domain:
43 qreal width = geometry().width();
43 qreal width = geometry().width();
44 qreal height = geometry().height();
44 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
48 qreal scaleX = (width / rangeX);
49 qreal categoryWidth = width / categoryCount;
49 qreal categoryWidth = width / categoryCount;
50 qreal barWidth = categoryWidth / setCount - (categoryWidth / setCount) * m_series->d_func()->barMargin();
50 qreal barWidth = categoryWidth / setCount - (categoryWidth / setCount) * m_series->d_func()->barMargin();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
55 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
57
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
59 xPos -= setCount*barWidth/2;
59 xPos -= setCount*barWidth/2;
60 xPos += set*barWidth;
60 xPos += set*barWidth;
61 qreal barHeight = barSet->at(category).y() * scaleY;
61 qreal barHeight = barSet->at(category).y() * scaleY;
62 Bar* bar = m_bars.at(itemIndex);
62 Bar* bar = m_bars.at(itemIndex);
63
63
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
65 layout.append(rect);
65 layout.append(rect);
66 bar->setPen(barSet->pen());
66 bar->setPen(barSet->pen());
67 bar->setBrush(barSet->brush());
67 bar->setBrush(barSet->brush());
68
68
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70
70
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
72 label->setText(QString::number(barSet->at(category).y()));
72 label->setText(QString::number(barSet->at(category).y()));
73 } else {
73 } else {
74 label->setText(QString(""));
74 label->setText(QString(""));
75 }
75 }
76
76
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
79 label->setFont(barSet->labelFont());
79 label->setFont(barSet->labelFont());
80 label->setBrush(barSet->labelBrush());
80
81
81 itemIndex++;
82 itemIndex++;
82 }
83 }
83 }
84 }
84 return layout;
85 return layout;
85 }
86 }
86
87
87 #include "moc_groupedbarchartitem_p.cpp"
88 #include "moc_groupedbarchartitem_p.cpp"
88
89
89 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,93 +1,94
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "percentbarchartitem_p.h"
21 #include "percentbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarseries_p.h"
23 #include "qbarseries_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include <QDebug>
25 #include <QDebug>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
34 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accurancy
38 // Use temporary qreals for accurancy
39 qreal categoryCount = m_series->categoryCount();
39 qreal categoryCount = m_series->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->barsetCount();
41
41
42 // Domain:
42 // Domain:
43 qreal width = geometry().width();
43 qreal width = geometry().width();
44 qreal height = geometry().height();
44 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
48 qreal scaleX = (width / rangeX);
49 qreal categoryWidth = width / categoryCount;
49 qreal categoryWidth = width / categoryCount;
50 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
50 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal percentage = (100 / colSum);
55 qreal percentage = (100 / colSum);
56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
57 for (int set=0; set < setCount; set++) {
57 for (int set=0; set < setCount; set++) {
58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
59
59
60 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
60 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
61
61
62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
63 Bar* bar = m_bars.at(itemIndex);
63 Bar* bar = m_bars.at(itemIndex);
64 bar->setPen(barSet->pen());
64 bar->setPen(barSet->pen());
65 bar->setBrush(barSet->brush());
65 bar->setBrush(barSet->brush());
66 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
66 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
67 layout.append(rect);
67 layout.append(rect);
68
68
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70
70
71 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
71 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
72 int p = m_series->d_func()->percentageAt(set,category) * 100;
72 int p = m_series->d_func()->percentageAt(set,category) * 100;
73 QString vString(QString::number(p));
73 QString vString(QString::number(p));
74 vString.truncate(3);
74 vString.truncate(3);
75 vString.append("%");
75 vString.append("%");
76 label->setText(vString);
76 label->setText(vString);
77 } else {
77 } else {
78 label->setText(QString(""));
78 label->setText(QString(""));
79 }
79 }
80
80
81 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
81 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
82 ,yPos - barHeight/2 - label->boundingRect().height()/2);
82 ,yPos - barHeight/2 - label->boundingRect().height()/2);
83 label->setFont(barSet->labelFont());
83 label->setFont(barSet->labelFont());
84 label->setBrush(barSet->labelBrush());
84 itemIndex++;
85 itemIndex++;
85 yPos -= barHeight;
86 yPos -= barHeight;
86 }
87 }
87 }
88 }
88 return layout;
89 return layout;
89 }
90 }
90
91
91 #include "moc_percentbarchartitem_p.cpp"
92 #include "moc_percentbarchartitem_p.cpp"
92
93
93 QTCOMMERCIALCHART_END_NAMESPACE
94 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,88
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "stackedbarchartitem_p.h"
21 #include "stackedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> StackedBarChartItem::calculateLayout()
34 QVector<QRectF> StackedBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37 // Use temporary qreals for accurancy
37 // Use temporary qreals for accurancy
38 qreal categoryCount = m_series->categoryCount();
38 qreal categoryCount = m_series->categoryCount();
39 qreal setCount = m_series->barsetCount();
39 qreal setCount = m_series->barsetCount();
40
40
41 // Domain:
41 // Domain:
42 qreal width = geometry().width();
42 qreal width = geometry().width();
43 qreal height = geometry().height();
43 qreal height = geometry().height();
44 qreal rangeY = m_domainMaxY - m_domainMinY;
44 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeX = m_domainMaxX - m_domainMinX;
45 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal scaleY = (height / rangeY);
46 qreal scaleY = (height / rangeY);
47 qreal scaleX = (width / rangeX);
47 qreal scaleX = (width / rangeX);
48 qreal categoryWidth = width / categoryCount;
48 qreal categoryWidth = width / categoryCount;
49 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
49 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
50
50
51 int itemIndex(0);
51 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
52 for (int category = 0; category < categoryCount; category++) {
53 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
53 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
54 for (int set=0; set < setCount; set++) {
54 for (int set=0; set < setCount; set++) {
55 QBarSet* barSet = m_series->d_func()->barsetAt(set);
55 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56
56
57 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
57 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
58
58
59 qreal barHeight = barSet->at(category).y() * scaleY;
59 qreal barHeight = barSet->at(category).y() * scaleY;
60 Bar* bar = m_bars.at(itemIndex);
60 Bar* bar = m_bars.at(itemIndex);
61 bar->setPen(barSet->pen());
61 bar->setPen(barSet->pen());
62 bar->setBrush(barSet->brush());
62 bar->setBrush(barSet->brush());
63 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
63 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
64 layout.append(rect);
64 layout.append(rect);
65
65
66 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
66 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
67
67
68 if (!qFuzzyIsNull(barSet->at(category).y())) {
68 if (!qFuzzyIsNull(barSet->at(category).y())) {
69 label->setText(QString::number(barSet->at(category).y()));
69 label->setText(QString::number(barSet->at(category).y()));
70 } else {
70 } else {
71 label->setText(QString(""));
71 label->setText(QString(""));
72 }
72 }
73
73
74 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
74 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
75 ,yPos - barHeight/2 - label->boundingRect().height()/2);
75 ,yPos - barHeight/2 - label->boundingRect().height()/2);
76 label->setFont(barSet->labelFont());
76 label->setFont(barSet->labelFont());
77 label->setBrush(barSet->labelBrush());
77 itemIndex++;
78 itemIndex++;
78 yPos -= barHeight;
79 yPos -= barHeight;
79 }
80 }
80 }
81 }
81
82
82 return layout;
83 return layout;
83 }
84 }
84
85
85 #include "moc_stackedbarchartitem_p.cpp"
86 #include "moc_stackedbarchartitem_p.cpp"
86
87
87 QTCOMMERCIALCHART_END_NAMESPACE
88 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now