diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index 113e498..ff270b4 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -1,5 +1,6 @@ #include "bargroup.h" #include "bar.h" +#include "barlabel_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -57,6 +58,7 @@ void BarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, // Layout or data has changed. Need to redraw. foreach(QGraphicsItem* i, childItems()) { i->paint(painter,option,widget); + mLayoutDirty = false; } } } @@ -88,6 +90,16 @@ void BarGroup::dataChanged() childItems().append(bar); } + // TODO: labels from series. This creates just some example labels + int count = mSeries.countColumns(); + for (int i=0; iset(text); + childItems().append(label); + } + + // TODO: if (autolayout) { layoutChanged() } or something mLayoutDirty = true; } @@ -113,10 +125,10 @@ void BarGroup::layoutChanged() qreal tC = columnCount+1; qreal xStepPerSeries = (tW/tC); - qDebug() << "XSTEP:" << xStepPerSeries; - // Scaling. int itemIndex(0); + int labelIndex = mSeries.countColumns() * mSeries.countRows(); + for (int column=0; column < columnCount; column++) { qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); qreal yPos = mHeight; @@ -131,6 +143,12 @@ void BarGroup::layoutChanged() itemIndex++; xPos += mBarDefaultWidth; } + + // TODO: Layout for labels, remove magic number + xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); + BarLabel* label = reinterpret_cast (childItems().at(labelIndex)); + label->setPos(xPos, mHeight + 20); + labelIndex++; } mLayoutDirty = true; diff --git a/src/barchart/barlabel.cpp b/src/barchart/barlabel.cpp new file mode 100644 index 0000000..578134b --- /dev/null +++ b/src/barchart/barlabel.cpp @@ -0,0 +1,42 @@ +#include "barlabel_p.h" +#include +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +BarLabel::BarLabel(QGraphicsItem* parent) : ChartItem(parent) +{ +} + + +void BarLabel::set(QString label) +{ + mLabel = label; +} + +void BarLabel::setPos(qreal x, qreal y) +{ + mXpos = x; + mYpos = y; +} + +void BarLabel::setSize(const QSize &size) +{ + mSize = size; +} + +void BarLabel::setPlotDomain(const PlotDomain& data) +{ + mDomain = data; +} + +void BarLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + painter->drawText(boundingRect(),mLabel); +} + +QRectF BarLabel::boundingRect() const +{ + QRectF r(mXpos, mYpos, mXpos + mSize.width(), mYpos + mSize.height()); + return r; +} + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/barlabel_p.h b/src/barchart/barlabel_p.h new file mode 100644 index 0000000..a8c9bdd --- /dev/null +++ b/src/barchart/barlabel_p.h @@ -0,0 +1,36 @@ +#ifndef BARLABEL_H +#define BARLABEL_H + +#include "chartitem_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class BarLabel : public ChartItem +{ +public: + BarLabel(QGraphicsItem* parent = 0); + + void set(QString label); + void setPos(qreal x, qreal y); + + // From ChartItem + void setSize(const QSize &size); + void setPlotDomain(const PlotDomain& data); + + // From QGraphicsItem + void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); + QRectF boundingRect() const; + +private: + + PlotDomain mDomain; + QSize mSize; + QString mLabel; + qreal mXpos; + qreal mYpos; + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // BARLABEL_H diff --git a/src/barchart/percentbargroup.cpp b/src/barchart/percentbargroup.cpp index 3d1347e..f8d8a96 100644 --- a/src/barchart/percentbargroup.cpp +++ b/src/barchart/percentbargroup.cpp @@ -1,5 +1,6 @@ #include "percentbargroup.h" #include "bar.h" +#include "barlabel_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -58,6 +59,7 @@ void PercentBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *o foreach(QGraphicsItem* i, childItems()) { i->paint(painter,option,widget); } + mLayoutDirty = false; } } @@ -88,6 +90,16 @@ void PercentBarGroup::dataChanged() childItems().append(bar); } + // TODO: labels from series. This creates just some example labels + int count = mSeries.countColumns(); + for (int i=0; iset(text); + childItems().append(label); + } + + // TODO: if (autolayout) { layoutChanged() } or something mLayoutDirty = true; } @@ -107,7 +119,9 @@ void PercentBarGroup::layoutChanged() qreal tW = mWidth; qreal tC = count+1; qreal xStep = (tW/tC); - qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); +// qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); + qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); + int labelIndex = mSeries.countColumns() * mSeries.countRows(); for (int column = 0; column < mSeries.countColumns(); column++) { qreal colSum = mSeries.columnSum(column); @@ -125,6 +139,11 @@ void PercentBarGroup::layoutChanged() itemIndex++; yPos -= barHeight; } + + // TODO: Layout for labels, remove magic number + BarLabel* label = reinterpret_cast (childItems().at(labelIndex)); + label->setPos(xPos, mHeight + 20); + labelIndex++; xPos += xStep; } mLayoutDirty = true; diff --git a/src/barchart/stackedbargroup.cpp b/src/barchart/stackedbargroup.cpp index 995f229..f91978c 100644 --- a/src/barchart/stackedbargroup.cpp +++ b/src/barchart/stackedbargroup.cpp @@ -1,5 +1,6 @@ #include "stackedbargroup.h" #include "bar.h" +#include "barlabel_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -64,7 +65,10 @@ void StackedBarGroup::paint(QPainter *painter, const QStyleOptionGraphicsItem *o foreach(QGraphicsItem* i, childItems()) { i->paint(painter,option,widget); } + mLayoutDirty = false; + //TODO: draw labels. } + } QRectF StackedBarGroup::boundingRect() const @@ -94,6 +98,16 @@ void StackedBarGroup::dataChanged() childItems().append(bar); } + // TODO: labels from series. This creates just some example labels + int count = mSeries.countColumns(); + for (int i=0; iset(text); + childItems().append(label); + } + + // TODO: if (autolayout) { layoutChanged() } or something mLayoutDirty = true; } @@ -118,6 +132,7 @@ void StackedBarGroup::layoutChanged() qreal tC = count+1; qreal xStep = (tW/tC); qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); + int labelIndex = mSeries.countColumns() * mSeries.countRows(); for (int column = 0; column < mSeries.countColumns(); column++) { qreal yPos = h; @@ -134,8 +149,14 @@ void StackedBarGroup::layoutChanged() itemIndex++; yPos -= barHeight; } + + // TODO: Layout for labels, remove magic number + BarLabel* label = reinterpret_cast (childItems().at(labelIndex)); + label->setPos(xPos, mHeight + 20); + labelIndex++; xPos += xStep; } + mLayoutDirty = true; } diff --git a/src/barchart/stackedbargroup.h b/src/barchart/stackedbargroup.h index 804b917..6a6d909 100644 --- a/src/barchart/stackedbargroup.h +++ b/src/barchart/stackedbargroup.h @@ -3,6 +3,7 @@ #include "charttheme_p.h" #include "chartitem_p.h" +#include "barlabel_p.h" #include "bar.h" #include "stackedbarchartseries.h" #include @@ -55,6 +56,7 @@ private: QList mColors; // List of colors for series for now ChartTheme* mTheme; +// QList mLabels; }; diff --git a/src/src.pro b/src/src.pro index 0a6ac6e..4f03d38 100644 --- a/src/src.pro +++ b/src/src.pro @@ -19,6 +19,7 @@ SOURCES += \ barchart/stackedbargroup.cpp \ barchart/percentbarchartseries.cpp \ barchart/percentbargroup.cpp \ + barchart/barlabel.cpp \ xylinechart/qxychartseries.cpp \ xylinechart/xylinechartitem.cpp \ plotdomain.cpp \ @@ -35,7 +36,8 @@ SOURCES += \ PRIVATE_HEADERS += \ xylinechart/xylinechartitem_p.h \ - plotdomain_p.h \ + barchart/barlabel_p.h \ + plotdomain_p.h \ qscatterseries_p.h \ qpieseries_p.h \ pieslice.h \