##// END OF EJS Templates
domain applied to barcharts. x-axis domain doesn't make sense in this case
domain applied to barcharts. x-axis domain doesn't make sense in this case

File last commit:

r106:bc1381ca9ce2
r107:d78281b0a001
Show More
bargroup.cpp
144 lines | 3.3 KiB | text/x-c | CppLexer
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 #include "bargroup.h"
#include "bar.h"
#include <QDebug>
QTCOMMERCIALCHART_BEGIN_NAMESPACE
sauimone
Added bar chart example
r78 BarGroup::BarGroup(BarChartSeries& series, QGraphicsItem *parent) :
Tero Ahola
Refactoring continued: restored ChartItem class
r104 ChartItem(parent)
,mSeries(series)
,mLayoutSet(false)
,mLayoutDirty(true)
,mBarDefaultWidth(10)
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 {
dataChanged();
}
Tero Ahola
Refactoring continued: restored ChartItem class
r104 void BarGroup::setSize(const QSize& size)
sauimone
Added bar chart example
r78 {
qDebug() << "BarGroup::setSize";
sauimone
removed old resize method from barchart
r90 mWidth = size.width();
mHeight = size.height();
layoutChanged();
mLayoutSet = true;
sauimone
Added bar chart example
r78 }
void BarGroup::setPlotDomain(const PlotDomain& data)
{
qDebug() << "BarGroup::setPlotDomain";
sauimone
correct drawing for barchart
r82 // TODO:
sauimone
percent bar chart
r101 mPlotDomain = data;
sauimone
Added bar chart example
r78 }
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 void BarGroup::setTheme(ChartTheme *theme)
{
sauimone
adopted theme interface to barcharts
r106 qDebug() << "BarGroup::setTheme";
// TODO:
Tero Ahola
Refactored themes; now enabled for line, scatter and pies...
r103 }
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 void BarGroup::setBarWidth( int w )
{
mBarDefaultWidth = w;
}
sauimone
correct drawing for barchart
r82 int BarGroup::addColor( QColor color )
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 {
sauimone
correct drawing for barchart
r82 int colorIndex = mColors.count();
mColors.append(color);
return colorIndex;
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 }
sauimone
reset colors for barchart
r92 void BarGroup::resetColors()
{
mColors.clear();
}
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
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);
}
}
}
QRectF BarGroup::boundingRect() const
{
return QRectF(0,0,mWidth,mHeight);
}
void BarGroup::dataChanged()
{
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
sauimone
Improved bar chart series
r71 int totalItems = mSeries.countTotalItems();
for (int i=0; i<totalItems; i++) {
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 Bar *bar = new Bar(this);
childItems().append(bar);
}
mLayoutDirty = true;
}
void BarGroup::layoutChanged()
{
// Scale bars to new layout
// Layout for bars:
sauimone
bar chart layout fixing
r99 if (mSeries.countRows() <= 0) {
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 // Nothing to do.
return;
}
sauimone
percent bar chart
r101 // TODO: better way to auto-layout?
sauimone
bar chart layout fixing
r99 // Use reals for accurancy (we might get some compiler warnings... :)
int columnCount = mSeries.countColumns();
int rowCount = mSeries.countRows();
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56
sauimone
bar chart layout fixing
r99 qreal tW = mWidth;
qreal tH = mHeight;
qreal tM = mMax;
qreal scale = (tH/tM);
qreal tC = columnCount+1;
qreal xStepPerSeries = (tW/tC);
qDebug() << "XSTEP:" << xStepPerSeries;
sauimone
correct drawing for barchart
r82 // Scaling.
sauimone
Improved bar chart series
r71 int itemIndex(0);
sauimone
bar chart layout fixing
r99 for (int column=0; column < columnCount; column++) {
qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2));
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));
bar->setPos(xPos, mHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight);
itemIndex++;
xPos += mBarDefaultWidth;
}
}
sauimone
Integrating bar chart. Cleaned up old implementation. TODO: show this in test application. how?
r56 mLayoutDirty = true;
}
QTCOMMERCIALCHART_END_NAMESPACE