bargroup.cpp
70 lines
| 2.0 KiB
| text/x-c
|
CppLexer
|
r56 | #include "bargroup.h" | ||
|
r118 | #include "bar_p.h" | ||
|
r114 | #include "barlabel_p.h" | ||
|
r56 | #include <QDebug> | ||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
|
r78 | BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) : | ||
|
r126 | BarGroupBase(series,parent) | ||
|
r56 | { | ||
|
r126 | mBarDefaultWidth = 10; | ||
|
r56 | } | ||
void BarGroup::layoutChanged() | ||||
{ | ||||
// Scale bars to new layout | ||||
// Layout for bars: | ||||
|
r159 | /* | ||
|
r99 | if (mSeries.countRows() <= 0) { | ||
|
r56 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r159 | */ | ||
if (mModel.countSeries() <= 0) { | ||||
return; | ||||
} | ||||
|
r56 | |||
|
r101 | // TODO: better way to auto-layout? | ||
|
r99 | // Use reals for accurancy (we might get some compiler warnings... :) | ||
|
r159 | int itemCount = mModel.countItemsInSeries(); | ||
int seriesCount = mModel.countSeries(); | ||||
|
r56 | |||
|
r99 | qreal tW = mWidth; | ||
qreal tH = mHeight; | ||||
qreal tM = mMax; | ||||
qreal scale = (tH/tM); | ||||
|
r159 | qreal tC = itemCount+1; | ||
|
r99 | qreal xStepPerSeries = (tW/tC); | ||
|
r82 | // Scaling. | ||
|
r71 | int itemIndex(0); | ||
|
r159 | int labelIndex = itemCount * seriesCount; | ||
|
r114 | |||
|
r159 | for (int item=0; item < itemCount; item++) { | ||
qreal xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); | ||||
|
r113 | qreal yPos = mHeight; | ||
|
r159 | for (int series = 0; series < seriesCount; series++) { | ||
qreal barHeight = mModel.valueAt(series, item) * scale; | ||||
|
r99 | Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||
// TODO: width settable per bar? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
|
r159 | bar->setColor(mColors.at(series)); | ||
|
r113 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | ||
|
r99 | itemIndex++; | ||
xPos += mBarDefaultWidth; | ||||
} | ||||
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
|
r159 | xPos = xStepPerSeries * item + ((tW + mBarDefaultWidth*seriesCount)/(itemCount*2)); | ||
|
r114 | BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | ||
label->setPos(xPos, mHeight + 20); | ||||
labelIndex++; | ||||
|
r99 | } | ||
|
r56 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||