##// END OF EJS Templates
margin calculations in groupedbarchart
sauimone -
r1286:500f6258ec3a
parent child
Show More
@@ -1,89 +1,89
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "groupedbarchartitem_p.h"
21 #include "groupedbarchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset_p.h"
23 #include "qbarset_p.h"
24 #include "qbarseries_p.h"
24 #include "qbarseries_p.h"
25 #include "qbarset.h"
25 #include "qbarset.h"
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
29 GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) :
30 BarChartItem(series, presenter)
30 BarChartItem(series, presenter)
31 {
31 {
32 }
32 }
33
33
34 QVector<QRectF> GroupedBarChartItem::calculateLayout()
34 QVector<QRectF> GroupedBarChartItem::calculateLayout()
35 {
35 {
36 QVector<QRectF> layout;
36 QVector<QRectF> layout;
37
37
38 // Use temporary qreals for accurancy
38 // Use temporary qreals for accurancy
39 qreal categoryCount = m_series->categoryCount();
39 qreal categoryCount = m_series->categoryCount();
40 qreal setCount = m_series->barsetCount();
40 qreal setCount = m_series->barsetCount();
41
41
42 // Domain:
42 // Domain:
43 qreal width = geometry().width();
43 qreal width = geometry().width();
44 qreal height = geometry().height();
44 qreal height = geometry().height();
45 qreal rangeY = m_domainMaxY - m_domainMinY;
45 qreal rangeY = m_domainMaxY - m_domainMinY;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
46 qreal rangeX = m_domainMaxX - m_domainMinX;
47 qreal scaleY = (height / rangeY);
47 qreal scaleY = (height / rangeY);
48 qreal scaleX = (width / rangeX);
48 qreal scaleX = (width / rangeX);
49 qreal categoryWidth = width / categoryCount;
49 qreal categoryWidth = width / categoryCount;
50 qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin();
50 qreal barWidth = categoryWidth / setCount - (categoryWidth / setCount) * m_series->d_func()->barMargin();
51
51
52 int itemIndex(0);
52 int itemIndex(0);
53 for (int category = 0; category < categoryCount; category++) {
53 for (int category = 0; category < categoryCount; category++) {
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
54 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
55 for (int set = 0; set < setCount; set++) {
55 for (int set = 0; set < setCount; set++) {
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
56 QBarSet* barSet = m_series->d_func()->barsetAt(set);
57
57
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
58 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left();
59 xPos -= setCount*barWidth/2;
59 xPos -= setCount*barWidth/2;
60 xPos += set*barWidth;
60 xPos += set*barWidth;
61 qreal barHeight = barSet->at(category).y() * scaleY;
61 qreal barHeight = barSet->at(category).y() * scaleY;
62 Bar* bar = m_bars.at(itemIndex);
62 Bar* bar = m_bars.at(itemIndex);
63
63
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
64 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
65 layout.append(rect);
65 layout.append(rect);
66 bar->setPen(barSet->pen());
66 bar->setPen(barSet->pen());
67 bar->setBrush(barSet->brush());
67 bar->setBrush(barSet->brush());
68
68
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
69 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
70
70
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
71 if (!qFuzzyIsNull(barSet->at(category).y())) {
72 label->setText(QString::number(barSet->at(category).y()));
72 label->setText(QString::number(barSet->at(category).y()));
73 } else {
73 } else {
74 label->setText(QString(""));
74 label->setText(QString(""));
75 }
75 }
76
76
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
77 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
78 ,yPos - barHeight/2 - label->boundingRect().height()/2);
79 label->setFont(barSet->labelFont());
79 label->setFont(barSet->labelFont());
80
80
81 itemIndex++;
81 itemIndex++;
82 }
82 }
83 }
83 }
84 return layout;
84 return layout;
85 }
85 }
86
86
87 #include "moc_groupedbarchartitem_p.cpp"
87 #include "moc_groupedbarchartitem_p.cpp"
88
88
89 QTCOMMERCIALCHART_END_NAMESPACE
89 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now