diff --git a/examples/stackedbarchartdrilldown/main.cpp b/examples/stackedbarchartdrilldown/main.cpp index b40eea7..8cf510e 100644 --- a/examples/stackedbarchartdrilldown/main.cpp +++ b/examples/stackedbarchartdrilldown/main.cpp @@ -138,9 +138,9 @@ int main(int argc, char *argv[]) //! [6] // Disable axis, since they don't really apply to bar chart - drilldownChart->axisX()->setAxisVisible(false); +// drilldownChart->axisX()->setAxisVisible(false); drilldownChart->axisX()->setGridVisible(false); - drilldownChart->axisX()->setLabelsVisible(false); +// drilldownChart->axisX()->setLabelsVisible(false); window.setCentralWidget(drilldownChart); window.resize(400, 300); diff --git a/src/barchart/barpresenter.cpp b/src/barchart/barpresenter.cpp index 2524f62..41cd6af 100644 --- a/src/barchart/barpresenter.cpp +++ b/src/barchart/barpresenter.cpp @@ -7,10 +7,9 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarPresenter::BarPresenter(QBarSeries *series, QGraphicsItem *parent) : +BarPresenter::BarPresenter(QBarSeries *series, QChart *parent) : BarPresenterBase(series, parent) { - mBarWidth = 5; } void BarPresenter::layoutChanged() diff --git a/src/barchart/barpresenter_p.h b/src/barchart/barpresenter_p.h index 383ecbd..418aa8f 100644 --- a/src/barchart/barpresenter_p.h +++ b/src/barchart/barpresenter_p.h @@ -14,7 +14,7 @@ class BarPresenter : public BarPresenterBase { Q_OBJECT public: - explicit BarPresenter(QBarSeries *series, QGraphicsItem *parent = 0); + explicit BarPresenter(QBarSeries *series, QChart *parent = 0); private: diff --git a/src/barchart/barpresenterbase.cpp b/src/barchart/barpresenterbase.cpp index 4940db6..b2103d7 100644 --- a/src/barchart/barpresenterbase.cpp +++ b/src/barchart/barpresenterbase.cpp @@ -5,19 +5,23 @@ #include "separator_p.h" #include "qbarset.h" #include "qbarseries.h" +#include "qchart.h" +#include "qchartaxis.h" +#include "qchartaxiscategories.h" #include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -BarPresenterBase::BarPresenterBase(QBarSeries *series, QGraphicsItem *parent) +BarPresenterBase::BarPresenterBase(QBarSeries *series, QChart *parent) : ChartItem(parent) - ,mBarWidth(20) // TODO: remove hard coding, when we have layout code ready ,mLayoutSet(false) ,mSeparatorsEnabled(false) ,mSeries(series) + ,mChart(parent) { connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString))); + initAxisLabels(); dataChanged(); } @@ -42,11 +46,6 @@ QRectF BarPresenterBase::boundingRect() const return QRectF(0,0,mWidth,mHeight); } -void BarPresenterBase::setBarWidth( int w ) -{ - mBarWidth = w; -} - void BarPresenterBase::dataChanged() { // TODO: performance optimizations. Do we really need to delete and create items every time data is changed or can we reuse them? @@ -76,6 +75,7 @@ void BarPresenterBase::dataChanged() } // Create labels + /* int count = mSeries->categoryCount(); for (int i=0; icategoryCount() - 1; // There is one less separator than columns + int count = mSeries->categoryCount() - 1; // There is one less separator than columns for (int i=0; isetColor(QColor(255,0,0,255)); // TODO: color for separations from theme @@ -106,6 +107,31 @@ void BarPresenterBase::dataChanged() } } +void BarPresenterBase::initAxisLabels() +{ + int count = mSeries->categoryCount(); + if (0 == count) { + return; + } + + mChart->axisX()->setTicksCount(count); + + qreal min = 0; + qreal max = mSeries->categoryCount(); + + mChart->axisX()->setMin(min); + mChart->axisX()->setMax(max); + qreal step = (max-min)/count; + QChartAxisCategories& categories = mChart->axisX()->categories(); + categories.clear(); + for (int i=0; icategoryName(i); + categories.insert(min,mSeries->categoryName(i)); + min += step; + } + mChart->axisX()->setLabelsVisible(true); +} + //handlers void BarPresenterBase::handleModelChanged(int index) @@ -116,10 +142,24 @@ void BarPresenterBase::handleModelChanged(int index) void BarPresenterBase::handleDomainChanged(const Domain& domain) { -// qDebug() << "BarPresenterBase::handleDomainChanged"; - // TODO: Figure out the use case for this. - // Affects the size of visible item, so layout is changed. -// layoutChanged(); + qDebug() << "BarPresenterBase::handleDomainChanged"; + /* + int count = mSeries->categoryCount(); + if (0 == count) { + return; + } + + // Position labels to domain + qreal min = domain.minX(); + qreal max = domain.maxX(); + qreal step = (max-min)/count; + QChartAxisCategories& categories = mChart->axisX()->categories(); + categories.clear(); + for (int i=0; icategoryName(i)); + min += step; + } + */ } void BarPresenterBase::handleGeometryChanged(const QRectF& rect) diff --git a/src/barchart/barpresenterbase_p.h b/src/barchart/barpresenterbase_p.h index 4a22852..ae64abb 100644 --- a/src/barchart/barpresenterbase_p.h +++ b/src/barchart/barpresenterbase_p.h @@ -13,6 +13,8 @@ class Bar; class BarLabel; class Separator; class BarValue; +class QChartAxisCategories; +class QChart; // Common implemantation of different presenters. Not to be instantiated. // TODO: combine this with BarPresenter and derive other presenters from it? @@ -20,7 +22,7 @@ class BarPresenterBase : public QObject, public ChartItem { Q_OBJECT public: - BarPresenterBase(QBarSeries *series, QGraphicsItem *parent = 0); + BarPresenterBase(QBarSeries *series, QChart *parent = 0); virtual ~BarPresenterBase(); public: @@ -28,13 +30,13 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QRectF boundingRect() const; - // TODO: these may change with layout awarness. - void setBarWidth( int w ); - // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes virtual void layoutChanged() = 0; // layout has changed -> need to recalculate bar sizes +protected: + void initAxisLabels(); + public slots: void handleModelChanged(int index); void handleDomainChanged(const Domain& domain); @@ -60,7 +62,7 @@ protected: QList mLabels; QList mSeparators; QList mFloatingValues; - + QChart* mChart; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/percentbarpresenter.cpp b/src/barchart/percentbarpresenter.cpp index 493df19..57651bc 100644 --- a/src/barchart/percentbarpresenter.cpp +++ b/src/barchart/percentbarpresenter.cpp @@ -9,7 +9,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QGraphicsItem *parent) : +PercentBarPresenter::PercentBarPresenter(QBarSeries *series, QChart *parent) : BarPresenterBase(series, parent) { } diff --git a/src/barchart/percentbarpresenter_p.h b/src/barchart/percentbarpresenter_p.h index 3002365..8eeb455 100644 --- a/src/barchart/percentbarpresenter_p.h +++ b/src/barchart/percentbarpresenter_p.h @@ -13,7 +13,7 @@ class PercentBarPresenter : public BarPresenterBase { Q_OBJECT public: - PercentBarPresenter(QBarSeries *series, QGraphicsItem *parent = 0); + PercentBarPresenter(QBarSeries *series, QChart *parent = 0); private: diff --git a/src/barchart/qbarseries.cpp b/src/barchart/qbarseries.cpp index b98c215..d182fb7 100644 --- a/src/barchart/qbarseries.cpp +++ b/src/barchart/qbarseries.cpp @@ -118,6 +118,7 @@ QString QBarSeries::categoryName(int category) */ void QBarSeries::setToolTipEnabled(bool enabled) { + // TODO: what if we add sets after call to this function? Those sets won't have tooltip enabled. if (enabled) { for (int i=0; ibarsetCount(); i++) { QBarSet *set = mModel->setAt(i); diff --git a/src/barchart/stackedbarpresenter.cpp b/src/barchart/stackedbarpresenter.cpp index fbe3bb3..7eabe61 100644 --- a/src/barchart/stackedbarpresenter.cpp +++ b/src/barchart/stackedbarpresenter.cpp @@ -8,7 +8,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent) : +StackedBarPresenter::StackedBarPresenter(QBarSeries *series, QChart *parent) : BarPresenterBase(series,parent) { } @@ -66,8 +66,8 @@ void StackedBarPresenter::layoutChanged() } // TODO: Layout for labels, remove magic number - BarLabel* label = mLabels.at(labelIndex); - label->setPos(xPos, mHeight + 20); +// BarLabel* label = mLabels.at(labelIndex); +// label->setPos(xPos, mHeight + 20); labelIndex++; xPos += xStep; } diff --git a/src/barchart/stackedbarpresenter_p.h b/src/barchart/stackedbarpresenter_p.h index 11ed65c..9faadbb 100644 --- a/src/barchart/stackedbarpresenter_p.h +++ b/src/barchart/stackedbarpresenter_p.h @@ -11,7 +11,7 @@ class StackedBarPresenter : public BarPresenterBase { Q_OBJECT public: - StackedBarPresenter(QBarSeries *series, QGraphicsItem *parent = 0); + StackedBarPresenter(QBarSeries *series, QChart *parent = 0); ~StackedBarPresenter(); private: