##// END OF EJS Templates
barchart visible property fix
sauimone -
r1322:5f7bdc0a2083
parent child
Show More
@@ -1,90 +1,92
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
42
42 // Domain:
43 // Domain:
43 qreal width = geometry().width();
44 qreal width = geometry().width();
44 qreal height = geometry().height();
45 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
49 qreal categoryWidth = width / categoryCount;
50 qreal categoryWidth = width / categoryCount;
50 qreal barWidth = categoryWidth / setCount - (categoryWidth / setCount) * m_series->d_func()->barMargin();
51 qreal barWidth = categoryWidth / setCount - (categoryWidth / setCount) * m_series->d_func()->barMargin();
51
52
52 int itemIndex(0);
53 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
54 for (int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
55 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
55 for (int set = 0; set < setCount; set++) {
56 for (int set = 0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
58
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
59 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
59 xPos -= setCount*barWidth/2;
60 xPos -= setCount*barWidth/2;
60 xPos += set*barWidth;
61 xPos += set*barWidth;
61 qreal barHeight = barSet->at(category).y() * scaleY;
62 qreal barHeight = barSet->at(category).y() * scaleY;
62 Bar* bar = m_bars.at(itemIndex);
63 Bar* bar = m_bars.at(itemIndex);
63
64
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
65 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
65 layout.append(rect);
66 layout.append(rect);
66 bar->setPen(barSet->pen());
67 bar->setPen(barSet->pen());
67 bar->setBrush(barSet->brush());
68 bar->setBrush(barSet->brush());
69 bar->setVisible(barsVisible);
68
70
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
71 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70
72
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
73 if (!qFuzzyIsNull(barSet->at(category).y())) {
72 label->setText(QString::number(barSet->at(category).y()));
74 label->setText(QString::number(barSet->at(category).y()));
73 } else {
75 } else {
74 label->setText(QString(""));
76 label->setText(QString(""));
75 }
77 }
76
78
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
79 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
80 ,yPos - barHeight/2 - label->boundingRect().height()/2);
79 label->setFont(barSet->labelFont());
81 label->setFont(barSet->labelFont());
80 label->setBrush(barSet->labelBrush());
82 label->setBrush(barSet->labelBrush());
81
83
82 itemIndex++;
84 itemIndex++;
83 }
85 }
84 }
86 }
85 return layout;
87 return layout;
86 }
88 }
87
89
88 #include "moc_groupedbarchartitem_p.cpp"
90 #include "moc_groupedbarchartitem_p.cpp"
89
91
90 QTCOMMERCIALCHART_END_NAMESPACE
92 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,94 +1,98
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
42
42 // Domain:
43 // Domain:
43 qreal width = geometry().width();
44 qreal width = geometry().width();
44 qreal height = geometry().height();
45 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
48 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
49 qreal scaleX = (width / rangeX);
49 qreal categoryWidth = width / categoryCount;
50 qreal categoryWidth = width / categoryCount;
50 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
51 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
51
52
52 int itemIndex(0);
53 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
54 for (int category = 0; category < categoryCount; category++) {
54 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal colSum = m_series->d_func()->categorySum(category);
55 qreal percentage = (100 / colSum);
56 qreal percentage = (100 / colSum);
56 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
57 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
57 for (int set=0; set < setCount; set++) {
58 for (int set=0; set < setCount; set++) {
58 QBarSet* barSet = m_series->d_func()->barsetAt(set);
59 QBarSet* barSet = m_series->d_func()->barsetAt(set);
59
60
60 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
61 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
61
62
62 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
63 qreal barHeight = barSet->at(category).y() * percentage * scaleY;
63 Bar* bar = m_bars.at(itemIndex);
64 Bar* bar = m_bars.at(itemIndex);
64 bar->setPen(barSet->pen());
65 bar->setPen(barSet->pen());
65 bar->setBrush(barSet->brush());
66 bar->setBrush(barSet->brush());
67 bar->setVisible(barsVisible);
68
66 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
69 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
67 layout.append(rect);
70 layout.append(rect);
68
71
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
72 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70
73
71 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
74 if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) {
72 int p = m_series->d_func()->percentageAt(set,category) * 100;
75 int p = m_series->d_func()->percentageAt(set,category) * 100;
73 QString vString(QString::number(p));
76 QString vString(QString::number(p));
74 vString.truncate(3);
77 vString.truncate(3);
75 vString.append("%");
78 vString.append("%");
76 label->setText(vString);
79 label->setText(vString);
77 } else {
80 } else {
78 label->setText(QString(""));
81 label->setText(QString(""));
79 }
82 }
80
83
81 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
84 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
82 ,yPos - barHeight/2 - label->boundingRect().height()/2);
85 ,yPos - barHeight/2 - label->boundingRect().height()/2);
83 label->setFont(barSet->labelFont());
86 label->setFont(barSet->labelFont());
84 label->setBrush(barSet->labelBrush());
87 label->setBrush(barSet->labelBrush());
88
85 itemIndex++;
89 itemIndex++;
86 yPos -= barHeight;
90 yPos -= barHeight;
87 }
91 }
88 }
92 }
89 return layout;
93 return layout;
90 }
94 }
91
95
92 #include "moc_percentbarchartitem_p.cpp"
96 #include "moc_percentbarchartitem_p.cpp"
93
97
94 QTCOMMERCIALCHART_END_NAMESPACE
98 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,88 +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 "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
41
41 // Domain:
42 // Domain:
42 qreal width = geometry().width();
43 qreal width = geometry().width();
43 qreal height = geometry().height();
44 qreal height = geometry().height();
44 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal scaleY = (height / rangeY);
47 qreal scaleY = (height / rangeY);
47 qreal scaleX = (width / rangeX);
48 qreal scaleX = (width / rangeX);
48 qreal categoryWidth = width / categoryCount;
49 qreal categoryWidth = width / categoryCount;
49 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
50 qreal barWidth = categoryWidth - categoryWidth * m_series->d_func()->barMargin();
50
51
51 int itemIndex(0);
52 int itemIndex(0);
52 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
53 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y();
54 for (int set=0; set < setCount; set++) {
55 for (int set=0; set < setCount; set++) {
55 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56
57
57 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
58
59
59 qreal barHeight = barSet->at(category).y() * scaleY;
60 qreal barHeight = barSet->at(category).y() * scaleY;
60 Bar* bar = m_bars.at(itemIndex);
61 Bar* bar = m_bars.at(itemIndex);
61 bar->setPen(barSet->pen());
62 bar->setPen(barSet->pen());
62 bar->setBrush(barSet->brush());
63 bar->setBrush(barSet->brush());
64 bar->setVisible(barsVisible);
65
63 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
66 QRectF rect(xPos, yPos-barHeight, barWidth, barHeight);
64 layout.append(rect);
67 layout.append(rect);
65
68
66 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
67
70
68 if (!qFuzzyIsNull(barSet->at(category).y())) {
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
69 label->setText(QString::number(barSet->at(category).y()));
72 label->setText(QString::number(barSet->at(category).y()));
70 } else {
73 } else {
71 label->setText(QString(""));
74 label->setText(QString(""));
72 }
75 }
73
76
74 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
75 ,yPos - barHeight/2 - label->boundingRect().height()/2);
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
76 label->setFont(barSet->labelFont());
79 label->setFont(barSet->labelFont());
77 label->setBrush(barSet->labelBrush());
80 label->setBrush(barSet->labelBrush());
78 itemIndex++;
81 itemIndex++;
79 yPos -= barHeight;
82 yPos -= barHeight;
80 }
83 }
81 }
84 }
82
85
83 return layout;
86 return layout;
84 }
87 }
85
88
86 #include "moc_stackedbarchartitem_p.cpp"
89 #include "moc_stackedbarchartitem_p.cpp"
87
90
88 QTCOMMERCIALCHART_END_NAMESPACE
91 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now