summaryrefslogtreecommitdiff
path: root/chart2
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2022-02-11 19:41:37 +0900
committerTomaž Vajngerl <quikee@gmail.com>2022-02-12 06:32:02 +0100
commitca22f1ce703c1d03fc1b1ed1f5ac1fc903b09e6f (patch)
tree6c46ac0ab38fcb70e54e842ad90a9f97be96c6ff /chart2
parent92da88582ae8d5ddbd786f9936e1b2b46eaddf2c (diff)
chart2: Break labels text and limit the size to the axis size
Previously a long label wouldn't be broken into multiple lines and be limited to the axis size if the chart sizing was automatic. This would cause the label to distort the whole chart and make the chart area very narrow. With this change the label text is limited to the axis width and gets broken into multiple lines if this is necessary. BarChartVeryLongLabel.odp provides a test document which includes automatic size and fixed size chart are. We make sure the area that the label text occupies is not larger than the chart wall size. Change-Id: If58bfa3e51ab68f720f22df5416ae305401bcd34 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129814 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'chart2')
-rw-r--r--chart2/qa/extras/chart2import.cxx86
-rw-r--r--chart2/qa/extras/data/odp/BarChartVeryLongLabel.odpbin0 -> 16390 bytes
-rw-r--r--chart2/qa/extras/data/odp/FixedSizeBarChartVeryLongLabel.odpbin13293 -> 0 bytes
-rw-r--r--chart2/source/view/axes/Tickmarks.cxx5
-rw-r--r--chart2/source/view/axes/Tickmarks.hxx11
-rw-r--r--chart2/source/view/axes/VCartesianAxis.cxx22
-rw-r--r--chart2/source/view/main/ChartView.cxx12
7 files changed, 123 insertions, 13 deletions
diff --git a/chart2/qa/extras/chart2import.cxx b/chart2/qa/extras/chart2import.cxx
index 76fa05a57028..0c129d26978b 100644
--- a/chart2/qa/extras/chart2import.cxx
+++ b/chart2/qa/extras/chart2import.cxx
@@ -132,6 +132,7 @@ public:
void testTdf48041();
void testTdf121205();
void testFixedSizeBarChartVeryLongLabel();
+ void testAutomaticSizeBarChartVeryLongLabel();
CPPUNIT_TEST_SUITE(Chart2ImportTest);
CPPUNIT_TEST(Fdo60083);
@@ -215,6 +216,7 @@ public:
CPPUNIT_TEST(testTdf48041);
CPPUNIT_TEST(testTdf121205);
CPPUNIT_TEST(testFixedSizeBarChartVeryLongLabel);
+ CPPUNIT_TEST(testAutomaticSizeBarChartVeryLongLabel);
CPPUNIT_TEST_SUITE_END();
};
@@ -2101,8 +2103,9 @@ void Chart2ImportTest::testFixedSizeBarChartVeryLongLabel()
// case the best course of action is to just crop the label text. This
// test checks that the rendered text is actually cropped.
- load(u"/chart2/qa/extras/data/odp/", "FixedSizeBarChartVeryLongLabel.odp");
+ load(u"/chart2/qa/extras/data/odp/", "BarChartVeryLongLabel.odp");
+ // Select shape 0 which has fixed size chart
Reference<chart2::XChartDocument> xChartDoc(getChartDocFromDrawImpress(0, 0), uno::UNO_QUERY);
CPPUNIT_ASSERT(xChartDoc.is());
@@ -2151,7 +2154,86 @@ void Chart2ImportTest::testFixedSizeBarChartVeryLongLabel()
// Check text is actually cropped
uno::Reference<text::XTextRange> xLabel(xIndexAccess->getByIndex(0), uno::UNO_QUERY);
- CPPUNIT_ASSERT_EQUAL(OUString("Very very very very..."), xLabel->getString());
+ CPPUNIT_ASSERT_EQUAL(OUString("Very very very very very very..."), xLabel->getString());
+
+ uno::Reference<drawing::XShape> xChartWall = getShapeByName(xShapes, "CID/DiagramWall=");
+ CPPUNIT_ASSERT(xChartWall.is());
+
+ // The text shape width should be smaller than the chart wall
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5085), xChartWall->getSize().Width);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(7113), xChartWall->getSize().Height);
+
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(4870), xXAxis->getSize().Width);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(398), xXAxis->getSize().Height);
+}
+
+void Chart2ImportTest::testAutomaticSizeBarChartVeryLongLabel()
+{
+ // Bar chart area size is automatic so we expect the label to be broken
+ // into multiple lines.
+
+ load(u"/chart2/qa/extras/data/odp/", "BarChartVeryLongLabel.odp");
+
+ // Select shape 1, which has an automatic sized chart
+ Reference<chart2::XChartDocument> xChartDoc(getChartDocFromDrawImpress(0, 1), uno::UNO_QUERY);
+ CPPUNIT_ASSERT(xChartDoc.is());
+
+ Reference<chart2::XAxis> xHAxis = getAxisFromDoc(xChartDoc, 0, 0, 0);
+ CPPUNIT_ASSERT(xHAxis.is());
+
+ chart2::ScaleData aScaleData = xHAxis->getScaleData();
+ CPPUNIT_ASSERT(aScaleData.Categories.is());
+
+ Reference<chart2::data::XLabeledDataSequence> xLabeledDataSequence = aScaleData.Categories;
+ CPPUNIT_ASSERT(xLabeledDataSequence.is());
+
+ Reference<chart2::data::XDataSequence> xDataSequence = xLabeledDataSequence->getValues();
+ CPPUNIT_ASSERT(xDataSequence.is());
+
+ Reference<chart2::data::XTextualDataSequence> xTextualDataSequence(xDataSequence, uno::UNO_QUERY);
+ CPPUNIT_ASSERT(xTextualDataSequence.is());
+
+ std::vector<OUString> aCategories;
+ const Sequence<OUString> aTextData(xTextualDataSequence->getTextualData());
+ ::std::copy(aTextData.begin(), aTextData.end(),
+ ::std::back_inserter(aCategories));
+
+ // Check that we have a very very long label text
+ CPPUNIT_ASSERT_EQUAL(OUString("Very very very very very very very very very very very loooooooooooong label"), aCategories[0]);
+
+ // Check visible text
+ uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(xChartDoc, uno::UNO_QUERY);
+ uno::Reference<drawing::XDrawPage> xDrawPage = xDrawPageSupplier->getDrawPage();
+ uno::Reference<drawing::XShapes> xShapes(xDrawPage->getByIndex(0), uno::UNO_QUERY);
+ CPPUNIT_ASSERT(xShapes.is());
+
+ uno::Reference<drawing::XShape> xXAxis = getShapeByName(xShapes, "CID/D=0:CS=0:Axis=0,0",
+ // Axis occurs twice in chart xshape representation so need to get the one related to labels
+ [](const uno::Reference<drawing::XShape>& rXShape) -> bool
+ {
+ uno::Reference<drawing::XShapes> xAxisShapes(rXShape, uno::UNO_QUERY);
+ CPPUNIT_ASSERT(xAxisShapes.is());
+ uno::Reference<drawing::XShape> xChildShape(xAxisShapes->getByIndex(0), uno::UNO_QUERY);
+ uno::Reference< drawing::XShapeDescriptor > xShapeDescriptor(xChildShape, uno::UNO_QUERY_THROW);
+ return (xShapeDescriptor->getShapeType() == "com.sun.star.drawing.TextShape");
+ });
+ CPPUNIT_ASSERT(xXAxis.is());
+
+ uno::Reference<container::XIndexAccess> xIndexAccess(xXAxis, UNO_QUERY_THROW);
+
+ // Check text is unmodified
+ uno::Reference<text::XTextRange> xLabel(xIndexAccess->getByIndex(0), uno::UNO_QUERY);
+ CPPUNIT_ASSERT_EQUAL(OUString("Very very very very very very very very very very very loooooooooooong label"), xLabel->getString());
+
+ uno::Reference<drawing::XShape> xChartWall = getShapeByName(xShapes, "CID/DiagramWall=");
+ CPPUNIT_ASSERT(xChartWall.is());
+
+ // The text shape width should be smaller than the chart wall
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5761), xChartWall->getSize().Width);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(7200), xChartWall->getSize().Height);
+
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5320), xXAxis->getSize().Width);
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1192), xXAxis->getSize().Height);
}
CPPUNIT_TEST_SUITE_REGISTRATION(Chart2ImportTest);
diff --git a/chart2/qa/extras/data/odp/BarChartVeryLongLabel.odp b/chart2/qa/extras/data/odp/BarChartVeryLongLabel.odp
new file mode 100644
index 000000000000..c627af79e4d5
--- /dev/null
+++ b/chart2/qa/extras/data/odp/BarChartVeryLongLabel.odp
Binary files differ
diff --git a/chart2/qa/extras/data/odp/FixedSizeBarChartVeryLongLabel.odp b/chart2/qa/extras/data/odp/FixedSizeBarChartVeryLongLabel.odp
deleted file mode 100644
index 18869b638d1e..000000000000
--- a/chart2/qa/extras/data/odp/FixedSizeBarChartVeryLongLabel.odp
+++ /dev/null
Binary files differ
diff --git a/chart2/source/view/axes/Tickmarks.cxx b/chart2/source/view/axes/Tickmarks.cxx
index d10583c1b77d..01e4a7216ac1 100644
--- a/chart2/source/view/axes/Tickmarks.cxx
+++ b/chart2/source/view/axes/Tickmarks.cxx
@@ -203,11 +203,6 @@ sal_Int32 TickFactory2D::getTickScreenDistance( TickIter& rIter )
return pFirstTickInfo->getScreenDistanceBetweenTicks( *pSecondTickInfo );
}
-const B2DVector & TickFactory2D::getXaxisStartPos() const
-{
- return m_aAxisStartScreenPosition2D;
-}
-
B2DVector TickFactory2D::getTickScreenPosition2D( double fScaledLogicTickValue ) const
{
B2DVector aRet(m_aAxisStartScreenPosition2D);
diff --git a/chart2/source/view/axes/Tickmarks.hxx b/chart2/source/view/axes/Tickmarks.hxx
index c712f6542b39..bb33be50421c 100644
--- a/chart2/source/view/axes/Tickmarks.hxx
+++ b/chart2/source/view/axes/Tickmarks.hxx
@@ -132,7 +132,16 @@ public:
bool isHorizontalAxis() const;
bool isVerticalAxis() const;
- const ::basegfx::B2DVector & getXaxisStartPos() const;
+
+ const ::basegfx::B2DVector & getXaxisStartPos() const
+ {
+ return m_aAxisStartScreenPosition2D;
+ }
+
+ const ::basegfx::B2DVector & getXaxisEndPos() const
+ {
+ return m_aAxisEndScreenPosition2D;
+ }
private:
::basegfx::B2DVector getTickScreenPosition2D( double fScaledLogicTickValue ) const;
diff --git a/chart2/source/view/axes/VCartesianAxis.cxx b/chart2/source/view/axes/VCartesianAxis.cxx
index d42c811ae092..e6b52f0eb0c2 100644
--- a/chart2/source/view/axes/VCartesianAxis.cxx
+++ b/chart2/source/view/axes/VCartesianAxis.cxx
@@ -721,10 +721,13 @@ bool VCartesianAxis::createTextShapes(
}
}
- if (!isBreakOfLabelsAllowed(rAxisLabelProperties, bIsHorizontalAxis, bIsVerticalAxis) &&
+ bool bIsBreakOfLabelsAllowed = isBreakOfLabelsAllowed( rAxisLabelProperties, bIsHorizontalAxis, bIsVerticalAxis );
+ if (!bIsBreakOfLabelsAllowed &&
!isAutoStaggeringOfLabelsAllowed(rAxisLabelProperties, bIsHorizontalAxis, bIsVerticalAxis) &&
!rAxisLabelProperties.isStaggered())
+ {
return createTextShapesSimple(xTarget, rTickIter, rAxisLabelProperties, pTickFactory);
+ }
FixedNumberFormatter aFixedNumberFormatter(
m_xNumberFormatsSupplier, rAxisLabelProperties.m_nNumberFormatKey );
@@ -733,9 +736,16 @@ bool VCartesianAxis::createTextShapes(
B2DVector aTextToTickDistance = pTickFactory->getDistanceAxisTickToText(m_aAxisProperties, true);
sal_Int32 nLimitedSpaceForText = -1;
- if( isBreakOfLabelsAllowed( rAxisLabelProperties, bIsHorizontalAxis, bIsVerticalAxis ) )
+ if (bIsBreakOfLabelsAllowed)
{
- nLimitedSpaceForText = nScreenDistanceBetweenTicks;
+ if (!m_aAxisProperties.m_bLimitSpaceForLabels)
+ {
+ basegfx::B2DVector nDeltaVector = pTickFactory->getXaxisEndPos() - pTickFactory->getXaxisStartPos();
+ nLimitedSpaceForText = nDeltaVector.getX();
+ }
+ if (nScreenDistanceBetweenTicks > 0)
+ nLimitedSpaceForText = nScreenDistanceBetweenTicks;
+
if( bIsStaggered )
nLimitedSpaceForText *= 2;
@@ -874,9 +884,11 @@ bool VCartesianAxis::createTextShapes(
recordMaximumTextSize( *pTickInfo->xTextShape, rAxisLabelProperties.m_fRotationAngleDegree );
// Label has multiple lines and the words are broken
- if( nLimitedSpaceForText>0 && !rAxisLabelProperties.m_bOverlapAllowed
+ if (nLimitedSpaceForText > 0
+ && !rAxisLabelProperties.m_bOverlapAllowed
&& rAxisLabelProperties.m_fRotationAngleDegree == 0.0
- && lcl_hasWordBreak( pTickInfo->xTextShape ) )
+ && nTick > 0
+ && lcl_hasWordBreak(pTickInfo->xTextShape))
{
// Label has multiple lines and belongs to a complex category
// axis. Rotate 90 degrees to try to avoid overlaps.
diff --git a/chart2/source/view/main/ChartView.cxx b/chart2/source/view/main/ChartView.cxx
index 56955f4c8f8b..1d8fce7bed9a 100644
--- a/chart2/source/view/main/ChartView.cxx
+++ b/chart2/source/view/main/ChartView.cxx
@@ -1489,7 +1489,9 @@ awt::Rectangle ChartView::impl_createDiagramAndContent( const CreateShapeParam2D
|| aConsumedOuterRect.getMaxX() < aAvailableOuterRect.getMaxX()
|| aConsumedOuterRect.getMinY() > aAvailableOuterRect.getMinY()
|| aConsumedOuterRect.getMinY() < aAvailableOuterRect.getMaxY() )
+ {
bLessSpaceConsumedThanExpected = true;
+ }
}
if (bLessSpaceConsumedThanExpected && !rParam.mbUseFixedInnerSize)
@@ -1497,6 +1499,16 @@ awt::Rectangle ChartView::impl_createDiagramAndContent( const CreateShapeParam2D
aVDiagram.adjustInnerSize( aConsumedOuterRect );
pVCooSys->setTransformationSceneToScreen( B3DHomMatrixToHomogenMatrix(
createTransformationSceneToScreen( aVDiagram.getCurrentRectangle() ) ));
+
+ // Need to re-adjust again if the labels have changed height because of
+ // text can break. Ideally this shouldn't be needed, but the chart height
+ // isn't readjusted otherwise.
+ pVCooSys->createAxesLabels();
+ aConsumedOuterRect = ShapeFactory::getRectangleOfShape(*xBoundingShape);
+ aVDiagram.adjustInnerSize(aConsumedOuterRect);
+ pVCooSys->setTransformationSceneToScreen(B3DHomMatrixToHomogenMatrix(
+ createTransformationSceneToScreen(aVDiagram.getCurrentRectangle())));
+
}
pVCooSys->updatePositions();//todo: logically this belongs to the condition above, but it seems also to be necessary to give the axes group shapes the right bounding rects for hit test - probably caused by bug i106183 -> check again if fixed
}