percentbarchartitem.cpp
81 lines
| 2.7 KiB
| text/x-c
|
CppLexer
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 | ||||
Michal Klocek
|
r677 | PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter* presenter) : | ||
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(); | ||||
qreal categoryCount = mSeries->categoryCount(); | ||||
qreal barWidth = width / (mSeries->categoryCount() * 2); | ||||
qreal xStep = width/categoryCount; | ||||
qreal xPos = xStep/2 - barWidth / 2; | ||||
sauimone
|
r101 | |||
sauimone
|
r473 | int itemIndex(0); | ||
Michal Klocek
|
r679 | for (int category = 0; category < categoryCount ; category++) { | ||
sauimone
|
r288 | qreal colSum = mSeries->categorySum(category); | ||
Michal Klocek
|
r679 | qreal scale = (height / colSum); | ||
qreal yPos = height; | ||||
sauimone
|
r366 | for (int set=0; set < mSeries->barsetCount(); set++) { | ||
sauimone
|
r288 | qreal barHeight = mSeries->valueAt(set, category) * scale; | ||
sauimone
|
r256 | Bar* bar = mBars.at(itemIndex); | ||
Tero Ahola
|
r653 | bar->setPen(mSeries->barsetAt(set)->pen()); | ||
sauimone
|
r357 | bar->setBrush(mSeries->barsetAt(set)->brush()); | ||
sauimone
|
r694 | QRectF rect(xPos, yPos-barHeight,barWidth, barHeight); | ||
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
|
r366 | for (int category=0; category < mSeries->categoryCount(); category++) { | ||
Michal Klocek
|
r679 | qreal yPos = height; | ||
sauimone
|
r288 | qreal colSum = mSeries->categorySum(category); | ||
Michal Klocek
|
r679 | qreal scale = (height / colSum); | ||
sauimone
|
r366 | for (int set=0; set < mSeries->barsetCount(); set++) { | ||
sauimone
|
r288 | qreal barHeight = mSeries->valueAt(set,category) * scale; | ||
sauimone
|
r263 | BarValue* value = mFloatingValues.at(itemIndex); | ||
sauimone
|
r512 | QBarSet* barSet = mSeries->barsetAt(set); | ||
value->resize(100,50); // TODO: proper layout for this. | ||||
sauimone
|
r276 | value->setPos(xPos, yPos-barHeight/2); | ||
sauimone
|
r512 | value->setPen(barSet->floatingValuePen()); | ||
sauimone
|
r263 | |||
sauimone
|
r288 | if (mSeries->valueAt(set,category) != 0) { | ||
int p = mSeries->percentageAt(set,category) * 100; | ||||
sauimone
|
r280 | QString vString(QString::number(p)); | ||
vString.truncate(3); | ||||
sauimone
|
r276 | vString.append("%"); | ||
value->setValueString(vString); | ||||
} else { | ||||
value->setValueString(QString("")); | ||||
} | ||||
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 | ||