barchartitem.cpp
96 lines
| 3.2 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" | ||
|
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); | ||||
|
r2073 | qreal rectWidth = (scaleX / setCount) * m_series->d_func()->barWidth(); | ||
|
r681 | |||
int itemIndex(0); | ||||
|
r737 | for (int category = 0; category < categoryCount; category++) { | ||
|
r2073 | qreal yPos = height + scaleY * m_domainMinY + geometry().top(); | ||
|
r681 | for (int set = 0; set < setCount; set++) { | ||
|
r1580 | QBarSetPrivate* barSet = m_series->d_func()->barsetAt(set)->d_ptr.data(); | ||
|
r817 | |||
|
r2073 | qreal xPos = (barSet->pos(category) - m_domainMinX) * scaleX + geometry().left(); | ||
xPos -= setCount*rectWidth/2; | ||||
xPos += set*rectWidth; | ||||
|
r1794 | |||
|
r2073 | qreal rectHeight = barSet->value(category) * scaleY; | ||
|
r763 | Bar* bar = m_bars.at(itemIndex); | ||
|
r1167 | |||
|
r2073 | QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight); | ||
|
r681 | layout.append(rect); | ||
|
r1580 | bar->setPen(barSet->m_pen); | ||
bar->setBrush(barSet->m_brush); | ||||
|
r2073 | if (qFuzzyIsNull(rectHeight)) { | ||
|
r1603 | 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) | ||
|
r2073 | ,yPos - rectHeight/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 | ||