From f065af9daaed390dc22627246a14d5bcac7b8836 2013-01-21 11:42:28 From: Michal Klocek Date: 2013-01-21 11:42:28 Subject: [PATCH] adds Andy's customer request, first call to createDaultAxis scales whole domain (if on common axis) --- diff --git a/src/chartdataset.cpp b/src/chartdataset.cpp index 5558b7d..05a0510 100644 --- a/src/chartdataset.cpp +++ b/src/chartdataset.cpp @@ -303,11 +303,13 @@ void ChartDataSet::createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation ori //create one axis for all addAxis(axis,orientation==Qt::Horizontal?Qt::AlignBottom:Qt::AlignLeft); - + qreal min = 0; + qreal max = 0; + findMinMaxForSeries(m_seriesList,orientation,min,max); foreach(QAbstractSeries *s, m_seriesList) { attachAxis(s,axis); } - + axis->setRange(min,max); } else if (!type.testFlag(QAbstractAxis::AxisTypeNoAxis)) { //create separate axis @@ -321,6 +323,21 @@ void ChartDataSet::createAxes(QAbstractAxis::AxisTypes type, Qt::Orientation ori } } +void ChartDataSet::findMinMaxForSeries(QList series,Qt::Orientations orientation, qreal &min, qreal &max) +{ + Q_ASSERT(!series.isEmpty()); + + AbstractDomain* domain = series.first()->d_ptr->domain(); + min = (orientation == Qt::Vertical) ? domain->minY() : domain->minX(); + max = (orientation == Qt::Vertical) ? domain->maxY() : domain->maxX(); + + for(int i = 1; i< series.size(); i++) { + AbstractDomain* domain = series[i]->d_ptr->domain(); + min = qMin((orientation == Qt::Vertical) ? domain->minY() : domain->minX(), min); + max = qMax((orientation == Qt::Vertical) ? domain->maxY() : domain->maxX(), max); + } +} + void ChartDataSet::deleteAllSeries() { foreach (QAbstractSeries *s , m_seriesList){ diff --git a/src/chartdataset_p.h b/src/chartdataset_p.h index 939e5d7..49a41d0 100644 --- a/src/chartdataset_p.h +++ b/src/chartdataset_p.h @@ -80,6 +80,7 @@ private: AbstractDomain* createDomain(AbstractDomain::DomainType type); void deleteAllAxes(); void deleteAllSeries(); + void findMinMaxForSeries(QList series,Qt::Orientations orientation, qreal &min, qreal &max); private: QList m_seriesList; QList m_axisList; diff --git a/tests/auto/qchart/tst_qchart.cpp b/tests/auto/qchart/tst_qchart.cpp index 5fa266f..5acc3a0 100644 --- a/tests/auto/qchart/tst_qchart.cpp +++ b/tests/auto/qchart/tst_qchart.cpp @@ -99,7 +99,8 @@ private slots: void zoomIn(); void zoomOut_data(); void zoomOut(); - + void createDefaultAxesForLineSeries_data(); + void createDefaultAxesForLineSeries(); private: void createTestData(); @@ -793,6 +794,83 @@ void tst_QChart::zoomOut() } +void tst_QChart::createDefaultAxesForLineSeries_data() +{ + QTest::addColumn("series1minX"); + QTest::addColumn("series1midX"); + QTest::addColumn("series1maxX"); + QTest::addColumn("series2minX"); + QTest::addColumn("series2midX"); + QTest::addColumn("series2maxX"); + QTest::addColumn("overallminX"); + QTest::addColumn("overallmaxX"); + QTest::addColumn("series1minY"); + QTest::addColumn("series1midY"); + QTest::addColumn("series1maxY"); + QTest::addColumn("series2minY"); + QTest::addColumn("series2midY"); + QTest::addColumn("series2maxY"); + QTest::addColumn("overallminY"); + QTest::addColumn("overallmaxY"); + QTest::newRow("series1hasMinAndMax") << (qreal)1.0 << (qreal)2.0 << (qreal)3.0 << (qreal)1.1 << (qreal)1.7 << (qreal)2.9 << (qreal)1.0 << (qreal)3.0 + << (qreal)1.0 << (qreal)2.0 << (qreal)3.0 << (qreal)1.1 << (qreal)1.7 << (qreal)2.9 << (qreal)1.0 << (qreal)3.0; + QTest::newRow("series2hasMinAndMax") << (qreal)1.1 << (qreal)2.0 << (qreal)2.9 << (qreal)1.0 << (qreal)1.7 << (qreal)3.0 << (qreal)1.0 << (qreal)3.0 + << (qreal)1.1 << (qreal)2.0 << (qreal)2.9 << (qreal)1.0 << (qreal)1.7 << (qreal)3.0 << (qreal)1.0 << (qreal)3.0; + QTest::newRow("series1hasMinAndMaxX_series2hasMinAndMaxY") << (qreal)1.0 << (qreal)2.0 << (qreal)3.0 << (qreal)1.1 << (qreal)1.7 << (qreal)2.9 << (qreal)1.0 << (qreal)3.0 + << (qreal)1.1 << (qreal)2.0 << (qreal)2.9 << (qreal)1.0 << (qreal)2.0 << (qreal)3.0 << (qreal)1.0 << (qreal)3.0; + QTest::newRow("series1hasMin_series2hasMax") << (qreal)1.0 << (qreal)2.0 << (qreal)2.9 << (qreal)1.1 << (qreal)1.7 << (qreal)3.0 << (qreal)1.0 << (qreal)3.0 + << (qreal)1.0 << (qreal)2.0 << (qreal)2.9 << (qreal)1.1 << (qreal)1.7 << (qreal)3.0 << (qreal)1.0 << (qreal)3.0; +} + +void tst_QChart::createDefaultAxesForLineSeries() +{ + QFETCH(qreal, series1minX); + QFETCH(qreal, series1midX); + QFETCH(qreal, series1maxX); + QFETCH(qreal, series2minX); + QFETCH(qreal, series2midX); + QFETCH(qreal, series2maxX); + QFETCH(qreal, series1minY); + QFETCH(qreal, series1midY); + QFETCH(qreal, series1maxY); + QFETCH(qreal, series2minY); + QFETCH(qreal, series2midY); + QFETCH(qreal, series2maxY); + QFETCH(qreal, overallminX); + QFETCH(qreal, overallmaxX); + QFETCH(qreal, overallminY); + QFETCH(qreal, overallmaxY); + QLineSeries* series1 = new QLineSeries(this); + series1->append(series1minX, series1minY); + series1->append(series1midX, series1midY); + series1->append(series1maxX, series1maxY); + QLineSeries* series2 = new QLineSeries(this); + series2->append(series2minX, series2minY); + series2->append(series2midX, series2midY); + series2->append(series2maxX, series2maxY); + QChart chart; + chart.addSeries(series1); + chart.addSeries(series2); + chart.createDefaultAxes(); + QValueAxis *xAxis = (QValueAxis *)chart.axisX(); + QCOMPARE(xAxis->min(), overallminX); + QCOMPARE(xAxis->max(), overallmaxX); + QValueAxis *yAxis = (QValueAxis *)chart.axisY(); + QCOMPARE(yAxis->min(), overallminY); + QCOMPARE(yAxis->max(), overallmaxY); + QLineSeries *series3 = new QLineSeries(this); + // Numbers clearly out of existing range + series3->append(0, 0); + series3->append(100, 100); + // Adding a new series should not change the axes as they have not been told to update + chart.addSeries(series3); + QCOMPARE(xAxis->min(), overallminX); + QCOMPARE(xAxis->max(), overallmaxX); + QCOMPARE(yAxis->min(), overallminY); + QCOMPARE(yAxis->max(), overallmaxY); + +} + QTEST_MAIN(tst_QChart) #include "tst_qchart.moc"