From dca425203c0205a6c570fa36f0dfdfa078960249 Mon Sep 17 00:00:00 2001 From: Tünde Tóth Date: Thu, 6 Jan 2022 15:05:10 +0100 Subject: tdf#146066 chart: fix automatic axis scaling at dates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Automatic scaling of the Y axis was incorrect when the X axis was a date axis and the last Y value was the highest value. To fix this, increase maximum date value by one month/year. Regression from commit ed2c880a691a0b179bbc92a8ce4ee49eac004035 (tdf#133005 Chart: fix ODF import of date axis position). Change-Id: I0989ae8002e55f4ecfaf530ab845badb4409ecf1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/128209 Tested-by: László Németh Reviewed-by: László Németh (cherry picked from commit 81abc0d7657f194804681415a786627ab71475e3) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129085 Tested-by: Jenkins Reviewed-by: Tünde Tóth --- chart2/qa/extras/chart2import2.cxx | 48 ++++++++++++++++++++++++++ chart2/qa/extras/data/ods/tdf146066.ods | Bin 0 -> 15079 bytes chart2/source/view/axes/VCoordinateSystem.cxx | 22 +++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 chart2/qa/extras/data/ods/tdf146066.ods (limited to 'chart2') diff --git a/chart2/qa/extras/chart2import2.cxx b/chart2/qa/extras/chart2import2.cxx index 0693a1e64184..95ee2428b47d 100644 --- a/chart2/qa/extras/chart2import2.cxx +++ b/chart2/qa/extras/chart2import2.cxx @@ -75,6 +75,7 @@ public: void testTdfCustomShapePos(); void testTdf121281(); void testTdf139658(); + void testTdf146066(); CPPUNIT_TEST_SUITE(Chart2ImportTest2); @@ -114,6 +115,7 @@ public: CPPUNIT_TEST(testTdfCustomShapePos); CPPUNIT_TEST(testTdf121281); CPPUNIT_TEST(testTdf139658); + CPPUNIT_TEST(testTdf146066); CPPUNIT_TEST_SUITE_END(); }; @@ -874,6 +876,52 @@ void Chart2ImportTest2::testTdf139658() CPPUNIT_ASSERT_EQUAL(OUString("category\"3"), aCategories[2]); } +void Chart2ImportTest2::testTdf146066() +{ + load(u"/chart2/qa/extras/data/ods/", "tdf146066.ods"); + Reference xChartDoc(getChartDocFromSheet(0, mxComponent), + UNO_QUERY_THROW); + uno::Reference xDrawPageSupplier(xChartDoc, uno::UNO_QUERY); + uno::Reference xDrawPage = xDrawPageSupplier->getDrawPage(); + uno::Reference xShapes(xDrawPage->getByIndex(0), uno::UNO_QUERY); + CPPUNIT_ASSERT(xShapes.is()); + + uno::Reference xYAxisShape = getShapeByName( + xShapes, "CID/D=0:CS=0:Axis=1,0", // Y Axis + // Axis occurs twice in chart xshape representation so need to get the one related to labels + [](const uno::Reference& rXShape) -> bool { + uno::Reference xAxisShapes(rXShape, uno::UNO_QUERY); + CPPUNIT_ASSERT(xAxisShapes.is()); + uno::Reference xChildShape(xAxisShapes->getByIndex(0), uno::UNO_QUERY); + uno::Reference xShapeDescriptor(xChildShape, + uno::UNO_QUERY_THROW); + return (xShapeDescriptor->getShapeType() == "com.sun.star.drawing.TextShape"); + }); + CPPUNIT_ASSERT(xYAxisShape.is()); + + // Check label count + uno::Reference xIndexAccess(xYAxisShape, UNO_QUERY_THROW); + CPPUNIT_ASSERT_EQUAL(static_cast(8), xIndexAccess->getCount()); + + // Check text + uno::Reference xLabel0(xIndexAccess->getByIndex(0), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("0"), xLabel0->getString()); + uno::Reference xLabel1(xIndexAccess->getByIndex(1), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("5"), xLabel1->getString()); + uno::Reference xLabel2(xIndexAccess->getByIndex(2), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("10"), xLabel2->getString()); + uno::Reference xLabel3(xIndexAccess->getByIndex(3), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("15"), xLabel3->getString()); + uno::Reference xLabel4(xIndexAccess->getByIndex(4), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("20"), xLabel4->getString()); + uno::Reference xLabel5(xIndexAccess->getByIndex(5), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("25"), xLabel5->getString()); + uno::Reference xLabel6(xIndexAccess->getByIndex(6), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("30"), xLabel6->getString()); + uno::Reference xLabel7(xIndexAccess->getByIndex(7), uno::UNO_QUERY); + CPPUNIT_ASSERT_EQUAL(OUString("35"), xLabel7->getString()); +} + CPPUNIT_TEST_SUITE_REGISTRATION(Chart2ImportTest2); CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/chart2/qa/extras/data/ods/tdf146066.ods b/chart2/qa/extras/data/ods/tdf146066.ods new file mode 100644 index 000000000000..03abe9ae68ca Binary files /dev/null and b/chart2/qa/extras/data/ods/tdf146066.ods differ diff --git a/chart2/source/view/axes/VCoordinateSystem.cxx b/chart2/source/view/axes/VCoordinateSystem.cxx index f546af789ddd..9b330a0b4171 100644 --- a/chart2/source/view/axes/VCoordinateSystem.cxx +++ b/chart2/source/view/axes/VCoordinateSystem.cxx @@ -18,6 +18,7 @@ */ #include +#include #include #include "VCartesianCoordinateSystem.hxx" #include "VPolarCoordinateSystem.hxx" @@ -33,6 +34,7 @@ #include #include #include +#include #include #include @@ -366,8 +368,26 @@ void VCoordinateSystem::prepareAutomaticAxisScaling( ScaleAutomatism& rScaleAuto { // y dimension ExplicitScaleData aScale = getExplicitScale( 0, 0 ); + double fMaximum = aScale.Maximum; + if (!aScale.m_bShiftedCategoryPosition && aScale.AxisType == AxisType::DATE) + { + // tdf#146066 Increase maximum date value by one month/year, + // because the automatic scaling of the Y axis was incorrect when the last Y value was the highest value. + Date aMaxDate(aScale.NullDate); + aMaxDate.AddDays(::rtl::math::approxFloor(fMaximum)); + switch (aScale.TimeResolution) + { + case css::chart::TimeUnit::MONTH: + aMaxDate = DateHelper::GetDateSomeMonthsAway(aMaxDate, 1); + break; + case css::chart::TimeUnit::YEAR: + aMaxDate = DateHelper::GetDateSomeYearsAway(aMaxDate, 1); + break; + } + fMaximum = aMaxDate - aScale.NullDate; + } fMin = m_aMergedMinMaxSupplier.getMinimumYInRange(aScale.Minimum,aScale.Maximum, nAxisIndex); - fMax = m_aMergedMinMaxSupplier.getMaximumYInRange(aScale.Minimum,aScale.Maximum, nAxisIndex); + fMax = m_aMergedMinMaxSupplier.getMaximumYInRange(aScale.Minimum, fMaximum, nAxisIndex); } else if( nDimIndex == 2 ) { -- cgit