percentbarchartitem.cpp
101 lines
| 3.4 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
sauimone
|
r666 | #include "percentbarchartitem_p.h" | ||
sauimone
|
r118 | #include "bar_p.h" | ||
sauimone
|
r263 | #include "barvalue_p.h" | ||
sauimone
|
r214 | #include "qbarset.h" | ||
sauimone
|
r101 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Tero Ahola
|
r737 | PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) : | ||
Michal Klocek
|
r677 | BarChartItem(series, presenter) | ||
sauimone
|
r101 | { | ||
} | ||||
sauimone
|
r694 | QVector<QRectF> PercentBarChartItem::calculateLayout() | ||
sauimone
|
r101 | { | ||
sauimone
|
r694 | QVector<QRectF> layout; | ||
sauimone
|
r165 | |||
sauimone
|
r473 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | ||
Michal Klocek
|
r679 | qreal width = geometry().width(); | ||
qreal height = geometry().height(); | ||||
sauimone
|
r763 | qreal categoryCount = m_series->categoryCount(); | ||
qreal barWidth = width / (m_series->categoryCount() * 2); | ||||
Tero Ahola
|
r737 | qreal xStep = width / categoryCount; | ||
qreal xPos = xStep / 2 - barWidth / 2; | ||||
sauimone
|
r101 | |||
sauimone
|
r473 | int itemIndex(0); | ||
Tero Ahola
|
r737 | for (int category = 0; category < categoryCount; category++) { | ||
sauimone
|
r763 | qreal colSum = m_series->categorySum(category); | ||
Michal Klocek
|
r679 | qreal scale = (height / colSum); | ||
qreal yPos = height; | ||||
sauimone
|
r763 | for (int set=0; set < m_series->barsetCount(); set++) { | ||
qreal barHeight = m_series->valueAt(set, category) * scale; | ||||
Bar* bar = m_bars.at(itemIndex); | ||||
bar->setPen(m_series->barsetAt(set)->pen()); | ||||
bar->setBrush(m_series->barsetAt(set)->brush()); | ||||
Tero Ahola
|
r737 | QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); | ||
sauimone
|
r694 | layout.append(rect); | ||
sauimone
|
r101 | itemIndex++; | ||
yPos -= barHeight; | ||||
} | ||||
xPos += xStep; | ||||
} | ||||
sauimone
|
r654 | |||
sauimone
|
r263 | // Position floating values | ||
itemIndex = 0; | ||||
Michal Klocek
|
r679 | xPos = (width/categoryCount); | ||
sauimone
|
r763 | for (int category=0; category < m_series->categoryCount(); category++) { | ||
Michal Klocek
|
r679 | qreal yPos = height; | ||
sauimone
|
r763 | qreal colSum = m_series->categorySum(category); | ||
Michal Klocek
|
r679 | qreal scale = (height / colSum); | ||
sauimone
|
r763 | for (int set=0; set < m_series->barsetCount(); set++) { | ||
qreal barHeight = m_series->valueAt(set,category) * scale; | ||||
BarValue* value = m_floatingValues.at(itemIndex); | ||||
sauimone
|
r263 | |||
sauimone
|
r763 | QBarSet* barSet = m_series->barsetAt(set); | ||
Tero Ahola
|
r737 | value->resize(100, 50); // TODO: proper layout for this. | ||
value->setPos(xPos, yPos-barHeight / 2); | ||||
sauimone
|
r512 | value->setPen(barSet->floatingValuePen()); | ||
sauimone
|
r263 | |||
Jani Honkonen
|
r768 | if (!qFuzzyIsNull(m_series->valueAt(set,category))) { | ||
sauimone
|
r763 | int p = m_series->percentageAt(set,category) * 100; | ||
sauimone
|
r280 | QString vString(QString::number(p)); | ||
vString.truncate(3); | ||||
sauimone
|
r276 | vString.append("%"); | ||
sauimone
|
r776 | value->setText(vString); | ||
sauimone
|
r276 | } else { | ||
sauimone
|
r776 | value->setText(QString("")); | ||
sauimone
|
r276 | } | ||
sauimone
|
r263 | |||
itemIndex++; | ||||
yPos -= barHeight; | ||||
} | ||||
xPos += xStep; | ||||
sauimone
|
r126 | } | ||
sauimone
|
r694 | return layout; | ||
sauimone
|
r101 | } | ||
sauimone
|
r666 | #include "moc_percentbarchartitem_p.cpp" | ||
sauimone
|
r288 | |||
sauimone
|
r101 | QTCOMMERCIALCHART_END_NAMESPACE | ||