From 779eeed6a4020fada8052b9cd3bc83d1760d3dd6 2012-07-03 08:05:20 From: Michal Klocek Date: 2012-07-03 08:05:20 Subject: [PATCH] Refactor QChart API * adds createDefautAxes * updates examples * small adjustem in axis implementation --- diff --git a/demos/chartthemes/themewidget.cpp b/demos/chartthemes/themewidget.cpp index 0fb8391..5c8bdc3 100644 --- a/demos/chartthemes/themewidget.cpp +++ b/demos/chartthemes/themewidget.cpp @@ -228,7 +228,7 @@ QChart* ThemeWidget::createBarChart(int valueCount) const series->append(set); } chart->addSeries(series); - chart->setAxisX(series,axis); + chart->createDefaultAxes(); return chart; } @@ -249,6 +249,8 @@ QChart* ThemeWidget::createLineChart() const series->setName(name + QString::number(nameIndex)); nameIndex++; chart->addSeries(series); + chart->createDefaultAxes(); + } return chart; diff --git a/examples/areachart/main.cpp b/examples/areachart/main.cpp index 0b86490..cb788b5 100644 --- a/examples/areachart/main.cpp +++ b/examples/areachart/main.cpp @@ -60,6 +60,7 @@ int main(int argc, char *argv[]) QChart* chart = new QChart(); chart->addSeries(series); chart->setTitle("Simple areachart example"); + chart->createDefaultAxes(); chart->axisX()->setRange(0, 20); chart->axisY()->setRange(0, 10); //![4] diff --git a/examples/barmodelmapper/tablewidget.cpp b/examples/barmodelmapper/tablewidget.cpp index 3a30fea..6168712 100644 --- a/examples/barmodelmapper/tablewidget.cpp +++ b/examples/barmodelmapper/tablewidget.cpp @@ -90,7 +90,7 @@ TableWidget::TableWidget(QWidget *parent) categories << "April" << "May" << "June" << "July" << "August"; QCategoriesAxis* axis = new QCategoriesAxis(); axis->append(categories); - chart->setAxisX(series,axis); + chart->createDefaultAxes(); //! [6] //! [7] diff --git a/examples/groupedbarchart/main.cpp b/examples/groupedbarchart/main.cpp index 264532e..175380d 100644 --- a/examples/groupedbarchart/main.cpp +++ b/examples/groupedbarchart/main.cpp @@ -67,7 +67,7 @@ int main(int argc, char *argv[]) categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; QCategoriesAxis* axis = new QCategoriesAxis(); axis->append(categories); - chart->setAxisX(series,axis); + chart->createDefaultAxes(); //![4] //![5] diff --git a/examples/linechart/main.cpp b/examples/linechart/main.cpp index 9b2e0fe..df09d6a 100644 --- a/examples/linechart/main.cpp +++ b/examples/linechart/main.cpp @@ -46,6 +46,7 @@ int main(int argc, char *argv[]) QChart* chart = new QChart(); chart->legend()->hide(); chart->addSeries(series); + chart->createDefaultAxes(); chart->setTitle("Simple line chart example"); //![3] @@ -54,6 +55,7 @@ int main(int argc, char *argv[]) chartView->setRenderHint(QPainter::Antialiasing); //![4] + //![5] QMainWindow window; window.setCentralWidget(chartView); diff --git a/examples/percentbarchart/main.cpp b/examples/percentbarchart/main.cpp index 5d95c90..017d01d 100644 --- a/examples/percentbarchart/main.cpp +++ b/examples/percentbarchart/main.cpp @@ -66,7 +66,8 @@ int main(int argc, char *argv[]) categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; QCategoriesAxis* axis = new QCategoriesAxis(); axis->append(categories); - chart->setAxisX(series,axis); + chart->createDefaultAxes(); + chart->setAxisX(axis,series); //![4] //![5] diff --git a/examples/stackedbarchart/main.cpp b/examples/stackedbarchart/main.cpp index 3d16107..f97da22 100644 --- a/examples/stackedbarchart/main.cpp +++ b/examples/stackedbarchart/main.cpp @@ -66,10 +66,7 @@ int main(int argc, char *argv[]) categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun"; QCategoriesAxis* axis = new QCategoriesAxis(); axis->append(categories); - chart->setAxisX(series,axis); -// axis->setMinCategory(QString("Feb")); -// axis->setMaxCategory(QString("May")); -// axis->setRange(QString("Feb"), QString("Apr")); + chart->setAxisX(axis,series); //![4] //![5] diff --git a/examples/stackedbarchartdrilldown/drilldownchart.cpp b/examples/stackedbarchartdrilldown/drilldownchart.cpp index c9d2c40..ea8f65c 100644 --- a/examples/stackedbarchartdrilldown/drilldownchart.cpp +++ b/examples/stackedbarchartdrilldown/drilldownchart.cpp @@ -43,7 +43,7 @@ void DrilldownChart::changeSeries(DrilldownBarSeries *series) addSeries(series); - setAxisX(series,axis); + createDefaultAxes(); setTitle(series->name()); } diff --git a/plugins/declarative/declarativeareaseries.cpp b/plugins/declarative/declarativeareaseries.cpp index 3ca63b1..1ec2f37 100644 --- a/plugins/declarative/declarativeareaseries.cpp +++ b/plugins/declarative/declarativeareaseries.cpp @@ -32,7 +32,7 @@ DeclarativeAreaSeries::DeclarativeAreaSeries(QObject *parent) : void DeclarativeAreaSeries::setAxisX(QAbstractAxis *axis) { - chart()->setAxisX(this, axis); + chart()->setAxisX(axis,this); } QAbstractAxis *DeclarativeAreaSeries::axisX() @@ -42,7 +42,7 @@ QAbstractAxis *DeclarativeAreaSeries::axisX() void DeclarativeAreaSeries::setAxisY(QAbstractAxis *axis) { - chart()->setAxisY(this, axis); + chart()->setAxisY(axis,this); } QAbstractAxis *DeclarativeAreaSeries::axisY() diff --git a/plugins/declarative/declarativebarseries.cpp b/plugins/declarative/declarativebarseries.cpp index ee575c6..457d799 100644 --- a/plugins/declarative/declarativebarseries.cpp +++ b/plugins/declarative/declarativebarseries.cpp @@ -106,7 +106,7 @@ void DeclarativeBarSeries::componentComplete() void DeclarativeBarSeries::setAxisX(QAbstractAxis *axis) { - chart()->setAxisX(this, axis); + chart()->setAxisX(axis, this); } QAbstractAxis *DeclarativeBarSeries::axisX() @@ -116,7 +116,7 @@ QAbstractAxis *DeclarativeBarSeries::axisX() void DeclarativeBarSeries::setAxisY(QAbstractAxis *axis) { - chart()->setAxisY(this, axis); + chart()->setAxisY(axis, this); } QAbstractAxis *DeclarativeBarSeries::axisY() @@ -188,7 +188,7 @@ void DeclarativeGroupedBarSeries::componentComplete() void DeclarativeGroupedBarSeries::setAxisX(QAbstractAxis *axis) { - chart()->setAxisX(this, axis); + chart()->setAxisX(axis, this); } QAbstractAxis *DeclarativeGroupedBarSeries::axisX() @@ -198,7 +198,7 @@ QAbstractAxis *DeclarativeGroupedBarSeries::axisX() void DeclarativeGroupedBarSeries::setAxisY(QAbstractAxis *axis) { - chart()->setAxisY(this, axis); + chart()->setAxisY(axis, this); } QAbstractAxis *DeclarativeGroupedBarSeries::axisY() @@ -264,7 +264,7 @@ void DeclarativeStackedBarSeries::componentComplete() void DeclarativeStackedBarSeries::setAxisX(QAbstractAxis *axis) { - chart()->setAxisX(this, axis); + chart()->setAxisX(axis, this); } QAbstractAxis *DeclarativeStackedBarSeries::axisX() @@ -274,7 +274,7 @@ QAbstractAxis *DeclarativeStackedBarSeries::axisX() void DeclarativeStackedBarSeries::setAxisY(QAbstractAxis *axis) { - chart()->setAxisY(this, axis); + chart()->setAxisY(axis, this); } QAbstractAxis *DeclarativeStackedBarSeries::axisY() @@ -340,7 +340,7 @@ void DeclarativePercentBarSeries::componentComplete() void DeclarativePercentBarSeries::setAxisX(QAbstractAxis *axis) { - chart()->setAxisX(this, axis); + chart()->setAxisX(axis, this); } QAbstractAxis *DeclarativePercentBarSeries::axisX() @@ -350,7 +350,7 @@ QAbstractAxis *DeclarativePercentBarSeries::axisX() void DeclarativePercentBarSeries::setAxisY(QAbstractAxis *axis) { - chart()->setAxisY(this, axis); + chart()->setAxisY(axis, this); } QAbstractAxis *DeclarativePercentBarSeries::axisY() diff --git a/plugins/declarative/declarativelineseries.cpp b/plugins/declarative/declarativelineseries.cpp index 6428964..3fe15ff 100644 --- a/plugins/declarative/declarativelineseries.cpp +++ b/plugins/declarative/declarativelineseries.cpp @@ -39,7 +39,7 @@ QXYSeries *DeclarativeLineSeries::xySeries() void DeclarativeLineSeries::setAxisX(QAbstractAxis *axis) { - chart()->setAxisX(this, axis); + chart()->setAxisX(axis,this); } QAbstractAxis *DeclarativeLineSeries::axisX() @@ -49,7 +49,7 @@ QAbstractAxis *DeclarativeLineSeries::axisX() void DeclarativeLineSeries::setAxisY(QAbstractAxis *axis) { - chart()->setAxisY(this, axis); + chart()->setAxisY(axis,this); } QAbstractAxis *DeclarativeLineSeries::axisY() diff --git a/plugins/declarative/declarativescatterseries.cpp b/plugins/declarative/declarativescatterseries.cpp index ed1c3f8..3b2bc6c 100644 --- a/plugins/declarative/declarativescatterseries.cpp +++ b/plugins/declarative/declarativescatterseries.cpp @@ -39,7 +39,7 @@ QXYSeries *DeclarativeScatterSeries::xySeries() void DeclarativeScatterSeries::setAxisX(QAbstractAxis *axis) { - chart()->setAxisX(this, axis); + chart()->setAxisX(axis,this); } QAbstractAxis *DeclarativeScatterSeries::axisX() @@ -49,7 +49,7 @@ QAbstractAxis *DeclarativeScatterSeries::axisX() void DeclarativeScatterSeries::setAxisY(QAbstractAxis *axis) { - chart()->setAxisY(this, axis); + chart()->setAxisY(axis,this); } QAbstractAxis *DeclarativeScatterSeries::axisY() diff --git a/plugins/declarative/declarativesplineseries.cpp b/plugins/declarative/declarativesplineseries.cpp index 2285424..636d3bb 100644 --- a/plugins/declarative/declarativesplineseries.cpp +++ b/plugins/declarative/declarativesplineseries.cpp @@ -39,7 +39,7 @@ QXYSeries *DeclarativeSplineSeries::xySeries() void DeclarativeSplineSeries::setAxisX(QAbstractAxis *axis) { - chart()->setAxisX(this, axis); + chart()->setAxisX(axis,this); } QAbstractAxis *DeclarativeSplineSeries::axisX() @@ -49,7 +49,7 @@ QAbstractAxis *DeclarativeSplineSeries::axisX() void DeclarativeSplineSeries::setAxisY(QAbstractAxis *axis) { - chart()->setAxisY(this, axis); + chart()->setAxisY(axis,this); } QAbstractAxis *DeclarativeSplineSeries::axisY() diff --git a/src/axis/chartaxis.cpp b/src/axis/chartaxis.cpp index 8f744d5..9bbb3f5 100644 --- a/src/axis/chartaxis.cpp +++ b/src/axis/chartaxis.cpp @@ -25,7 +25,7 @@ #include "chartanimator_p.h" #include #include -#include +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -143,21 +143,6 @@ void ChartAxis::updateLayout(QVector &layout) } } -bool ChartAxis::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const -{ - Q_ASSERT(max>min); - Q_ASSERT(ticks>1); - - int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); - n++; - for (int i=0; i< ticks; i++) { - qreal value = min + (i * (max - min)/ (ticks-1)); - Q_UNUSED(value); - labels << QString::number(value,'f',n); - } - return true; -} - void ChartAxis::setAxisOpacity(qreal opacity) { m_axis->setOpacity(opacity); @@ -354,6 +339,41 @@ void ChartAxis::axisSelected() qDebug()<<"TODO: axis clicked"; } + +void ChartAxis::createNumberLabels(QStringList &labels,qreal min, qreal max, int ticks) const +{ + Q_ASSERT(max>min); + Q_ASSERT(ticks>1); + + int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); + n++; + for (int i=0; i< ticks; i++) { + qreal value = min + (i * (max - min)/ (ticks-1)); + labels << QString::number(value,'f',n); + } +} + +void ChartAxis::createCategoryLabels(QStringList &labels,qreal min, qreal max,const QStringList &categories) const +{ + Q_ASSERT(max>min); + Q_UNUSED(max); + + int x = qCeil(min); + int count = 0; + + // Try to find category for x coordinate + while (count < m_ticksCount) { + if ((x < categories.count()) && (x >= 0)) { + labels << categories.at(x); + } else { + // No label for x coordinate + labels << ""; + } + x++; + count++; + } +} + #include "moc_chartaxis_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/chartaxis_p.h b/src/axis/chartaxis_p.h index 4ebdce5..b312b62 100644 --- a/src/axis/chartaxis_p.h +++ b/src/axis/chartaxis_p.h @@ -91,7 +91,8 @@ public: protected: virtual void updateGeometry() = 0; virtual QVector calculateLayout() const = 0; - virtual bool createLabels(QStringList &labels,qreal min, qreal max,int ticks) const; + void createNumberLabels(QStringList &labels,qreal min, qreal max,int ticks) const; + void createCategoryLabels(QStringList &labels,qreal min, qreal max,const QStringList &categories) const; public Q_SLOTS: void handleAxisUpdated(); diff --git a/src/axis/chartcategoriesaxisx.cpp b/src/axis/chartcategoriesaxisx.cpp index 4fbcfc0..aff32fb 100644 --- a/src/axis/chartcategoriesaxisx.cpp +++ b/src/axis/chartcategoriesaxisx.cpp @@ -26,7 +26,6 @@ #include #include #include -#include static int label_padding = 5; @@ -35,36 +34,11 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE ChartCategoriesAxisX::ChartCategoriesAxisX(QCategoriesAxis *axis,ChartPresenter *presenter) : ChartAxis(axis,presenter), m_categoriesAxis(axis) { -} -ChartCategoriesAxisX::~ChartCategoriesAxisX() -{ } -bool ChartCategoriesAxisX::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const +ChartCategoriesAxisX::~ChartCategoriesAxisX() { - Q_ASSERT(max>min); - Q_UNUSED(ticks); - Q_UNUSED(max); - - QStringList categories = m_categoriesAxis->categories(); - - int x = qCeil(min); - int count = 0; - - // Try to find category for x coordinate - while (count < ticks) { - if ((x < categories.count()) && (x >= 0)) { - labels << categories.at(x); - } else { - // No label for x coordinate - labels << ""; - } - x++; - count++; - } - - return true; } QVector ChartCategoriesAxisX::calculateLayout() const @@ -94,7 +68,7 @@ void ChartCategoriesAxisX::updateGeometry() QStringList ticksList; - createLabels(ticksList,m_min,m_max,layout.size()); + createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories()); QList lines = m_grid->childItems(); QList labels = m_labels->childItems(); diff --git a/src/axis/chartcategoriesaxisx_p.h b/src/axis/chartcategoriesaxisx_p.h index 737b6e2..ebacb24 100644 --- a/src/axis/chartcategoriesaxisx_p.h +++ b/src/axis/chartcategoriesaxisx_p.h @@ -45,7 +45,6 @@ public: ~ChartCategoriesAxisX(); AxisType axisType() const { return X_AXIS;} - bool createLabels(QStringList &labels,qreal min, qreal max,int ticks) const; protected: QVector calculateLayout() const; diff --git a/src/axis/chartcategoriesaxisy.cpp b/src/axis/chartcategoriesaxisy.cpp index 22881e1..535164d 100644 --- a/src/axis/chartcategoriesaxisy.cpp +++ b/src/axis/chartcategoriesaxisy.cpp @@ -25,6 +25,7 @@ #include #include #include +#include static int label_padding = 5; @@ -65,7 +66,7 @@ void ChartCategoriesAxisY::updateGeometry() QStringList ticksList; - bool categories = createLabels(ticksList,m_min,m_max,layout.size()); + createCategoryLabels(ticksList,m_min,m_max,m_categoriesAxis->categories()); QList lines = m_grid->childItems(); QList labels = m_labels->childItems(); @@ -78,38 +79,18 @@ void ChartCategoriesAxisY::updateGeometry() QGraphicsLineItem *lineItem = static_cast(axis.at(0)); lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); - qreal width = 0; - for (int i = 0; i < layout.size(); ++i) { + for (int i = 1; i < layout.size(); ++i) { QGraphicsLineItem *lineItem = static_cast(lines.at(i)); lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - if (!categories || i<1) { - labelItem->setText(ticksList.at(i)); - const QRectF& rect = labelItem->boundingRect(); - QPointF center = rect.center(); - labelItem->setTransformOriginPoint(center.x(), center.y()); - labelItem->setPos(layout[i] - center.x(), m_rect.bottom() + label_padding); - - if(labelItem->pos().x()<=width){ - labelItem->setVisible(false); - lineItem->setVisible(false); - }else{ - labelItem->setVisible(true); - lineItem->setVisible(true); - width=rect.width()+labelItem->pos().x(); - } - m_minWidth+=rect.width(); - m_minHeight=qMax(rect.height(),m_minHeight); - } - else { - labelItem->setText(ticksList.at(i)); - const QRectF& rect = labelItem->boundingRect(); - QPointF center = rect.center(); - labelItem->setTransformOriginPoint(center.x(), center.y()); - labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding); - m_minWidth+=rect.width(); - m_minHeight=qMax(rect.height()+label_padding,m_minHeight); - } + + labelItem->setText(ticksList.at(i)); + const QRectF& rect = labelItem->boundingRect(); + QPointF center = rect.center(); + labelItem->setTransformOriginPoint(center.x(), center.y()); + labelItem->setPos(layout[i] - (layout[i] - layout[i-1])/2 - center.x(), m_rect.bottom() + label_padding); + m_minWidth+=rect.width(); + m_minHeight=qMax(rect.height()+label_padding,m_minHeight); if ((i+1)%2 && i>1) { QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); diff --git a/src/axis/chartcategoriesaxisy_p.h b/src/axis/chartcategoriesaxisy_p.h index 6e06dd0..c282e7e 100644 --- a/src/axis/chartcategoriesaxisy_p.h +++ b/src/axis/chartcategoriesaxisy_p.h @@ -35,6 +35,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QAbstractAxis; +class QCategoriesAxis; class ChartPresenter; class ChartCategoriesAxisY : public ChartAxis @@ -48,6 +49,9 @@ public: protected: QVector calculateLayout() const; void updateGeometry(); + +private: + QCategoriesAxis *m_categoriesAxis; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/chartvaluesaxisx.cpp b/src/axis/chartvaluesaxisx.cpp index 21c8f52..f086cea 100644 --- a/src/axis/chartvaluesaxisx.cpp +++ b/src/axis/chartvaluesaxisx.cpp @@ -39,21 +39,6 @@ ChartValuesAxisX::~ChartValuesAxisX() { } -bool ChartValuesAxisX::createLabels(QStringList &labels,qreal min, qreal max,int ticks) const -{ - Q_ASSERT(max>min); - Q_ASSERT(ticks>1); - - int n = qMax(int(-floor(log10((max-min)/(ticks-1)))),0); - n++; - for (int i=0; i< ticks; i++) { - qreal value = min + (i * (max - min)/ (ticks-1)); - Q_UNUSED(value); - labels << QString::number(value,'f',n); - } - return true; -} - QVector ChartValuesAxisX::calculateLayout() const { Q_ASSERT(m_ticksCount>=2); @@ -80,16 +65,7 @@ void ChartValuesAxisX::updateGeometry() QStringList ticksList; - int ticks = layout.size(); - int n = qMax(int(-floor(log10((m_max-m_min)/(ticks-1)))),0); - n++; - for (int i=0; i< ticks; i++) { - qreal value = m_min + (i * (m_max - m_min)/ (ticks-1)); - Q_UNUSED(value); - ticksList << QString::number(value,'f',n); - } - - bool categories = false; //createLabels(ticksList,m_min,m_max,layout.size()); + createNumberLabels(ticksList,m_min,m_max,layout.size()); QList lines = m_grid->childItems(); QList labels = m_labels->childItems(); @@ -107,7 +83,6 @@ void ChartValuesAxisX::updateGeometry() QGraphicsLineItem *lineItem = static_cast(lines.at(i)); lineItem->setLine(layout[i], m_rect.top(), layout[i], m_rect.bottom()); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - if (!categories || i<1) { labelItem->setText(ticksList.at(i)); const QRectF& rect = labelItem->boundingRect(); QPointF center = rect.center(); @@ -124,7 +99,7 @@ void ChartValuesAxisX::updateGeometry() } m_minWidth+=rect.width(); m_minHeight=qMax(rect.height(),m_minHeight); - } + if ((i+1)%2 && i>1) { QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); rectItem->setRect(layout[i-1],m_rect.top(),layout[i]-layout[i-1],m_rect.height()); diff --git a/src/axis/chartvaluesaxisx_p.h b/src/axis/chartvaluesaxisx_p.h index b599b3e..0723820 100644 --- a/src/axis/chartvaluesaxisx_p.h +++ b/src/axis/chartvaluesaxisx_p.h @@ -44,11 +44,14 @@ public: ~ChartValuesAxisX(); AxisType axisType() const { return X_AXIS;} - bool createLabels(QStringList &labels,qreal min, qreal max,int ticks) const; protected: QVector calculateLayout() const; void updateGeometry(); + +private: + void createLabels(QStringList &labels,qreal min, qreal max,int ticks) const; + }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/axis/chartvaluesaxisy.cpp b/src/axis/chartvaluesaxisy.cpp index 0dfb82b..b2d54c7 100644 --- a/src/axis/chartvaluesaxisy.cpp +++ b/src/axis/chartvaluesaxisy.cpp @@ -65,18 +65,7 @@ void ChartValuesAxisY::updateGeometry() QStringList ticksList; - int ticks = layout.size(); - int n = qMax(int(-floor(log10((m_max-m_min)/(ticks-1)))),0); - n++; - for (int i=0; i< ticks; i++) { - qreal value = m_min + (i * (m_max - m_min)/ (ticks-1)); - Q_UNUSED(value); - ticksList << QString::number(value,'f',n); - } - - bool categories = false; - -// bool categories = createLabels(ticksList,m_min,m_max,layout.size()); + createNumberLabels(ticksList,m_min,m_max,layout.size()); QList lines = m_grid->childItems(); QList labels = m_labels->childItems(); @@ -96,7 +85,6 @@ void ChartValuesAxisY::updateGeometry() lineItem->setLine(m_rect.left() , layout[i], m_rect.right(), layout[i]); QGraphicsSimpleTextItem *labelItem = static_cast(labels.at(i)); - if (!categories || i<1) { labelItem->setText(ticksList.at(i)); const QRectF& rect = labelItem->boundingRect(); @@ -116,16 +104,6 @@ void ChartValuesAxisY::updateGeometry() m_minWidth=qMax(rect.width()+label_padding,m_minWidth); m_minHeight+=rect.height(); - } - else { - labelItem->setText(ticksList.at(i)); - const QRectF& rect = labelItem->boundingRect(); - m_minWidth=qMax(rect.width(),m_minWidth); - m_minHeight+=rect.height(); - QPointF center = rect.center(); - labelItem->setTransformOriginPoint(center.x(), center.y()); - labelItem->setPos(m_rect.left() - rect.width() - label_padding , layout[i] - (layout[i] - layout[i-1])/2 -center.y()); - } if ((i+1)%2 && i>1) { QGraphicsRectItem *rectItem = static_cast(shades.at(i/2-1)); diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index d2a5282..bcce1a6 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -43,35 +43,16 @@ ChartDataSet::~ChartDataSet() void ChartDataSet::addSeries(QAbstractSeries* series) { - QAbstractAxis* axis = m_seriesAxisXMap.value(series); + Domain* domain = m_seriesDomainMap.value(series); - if(axis) { + if(domain) { qWarning() << "Can not add series. Series already on the chart"; return; } - QAbstractAxis* axisX = series->d_ptr->createAxisX(this); - QAbstractAxis* axisY = series->d_ptr->createAxisY(this); - series->setParent(this); // take ownership - Domain* domain = new Domain(series); - - if(axisX){ - QObject::connect(axisX->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisXChanged(qreal,qreal,int,bool))); - QObject::connect(domain,SIGNAL(rangeXChanged(qreal,qreal,int)),axisX->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); - axisX->d_ptr->m_orientation=Qt::Horizontal; - emit axisAdded(axisX,domain); - m_seriesAxisXMap.insert(series,axisX); - } - - if(axisY){ - QObject::connect(axisY->d_ptr.data(),SIGNAL(changed(qreal,qreal,int,bool)),domain,SLOT(handleAxisYChanged(qreal,qreal,int,bool))); - QObject::connect(domain,SIGNAL(rangeYChanged(qreal,qreal,int)),axisY->d_ptr.data(),SLOT(handleAxisRangeChanged(qreal,qreal,int))); - axisY->d_ptr->m_orientation=Qt::Vertical; - emit axisAdded(axisY,domain); - m_seriesAxisYMap.insert(series,axisY); - } + domain = new Domain(series); m_seriesDomainMap.insert(series,domain); @@ -97,6 +78,44 @@ void ChartDataSet::addSeries(QAbstractSeries* series) } +void ChartDataSet::createDefaultAxes() +{ + QMapIterator i(m_seriesDomainMap); + + while (i.hasNext()) { + i.next(); + + if(!m_seriesAxisXMap.value(i.key())) + { + + QAbstractAxis* axisX = i.key()->d_ptr->createAxisX(this); + + 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(!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); + } + } + } +} + + void ChartDataSet::removeSeries(QAbstractSeries* series) { Domain* domain = m_seriesDomainMap.take(series); @@ -180,7 +199,7 @@ void ChartDataSet::zoomOutDomain(const QRectF& rect, const QSizeF& size) int ChartDataSet::seriesCount(QAbstractSeries::SeriesType type) { int count=0; - QMapIterator i(m_seriesAxisXMap); + QMapIterator i(m_seriesDomainMap); while (i.hasNext()) { i.next(); if(i.key()->type()==type) count++; @@ -213,37 +232,38 @@ QAbstractAxis* ChartDataSet::axisY(QAbstractSeries *series) const void ChartDataSet::setAxisX(QAbstractSeries *series, QAbstractAxis *axis) { - Q_ASSERT(axis); - QAbstractAxis *oldAxis = m_seriesAxisXMap.take(series); + Domain* domain = m_seriesDomainMap.value(series); - if(!oldAxis) { - qWarning()<<"Series not found on the chart."; - return; + if(!domain) { + qWarning() << "Series not found on the chart."; + return; } if(axis->d_ptr->m_orientation==Qt::Vertical) { - qWarning()<<"Axis already defined as axis Y"; - return; + qWarning()<<"Axis already defined as axis Y"; + return; } - QList axesX = m_seriesAxisXMap.values(); - int x = axesX.indexOf(oldAxis); - if(x==-1) { - emit axisRemoved(oldAxis); - oldAxis->deleteLater(); - } + QAbstractAxis *oldAxis = m_seriesAxisXMap.take(series); + QList axesX = m_seriesAxisXMap.values(); - Domain* domain = m_seriesDomainMap.value(series); - Q_ASSERT(domain); + if(oldAxis) { + + int x = axesX.indexOf(oldAxis); + if(x==-1) { + emit axisRemoved(oldAxis); + oldAxis->deleteLater(); + } + } 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))); - x = axesX.indexOf(axis); + int x = axesX.indexOf(axis); if(x==-1) { - axis->d_ptr->m_orientation=Qt::Horizontal; - emit axisAdded(axis,domain); + axis->d_ptr->m_orientation=Qt::Horizontal; + emit axisAdded(axis,domain); } m_seriesAxisXMap.insert(series,axis); @@ -254,39 +274,40 @@ void ChartDataSet::setAxisX(QAbstractSeries *series, QAbstractAxis *axis) void ChartDataSet::setAxisY(QAbstractSeries *series, QAbstractAxis *axis) { - Q_ASSERT(axis); - QAbstractAxis *oldAxis = m_seriesAxisYMap.take(series); + Q_ASSERT(axis); + Domain* domain = m_seriesDomainMap.value(series); - if(!oldAxis) { - qWarning()<<"Series not found on the chart or axis is unsupported for given series type"; - return; - } + if(!domain) { + qWarning() << "Series not found on the chart."; + return; + } if(axis->d_ptr->m_orientation==Qt::Horizontal) { qWarning()<<"Axis already defined as axis X"; return; } - QList axesY = m_seriesAxisYMap.values(); - int y = axesY.indexOf(oldAxis); - if(y==-1) { - emit axisRemoved(oldAxis); - oldAxis->deleteLater(); - } + QAbstractAxis *oldAxis = m_seriesAxisYMap.take(series); + QList axesY = m_seriesAxisYMap.values(); - Domain* domain = m_seriesDomainMap.value(series); - Q_ASSERT(domain); + if(oldAxis) { + int y = axesY.indexOf(oldAxis); + if(y==-1) { + emit axisRemoved(oldAxis); + oldAxis->deleteLater(); + } + } - 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))); + 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))); - y = axesY.indexOf(axis); + int y = axesY.indexOf(axis); if(y==-1) { axis->d_ptr->m_orientation=Qt::Vertical; emit axisAdded(axis,domain); } - m_seriesAxisYMap.insert(series,axis); + m_seriesAxisYMap.insert(series,axis); } Domain* ChartDataSet::domain(QAbstractSeries *series) const diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index 9daa040..7adde54 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -66,6 +66,8 @@ public: QList series() const; Domain* domain(QAbstractSeries *series) const; + void createDefaultAxes(); + Q_SIGNALS: void seriesAdded(QAbstractSeries* series, Domain* domain); void seriesRemoved(QAbstractSeries* series); diff --git a/src/qchart.cpp b/src/qchart.cpp index d0e948e..a17295b 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -322,6 +322,11 @@ QAbstractAxis* QChart::axisY(QAbstractSeries *series) const return d_ptr->m_dataset->axisY(series); } +void QChart::createDefaultAxes() +{ + d_ptr->m_dataset->createDefaultAxes(); +} + /*! Returns the legend object of the chart. Ownership stays in chart. */ @@ -396,14 +401,14 @@ void QChart::setMarginsMinimum(const QRectF& margins) d_ptr->m_presenter->setMarginsMinimum(margins); } -void QChart::setAxisX(QAbstractSeries *series, QAbstractAxis* axis) +void QChart::setAxisX(QAbstractAxis* axis , QAbstractSeries *series) { - d_ptr->m_dataset->setAxisX(series, axis); + d_ptr->m_dataset->setAxisX(series,axis); } -void QChart::setAxisY(QAbstractSeries *series, QAbstractAxis* axis) +void QChart::setAxisY( QAbstractAxis* axis , QAbstractSeries *series) { - d_ptr->m_dataset->setAxisY(series, axis); + d_ptr->m_dataset->setAxisY(series,axis); } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/qchart.h b/src/qchart.h index efa9598..0b89a23 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -75,12 +75,14 @@ public: void removeAllSeries(); QList series() const; - void setAxisX(QAbstractSeries *series, QAbstractAxis* axis); - void setAxisY(QAbstractSeries *series, QAbstractAxis* axis); + void setAxisX(QAbstractAxis* axis, QAbstractSeries *series = 0); + void setAxisY(QAbstractAxis* axis, QAbstractSeries *series = 0); QAbstractAxis* axisX(QAbstractSeries* series = 0) const; QAbstractAxis* axisY(QAbstractSeries* series = 0) const; + void createDefaultAxes(); + void setTheme(QChart::ChartTheme theme); QChart::ChartTheme theme() const; @@ -109,7 +111,7 @@ public: void zoom(qreal factor); void scroll(qreal dx, qreal dy); - void adjustViewToSeries(QAbstractSeries* series= 0); + void adjustViewToSeries(QAbstractSeries* series = 0); QLegend* legend() const; diff --git a/tests/auto/chartdataset/tst_chartdataset.cpp b/tests/auto/chartdataset/tst_chartdataset.cpp index 85d4779..6958d4e 100644 --- a/tests/auto/chartdataset/tst_chartdataset.cpp +++ b/tests/auto/chartdataset/tst_chartdataset.cpp @@ -123,6 +123,7 @@ void tst_ChartDataSet::chartdataset() QVERIFY(m_dataset->domain(series) == 0); QVERIFY(m_dataset->axisX(series) == 0); QVERIFY(m_dataset->axisY(series) == 0); + m_dataset->createDefaultAxes(); } @@ -160,7 +161,7 @@ void tst_ChartDataSet::addSeries() QSignalSpy spy3(m_dataset, SIGNAL(seriesRemoved(QAbstractSeries *))); m_dataset->addSeries(series); - + m_dataset->createDefaultAxes(); if(series->type()==QAbstractSeries::SeriesTypePie){ TRY_COMPARE(spy0.count(), 0); }else{ @@ -221,7 +222,7 @@ void tst_ChartDataSet::setAxisX() m_dataset->addSeries(series); } - TRY_COMPARE(spy0.count(), seriesList.count()*2); + TRY_COMPARE(spy0.count(), 0); TRY_COMPARE(spy1.count(), 0); TRY_COMPARE(spy2.count(), seriesList.count()); TRY_COMPARE(spy3.count(), 0); @@ -236,7 +237,7 @@ void tst_ChartDataSet::setAxisX() } TRY_COMPARE(spy4.count(), axisCount); - TRY_COMPARE(spy5.count(), seriesList.count()); + TRY_COMPARE(spy5.count(), 0); TRY_COMPARE(spy6.count(), 0); TRY_COMPARE(spy7.count(), 0); @@ -267,7 +268,7 @@ void tst_ChartDataSet::setAxisY() m_dataset->addSeries(series); } - TRY_COMPARE(spy0.count(), seriesList.count()*2); + TRY_COMPARE(spy0.count(), 0); TRY_COMPARE(spy1.count(), 0); TRY_COMPARE(spy2.count(), seriesList.count()); TRY_COMPARE(spy3.count(), 0); @@ -282,7 +283,7 @@ void tst_ChartDataSet::setAxisY() } TRY_COMPARE(spy4.count(), axisCount); - TRY_COMPARE(spy5.count(), seriesList.count()); + TRY_COMPARE(spy5.count(), 0); TRY_COMPARE(spy6.count(), 0); TRY_COMPARE(spy7.count(), 0); @@ -301,6 +302,7 @@ void tst_ChartDataSet::removeSeries() QFETCH(QAbstractSeries*, series); m_dataset->addSeries(series); + m_dataset->createDefaultAxes(); QSignalSpy spy0(m_dataset, SIGNAL(axisAdded(QAbstractAxis*, Domain *))); QSignalSpy spy1(m_dataset, SIGNAL(axisRemoved(QAbstractAxis*))); @@ -356,7 +358,7 @@ void tst_ChartDataSet::removeAllSeries() m_dataset->removeAllSeries(); TRY_COMPARE(spy0.count(), 0); - TRY_COMPARE(spy1.count(), axisCount + seriesList.count()); + TRY_COMPARE(spy1.count(), axisCount); TRY_COMPARE(spy2.count(), 0); TRY_COMPARE(spy3.count(), seriesList.count()); } @@ -503,10 +505,8 @@ void tst_ChartDataSet::domain() m_dataset->addSeries(series); QVERIFY(m_dataset->domain(series)); - if(series->type()!=QAbstractSeries::SeriesTypePie){ - TRY_COMPARE(spy0.count(), 2); - } + TRY_COMPARE(spy0.count(), 0); TRY_COMPARE(spy1.count(), 0); TRY_COMPARE(spy2.count(), 1); @@ -532,9 +532,14 @@ void tst_ChartDataSet::zoomInDomain() m_dataset->addSeries(series); } - for (int i = 1; i < seriesList.count(); i++) { - m_dataset->setAxisX(seriesList.at(i), m_dataset->axisX(seriesList.at(0))); + /* + QValuesAxis* axis = new QValuesAxis(); + + for (int i = 0; i < seriesList.count(); i++) { + m_dataset->setAxisX(seriesList.at(i), axis); } +*/ + m_dataset->createDefaultAxes(); QList spyList; diff --git a/tests/auto/qchart/tst_qchart.cpp b/tests/auto/qchart/tst_qchart.cpp index 0723092..16d6a87 100644 --- a/tests/auto/qchart/tst_qchart.cpp +++ b/tests/auto/qchart/tst_qchart.cpp @@ -212,7 +212,7 @@ void tst_QChart::addSeries() QVERIFY(!series->chart()); QCOMPARE(m_chart->series().count(), 0); m_chart->addSeries(series); - m_chart->setAxisY(series,axis); + m_chart->setAxisY(axis,series); QCOMPARE(m_chart->series().count(), 1); QCOMPARE(m_chart->series().first(), series); QVERIFY(series->chart() == m_chart); @@ -275,15 +275,15 @@ void tst_QChart::axisY() QLineSeries* series0 = new QLineSeries(); m_chart->addSeries(series0); - m_chart->setAxisY(series0,axis0); + m_chart->setAxisY(axis0,series0); QLineSeries* series1 = new QLineSeries(); m_chart->addSeries(series1); - m_chart->setAxisY(series1,axis1); + m_chart->setAxisY(axis1,series1); QLineSeries* series2 = new QLineSeries(); m_chart->addSeries(series2); - m_chart->setAxisY(series2,axis2); + m_chart->setAxisY(axis2,series2); if (!axis0) axis0 = defaultAxisY; @@ -452,7 +452,7 @@ void tst_QChart::removeSeries() QTest::qWaitForWindowShown(m_view); if(!axis) axis = m_chart->axisY(); m_chart->addSeries(series); - m_chart->setAxisY(series,axis); + m_chart->setAxisY(axis,series); QCOMPARE(m_chart->axisY(series),axis); m_chart->removeSeries(series); QVERIFY(m_chart->axisY(series)==0); diff --git a/tests/chartwidgettest/mainwidget.cpp b/tests/chartwidgettest/mainwidget.cpp index fe77010..0fa84b1 100644 --- a/tests/chartwidgettest/mainwidget.cpp +++ b/tests/chartwidgettest/mainwidget.cpp @@ -279,22 +279,22 @@ void MainWidget::addSeries(QString seriesName, int columnCount, int rowCount, QS series = new QBarSeries(this); QCategoriesAxis* axis = new QCategoriesAxis(); axis->append(category); - m_chart->setAxisX(series,axis); + m_chart->setAxisX(axis,series); } else if (seriesName == "Grouped bar") { series = new QGroupedBarSeries(this); QCategoriesAxis* axis = new QCategoriesAxis(); axis->append(category); - m_chart->setAxisX(series,axis); + m_chart->setAxisX(axis,series); } else if (seriesName == "Stacked bar") { series = new QStackedBarSeries(this); QCategoriesAxis* axis = new QCategoriesAxis(); axis->append(category); - m_chart->setAxisX(series,axis); + m_chart->setAxisX(axis,series); } else { series = new QPercentBarSeries(this); QCategoriesAxis* axis = new QCategoriesAxis(); axis->append(category); - m_chart->setAxisX(series,axis); + m_chart->setAxisX(axis,series); } for (int j(0); j < data.count(); j++) {