bargroup.cpp
65 lines
| 1.9 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: | ||||
|
r99 | if (mSeries.countRows() <= 0) { | ||
|
r56 | // Nothing to do. | ||
return; | ||||
} | ||||
|
r101 | // TODO: better way to auto-layout? | ||
|
r99 | // Use reals for accurancy (we might get some compiler warnings... :) | ||
int columnCount = mSeries.countColumns(); | ||||
int rowCount = mSeries.countRows(); | ||||
|
r56 | |||
|
r99 | qreal tW = mWidth; | ||
qreal tH = mHeight; | ||||
qreal tM = mMax; | ||||
qreal scale = (tH/tM); | ||||
qreal tC = columnCount+1; | ||||
qreal xStepPerSeries = (tW/tC); | ||||
|
r82 | // Scaling. | ||
|
r71 | int itemIndex(0); | ||
|
r114 | int labelIndex = mSeries.countColumns() * mSeries.countRows(); | ||
|
r99 | for (int column=0; column < columnCount; column++) { | ||
qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); | ||||
|
r113 | qreal yPos = mHeight; | ||
|
r99 | for (int row = 0; row < rowCount; row++) { | ||
qreal barHeight = mSeries.valueAt(row, column) * scale; | ||||
Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex)); | ||||
// TODO: width settable per bar? | ||||
bar->resize(mBarDefaultWidth, barHeight); | ||||
bar->setColor(mColors.at(row)); | ||||
|
r113 | bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); | ||
|
r99 | itemIndex++; | ||
xPos += mBarDefaultWidth; | ||||
} | ||||
|
r114 | |||
// TODO: Layout for labels, remove magic number | ||||
xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); | ||||
BarLabel* label = reinterpret_cast<BarLabel*> (childItems().at(labelIndex)); | ||||
label->setPos(xPos, mHeight + 20); | ||||
labelIndex++; | ||||
|
r99 | } | ||
|
r56 | mLayoutDirty = true; | ||
} | ||||
QTCOMMERCIALCHART_END_NAMESPACE | ||||