diff --git a/examples/percentbarchart/main.cpp b/examples/percentbarchart/main.cpp index a493b78..a0f21c9 100644 --- a/examples/percentbarchart/main.cpp +++ b/examples/percentbarchart/main.cpp @@ -69,7 +69,7 @@ int main(int argc, char *argv[]) //! [5] //! [6] - chartView->axisX()->setAxisVisible(false); + //chartView->axisX()->setAxisVisible(false); chartView->axisX()->setGridLineVisible(false); chartView->axisX()->setLabelsVisible(false); //! [6] diff --git a/src/axis/axisitem_p.h b/src/axis/axisitem_p.h index b1a57ea..4551e1d 100644 --- a/src/axis/axisitem_p.h +++ b/src/axis/axisitem_p.h @@ -103,15 +103,17 @@ protected: QRectF boundingRect() const { - return QGraphicsLineItem::boundingRect().adjusted(0,0,m_axis->axisType()!=Axis::X_AXIS?10:0,m_axis->axisType()!=Axis::Y_AXIS?10:0); + return shape().boundingRect(); } QPainterPath shape() const { - QPainterPath path; - path.addRect(boundingRect()); + QPainterPath path = QGraphicsLineItem::shape(); + QRectF rect = path.boundingRect(); + path.addRect(rect.adjusted(0,0,m_axis->axisType()!=Axis::X_AXIS?8:0,m_axis->axisType()!=Axis::Y_AXIS?8:0)); return path; } + private: Axis* m_axis; diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index bed5d7c..adb4a89 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -6,75 +6,13 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE Bar::Bar(QString category, QGraphicsItem *parent) - : QGraphicsObject(parent), - mXpos(0), - mYpos(0), - mWidth(0), - mHeight(0), - mBrush(QBrush()), - mPen(QPen()), + : QGraphicsRectItem(parent), mCategory(category) { setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton); setAcceptHoverEvents(true); } -void Bar::setSize(const QSizeF& size) -{ - mWidth = size.width(); - mHeight = size.height(); -} - - -void Bar::resize( qreal w, qreal h ) -{ - mWidth = w; - mHeight = h; -} - -void Bar::setPos(qreal x, qreal y) -{ - mXpos = x; - mYpos = y; -} - -void Bar::setPen(QPen pen) -{ - mPen = pen; -} - -void Bar::setBrush(QBrush brush) -{ - mBrush = brush; -} - -void Bar::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) -{ - Q_UNUSED(option) - Q_UNUSED(widget) - - if (0 == mHeight) { - return; - } - painter->setPen(mPen); - painter->setBrush(mBrush); - - // 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 -{ - QRectF r(mXpos, mYpos, mWidth, mHeight); - return r; -} - void Bar::mousePressEvent(QGraphicsSceneMouseEvent* event) { if (event->button() == Qt::LeftButton) { diff --git a/src/barchart/bar_p.h b/src/barchart/bar_p.h index c9f2c47..25479e6 100644 --- a/src/barchart/bar_p.h +++ b/src/barchart/bar_p.h @@ -2,32 +2,18 @@ #define BAR_H #include "qchartglobal.h" -#include -#include -#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE // Single visual bar item of chart -class Bar : public QGraphicsObject +class Bar : public QObject, public QGraphicsRectItem { Q_OBJECT public: Bar(QString category, QGraphicsItem *parent=0); -public: // from ChartItem - void setSize(const QSizeF &size); - - // Layout Stuff - void resize(qreal w, qreal h); - void setPos(qreal x, qreal y); - void setPen(QPen pen); - void setBrush(QBrush brush); - public: - // From QGraphicsItem - void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0); - QRectF boundingRect() const; void mousePressEvent(QGraphicsSceneMouseEvent *event); void hoverEnterEvent(QGraphicsSceneHoverEvent *event); void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); @@ -39,15 +25,6 @@ Q_SIGNALS: void hoverLeaved(); private: - - qreal mXpos; - qreal mYpos; - qreal mWidth; - qreal mHeight; - - QBrush mBrush; - QPen mPen; - QString mCategory; }; diff --git a/src/barchart/barchartitem.cpp b/src/barchart/barchartitem.cpp index 47e5b42..26569d4 100644 --- a/src/barchart/barchartitem.cpp +++ b/src/barchart/barchartitem.cpp @@ -8,6 +8,7 @@ #include "qchartaxiscategories.h" #include "chartpresenter_p.h" #include "chartanimator_p.h" +#include "chartdataset_p.h" #include #include @@ -15,8 +16,6 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : ChartItem(presenter), - mHeight(0), - mWidth(0), mLayoutSet(false), mSeries(series) { @@ -47,7 +46,7 @@ void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *opti QRectF BarChartItem::boundingRect() const { - return QRectF(0, 0, mWidth, mHeight); + return m_rect; } void BarChartItem::dataChanged() @@ -102,47 +101,46 @@ void BarChartItem::layoutChanged() return; } - // Use temporary qreals for accurancy (we might get some compiler warnings... :) - int categoryCount = mSeries->categoryCount(); - int setCount = mSeries->barsetCount(); - qreal tW = mWidth; - qreal tH = mHeight; - qreal tM = mSeries->max(); + // Use temporary qreals for accurancy (we might get some compiler warnings... :) + qreal categoryCount = mSeries->categoryCount(); + qreal setCount = mSeries->barsetCount(); + qreal max = mSeries->max(); // Domain: - if (mDomainMaxY > tM) { - tM = mDomainMaxY; - } + if (mDomainMaxY > max) { + max = mDomainMaxY; + } - qreal scale = (tH/tM); - qreal tC = categoryCount + 1; - qreal categoryWidth = tW/tC; - mBarWidth = categoryWidth / (setCount+1); + qreal width = geometry().width(); + qreal height = geometry().height(); + qreal scale = (height/max); + qreal categoryWidth = width/categoryCount; + qreal barWidth = categoryWidth / (setCount+1); int itemIndex(0); for (int category=0; category < categoryCount; category++) { - qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2; - qreal yPos = mHeight; + qreal xPos = categoryWidth * category + barWidth/2; + qreal yPos = height; for (int set = 0; set < setCount; set++) { qreal barHeight = mSeries->valueAt(set,category) * scale; Bar* bar = mBars.at(itemIndex); // TODO: width settable per bar? - bar->resize(mBarWidth, barHeight); + bar->setRect(xPos, yPos-barHeight,barWidth, barHeight); bar->setPen(mSeries->barsetAt(set)->pen()); bar->setBrush(mSeries->barsetAt(set)->brush()); - bar->setPos(xPos, yPos-barHeight); + itemIndex++; - xPos += mBarWidth; + xPos += barWidth; } } // Position floating values itemIndex = 0; for (int category=0; category < mSeries->categoryCount(); category++) { - qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth; - qreal yPos = mHeight; + qreal xPos = categoryWidth * category + categoryWidth/2 + barWidth; + qreal yPos = height; for (int set=0; set < mSeries->barsetCount(); set++) { qreal barHeight = mSeries->valueAt(set,category) * scale; BarValue* value = mFloatingValues.at(itemIndex); @@ -159,7 +157,7 @@ void BarChartItem::layoutChanged() } itemIndex++; - xPos += mBarWidth; + xPos += barWidth; } } update(); @@ -169,7 +167,7 @@ BarLayout BarChartItem::calculateLayout() { BarLayout layout; foreach(Bar* bar, mBars) { - layout.insert(bar,bar->boundingRect().size()); + layout.insert(bar,bar->boundingRect()); } return layout; @@ -177,8 +175,8 @@ BarLayout BarChartItem::calculateLayout() void BarChartItem::applyLayout(const BarLayout &layout) { - if (m_animator) - m_animator->updateLayout(this, layout); + if (animator()) + animator()->updateLayout(this, layout); else setLayout(layout); } @@ -186,7 +184,7 @@ void BarChartItem::applyLayout(const BarLayout &layout) void BarChartItem::setLayout(const BarLayout &layout) { foreach (Bar *bar, layout.keys()) { - bar->setSize(layout.value(bar)); + bar->setRect(layout.value(bar)); } update(); } @@ -198,23 +196,12 @@ void BarChartItem::initAxisLabels() return; } - mChart->axisX()->setTicksCount(count+2); + Domain* domain = presenter()->dataSet()->domain(mSeries); qreal min = 0; qreal max = count+1; - mChart->axisX()->setMin(min); - mChart->axisX()->setMax(max); - - QChartAxisCategories* categories = mChart->axisX()->categories(); - categories->clear(); - for (int i=0; iinsert(i+1,mSeries->categoryName(i)); - } - - - - mChart->axisX()->setLabelsVisible(true); + domain->setRangeX(min,max,count+1); } //handlers @@ -254,8 +241,7 @@ void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal void BarChartItem::handleGeometryChanged(const QRectF& rect) { - mWidth = rect.width(); - mHeight = rect.height(); + m_rect=rect; layoutChanged(); mLayoutSet = true; setPos(rect.topLeft()); diff --git a/src/barchart/barchartitem_p.h b/src/barchart/barchartitem_p.h index fbd282c..a61b12c 100644 --- a/src/barchart/barchartitem_p.h +++ b/src/barchart/barchartitem_p.h @@ -14,9 +14,9 @@ class BarValue; class QChartAxisCategories; class QChart; -typedef QHash BarLayout; +typedef QHash BarLayout; -class BarChartItem : public QObject, public ChartItem +class BarChartItem : public ChartItem { Q_OBJECT public: @@ -41,6 +41,8 @@ public: void applyLayout(const BarLayout &layout); void setLayout(const BarLayout &layout); + QRectF geometry() const { return m_rect;} + protected: void initAxisLabels(); @@ -56,15 +58,12 @@ public slots: protected: // TODO: consider these. - int mHeight; // Layout spesific - int mWidth; - qreal mBarWidth; - qreal mDomainMinX; qreal mDomainMaxX; qreal mDomainMinY; qreal mDomainMaxY; + QRectF m_rect; bool mLayoutSet; // True, if component has been laid out. // Not owned. diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index e61216b..17347d5 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -27,28 +27,29 @@ void PercentBarChartItem::layoutChanged() } // Use temporary qreals for accurancy (we might get some compiler warnings... :) - qreal tW = mWidth; - qreal tC = mSeries->categoryCount() + 1; - qreal cC = mSeries->categoryCount() * 2 + 1; - mBarWidth = tW / cC; - qreal xStep = (tW/tC); - qreal xPos = ((tW/tC) - mBarWidth / 2); - qreal h = mHeight; + qreal width = geometry().width(); + qreal height = geometry().height(); + + qreal categoryCount = mSeries->categoryCount(); + qreal barWidth = width / (mSeries->categoryCount() * 2); + qreal xStep = width/categoryCount; + qreal xPos = xStep/2 - barWidth / 2; int itemIndex(0); - for (int category = 0; category < mSeries->categoryCount(); category++) { + for (int category = 0; category < categoryCount ; category++) { qreal colSum = mSeries->categorySum(category); - qreal scale = (h / colSum); - qreal yPos = h; + qreal scale = (height / colSum); + qreal yPos = height; for (int set=0; set < mSeries->barsetCount(); set++) { qreal barHeight = mSeries->valueAt(set, category) * scale; Bar* bar = mBars.at(itemIndex); // TODO: width settable per bar? - bar->resize(mBarWidth, barHeight); + bar->setPen(mSeries->barsetAt(set)->pen()); + bar->setRect(xPos, yPos-barHeight,barWidth, barHeight); bar->setBrush(mSeries->barsetAt(set)->brush()); - bar->setPos(xPos, yPos-barHeight); + itemIndex++; yPos -= barHeight; } @@ -57,11 +58,11 @@ void PercentBarChartItem::layoutChanged() // Position floating values itemIndex = 0; - xPos = (tW/tC); + xPos = (width/categoryCount); for (int category=0; category < mSeries->categoryCount(); category++) { - qreal yPos = h; + qreal yPos = height; qreal colSum = mSeries->categorySum(category); - qreal scale = (h / colSum); + qreal scale = (height / colSum); for (int set=0; set < mSeries->barsetCount(); set++) { qreal barHeight = mSeries->valueAt(set,category) * scale; BarValue* value = mFloatingValues.at(itemIndex); diff --git a/src/barchart/percentbarchartitem_p.h b/src/barchart/percentbarchartitem_p.h index 46e68c4..a6eb3ae 100644 --- a/src/barchart/percentbarchartitem_p.h +++ b/src/barchart/percentbarchartitem_p.h @@ -12,7 +12,7 @@ class PercentBarChartItem : public BarChartItem { Q_OBJECT public: - PercentBarChartItem(QBarSeries *series, QChart *parent = 0); + PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter); private: diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index 402e2cd..06866bf 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -38,32 +38,31 @@ void StackedBarChartItem::layoutChanged() } // Use temporary qreals for accurancy (we might get some compiler warnings... :) + qreal maxSum = mSeries->maxCategorySum(); // Domain: if (mDomainMaxY > maxSum) { maxSum = mDomainMaxY; } - qreal h = mHeight; - qreal scale = (h / maxSum); - qreal tW = mWidth; - qreal tC = mSeries->categoryCount() + 1; - qreal cC = mSeries->categoryCount() * 2 + 1; - mBarWidth = tW / cC; - qreal xStep = (tW/tC); - qreal xPos = ((tW/tC) - mBarWidth / 2); + qreal height = geometry().height(); + qreal width = geometry().width(); + qreal scale = (height / mSeries->maxCategorySum()); + qreal categotyCount = mSeries->categoryCount(); + qreal barWidth = width / (categotyCount *2); + qreal xStep = width/categotyCount; + qreal xPos = xStep/2 - barWidth/2; + int itemIndex(0); - for (int category = 0; category < mSeries->categoryCount(); category++) { - qreal yPos = h; + for (int category = 0; category < categotyCount; category++) { + qreal yPos = height; for (int set=0; set < mSeries->barsetCount(); set++) { qreal barHeight = mSeries->valueAt(set, category) * scale; Bar* bar = mBars.at(itemIndex); - - bar->resize(mBarWidth, barHeight); bar->setPen(mSeries->barsetAt(set)->pen()); bar->setBrush(mSeries->barsetAt(set)->brush()); - bar->setPos(xPos, yPos-barHeight); + bar->setRect(xPos, yPos-barHeight,barWidth, barHeight); itemIndex++; yPos -= barHeight; } @@ -72,9 +71,9 @@ void StackedBarChartItem::layoutChanged() // Position floating values itemIndex = 0; - xPos = (tW/tC); + xPos = (width/categotyCount); for (int category=0; category < mSeries->categoryCount(); category++) { - qreal yPos = h; + qreal yPos = height; for (int set=0; set < mSeries->barsetCount(); set++) { qreal barHeight = mSeries->valueAt(set,category) * scale; BarValue* value = mFloatingValues.at(itemIndex); diff --git a/src/barchart/stackedbarchartitem_p.h b/src/barchart/stackedbarchartitem_p.h index a035b42..6e02d56 100644 --- a/src/barchart/stackedbarchartitem_p.h +++ b/src/barchart/stackedbarchartitem_p.h @@ -11,7 +11,7 @@ class StackedBarChartItem : public BarChartItem { Q_OBJECT public: - StackedBarChartItem(QBarSeries *series, QChart *parent = 0); + StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter); ~StackedBarChartItem(); private: diff --git a/src/domain.cpp b/src/domain.cpp index 8ddf727..f8677d2 100644 --- a/src/domain.cpp +++ b/src/domain.cpp @@ -35,7 +35,7 @@ void Domain::setRange(qreal minX, qreal maxX, qreal minY, qreal maxY,int tickXCo } if(m_tickXCount!=tickYCount) { - m_tickXCount=tickYCount; + m_tickYCount=tickYCount; tickYChanged=true; } @@ -74,11 +74,22 @@ void Domain::setRangeX(qreal min, qreal max) { setRange(min,max,m_minY, m_maxY); } + +void Domain::setRangeX(qreal min, qreal max, int tickCount) +{ + setRange(min,max,m_minY, m_maxY,tickCount,m_tickYCount); +} + void Domain::setRangeY(qreal min, qreal max) { setRange(m_minX, m_maxX, min, max); } +void Domain::setRangeY(qreal min, qreal max,int tickCount) +{ + setRange(m_minX, m_maxX, min, max,m_tickXCount,tickCount); +} + void Domain::setMinX(qreal min) { setRange(min, m_maxX, m_minY, m_maxY); @@ -239,7 +250,7 @@ bool operator!= (const Domain &domain1, const Domain &domain2) QDebug operator<<(QDebug dbg, const Domain &domain) { - dbg.nospace() << "Domain("< slices) PieSliceData data = sliceData(s); - if (m_animator) - m_animator->addAnimation(this, s, data, isEmpty); + if (animator()) + animator()->addAnimation(this, s, data, isEmpty); else setLayout(s, data); } @@ -155,8 +155,8 @@ void PieChartItem::applyLayout(const PieLayout &layout) void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData) { - if (m_animator) - m_animator->updateLayout(this, slice, sliceData); + if (animator()) + animator()->updateLayout(this, slice, sliceData); else setLayout(slice, sliceData); }