##// END OF EJS Templates
barchart: layout calculation fix
sauimone -
r976:727d6e49349d
parent child
Show More
@@ -1,208 +1,206
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 "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset.h"
24 #include "qbarset.h"
25 #include "qbarset_p.h"
25 #include "qbarset_p.h"
26 #include "qbarseries.h"
26 #include "qbarseries.h"
27 #include "qbarseries_p.h"
27 #include "qbarseries_p.h"
28 #include "qchart.h"
28 #include "qchart.h"
29 #include "qchartaxis.h"
29 #include "qchartaxis.h"
30 #include "qchartaxiscategories.h"
30 #include "qchartaxiscategories.h"
31 #include "chartpresenter_p.h"
31 #include "chartpresenter_p.h"
32 #include "chartanimator_p.h"
32 #include "chartanimator_p.h"
33 #include "chartdataset_p.h"
33 #include "chartdataset_p.h"
34 #include <QPainter>
34 #include <QPainter>
35
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
37
38 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
38 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
39 ChartItem(presenter),
39 ChartItem(presenter),
40 m_layoutSet(false),
40 m_layoutSet(false),
41 m_series(series)
41 m_series(series)
42 {
42 {
43 setFlag(ItemClipsChildrenToShape);
43 setFlag(ItemClipsChildrenToShape);
44 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
44 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
45 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
45 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
46 setZValue(ChartPresenter::BarSeriesZValue);
46 setZValue(ChartPresenter::BarSeriesZValue);
47 dataChanged();
47 dataChanged();
48 }
48 }
49
49
50 BarChartItem::~BarChartItem()
50 BarChartItem::~BarChartItem()
51 {
51 {
52 }
52 }
53
53
54 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
54 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
55 {
55 {
56 Q_UNUSED(painter);
56 Q_UNUSED(painter);
57 Q_UNUSED(option);
57 Q_UNUSED(option);
58 Q_UNUSED(widget);
58 Q_UNUSED(widget);
59 }
59 }
60
60
61 QRectF BarChartItem::boundingRect() const
61 QRectF BarChartItem::boundingRect() const
62 {
62 {
63 return m_rect.translated(-m_rect.topLeft());
63 return m_rect;
64 }
64 }
65
65
66 void BarChartItem::dataChanged()
66 void BarChartItem::dataChanged()
67 {
67 {
68 foreach(QGraphicsItem *item, childItems()) {
68 foreach(QGraphicsItem *item, childItems()) {
69 delete item;
69 delete item;
70 }
70 }
71
71
72 m_bars.clear();
72 m_bars.clear();
73 m_labels.clear();
73 m_labels.clear();
74 m_layout.clear();
74 m_layout.clear();
75
75
76 // Create new graphic items for bars
76 // Create new graphic items for bars
77 for (int c = 0; c < m_series->categoryCount(); c++) {
77 for (int c = 0; c < m_series->categoryCount(); c++) {
78 QString category = m_series->d_func()->categoryName(c);
78 QString category = m_series->d_func()->categoryName(c);
79 for (int s = 0; s < m_series->barsetCount(); s++) {
79 for (int s = 0; s < m_series->barsetCount(); s++) {
80 QBarSet *set = m_series->d_func()->barsetAt(s);
80 QBarSet *set = m_series->d_func()->barsetAt(s);
81 Bar *bar = new Bar(set,category,this);
81 Bar *bar = new Bar(set,category,this);
82 m_bars.append(bar);
82 m_bars.append(bar);
83 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
83 connect(bar, SIGNAL(clicked(QString,Qt::MouseButtons)), set, SIGNAL(clicked(QString,Qt::MouseButtons)));
84 connect(bar, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), m_series, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)));
84 connect(bar, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)), m_series, SIGNAL(clicked(QBarSet*,QString,Qt::MouseButtons)));
85 connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool)));
85 connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool)));
86 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
86 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
87 m_layout.append(QRectF(0, 0, 0, 0));
87 m_layout.append(QRectF(0, 0, 0, 0));
88 }
88 }
89 }
89 }
90
90
91 // Create labels
91 // Create labels
92 for (int category = 0; category < m_series->categoryCount(); category++) {
92 for (int category = 0; category < m_series->categoryCount(); category++) {
93 for (int s = 0; s < m_series->barsetCount(); s++) {
93 for (int s = 0; s < m_series->barsetCount(); s++) {
94 QBarSet *set = m_series->d_func()->barsetAt(s);
94 QBarSet *set = m_series->d_func()->barsetAt(s);
95 BarLabel *value = new BarLabel(*set, this);
95 BarLabel *value = new BarLabel(*set, this);
96 m_labels.append(value);
96 m_labels.append(value);
97 connect(set->d_ptr.data(),SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
97 connect(set->d_ptr.data(),SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool)));
98 }
98 }
99 }
99 }
100 }
100 }
101
101
102 QVector<QRectF> BarChartItem::calculateLayout()
102 QVector<QRectF> BarChartItem::calculateLayout()
103 {
103 {
104 QVector<QRectF> layout;
104 QVector<QRectF> layout;
105
105
106 // Use temporary qreals for accurancy
106 // Use temporary qreals for accurancy
107 qreal categoryCount = m_series->categoryCount();
107 qreal categoryCount = m_series->categoryCount();
108 qreal setCount = m_series->barsetCount();
108 qreal setCount = m_series->barsetCount();
109
109
110 // Domain:
110 // Domain:
111 qreal width = geometry().width();
111 qreal width = geometry().width();
112 qreal height = geometry().height();
112 qreal height = geometry().height();
113 qreal range = m_domainMaxY - m_domainMinY;
113 qreal range = m_domainMaxY - m_domainMinY;
114 qreal scale = (height / range);
114 qreal scale = (height / range);
115 qreal categoryWidth = width / categoryCount;
115 qreal categoryWidth = width / categoryCount;
116 qreal barWidth = categoryWidth / (setCount+1);
116 qreal barWidth = categoryWidth / (setCount+1);
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 xPos = categoryWidth * category + barWidth / 2;
120 qreal xPos = categoryWidth * category + barWidth / 2 + geometry().topLeft().x();
121 qreal yPos = height + scale * m_domainMinY;
121 qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y();
122 for (int set = 0; set < setCount; set++) {
122 for (int set = 0; set < setCount; set++) {
123 QBarSet* barSet = m_series->d_func()->barsetAt(set);
123 QBarSet* barSet = m_series->d_func()->barsetAt(set);
124
124
125 qreal barHeight = barSet->valueAt(category) * scale;
125 qreal barHeight = barSet->valueAt(category) * scale;
126 Bar* bar = m_bars.at(itemIndex);
126 Bar* bar = m_bars.at(itemIndex);
127
127
128 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
128 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
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
132
133 BarLabel* label = m_labels.at(itemIndex);
133 BarLabel* label = m_labels.at(itemIndex);
134
134
135 if (!qFuzzyIsNull(barSet->valueAt(category))) {
135 if (!qFuzzyIsNull(barSet->valueAt(category))) {
136 label->setText(QString::number(barSet->valueAt(category)));
136 label->setText(QString::number(barSet->valueAt(category)));
137 } else {
137 } else {
138 label->setText(QString(""));
138 label->setText(QString(""));
139 }
139 }
140
140
141 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
141 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 ,yPos - barHeight/2 - label->boundingRect().height()/2);
142 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 label->setFont(barSet->labelFont());
143 label->setFont(barSet->labelFont());
144
144
145 itemIndex++;
145 itemIndex++;
146 xPos += barWidth;
146 xPos += barWidth;
147 }
147 }
148 }
148 }
149 return layout;
149 return layout;
150 }
150 }
151
151
152 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
152 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
153 {
153 {
154 if (animator())
154 if (animator())
155 animator()->updateLayout(this, m_layout, layout);
155 animator()->updateLayout(this, m_layout, layout);
156 else
156 else
157 setLayout(layout);
157 setLayout(layout);
158 }
158 }
159
159
160 void BarChartItem::setLayout(const QVector<QRectF> &layout)
160 void BarChartItem::setLayout(const QVector<QRectF> &layout)
161 {
161 {
162 m_layout = layout;
162 m_layout = layout;
163
163
164 for (int i=0; i < m_bars.count(); i++)
164 for (int i=0; i < m_bars.count(); i++)
165 m_bars.at(i)->setRect(layout.at(i));
165 m_bars.at(i)->setRect(layout.at(i));
166
166
167 update();
167 update();
168 }
168 }
169 //handlers
169 //handlers
170
170
171 void BarChartItem::handleModelChanged()
171 void BarChartItem::handleModelChanged()
172 {
172 {
173 dataChanged();
173 dataChanged();
174 }
174 }
175
175
176 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
176 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
177 {
177 {
178 m_domainMinX = minX;
178 m_domainMinX = minX;
179 m_domainMaxX = maxX;
179 m_domainMaxX = maxX;
180 m_domainMinY = minY;
180 m_domainMinY = minY;
181 m_domainMaxY = maxY;
181 m_domainMaxY = maxY;
182 handleLayoutChanged();
182 handleLayoutChanged();
183 }
183 }
184
184
185 void BarChartItem::handleGeometryChanged(const QRectF &rect)
185 void BarChartItem::handleGeometryChanged(const QRectF &rect)
186 {
186 {
187 prepareGeometryChange();
187 prepareGeometryChange();
188 m_clipRect = rect.translated(-rect.topLeft());
189 m_rect = rect;
188 m_rect = rect;
190 handleLayoutChanged();
189 handleLayoutChanged();
191 m_layoutSet = true;
190 m_layoutSet = true;
192 setPos(rect.topLeft());
193 }
191 }
194
192
195 void BarChartItem::handleLayoutChanged()
193 void BarChartItem::handleLayoutChanged()
196 {
194 {
197 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
195 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
198 // rect size zero.
196 // rect size zero.
199 return;
197 return;
200 }
198 }
201 QVector<QRectF> layout = calculateLayout();
199 QVector<QRectF> layout = calculateLayout();
202 applyLayout(layout);
200 applyLayout(layout);
203 update();
201 update();
204 }
202 }
205
203
206 #include "moc_barchartitem_p.cpp"
204 #include "moc_barchartitem_p.cpp"
207
205
208 QTCOMMERCIALCHART_END_NAMESPACE
206 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,87 +1,86
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 #ifndef BARCHARTITEM_H
21 #ifndef BARCHARTITEM_H
22 #define BARCHARTITEM_H
22 #define BARCHARTITEM_H
23
23
24 #include "chartitem_p.h"
24 #include "chartitem_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class Bar;
31 class Bar;
32 class BarLabel;
32 class BarLabel;
33 class QChartAxisCategories;
33 class QChartAxisCategories;
34 class QChart;
34 class QChart;
35
35
36 //typedef QVector<QRectF> BarLayout;
36 //typedef QVector<QRectF> BarLayout;
37
37
38 class BarChartItem : public ChartItem
38 class BarChartItem : public ChartItem
39 {
39 {
40 Q_OBJECT
40 Q_OBJECT
41 public:
41 public:
42 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
42 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
43 virtual ~BarChartItem();
43 virtual ~BarChartItem();
44
44
45 public:
45 public:
46 // From QGraphicsItem
46 // From QGraphicsItem
47 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
47 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
48 QRectF boundingRect() const;
48 QRectF boundingRect() const;
49
49
50 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
50 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
51 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
51 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
52
52
53 virtual QVector<QRectF> calculateLayout();
53 virtual QVector<QRectF> calculateLayout();
54 void applyLayout(const QVector<QRectF> &layout);
54 void applyLayout(const QVector<QRectF> &layout);
55 void setLayout(const QVector<QRectF> &layout);
55 void setLayout(const QVector<QRectF> &layout);
56 void updateLayout(const QVector<QRectF> &layout);
56 void updateLayout(const QVector<QRectF> &layout);
57
57
58 QRectF geometry() const { return m_rect;}
58 QRectF geometry() const { return m_rect;}
59
59
60 public Q_SLOTS:
60 public Q_SLOTS:
61 void handleModelChanged();
61 void handleModelChanged();
62 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
62 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
63 void handleGeometryChanged(const QRectF &size);
63 void handleGeometryChanged(const QRectF &size);
64 void handleLayoutChanged();
64 void handleLayoutChanged();
65
65
66 protected:
66 protected:
67
67
68 // TODO: consider these.
68 // TODO: consider these.
69 qreal m_domainMinX;
69 qreal m_domainMinX;
70 qreal m_domainMaxX;
70 qreal m_domainMaxX;
71 qreal m_domainMinY;
71 qreal m_domainMinY;
72 qreal m_domainMaxY;
72 qreal m_domainMaxY;
73
73
74 QRectF m_rect;
74 QRectF m_rect;
75 QRectF m_clipRect;
76 bool m_layoutSet; // True, if component has been laid out.
75 bool m_layoutSet; // True, if component has been laid out.
77 QVector<QRectF> m_layout;
76 QVector<QRectF> m_layout;
78
77
79 // Not owned.
78 // Not owned.
80 QBarSeries *m_series;
79 QBarSeries *m_series;
81 QList<Bar *> m_bars;
80 QList<Bar *> m_bars;
82 QList<BarLabel *> m_labels;
81 QList<BarLabel *> m_labels;
83 };
82 };
84
83
85 QTCOMMERCIALCHART_END_NAMESPACE
84 QTCOMMERCIALCHART_END_NAMESPACE
86
85
87 #endif // BARCHARTITEM_H
86 #endif // BARCHARTITEM_H
@@ -1,90 +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 "percentbarchartitem_p.h"
21 #include "percentbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26 #include <QDebug>
26 #include <QDebug>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
31 BarChartItem(series, presenter)
31 BarChartItem(series, presenter)
32 {
32 {
33 }
33 }
34
34
35 QVector<QRectF> PercentBarChartItem::calculateLayout()
35 QVector<QRectF> PercentBarChartItem::calculateLayout()
36 {
36 {
37 QVector<QRectF> layout;
37 QVector<QRectF> layout;
38
38
39 // Use temporary qreals for accurancy
39 // Use temporary qreals for accurancy
40 qreal width = geometry().width();
40 qreal width = geometry().width();
41 qreal height = geometry().height();
41 qreal height = geometry().height();
42
42
43 qreal categoryCount = m_series->categoryCount();
43 qreal categoryCount = m_series->categoryCount();
44 qreal barWidth = width / (m_series->categoryCount() * 2);
44 qreal barWidth = width / (m_series->categoryCount() * 2);
45 qreal xStep = width / categoryCount;
45 qreal xStep = width / categoryCount;
46 qreal xPos = xStep / 2 - barWidth / 2;
46 qreal xPos = xStep / 2 - barWidth / 2 + geometry().topLeft().x();
47
47
48 qreal range = m_domainMaxY - m_domainMinY;
48 qreal range = m_domainMaxY - m_domainMinY;
49 qreal domainScale = (height / range);
49 qreal domainScale = (height / range);
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 colSum = m_series->d_func()->categorySum(category);
53 qreal colSum = m_series->d_func()->categorySum(category);
54 qreal percentage = (100 / colSum);
54 qreal percentage = (100 / colSum);
55 qreal yPos = height + domainScale * m_domainMinY;
55 qreal yPos = height + domainScale * m_domainMinY + geometry().topLeft().y();
56 for (int set=0; set < m_series->barsetCount(); set++) {
56 for (int set=0; set < m_series->barsetCount(); set++) {
57 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57 QBarSet* barSet = m_series->d_func()->barsetAt(set);
58 qreal barHeight = barSet->valueAt(category) * percentage * domainScale;
58 qreal barHeight = barSet->valueAt(category) * percentage * domainScale;
59 Bar* bar = m_bars.at(itemIndex);
59 Bar* bar = m_bars.at(itemIndex);
60 bar->setPen(barSet->pen());
60 bar->setPen(barSet->pen());
61 bar->setBrush(barSet->brush());
61 bar->setBrush(barSet->brush());
62 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
62 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
63 layout.append(rect);
63 layout.append(rect);
64
64
65 BarLabel* label = m_labels.at(itemIndex);
65 BarLabel* label = m_labels.at(itemIndex);
66
66
67 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
67 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
68 int p = m_series->d_func()->percentageAt(set,category) * 100;
68 int p = m_series->d_func()->percentageAt(set,category) * 100;
69 QString vString(QString::number(p));
69 QString vString(QString::number(p));
70 vString.truncate(3);
70 vString.truncate(3);
71 vString.append("%");
71 vString.append("%");
72 label->setText(vString);
72 label->setText(vString);
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 itemIndex++;
80 itemIndex++;
81 yPos -= barHeight;
81 yPos -= barHeight;
82 }
82 }
83 xPos += xStep;
83 xPos += xStep;
84 }
84 }
85 return layout;
85 return layout;
86 }
86 }
87
87
88 #include "moc_percentbarchartitem_p.cpp"
88 #include "moc_percentbarchartitem_p.cpp"
89
89
90 QTCOMMERCIALCHART_END_NAMESPACE
90 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,85 +1,85
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 "barlabel_p.h"
23 #include "barlabel_p.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "qbarseries_p.h"
25 #include "qbarseries_p.h"
26 #include "qbarset.h"
26 #include "qbarset.h"
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
31 BarChartItem(series, presenter)
31 BarChartItem(series, presenter)
32 {
32 {
33 }
33 }
34
34
35 QVector<QRectF> StackedBarChartItem::calculateLayout()
35 QVector<QRectF> StackedBarChartItem::calculateLayout()
36 {
36 {
37 QVector<QRectF> layout;
37 QVector<QRectF> layout;
38 // Use temporary qreals for accurancy
38 // Use temporary qreals for accurancy
39
39
40 // Domain:
40 // Domain:
41 qreal range = m_domainMaxY - m_domainMinY;
41 qreal range = m_domainMaxY - m_domainMinY;
42 qreal height = geometry().height();
42 qreal height = geometry().height();
43 qreal width = geometry().width();
43 qreal width = geometry().width();
44 qreal scale = (height / range);
44 qreal scale = (height / range);
45 qreal categotyCount = m_series->categoryCount();
45 qreal categotyCount = m_series->categoryCount();
46 qreal barWidth = width / (categotyCount * 2);
46 qreal barWidth = width / (categotyCount * 2);
47 qreal xStep = width / categotyCount;
47 qreal xStep = width / categotyCount;
48 qreal xPos = xStep / 2 - barWidth / 2;
48 qreal xPos = xStep / 2 - barWidth / 2 + geometry().topLeft().x();
49
49
50 int itemIndex(0);
50 int itemIndex(0);
51 for (int category = 0; category < categotyCount; category++) {
51 for (int category = 0; category < categotyCount; category++) {
52 qreal yPos = height + scale * m_domainMinY;
52 qreal yPos = height + scale * m_domainMinY + geometry().topLeft().y();
53 for (int set=0; set < m_series->barsetCount(); set++) {
53 for (int set=0; set < m_series->barsetCount(); set++) {
54 QBarSet* barSet = m_series->d_func()->barsetAt(set);
54 QBarSet* barSet = m_series->d_func()->barsetAt(set);
55
55
56 qreal barHeight = barSet->valueAt(category) * scale;
56 qreal barHeight = barSet->valueAt(category) * scale;
57 Bar* bar = m_bars.at(itemIndex);
57 Bar* bar = m_bars.at(itemIndex);
58 bar->setPen(barSet->pen());
58 bar->setPen(barSet->pen());
59 bar->setBrush(barSet->brush());
59 bar->setBrush(barSet->brush());
60 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
60 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
61 layout.append(rect);
61 layout.append(rect);
62
62
63 BarLabel* label = m_labels.at(itemIndex);
63 BarLabel* label = m_labels.at(itemIndex);
64
64
65 if (!qFuzzyIsNull(barSet->valueAt(category))) {
65 if (!qFuzzyIsNull(barSet->valueAt(category))) {
66 label->setText(QString::number(barSet->valueAt(category)));
66 label->setText(QString::number(barSet->valueAt(category)));
67 } else {
67 } else {
68 label->setText(QString(""));
68 label->setText(QString(""));
69 }
69 }
70
70
71 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
71 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
72 ,yPos - barHeight/2 - label->boundingRect().height()/2);
72 ,yPos - barHeight/2 - label->boundingRect().height()/2);
73 label->setFont(barSet->labelFont());
73 label->setFont(barSet->labelFont());
74 itemIndex++;
74 itemIndex++;
75 yPos -= barHeight;
75 yPos -= barHeight;
76 }
76 }
77 xPos += xStep;
77 xPos += xStep;
78 }
78 }
79
79
80 return layout;
80 return layout;
81 }
81 }
82
82
83 #include "moc_stackedbarchartitem_p.cpp"
83 #include "moc_stackedbarchartitem_p.cpp"
84
84
85 QTCOMMERCIALCHART_END_NAMESPACE
85 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now