diff --git a/demos/chartthemes/themewidget.cpp b/demos/chartthemes/themewidget.cpp index 71bb916..47a3afe 100644 --- a/demos/chartthemes/themewidget.cpp +++ b/demos/chartthemes/themewidget.cpp @@ -251,9 +251,8 @@ QChart* ThemeWidget::createLineChart() const series->setName(name + QString::number(nameIndex)); nameIndex++; chart->addSeries(series); - chart->createDefaultAxes(); - } + chart->createDefaultAxes(); return chart; } @@ -298,8 +297,8 @@ QChart* ThemeWidget::createSplineChart() const series->setName(name + QString::number(nameIndex)); nameIndex++; chart->addSeries(series); - chart->createDefaultAxes(); } + chart->createDefaultAxes(); return chart; } @@ -318,8 +317,8 @@ QChart* ThemeWidget::createScatterChart() const series->setName(name + QString::number(nameIndex)); nameIndex++; chart->addSeries(series); - chart->createDefaultAxes(); } + chart->createDefaultAxes(); return chart; } diff --git a/src/areachart/qareaseries.cpp b/src/areachart/qareaseries.cpp index 757a3dd..22b7eeb 100644 --- a/src/areachart/qareaseries.cpp +++ b/src/areachart/qareaseries.cpp @@ -380,14 +380,24 @@ QList QAreaSeriesPrivate::createLegendMarker(QLegend* legend) } -QAbstractAxis* QAreaSeriesPrivate::createAxisX(QObject* parent) +void QAreaSeriesPrivate::initializeAxisX(QAbstractAxis* axis) { - return new QValuesAxis(parent); + Q_UNUSED(axis); } -QAbstractAxis* QAreaSeriesPrivate::createAxisY(QObject* parent) +void QAreaSeriesPrivate::initializeAxisY(QAbstractAxis* axis) { - return new QValuesAxis(parent); + Q_UNUSED(axis); +} + +QAbstractAxis::AxisType QAreaSeriesPrivate::defaultAxisXType() const +{ + return QAbstractAxis::AxisTypeValues; +} + +QAbstractAxis::AxisType QAreaSeriesPrivate::defaultAxisYType() const +{ + return QAbstractAxis::AxisTypeValues; } #include "moc_qareaseries.cpp" diff --git a/src/areachart/qareaseries_p.h b/src/areachart/qareaseries_p.h index d707b9c..c15441f 100644 --- a/src/areachart/qareaseries_p.h +++ b/src/areachart/qareaseries_p.h @@ -46,8 +46,10 @@ public: void scaleDomain(Domain& domain); Chart* createGraphics(ChartPresenter* presenter); QList createLegendMarker(QLegend* legend); - QAbstractAxis* createAxisX(QObject* parent = 0); - QAbstractAxis* createAxisY(QObject* parent = 0); + void initializeAxisX(QAbstractAxis* axis); + void initializeAxisY(QAbstractAxis* axis); + QAbstractAxis::AxisType defaultAxisXType() const; + QAbstractAxis::AxisType defaultAxisYType() const; Q_SIGNALS: void updated(); diff --git a/src/axis/qabstractaxis.h b/src/axis/qabstractaxis.h index 55c4016..e980178 100644 --- a/src/axis/qabstractaxis.h +++ b/src/axis/qabstractaxis.h @@ -48,10 +48,13 @@ class QTCOMMERCIALCHART_EXPORT QAbstractAxis : public QObject public: enum AxisType { - AxisTypeValues, - AxisTypeCategories + AxisTypeNoAxis = 0x0, + AxisTypeValues = 0x1, + AxisTypeCategories = 0x2 }; + Q_DECLARE_FLAGS(AxisTypes, AxisType) + protected: explicit QAbstractAxis(QAbstractAxisPrivate &d,QObject *parent = 0); diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 5d684b2..27c84ba 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -49,7 +49,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! \qmlclass BarSeries QAbstractBarSeries - \inherits AbstractSeries + \inherits QAbstractSeries The following QML shows how to create a simple bar chart: \snippet ../demos/qmlchart/qml/qmlchart/View6.qml 1 @@ -601,16 +601,6 @@ QList QAbstractBarSeriesPrivate::createLegendMarker(QLegend* lege return markers; } -QAbstractAxis* QAbstractBarSeriesPrivate::createAxisX(QObject* parent) -{ - return new QCategoriesAxis(parent); -} - -QAbstractAxis* QAbstractBarSeriesPrivate::createAxisY(QObject* parent) -{ - return new QValuesAxis(parent); -} - bool QAbstractBarSeriesPrivate::append(QBarSet *set) { Q_Q(QAbstractBarSeries); @@ -718,7 +708,28 @@ bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set) return true; } +void QAbstractBarSeriesPrivate::initializeAxisX(QAbstractAxis* axis) +{ + Q_UNUSED(axis); +} + +void QAbstractBarSeriesPrivate::initializeAxisY(QAbstractAxis* axis) +{ + Q_UNUSED(axis) +} + +QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisXType() const +{ + return QAbstractAxis::AxisTypeCategories; +} + +QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisYType() const +{ + return QAbstractAxis::AxisTypeValues; +} + #include "moc_qabstractbarseries.cpp" #include "moc_qabstractbarseries_p.cpp" + QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/qabstractbarseries_p.h b/src/barchart/qabstractbarseries_p.h index 0929c18..78835a9 100644 --- a/src/barchart/qabstractbarseries_p.h +++ b/src/barchart/qabstractbarseries_p.h @@ -56,8 +56,10 @@ public: Chart* createGraphics(ChartPresenter* presenter); QList createLegendMarker(QLegend* legend); - QAbstractAxis* createAxisX(QObject* parent = 0); - QAbstractAxis* createAxisY(QObject* parent = 0); + void initializeAxisX(QAbstractAxis* axis); + void initializeAxisY(QAbstractAxis* axis); + QAbstractAxis::AxisType defaultAxisXType() const; + QAbstractAxis::AxisType defaultAxisYType() const; bool append(QBarSet *set); bool remove(QBarSet *set); diff --git a/src/barchart/qpercentbarseries.cpp b/src/barchart/qpercentbarseries.cpp index 2bc58a1..ecfd514 100644 --- a/src/barchart/qpercentbarseries.cpp +++ b/src/barchart/qpercentbarseries.cpp @@ -45,7 +45,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE */ /*! \qmlclass PercentBarSeries QPercentBarSeries - \inherits BarSeries + \inherits QAbstractBarSeries The following QML shows how to create a simple percent bar chart: \snippet ../demos/qmlchart/qml/qmlchart/View9.qml 1 diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 66f840a..b472f53 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -21,6 +21,7 @@ #include "chartdataset_p.h" #include "qchart.h" #include "qvaluesaxis.h" +#include "qcategoriesaxis.h" #include "qvaluesaxis_p.h" #include "qabstractseries_p.h" #include "qabstractbarseries.h" @@ -80,42 +81,114 @@ void ChartDataSet::addSeries(QAbstractSeries* series) void ChartDataSet::createDefaultAxes() { - QMapIterator i(m_seriesDomainMap); + if(m_seriesDomainMap.isEmpty()) return; + + QAbstractAxis::AxisTypes typeX(0); + QAbstractAxis::AxisTypes typeY(0); + + QMapIterator i(m_seriesDomainMap); while (i.hasNext()) { - i.next(); + i.next(); + removeAxes(i.key()); + } - if(!m_seriesAxisXMap.value(i.key())) - { + i.toFront(); - QAbstractAxis* axisX = i.key()->d_ptr->createAxisX(this); + while (i.hasNext()) { + i.next(); + QAbstractAxis* axisX = m_seriesAxisXMap.value(i.key()); + QAbstractAxis* axisY = m_seriesAxisYMap.value(i.key()); + if(axisX) typeX&=axisX->type(); + else typeX|=i.key()->d_ptr->defaultAxisXType(); + if(axisY) typeY&=axisY->type(); + else typeY|=i.key()->d_ptr->defaultAxisYType(); + } - if(axisX) { - QObject::connect(axisX->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),i.value(),SLOT(handleAxisXChanged(qreal,qreal,int,bool))); - QObject::connect(i.value(),SIGNAL(rangeXChanged(qreal,qreal,int)),axisX->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); - axisX->d_ptr->m_orientation=Qt::Horizontal; - emit axisAdded(axisX,i.value()); - m_seriesAxisXMap.insert(i.key(),axisX); - } + if(typeX.testFlag(QAbstractAxis::AxisTypeValues) && typeX.testFlag(QAbstractAxis::AxisTypeCategories)) + { + i.toFront(); + while (i.hasNext()) { + i.next(); + QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisXType()); + i.key()->d_ptr->initializeAxisX(axis); + addAxisX(axis,i.key()); + emit axisAdded(axis,i.value()); } - if(!m_seriesAxisYMap.value(i.key())) - { - QAbstractAxis* axisY = i.key()->d_ptr->createAxisY(this); - - if(axisY) { - QObject::connect(axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),i.value(),SLOT(handleAxisYChanged(qreal,qreal,int,bool))); - QObject::connect(i.value(),SIGNAL(rangeYChanged(qreal,qreal,int)),axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); - axisY->d_ptr->m_orientation=Qt::Vertical; - emit axisAdded(axisY,i.value()); - m_seriesAxisYMap.insert(i.key(),axisY); - } + }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){ + QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeX))); + i.toFront(); + while (i.hasNext()) { + i.next(); + i.key()->d_ptr->initializeAxisX(axis); + addAxisX(axis,i.key()); + } + emit axisAdded(axis,i.value()); + + } + + if(typeY.testFlag(QAbstractAxis::AxisTypeValues) && typeY.testFlag(QAbstractAxis::AxisTypeCategories)) + { + i.toFront(); + while (i.hasNext()) { + i.next(); + QAbstractAxis* axis = createAxis(i.key()->d_ptr->defaultAxisYType()); + i.key()->d_ptr->initializeAxisY(axis); + addAxisY(axis,i.key()); + emit axisAdded(axis,i.value()); } + + }else if(!typeY.testFlag(QAbstractAxis::AxisTypeNoAxis)){ + QAbstractAxis* axis = createAxis(QAbstractAxis::AxisType(int(typeY))); + i.toFront(); + while (i.hasNext()) { + i.next(); + i.key()->d_ptr->initializeAxisY(axis); + addAxisY(axis,i.key()); + } + emit axisAdded(axis,i.value()); + } } +QAbstractAxis* ChartDataSet::createAxis(QAbstractAxis::AxisType type) +{ + QAbstractAxis* axis =0; + + switch(type) { + case QAbstractAxis::AxisTypeValues: + axis = new QValuesAxis(this); + break; + case QAbstractAxis::AxisTypeCategories: + axis = new QCategoriesAxis(this); + break; + default: + axis = 0; + break; + } + + return axis; +} + +void ChartDataSet::addAxisX(QAbstractAxis* axis,QAbstractSeries* series) { + Domain* domain = m_seriesDomainMap.value(series); + QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); + QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); + axis->d_ptr->m_orientation=Qt::Horizontal; + m_seriesAxisXMap.insert(series,axis); +} + +void ChartDataSet::addAxisY(QAbstractAxis* axis,QAbstractSeries* series) { + Domain* domain = m_seriesDomainMap.value(series); + QObject::connect(axis->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); + QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axis->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); + axis->d_ptr->m_orientation=Qt::Vertical; + m_seriesAxisYMap.insert(series,axis); +} + void ChartDataSet::removeSeries(QAbstractSeries* series) { Domain* domain = m_seriesDomainMap.take(series); @@ -138,6 +211,11 @@ void ChartDataSet::removeSeries(QAbstractSeries* series) series->d_ptr->m_chart = 0; series->d_ptr->m_dataset = 0; + removeAxes(series); +} + +void ChartDataSet::removeAxes(QAbstractSeries* series) +{ QAbstractAxis* axisX = m_seriesAxisXMap.take(series); if(axisX) { diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index 7adde54..9cd3ded 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -32,6 +32,7 @@ #include "qabstractseries.h" #include "domain_p.h" +#include "qabstractaxis_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -76,8 +77,11 @@ Q_SIGNALS: void axisRemoved(QAbstractAxis* axis); private: - QStringList createLabels(QAbstractAxis* axis,qreal min, qreal max); void calculateDomain(QAbstractSeries* series,Domain* domain); + QAbstractAxis* createAxis(QAbstractAxis::AxisType type); + void addAxisX(QAbstractAxis* axis,QAbstractSeries* series); + void addAxisY(QAbstractAxis* axis,QAbstractSeries* series); + void removeAxes(QAbstractSeries* series); private: QMap m_seriesAxisXMap; diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index 1f979e3..0e85f53 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -755,16 +755,24 @@ QList QPieSeriesPrivate::createLegendMarker(QLegend* legend) return markers; } -QAbstractAxis* QPieSeriesPrivate::createAxisX(QObject* parent) +void QPieSeriesPrivate::initializeAxisX(QAbstractAxis* axis) { - Q_UNUSED(parent); - return 0; + Q_UNUSED(axis); } -QAbstractAxis* QPieSeriesPrivate::createAxisY(QObject* parent) +void QPieSeriesPrivate::initializeAxisY(QAbstractAxis* axis) { - Q_UNUSED(parent); - return 0; + Q_UNUSED(axis); +} + +QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisXType() const +{ + return QAbstractAxis::AxisTypeNoAxis; +} + +QAbstractAxis::AxisType QPieSeriesPrivate::defaultAxisYType() const +{ + return QAbstractAxis::AxisTypeNoAxis; } #include "moc_qpieseries.cpp" diff --git a/src/piechart/qpieseries_p.h b/src/piechart/qpieseries_p.h index 68c5f17..b3f457b 100644 --- a/src/piechart/qpieseries_p.h +++ b/src/piechart/qpieseries_p.h @@ -47,8 +47,10 @@ public: void scaleDomain(Domain& domain); Chart* createGraphics(ChartPresenter *presenter); QList createLegendMarker(QLegend *legend); - QAbstractAxis* createAxisX(QObject* parent); - QAbstractAxis* createAxisY(QObject* parent); + void initializeAxisX(QAbstractAxis* axis); + void initializeAxisY(QAbstractAxis* axis); + QAbstractAxis::AxisType defaultAxisXType() const; + QAbstractAxis::AxisType defaultAxisYType() const; void updateDerivativeData(); diff --git a/src/qabstractseries.h b/src/qabstractseries.h index aaee47c..c7e54d9 100644 --- a/src/qabstractseries.h +++ b/src/qabstractseries.h @@ -22,6 +22,7 @@ #define QABSTRACTSERIES_H #include +#include #include #include @@ -57,6 +58,7 @@ protected: public: ~QAbstractSeries(); virtual SeriesType type() const = 0; + void setName(const QString& name); QString name() const; void setVisible(bool visible = true); diff --git a/src/qabstractseries_p.h b/src/qabstractseries_p.h index c34e541..7052b48 100644 --- a/src/qabstractseries_p.h +++ b/src/qabstractseries_p.h @@ -52,8 +52,10 @@ public: virtual void scaleDomain(Domain& domain) = 0; virtual Chart* createGraphics(ChartPresenter* presenter) = 0; virtual QList createLegendMarker(QLegend* legend) = 0; - virtual QAbstractAxis* createAxisX(QObject* parent) = 0; - virtual QAbstractAxis* createAxisY(QObject* parent) = 0; + virtual void initializeAxisX(QAbstractAxis* axis) = 0; + virtual void initializeAxisY(QAbstractAxis* axis) = 0; + virtual QAbstractAxis::AxisType defaultAxisXType() const = 0; + virtual QAbstractAxis::AxisType defaultAxisYType() const = 0; protected: QAbstractSeries *q_ptr; diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index d9a0e4c..4f9c25c 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -435,14 +435,24 @@ QList QXYSeriesPrivate::createLegendMarker(QLegend* legend) return list << new XYLegendMarker(q,legend); } -QAbstractAxis* QXYSeriesPrivate::createAxisX(QObject* parent) +void QXYSeriesPrivate::initializeAxisX(QAbstractAxis* axis) { - return new QValuesAxis(parent); + Q_UNUSED(axis); } -QAbstractAxis* QXYSeriesPrivate::createAxisY(QObject* parent) +void QXYSeriesPrivate::initializeAxisY(QAbstractAxis* axis) { - return new QValuesAxis(parent); + Q_UNUSED(axis); +} + +QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisXType() const +{ + return QAbstractAxis::AxisTypeValues; +} + +QAbstractAxis::AxisType QXYSeriesPrivate::defaultAxisYType() const +{ + return QAbstractAxis::AxisTypeValues; } #include "moc_qxyseries.cpp" diff --git a/src/xychart/qxyseries_p.h b/src/xychart/qxyseries_p.h index 67fd7cd..2ca80c9 100644 --- a/src/xychart/qxyseries_p.h +++ b/src/xychart/qxyseries_p.h @@ -47,8 +47,10 @@ public: void scaleDomain(Domain& domain); QList createLegendMarker(QLegend* legend); - QAbstractAxis* createAxisX(QObject* parent); - QAbstractAxis* createAxisY(QObject* parent); + void initializeAxisX(QAbstractAxis* axis); + void initializeAxisY(QAbstractAxis* axis); + QAbstractAxis::AxisType defaultAxisXType() const; + QAbstractAxis::AxisType defaultAxisYType() const; Q_SIGNALS: void updated(); diff --git a/tests/auto/chartdataset/tst_chartdataset.cpp b/tests/auto/chartdataset/tst_chartdataset.cpp index 9b6250e..66eebe9 100644 --- a/tests/auto/chartdataset/tst_chartdataset.cpp +++ b/tests/auto/chartdataset/tst_chartdataset.cpp @@ -541,6 +541,8 @@ void tst_ChartDataSet::zoomInDomain() */ m_dataset->createDefaultAxes(); + + QList spyList; foreach(QAbstractSeries* series, seriesList) {