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