chart2/source/view/main/DataTableView.cxx |   47 ++++++++++++++++++++++--------
 1 file changed, 35 insertions(+), 12 deletions(-)

New commits:
commit c0157b2df2005e76ed55369033758964a0f154e2
Author:     Tomaž Vajngerl <tomaz.vajng...@collabora.co.uk>
AuthorDate: Thu Aug 18 00:09:25 2022 +0200
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Thu Aug 25 13:09:54 2022 +0200

    chart2: improve keys (symbols) rendering in a data table
    
    - better symbol positioning taking symbol height and cell height
      into account.
    - symbol height depends on font size (as with the legend)
    - take symbol aspect ratio into account
    - remove hardcoded values
    
    Change-Id: I3537d82538035cd31b928d4bb6fc5572914fcd13
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138463
    Tested-by: Jenkins
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    (cherry picked from commit 9923f2416b63abc09bf8cd83796d1d681e9de225)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138795
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>

diff --git a/chart2/source/view/main/DataTableView.cxx 
b/chart2/source/view/main/DataTableView.cxx
index 080b04cf919f..636690cb4e48 100644
--- a/chart2/source/view/main/DataTableView.cxx
+++ b/chart2/source/view/main/DataTableView.cxx
@@ -340,21 +340,40 @@ void DataTableView::createShapes(basegfx::B2DVector 
const& rStart, basegfx::B2DV
     // ROW HEADER
     // Prepare keys (symbols)
     sal_Int32 nMaxSymbolWidth = 0;
+    constexpr const sal_Int32 constSymbolMargin = 100; // 1mm
     if (bKeys)
     {
         uno::Reference<lang::XMultiServiceFactory> xFactory(m_xChartModel, 
uno::UNO_QUERY);
+
+        uno::Reference<beans::XPropertySet> 
xDataTableProperties(m_xDataTableModel);
+        float fFontHeight = 0.0;
+        xDataTableProperties->getPropertyValue("CharHeight") >>= fFontHeight;
+        fFontHeight = o3tl::convert(fFontHeight, o3tl::Length::pt, 
o3tl::Length::mm100);
+
+        sal_Int32 nSymbolHeight = sal_Int32(fFontHeight * 0.6);
+        sal_Int32 nSymbolWidth = nSymbolHeight;
+
         for (VSeriesPlotter* pSeriesPlotter : m_pSeriesPlotterList)
         {
             if (pSeriesPlotter)
             {
-                sal_Int32 nSymbolWidth = 300;
-                if (pSeriesPlotter->getLegendSymbolStyle() == 
LegendSymbolStyle::Line)
-                    nSymbolWidth = 600;
-
-                nMaxSymbolWidth = std::max(nSymbolWidth, nMaxSymbolWidth);
+                awt::Size aCurrentRatio = 
pSeriesPlotter->getPreferredLegendKeyAspectRatio();
+                sal_Int32 nCurrentWidth = aCurrentRatio.Width;
+                if (aCurrentRatio.Height > 0)
+                    nCurrentWidth = nSymbolHeight * aCurrentRatio.Width / 
aCurrentRatio.Height;
+                nSymbolWidth = std::max(nSymbolWidth, nCurrentWidth);
+            }
+        }
+        nMaxSymbolWidth = nSymbolWidth;
 
+        for (VSeriesPlotter* pSeriesPlotter : m_pSeriesPlotterList)
+        {
+            if (pSeriesPlotter)
+            {
+                awt::Size aSize(nSymbolWidth, nSymbolHeight);
                 std::vector<ViewLegendSymbol> aNewEntries = 
pSeriesPlotter->createSymbols(
-                    { nSymbolWidth, 300 }, m_xTarget, xFactory, 
m_xComponentContext);
+                    aSize, m_xTarget, xFactory, m_xComponentContext);
+
                 aSymbols.insert(aSymbols.end(), aNewEntries.begin(), 
aNewEntries.end());
             }
         }
@@ -384,8 +403,8 @@ void DataTableView::createShapes(basegfx::B2DVector const& 
rStart, basegfx::B2DV
                                                
uno::makeAny(style::ParagraphAdjust_LEFT));
             if (bKeys)
             {
-                xCellPropertySet->setPropertyValue("ParaLeftMargin",
-                                                   
uno::makeAny(nMaxSymbolWidth + sal_Int32(200)));
+                xCellPropertySet->setPropertyValue(
+                    "ParaLeftMargin", uno::makeAny(nMaxSymbolWidth + 
sal_Int32(2 * constSymbolMargin)));
             }
         }
         nRow++;
@@ -436,8 +455,8 @@ void DataTableView::createShapes(basegfx::B2DVector const& 
rStart, basegfx::B2DV
 
     xBroadcaster->unlockBroadcasts();
 
-    pTableObject->DistributeColumns(0, nColumnCount - 1, true, true);
-    pTableObject->DistributeRows(0, nRowCount - 1, true, true);
+    pTableObject->DistributeColumns(0, nColumnCount, true, true);
+    pTableObject->DistributeRows(0, nRowCount, true, true);
 
     xBroadcaster->lockBroadcasts();
 
@@ -467,8 +486,12 @@ void DataTableView::createShapes(basegfx::B2DVector const& 
rStart, basegfx::B2DV
             xPropertySet->getPropertyValue("Height") >>= nHeight;
             if (i > 0)
             {
-                aSymbols[nSymbolIndex].aSymbol->setPosition(
-                    { nTableX + 100, nTableY + nTotalHeight + 100 });
+                auto& rSymbol = aSymbols[nSymbolIndex].aSymbol;
+                sal_Int32 nSymbolHeight = rSymbol->getSize().Height;
+                sal_Int32 nSymbolY
+                    = basegfx::fround(double(nHeight) / 2.0 - 
double(nSymbolHeight) / 2.0);
+                rSymbol->setPosition(
+                    { nTableX + constSymbolMargin, nTableY + nTotalHeight + 
nSymbolY });
             }
             nTotalHeight += nHeight;
         }

Reply via email to