percentbarchartitem.cpp
94 lines
| 3.0 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 | ||||
|
r677 | PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter* presenter) : | ||
BarChartItem(series, presenter) | ||||
|
r101 | { | ||
} | ||||
|
r666 | void PercentBarChartItem::layoutChanged() | ||
|
r101 | { | ||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
|
r366 | if (mSeries->barsetCount() <= 0) { | ||
|
r173 | qDebug() << "No sets in model!"; | ||
|
r101 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r165 | if (childItems().count() == 0) { | ||
|
r666 | qDebug() << "WARNING: PercentBarChartItem::layoutChanged called before graphics items are created!"; | ||
|
r165 | return; | ||
} | ||||
|
r473 | // Use temporary qreals for accurancy (we might get some compiler warnings... :) | ||
|
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; | ||||
|
r101 | |||
|
r473 | int itemIndex(0); | ||
|
r679 | for (int category = 0; category < categoryCount ; category++) { | ||
|
r288 | qreal colSum = mSeries->categorySum(category); | ||
|
r679 | qreal scale = (height / colSum); | ||
qreal yPos = height; | ||||
|
r366 | for (int set=0; set < mSeries->barsetCount(); set++) { | ||
|
r288 | qreal barHeight = mSeries->valueAt(set, category) * scale; | ||
|
r256 | Bar* bar = mBars.at(itemIndex); | ||
|
r101 | |||
// TODO: width settable per bar? | ||||
|
r679 | |||
|
r653 | bar->setPen(mSeries->barsetAt(set)->pen()); | ||
|
r679 | bar->setRect(xPos, yPos-barHeight,barWidth, barHeight); | ||
|
r357 | bar->setBrush(mSeries->barsetAt(set)->brush()); | ||
|
r679 | |||
|
r101 | itemIndex++; | ||
yPos -= barHeight; | ||||
} | ||||
xPos += xStep; | ||||
} | ||||
|
r654 | |||
|
r263 | // Position floating values | ||
itemIndex = 0; | ||||
|
r679 | xPos = (width/categoryCount); | ||
|
r366 | for (int category=0; category < mSeries->categoryCount(); category++) { | ||
|
r679 | qreal yPos = height; | ||
|
r288 | qreal colSum = mSeries->categorySum(category); | ||
|
r679 | qreal scale = (height / colSum); | ||
|
r366 | for (int set=0; set < mSeries->barsetCount(); set++) { | ||
|
r288 | qreal barHeight = mSeries->valueAt(set,category) * scale; | ||
|
r263 | BarValue* value = mFloatingValues.at(itemIndex); | ||
|
r512 | QBarSet* barSet = mSeries->barsetAt(set); | ||
value->resize(100,50); // TODO: proper layout for this. | ||||
|
r276 | value->setPos(xPos, yPos-barHeight/2); | ||
|
r512 | value->setPen(barSet->floatingValuePen()); | ||
|
r263 | |||
|
r288 | if (mSeries->valueAt(set,category) != 0) { | ||
int p = mSeries->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 | } | ||
|
r101 | } | ||
|
r666 | #include "moc_percentbarchartitem_p.cpp" | ||
|
r288 | |||
|
r101 | QTCOMMERCIALCHART_END_NAMESPACE | ||