##// END OF EJS Templates
fixed naming mixup with percent and stacked groups
fixed naming mixup with percent and stacked groups

File last commit:

r105:37fe9cea6214
r105:37fe9cea6214
Show More
stackedbargroup.cpp
134 lines | 3.2 KiB | text/x-c | CppLexer
/ src / barchart / stackedbargroup.cpp
sauimone
fixed naming mixup with percent and stacked groups
r105 #include "stackedbargroup.h"
sauimone
added missing example files :)
r96 #include "bar.h"
#include <QDebug>
QTCOMMERCIALCHART_BEGIN_NAMESPACE
sauimone
fixed naming mixup with percent and stacked groups
r105 StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent) :
ChartItem(parent)
,mSeries(series)
,mLayoutSet(false)
,mLayoutDirty(true)
,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready
sauimone
added missing example files :)
r96 {
dataChanged();
}
sauimone
fixed naming mixup with percent and stacked groups
r105
void StackedBarGroup::setSize(const QSize& size)
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 {
sauimone
fixed naming mixup with percent and stacked groups
r105 // qDebug() << "StackedBarGroup::setSize";
sauimone
added missing example files :)
r96 mWidth = size.width();
mHeight = size.height();
layoutChanged();
mLayoutSet = true;
}
sauimone
fixed naming mixup with percent and stacked groups
r105 void StackedBarGroup::setPlotDomain(const PlotDomain& data)
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 {
sauimone
fixed naming mixup with percent and stacked groups
r105 qDebug() << "StackedBarGroup::setPlotDomain";
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 // TODO:
}
sauimone
fixed naming mixup with percent and stacked groups
r105 void StackedBarGroup::setBarWidth( int w )
sauimone
added missing example files :)
r96 {
mBarDefaultWidth = w;
}
sauimone
fixed naming mixup with percent and stacked groups
r105 int StackedBarGroup::addColor( QColor color )
sauimone
added missing example files :)
r96 {
int colorIndex = mColors.count();
mColors.append(color);
return colorIndex;
}
sauimone
fixed naming mixup with percent and stacked groups
r105 void StackedBarGroup::resetColors()
sauimone
added missing example files :)
r96 {
mColors.clear();
}
sauimone
fixed naming mixup with percent and stacked groups
r105 void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
sauimone
added missing example files :)
r96 {
if (!mLayoutSet) {
qDebug() << "QBarChart::paint called without layout set. Aborting.";
return;
}
if (mLayoutDirty) {
// Layout or data has changed. Need to redraw.
foreach(QGraphicsItem* i, childItems()) {
i->paint(painter,option,widget);
}
}
}
sauimone
fixed naming mixup with percent and stacked groups
r105 QRectF StackedBarGroup::boundingRect() const
sauimone
added missing example files :)
r96 {
return QRectF(0,0,mWidth,mHeight);
}
sauimone
fixed naming mixup with percent and stacked groups
r105 void StackedBarGroup::dataChanged()
sauimone
added missing example files :)
r96 {
qDebug() << "QBarChart::dataChanged mSeries";
// Find out maximum and minimum of all series
mMax = mSeries.max();
mMin = mSeries.min();
// Delete old bars
// Is this correct way to delete childItems?
foreach (QGraphicsItem* item, childItems()) {
delete item;
}
// Create new graphic items for bars
int totalItems = mSeries.countTotalItems();
for (int i=0; i<totalItems; i++) {
Bar *bar = new Bar(this);
childItems().append(bar);
}
mLayoutDirty = true;
}
sauimone
fixed naming mixup with percent and stacked groups
r105 void StackedBarGroup::layoutChanged()
sauimone
added missing example files :)
r96 {
// Scale bars to new layout
// Layout for bars:
if (mSeries.countRows() <= 0) {
// Nothing to do.
return;
}
// TODO: better way to auto-layout
sauimone
bar chart layout fixing
r99 // Use reals for accurancy (we might get some compiler warnings... :)
sauimone
fixed naming mixup with percent and stacked groups
r105 qreal maxSum = mSeries.maxColumnSum();
qreal h = mHeight;
qreal scale = (h / maxSum);
sauimone
added missing example files :)
r96 int count = mSeries.countColumns();
int itemIndex(0);
sauimone
fixing layout bug on stacked bar chart
r98 qreal tW = mWidth;
qreal tC = count+1;
qreal xStep = (tW/tC);
qreal xPos = ((tW/tC) + mBarDefaultWidth / 2);
sauimone
added missing example files :)
r96 for (int column = 0; column < mSeries.countColumns(); column++) {
sauimone
fixing layout bug on stacked bar chart
r98 qreal yPos = h;
sauimone
added missing example files :)
r96 for (int row=0; row < mSeries.countRows(); row++) {
sauimone
fixing layout bug on stacked bar chart
r98 qreal barHeight = mSeries.valueAt(row, column) * scale;
sauimone
added missing example files :)
r96 Bar* bar = reinterpret_cast<Bar*> (childItems().at(itemIndex));
// TODO: width settable per bar?
bar->resize(mBarDefaultWidth, barHeight);
bar->setColor(mColors.at(row));
bar->setPos(xPos, yPos);
itemIndex++;
sauimone
Some fixes to bar charts. Now bars draw almost correctly
r97 yPos -= barHeight;
sauimone
added missing example files :)
r96 }
sauimone
fixing layout bug on stacked bar chart
r98 xPos += xStep;
sauimone
added missing example files :)
r96 }
mLayoutDirty = true;
}
QTCOMMERCIALCHART_END_NAMESPACE