barchartitem.cpp
109 lines
| 3.6 KiB
| text/x-c
|
CppLexer
|
r794 | /**************************************************************************** | ||
** | ||||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r666 | #include "barchartitem_p.h" | ||
|
r126 | #include "bar_p.h" | ||
|
r962 | #include "qbarset_p.h" | ||
|
r1586 | #include "qabstractbarseries_p.h" | ||
|
r1674 | #include "qbarset.h" | ||
#include "qbarset_p.h" | ||||
|
r126 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r1584 | BarChartItem::BarChartItem(QAbstractBarSeries *series, ChartPresenter *presenter) : | ||
|
r1674 | AbstractBarChartItem(series, presenter) | ||
|
r126 | { | ||
} | ||||
|
r165 | |||
|
r681 | QVector<QRectF> BarChartItem::calculateLayout() | ||
|
r671 | { | ||
|
r681 | QVector<QRectF> layout; | ||
|
r1321 | // Use temporary qreals for accuracy | ||
qreal categoryCount = m_series->d_func()->categoryCount(); | ||||
|
r1462 | qreal setCount = m_series->count(); | ||
|
r1284 | bool barsVisible = m_series->isVisible(); | ||
|
r681 | |||
|
r850 | // Domain: | ||
|
r682 | qreal width = geometry().width(); | ||
qreal height = geometry().height(); | ||||
|
r1167 | qreal rangeY = m_domainMaxY - m_domainMinY; | ||
qreal rangeX = m_domainMaxX - m_domainMinX; | ||||
qreal scaleY = (height / rangeY); | ||||
qreal scaleX = (width / rangeX); | ||||
|
r1794 | qreal barWidth; | ||
|
r1866 | if (m_series->d_func()->m_overlap) { | ||
|
r1794 | barWidth = scaleX * m_series->d_func()->barWidth(); | ||
|
r1866 | } else { | ||
barWidth = (scaleX / setCount) * m_series->d_func()->barWidth(); | ||||
|
r1794 | } | ||
|
r681 | |||
int itemIndex(0); | ||||
|
r737 | for (int category = 0; category < categoryCount; category++) { | ||
|
r1167 | qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); | ||
|
r681 | for (int set = 0; set < setCount; set++) { | ||
|
r1580 | QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); | ||
|
r817 | |||
|
r1794 | qreal xPos; | ||
|
r1866 | if (m_series->d_func()->m_overlap) { | ||
xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; | ||||
} else { | ||||
|
r1794 | xPos = (barSet->pos(category) - m_domainMinX) * scaleX + m_rect.left(); | ||
xPos -= setCount*barWidth/2; | ||||
xPos += set*barWidth; | ||||
} | ||||
|
r1681 | qreal barHeight = barSet->value(category) * scaleY; | ||
|
r763 | Bar* bar = m_bars.at(itemIndex); | ||
|
r1167 | |||
|
r1674 | QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); | ||
|
r681 | layout.append(rect); | ||
|
r1580 | bar->setPen(barSet->m_pen); | ||
bar->setBrush(barSet->m_brush); | ||||
|
r1603 | if (qFuzzyIsNull(barHeight)) { | ||
bar->setVisible(false); | ||||
} else { | ||||
bar->setVisible(barsVisible); | ||||
} | ||||
|
r681 | |||
|
r1246 | QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); | ||
|
r681 | |||
|
r1681 | if (!qFuzzyIsNull(barSet->value(category))) { | ||
label->setText(QString::number(barSet->value(category))); | ||||
|
r681 | } else { | ||
|
r820 | label->setText(QString("")); | ||
|
r681 | } | ||
|
r820 | label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) | ||
,yPos - barHeight/2 - label->boundingRect().height()/2); | ||||
|
r1580 | label->setFont(barSet->m_labelFont); | ||
label->setBrush(barSet->m_labelBrush); | ||||
|
r811 | |||
|
r681 | itemIndex++; | ||
} | ||||
} | ||||
|
r671 | return layout; | ||
} | ||||
|
r666 | #include "moc_barchartitem_p.cpp" | ||
|
r139 | |||
|
r126 | QTCOMMERCIALCHART_END_NAMESPACE | ||