##// END OF EJS Templates
barseries bar scaling change
sauimone -
r1326:dacdfd6d84ab
parent child
Show More
@@ -1,215 +1,214
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->d_func()->categoryCount(); c++) {
76 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
77 for (int s = 0; s < m_series->barsetCount(); s++) {
77 for (int s = 0; s < m_series->barsetCount(); s++) {
78 QBarSet *set = m_series->d_func()->barsetAt(s);
78 QBarSet *set = m_series->d_func()->barsetAt(s);
79
79
80 // Bars
80 // Bars
81 Bar *bar = new Bar(set,c,this);
81 Bar *bar = new Bar(set,c,this);
82 m_bars.append(bar);
82 m_bars.append(bar);
83 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
83 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
84 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
84 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
85 m_layout.append(QRectF(0, 0, 0, 0));
85 m_layout.append(QRectF(0, 0, 0, 0));
86
86
87 // Labels
87 // Labels
88 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
88 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
89 label->setVisible(labelsVisible);
89 label->setVisible(labelsVisible);
90 m_labels.append(label);
90 m_labels.append(label);
91 }
91 }
92 }
92 }
93
93
94 // TODO: Is this the right place to call it?
94 // TODO: Is this the right place to call it?
95 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
95 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
96 }
96 }
97
97
98 QVector<QRectF> BarChartItem::calculateLayout()
98 QVector<QRectF> BarChartItem::calculateLayout()
99 {
99 {
100 QVector<QRectF> layout;
100 QVector<QRectF> layout;
101
101
102 // Use temporary qreals for accuracy
102 // Use temporary qreals for accuracy
103 qreal categoryCount = m_series->d_func()->categoryCount();
103 qreal categoryCount = m_series->d_func()->categoryCount();
104 qreal setCount = m_series->barsetCount();
104 qreal setCount = m_series->barsetCount();
105 bool barsVisible = m_series->isVisible();
105 bool barsVisible = m_series->isVisible();
106
106
107 // Domain:
107 // Domain:
108 qreal width = geometry().width();
108 qreal width = geometry().width();
109 qreal height = geometry().height();
109 qreal height = geometry().height();
110 qreal rangeY = m_domainMaxY - m_domainMinY;
110 qreal rangeY = m_domainMaxY - m_domainMinY;
111 qreal rangeX = m_domainMaxX - m_domainMinX;
111 qreal rangeX = m_domainMaxX - m_domainMinX;
112 qreal scaleY = (height / rangeY);
112 qreal scaleY = (height / rangeY);
113 qreal scaleX = (width / rangeX);
113 qreal scaleX = (width / rangeX);
114 qreal categoryWidth = width / categoryCount;
114 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
115 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
116
115
117 int itemIndex(0);
116 int itemIndex(0);
118 for (int category = 0; category < categoryCount; category++) {
117 for (int category = 0; category < categoryCount; category++) {
119 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
118 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
120 for (int set = 0; set < setCount; set++) {
119 for (int set = 0; set < setCount; set++) {
121 QBarSet* barSet = m_series->d_func()->barsetAt(set);
120 QBarSet* barSet = m_series->d_func()->barsetAt(set);
122 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
121 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
123 qreal barHeight = barSet->at(category).y() * scaleY;
122 qreal barHeight = barSet->at(category).y() * scaleY;
124
123
125 Bar* bar = m_bars.at(itemIndex);
124 Bar* bar = m_bars.at(itemIndex);
126 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
125 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
127
126
128 layout.append(rect);
127 layout.append(rect);
129 bar->setPen(barSet->pen());
128 bar->setPen(barSet->pen());
130 bar->setBrush(barSet->brush());
129 bar->setBrush(barSet->brush());
131 bar->setVisible(barsVisible);
130 bar->setVisible(barsVisible);
132
131
133 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
132 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
134
133
135 if (!qFuzzyIsNull(barSet->at(category).y())) {
134 if (!qFuzzyIsNull(barSet->at(category).y())) {
136 label->setText(QString::number(barSet->at(category).y()));
135 label->setText(QString::number(barSet->at(category).y()));
137 } else {
136 } else {
138 label->setText(QString(""));
137 label->setText(QString(""));
139 }
138 }
140
139
141 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
140 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 ,yPos - barHeight/2 - label->boundingRect().height()/2);
141 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 label->setFont(barSet->labelFont());
142 label->setFont(barSet->labelFont());
144 label->setBrush(barSet->labelBrush());
143 label->setBrush(barSet->labelBrush());
145
144
146 itemIndex++;
145 itemIndex++;
147 }
146 }
148 }
147 }
149
148
150 return layout;
149 return layout;
151 }
150 }
152
151
153 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
152 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
154 {
153 {
155 if (animator())
154 if (animator())
156 animator()->updateLayout(this, m_layout, layout);
155 animator()->updateLayout(this, m_layout, layout);
157 else
156 else
158 setLayout(layout);
157 setLayout(layout);
159 }
158 }
160
159
161 void BarChartItem::setLayout(const QVector<QRectF> &layout)
160 void BarChartItem::setLayout(const QVector<QRectF> &layout)
162 {
161 {
163 m_layout = layout;
162 m_layout = layout;
164
163
165 for (int i=0; i < m_bars.count(); i++)
164 for (int i=0; i < m_bars.count(); i++)
166 m_bars.at(i)->setRect(layout.at(i));
165 m_bars.at(i)->setRect(layout.at(i));
167
166
168 update();
167 update();
169 }
168 }
170 //handlers
169 //handlers
171
170
172 void BarChartItem::handleModelChanged()
171 void BarChartItem::handleModelChanged()
173 {
172 {
174 // dataChanged();
173 // dataChanged();
175 presenter()->resetAllElements();
174 presenter()->resetAllElements();
176 }
175 }
177
176
178 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
177 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
179 {
178 {
180 m_domainMinX = minX;
179 m_domainMinX = minX;
181 m_domainMaxX = maxX;
180 m_domainMaxX = maxX;
182 m_domainMinY = minY;
181 m_domainMinY = minY;
183 m_domainMaxY = maxY;
182 m_domainMaxY = maxY;
184 handleLayoutChanged();
183 handleLayoutChanged();
185 }
184 }
186
185
187 void BarChartItem::handleGeometryChanged(const QRectF &rect)
186 void BarChartItem::handleGeometryChanged(const QRectF &rect)
188 {
187 {
189 prepareGeometryChange();
188 prepareGeometryChange();
190 m_rect = rect;
189 m_rect = rect;
191 handleLayoutChanged();
190 handleLayoutChanged();
192 }
191 }
193
192
194 void BarChartItem::handleLayoutChanged()
193 void BarChartItem::handleLayoutChanged()
195 {
194 {
196 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
195 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
197 // rect size zero.
196 // rect size zero.
198 return;
197 return;
199 }
198 }
200 QVector<QRectF> layout = calculateLayout();
199 QVector<QRectF> layout = calculateLayout();
201 applyLayout(layout);
200 applyLayout(layout);
202 update();
201 update();
203 }
202 }
204
203
205 void BarChartItem::labelsVisibleChanged(bool visible)
204 void BarChartItem::labelsVisibleChanged(bool visible)
206 {
205 {
207 foreach (QGraphicsSimpleTextItem* label, m_labels) {
206 foreach (QGraphicsSimpleTextItem* label, m_labels) {
208 label->setVisible(visible);
207 label->setVisible(visible);
209 }
208 }
210 update();
209 update();
211 }
210 }
212
211
213 #include "moc_barchartitem_p.cpp"
212 #include "moc_barchartitem_p.cpp"
214
213
215 QTCOMMERCIALCHART_END_NAMESPACE
214 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,92 +1,91
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 accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->barsetCount();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal categoryWidth = width / categoryCount;
50 qreal barWidth = scaleX / setCount - (scaleX / setCount) * m_series->d_func()->barMargin();
51 qreal barWidth = categoryWidth / setCount - (categoryWidth / setCount) * m_series->d_func()->barMargin();
52
51
53 int itemIndex(0);
52 int itemIndex(0);
54 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
55 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
56 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
57 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
58
57
59 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();
60 xPos -= setCount*barWidth/2;
59 xPos -= setCount*barWidth/2;
61 xPos += set*barWidth;
60 xPos += set*barWidth;
62 qreal barHeight = barSet->at(category).y() * scaleY;
61 qreal barHeight = barSet->at(category).y() * scaleY;
63 Bar* bar = m_bars.at(itemIndex);
62 Bar* bar = m_bars.at(itemIndex);
64
63
65 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
66 layout.append(rect);
65 layout.append(rect);
67 bar->setPen(barSet->pen());
66 bar->setPen(barSet->pen());
68 bar->setBrush(barSet->brush());
67 bar->setBrush(barSet->brush());
69 bar->setVisible(barsVisible);
68 bar->setVisible(barsVisible);
70
69
71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
72
71
73 if (!qFuzzyIsNull(barSet->at(category).y())) {
72 if (!qFuzzyIsNull(barSet->at(category).y())) {
74 label->setText(QString::number(barSet->at(category).y()));
73 label->setText(QString::number(barSet->at(category).y()));
75 } else {
74 } else {
76 label->setText(QString(""));
75 label->setText(QString(""));
77 }
76 }
78
77
79 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
78 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
80 ,yPos - barHeight/2 - label->boundingRect().height()/2);
79 ,yPos - barHeight/2 - label->boundingRect().height()/2);
81 label->setFont(barSet->labelFont());
80 label->setFont(barSet->labelFont());
82 label->setBrush(barSet->labelBrush());
81 label->setBrush(barSet->labelBrush());
83
82
84 itemIndex++;
83 itemIndex++;
85 }
84 }
86 }
85 }
87 return layout;
86 return layout;
88 }
87 }
89
88
90 #include "moc_groupedbarchartitem_p.cpp"
89 #include "moc_groupedbarchartitem_p.cpp"
91
90
92 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,98 +1,97
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 accuracy
38 // Use temporary qreals for accuracy
39 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal categoryCount = m_series->d_func()->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->barsetCount();
41 bool barsVisible = m_series->isVisible();
41 bool barsVisible = m_series->isVisible();
42
42
43 // Domain:
43 // Domain:
44 qreal width = geometry().width();
44 qreal width = geometry().width();
45 qreal height = geometry().height();
45 qreal height = geometry().height();
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
48 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
49 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
50 qreal categoryWidth = width / categoryCount;
50 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
51 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
52
51
53 int itemIndex(0);
52 int itemIndex(0);
54 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
55 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal colSum = m_series->d_func()->categorySum(category);
56 qreal percentage = (100 / colSum);
55 qreal percentage = (100 / colSum);
57 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
58 for (int set=0; set < setCount; set++) {
57 for (int set=0; set < setCount; set++) {
59 QBarSet* barSet = m_series->d_func()->barsetAt(set);
58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
60
59
61 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;
62
61
63 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
64 Bar* bar = m_bars.at(itemIndex);
63 Bar* bar = m_bars.at(itemIndex);
65 bar->setPen(barSet->pen());
64 bar->setPen(barSet->pen());
66 bar->setBrush(barSet->brush());
65 bar->setBrush(barSet->brush());
67 bar->setVisible(barsVisible);
66 bar->setVisible(barsVisible);
68
67
69 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
68 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
70 layout.append(rect);
69 layout.append(rect);
71
70
72 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
73
72
74 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
73 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
75 int p = m_series->d_func()->percentageAt(set,category) * 100;
74 int p = m_series->d_func()->percentageAt(set,category) * 100;
76 QString vString(QString::number(p));
75 QString vString(QString::number(p));
77 vString.truncate(3);
76 vString.truncate(3);
78 vString.append("%");
77 vString.append("%");
79 label->setText(vString);
78 label->setText(vString);
80 } else {
79 } else {
81 label->setText(QString(""));
80 label->setText(QString(""));
82 }
81 }
83
82
84 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
83 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
85 ,yPos - barHeight/2 - label->boundingRect().height()/2);
84 ,yPos - barHeight/2 - label->boundingRect().height()/2);
86 label->setFont(barSet->labelFont());
85 label->setFont(barSet->labelFont());
87 label->setBrush(barSet->labelBrush());
86 label->setBrush(barSet->labelBrush());
88
87
89 itemIndex++;
88 itemIndex++;
90 yPos -= barHeight;
89 yPos -= barHeight;
91 }
90 }
92 }
91 }
93 return layout;
92 return layout;
94 }
93 }
95
94
96 #include "moc_percentbarchartitem_p.cpp"
95 #include "moc_percentbarchartitem_p.cpp"
97
96
98 QTCOMMERCIALCHART_END_NAMESPACE
97 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,91 +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 "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 accuracy
37 // Use temporary qreals for accuracy
38 qreal categoryCount = m_series->d_func()->categoryCount();
38 qreal categoryCount = m_series->d_func()->categoryCount();
39 qreal setCount = m_series->barsetCount();
39 qreal setCount = m_series->barsetCount();
40 bool barsVisible = m_series->isVisible();
40 bool barsVisible = m_series->isVisible();
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 barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
50 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
51
50
52 int itemIndex(0);
51 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
52 for (int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
53 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
55 for (int set=0; set < setCount; set++) {
54 for (int set=0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
55 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
56
58 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;
59
58
60 qreal barHeight = barSet->at(category).y() * scaleY;
59 qreal barHeight = barSet->at(category).y() * scaleY;
61 Bar* bar = m_bars.at(itemIndex);
60 Bar* bar = m_bars.at(itemIndex);
62 bar->setPen(barSet->pen());
61 bar->setPen(barSet->pen());
63 bar->setBrush(barSet->brush());
62 bar->setBrush(barSet->brush());
64 bar->setVisible(barsVisible);
63 bar->setVisible(barsVisible);
65
64
66 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
65 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
67 layout.append(rect);
66 layout.append(rect);
68
67
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
68 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70
69
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
70 if (!qFuzzyIsNull(barSet->at(category).y())) {
72 label->setText(QString::number(barSet->at(category).y()));
71 label->setText(QString::number(barSet->at(category).y()));
73 } else {
72 } else {
74 label->setText(QString(""));
73 label->setText(QString(""));
75 }
74 }
76
75
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
76 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
77 ,yPos - barHeight/2 - label->boundingRect().height()/2);
79 label->setFont(barSet->labelFont());
78 label->setFont(barSet->labelFont());
80 label->setBrush(barSet->labelBrush());
79 label->setBrush(barSet->labelBrush());
81 itemIndex++;
80 itemIndex++;
82 yPos -= barHeight;
81 yPos -= barHeight;
83 }
82 }
84 }
83 }
85
84
86 return layout;
85 return layout;
87 }
86 }
88
87
89 #include "moc_stackedbarchartitem_p.cpp"
88 #include "moc_stackedbarchartitem_p.cpp"
90
89
91 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now