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