diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index ef304a8..ed36d0e 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -20,7 +20,7 @@ void Bar::setPlotDomain(const PlotDomain& data) mPlotDomain = data; } -void Bar::resize( int w, int h ) +void Bar::resize( qreal w, qreal h ) { // qDebug() << "bar::resize" << w << h; mWidth = w; @@ -46,7 +46,15 @@ void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidg // TODO: accept brush instead of color QBrush brush(mColor); painter->setBrush(brush); - painter->drawRect(mXpos-mWidth, mYpos-mHeight ,mWidth ,mHeight); // Evil inverse rect, because we want bars to grow from bottom to top :) + + // This compensates for rounding errors. drawRect takes ints and cumulative error of pos + size may be over 1. + int x0 = mXpos; + int x1 = (mXpos + mWidth); + int w = x1-x0; + int y0 = mYpos; + int y1 = (mYpos + mHeight); + int h = y1-y0; + painter->drawRect(x0, y0 ,w ,h); } QRectF Bar::boundingRect() const diff --git a/src/barchart/bar.h b/src/barchart/bar.h index d5fc43c..7a8e956 100644 --- a/src/barchart/bar.h +++ b/src/barchart/bar.h @@ -13,12 +13,12 @@ class Bar : public ChartItem public: Bar(QGraphicsItem *parent=0); -public: // from ChartItemControl +public: // from ChartItem void setSize(const QSize &size); void setPlotDomain(const PlotDomain& data); // Layout Stuff - void resize( int w, int h ); // Size of bar. in screen coordinates. + void resize( qreal w, qreal h ); // Size of bar. void setColor( QColor col ); // Color of bar void setPos(qreal x, qreal y); @@ -30,8 +30,8 @@ public: private: - int mHeight; - int mWidth; + qreal mHeight; + qreal mWidth; qreal mXpos; qreal mYpos; QColor mColor; diff --git a/src/barchart/bargroup.cpp b/src/barchart/bargroup.cpp index 9aefa45..113e498 100644 --- a/src/barchart/bargroup.cpp +++ b/src/barchart/bargroup.cpp @@ -119,6 +119,7 @@ void BarGroup::layoutChanged() int itemIndex(0); for (int column=0; column < columnCount; column++) { qreal xPos = xStepPerSeries * column + ((tW + mBarDefaultWidth*rowCount)/(columnCount*2)); + qreal yPos = mHeight; for (int row = 0; row < rowCount; row++) { qreal barHeight = mSeries.valueAt(row, column) * scale; Bar* bar = reinterpret_cast (childItems().at(itemIndex)); @@ -126,7 +127,7 @@ void BarGroup::layoutChanged() // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); bar->setColor(mColors.at(row)); - bar->setPos(xPos, mHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); + bar->setPos(xPos, yPos-barHeight); // item*posStep+startPos + series * mBarDefaultWidth, mHeight); itemIndex++; xPos += mBarDefaultWidth; } diff --git a/src/barchart/percentbargroup.cpp b/src/barchart/percentbargroup.cpp index 72ec026..3d1347e 100644 --- a/src/barchart/percentbargroup.cpp +++ b/src/barchart/percentbargroup.cpp @@ -121,7 +121,7 @@ void PercentBarGroup::layoutChanged() // TODO: width settable per bar? bar->resize(mBarDefaultWidth, barHeight); bar->setColor(mColors.at(row)); - bar->setPos(xPos, yPos); + bar->setPos(xPos, yPos-barHeight); itemIndex++; yPos -= barHeight; } diff --git a/src/barchart/stackedbargroup.cpp b/src/barchart/stackedbargroup.cpp index 37d5835..995f229 100644 --- a/src/barchart/stackedbargroup.cpp +++ b/src/barchart/stackedbargroup.cpp @@ -10,6 +10,7 @@ StackedBarGroup::StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *p ,mLayoutSet(false) ,mLayoutDirty(true) ,mBarDefaultWidth(20) // TODO: remove hard coding, when we have layout code ready + ,mTheme(0) { dataChanged(); } @@ -30,6 +31,11 @@ void StackedBarGroup::setPlotDomain(const PlotDomain& data) // TODO: } +void StackedBarGroup::themeChanged(ChartTheme *theme) +{ + mTheme = theme; +} + void StackedBarGroup::setBarWidth( int w ) { mBarDefaultWidth = w; @@ -111,7 +117,7 @@ void StackedBarGroup::layoutChanged() qreal tW = mWidth; qreal tC = count+1; qreal xStep = (tW/tC); - qreal xPos = ((tW/tC) + mBarDefaultWidth / 2); + qreal xPos = ((tW/tC) - mBarDefaultWidth / 2); for (int column = 0; column < mSeries.countColumns(); column++) { qreal yPos = h; @@ -120,9 +126,11 @@ void StackedBarGroup::layoutChanged() Bar* bar = reinterpret_cast (childItems().at(itemIndex)); // TODO: width settable per bar? + // TODO: how to get color for series(x) from theme? +// mTheme->themeForSeries(); bar->resize(mBarDefaultWidth, barHeight); bar->setColor(mColors.at(row)); - bar->setPos(xPos, yPos); + bar->setPos(xPos, yPos-barHeight); itemIndex++; yPos -= barHeight; } diff --git a/src/barchart/stackedbargroup.h b/src/barchart/stackedbargroup.h index c485a00..804b917 100644 --- a/src/barchart/stackedbargroup.h +++ b/src/barchart/stackedbargroup.h @@ -1,6 +1,7 @@ #ifndef STACKEDBARGROUP_H #define STACKEDBARGROUP_H +#include "charttheme_p.h" #include "chartitem_p.h" #include "bar.h" #include "stackedbarchartseries.h" @@ -9,7 +10,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE // TODO: derive this from ChartObjectInterface, when setSize is back in ChartItem -class StackedBarGroup : public ChartItem +class StackedBarGroup : public ChartItem, public ChartThemeObserver { public: StackedBarGroup(StackedBarChartSeries& series, QGraphicsItem *parent = 0); @@ -18,6 +19,9 @@ public: // From ChartItem void setSize(const QSize &size); void setPlotDomain(const PlotDomain& data); + // From ChartThemeObserver + void themeChanged(ChartTheme *theme); + public: // Layout "api" void setPos(qreal x, qreal y); void setBarWidth( int w ); // Default width for each bar @@ -50,6 +54,8 @@ private: QList mColors; // List of colors for series for now + ChartTheme* mTheme; + }; QTCOMMERCIALCHART_END_NAMESPACE