Coverage Report

Created: 2026-05-16 09:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/oox/source/drawingml/chart/chartspaceconverter.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <drawingml/chart/chartspaceconverter.hxx>
21
22
#include <com/sun/star/chart/MissingValueTreatment.hpp>
23
#include <com/sun/star/chart/XChartDocument.hpp>
24
#include <com/sun/star/chart2/XChartDocument.hpp>
25
#include <com/sun/star/chart2/XChartType.hpp>
26
#include <com/sun/star/chart2/XChartTypeContainer.hpp>
27
#include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
28
#include <com/sun/star/chart2/XDataSeriesContainer.hpp>
29
#include <com/sun/star/chart2/XTitled.hpp>
30
#include <com/sun/star/drawing/XDrawPageSupplier.hpp>
31
#include <com/sun/star/util/DateTime.hpp>
32
#include <oox/core/xmlfilterbase.hxx>
33
#include <oox/drawingml/chart/chartconverter.hxx>
34
#include <oox/token/properties.hxx>
35
#include <oox/token/tokens.hxx>
36
#include <drawingml/chart/chartdrawingfragment.hxx>
37
#include <drawingml/chart/chartspacemodel.hxx>
38
#include <drawingml/chart/plotareaconverter.hxx>
39
#include <drawingml/chart/titleconverter.hxx>
40
#include <ooxresid.hxx>
41
#include <strings.hrc>
42
#include <drawingml/textbody.hxx>
43
44
using namespace ::com::sun::star;
45
using ::com::sun::star::uno::Reference;
46
using ::com::sun::star::uno::Exception;
47
using ::com::sun::star::uno::UNO_QUERY;
48
using ::com::sun::star::uno::UNO_QUERY_THROW;
49
using ::com::sun::star::uno::Any;
50
using ::com::sun::star::drawing::XDrawPageSupplier;
51
using ::com::sun::star::drawing::XShapes;
52
using ::com::sun::star::chart2::XDiagram;
53
using ::com::sun::star::chart2::XTitled;
54
55
namespace oox::drawingml::chart {
56
57
using namespace ::com::sun::star::awt;
58
using namespace ::com::sun::star::chart2;
59
using namespace ::com::sun::star::chart2::data;
60
using namespace ::com::sun::star::drawing;
61
using namespace ::com::sun::star::uno;
62
63
ChartSpaceConverter::ChartSpaceConverter( const ConverterRoot& rParent, ChartSpaceModel& rModel ) :
64
0
    ConverterBase< ChartSpaceModel >( rParent, rModel )
65
0
{
66
0
}
67
68
ChartSpaceConverter::~ChartSpaceConverter()
69
0
{
70
0
}
71
72
// Formulas with no numeric values and strings are zeroes in OOXML line charts also in the mode DispBlanksAs=gap,
73
// unlike in OpenDocument LEAVE_GAP mode. As a workaround, we will use the OpenDocument mode USE_ZERO, if the OOXML
74
// line chart has got formulas with no numeric values and strings, but it doesn't have empty cells, showing the
75
// same chart as in MSO. (Empty cells need gaps, so in that case, we cannot use this workaround).
76
static bool lcl_useWorkaroundForNoGapInOOXML( Reference< chart2::XChartDocument > const & xChartDoc)
77
0
{
78
0
    Reference <chart2::XDiagram > xDiagram = xChartDoc->getFirstDiagram();
79
0
    if ( !xDiagram.is() )
80
0
        return false;
81
82
0
    Reference< chart2::XCoordinateSystemContainer > xCooSysContainer( xDiagram, UNO_QUERY_THROW );
83
84
0
    Sequence< Reference< chart2::XCoordinateSystem > > xCooSysSequence( xCooSysContainer->getCoordinateSystems());
85
0
    if ( !xCooSysSequence.hasElements() )
86
0
        return false;
87
88
0
    Reference< chart2::XChartTypeContainer > xChartTypeContainer( xCooSysSequence[0], UNO_QUERY_THROW );
89
90
0
    Sequence< Reference< chart2::XChartType > > xChartTypeSequence( xChartTypeContainer->getChartTypes() );
91
0
    if ( !xChartTypeSequence.hasElements() )
92
0
        return false;
93
94
0
    const Reference<chart2::XChartType>& xCT = xChartTypeSequence[0];
95
96
0
    if ( xCT->getChartType() != "com.sun.star.chart2.LineChartType" )
97
0
        return false;
98
99
0
    Reference<chart2::XDataSeriesContainer> xDSCont(xCT, uno::UNO_QUERY);
100
101
0
    if (!xDSCont.is())
102
0
        return false;
103
104
0
    const Sequence<uno::Reference<chart2::XDataSeries> > aDataSeriesSeq = xDSCont->getDataSeries();
105
106
0
    bool bHasNoGapBlankValue = false;
107
0
    bool bHasEmptyCell = false;
108
109
0
    for (const auto& rDataSeries : aDataSeriesSeq)
110
0
    {
111
0
        uno::Reference<chart2::data::XDataSource> xDSrc(rDataSeries, uno::UNO_QUERY);
112
0
        if (!xDSrc.is())
113
0
            return false;
114
115
0
        const uno::Sequence<Reference<chart2::data::XLabeledDataSequence> > aDataSeqs = xDSrc->getDataSequences();
116
0
        for (const auto& rDataSeq : aDataSeqs)
117
0
        {
118
0
            Reference<chart2::data::XDataSequence> xValues = rDataSeq->getValues();
119
0
            if(!xValues.is())
120
0
                return false;
121
0
            Reference<beans::XPropertySet> xPropSet(xValues, uno::UNO_QUERY);
122
0
            if (!xPropSet.is())
123
0
                continue;
124
125
0
            OUString aRoleName;
126
0
            xPropSet->getPropertyValue(u"Role"_ustr) >>= aRoleName;
127
0
            if (aRoleName == "values-y")
128
0
            {
129
0
                const uno::Sequence<uno::Any> aData = xValues->getData();
130
0
                for (const auto& rVal : aData)
131
0
                {
132
0
                    double fVal;
133
0
                    OUString sStr;
134
0
                    if (rVal >>= fVal)
135
0
                        continue;
136
0
                    else if (rVal >>= sStr)
137
0
                        bHasNoGapBlankValue = true;
138
0
                    else
139
0
                        bHasEmptyCell = true;
140
0
                }
141
0
            }
142
0
        }
143
0
    }
144
145
0
    return bHasNoGapBlankValue && !bHasEmptyCell;
146
0
}
147
148
void ChartSpaceConverter::convertFromModel( const Reference< XShapes >& rxExternalPage, const awt::Point& rChartPos )
149
0
{
150
    /*  create data provider (virtual function in the ChartConverter class,
151
        derived converters may create an external data provider) */
152
0
    getChartConverter().createDataProvider( getChartDocument() );
153
154
    // formatting of the chart background.  The default fill style varies with applications.
155
0
    PropertySet aBackPropSet( getChartDocument()->getPageBackground() );
156
0
    getFormatter().convertFrameFormatting( aBackPropSet, mrModel.mxShapeProp, OBJECTTYPE_CHARTSPACE );
157
158
0
    bool bMSO2007Doc = getFilter().isMSO2007Document();
159
    // convert plot area (container of all chart type groups)
160
0
    PlotAreaConverter aPlotAreaConv( *this, mrModel.mxPlotArea.getOrCreate() );
161
0
    aPlotAreaConv.convertFromModel( mrModel.mxView3D.getOrCreate(bMSO2007Doc),
162
0
            mrModel.maCxData.getOrCreate());
163
164
    // plot area converter has created the diagram object
165
0
    Reference< XDiagram > xDiagram = getChartDocument()->getFirstDiagram();
166
167
    // convert wall and floor formatting in 3D charts
168
0
    if( xDiagram.is() && aPlotAreaConv.isWall3dChart() )
169
0
    {
170
0
        WallFloorConverter aFloorConv( *this, mrModel.mxFloor.getOrCreate() );
171
0
        aFloorConv.convertFromModel( xDiagram, OBJECTTYPE_FLOOR );
172
173
0
        WallFloorConverter aWallConv( *this, mrModel.mxBackWall.getOrCreate() );
174
0
        aWallConv.convertFromModel( xDiagram, OBJECTTYPE_WALL );
175
0
    }
176
177
    // chart title
178
    /* tdf#119138 autoTitleDeleted might be omitted by generators other than Excel
179
       while providing custom title. mbAutoTitleDel is set only based on the attribute value
180
       and the default also varies on whether MSO 2007 or newer is the generator, see tdf#78080 */
181
0
    if( !mrModel.mbAutoTitleDel || mrModel.mxTitle.is() ) try
182
0
    {
183
        /*  If the title model is missing, but the chart shows exactly one
184
            series, the series title is shown as chart title. */
185
0
        OUString aAutoTitle = aPlotAreaConv.getAutomaticTitle();
186
0
        if( mrModel.mxTitle.is() || !aAutoTitle.isEmpty() )
187
0
        {
188
            // tdf#146487 In some cases, we need to show the empty title
189
0
            bool bShowEmptyTitle = aAutoTitle.isEmpty() && !mrModel.mbAutoTitleDel
190
0
                                   && aPlotAreaConv.isSingleSeriesTitle()
191
0
                                   && mrModel.mxTitle->mxShapeProp.is()
192
0
                                   && mrModel.mxTitle->mxTextProp.is()
193
0
                                   && mrModel.mxTitle->mxTextProp->isEmpty();
194
            // Also for tdf#146487
195
0
            bool bEmptyRichText = mrModel.mxTitle
196
0
                && mrModel.mxTitle->mxText.is()
197
0
                && mrModel.mxTitle->mxText->mxTextBody.is()
198
0
                && mrModel.mxTitle->mxText->mxTextBody->isEmpty();
199
200
0
            if (aAutoTitle.isEmpty() && !bShowEmptyTitle && !bEmptyRichText)
201
0
                aAutoTitle = OoxResId(STR_DIAGRAM_TITLE);
202
0
            Reference< XTitled > xTitled( getChartDocument(), UNO_QUERY_THROW );
203
0
            TitleConverter aTitleConv( *this, mrModel.mxTitle.getOrCreate() );
204
0
            aTitleConv.convertFromModel( xTitled, aAutoTitle, OBJECTTYPE_CHARTTITLE );
205
0
        }
206
0
    }
207
0
    catch( Exception& )
208
0
    {
209
0
    }
210
211
    // legend
212
0
    if( xDiagram.is() && mrModel.mxLegend.is() )
213
0
    {
214
0
        LegendConverter aLegendConv( *this, *mrModel.mxLegend );
215
0
        aLegendConv.convertFromModel( xDiagram );
216
0
    }
217
218
    // treatment of missing values
219
0
    if( xDiagram.is() )
220
0
    {
221
0
        using namespace ::com::sun::star::chart::MissingValueTreatment;
222
0
        sal_Int32 nMissingValues = LEAVE_GAP;
223
224
        // tdf#134118 leave gap if the time unit is month
225
0
        bool bIsMonthBasedTimeUnit = false;
226
0
        if( mrModel.mxPlotArea.is() && mrModel.mxPlotArea->maAxes.size() > 0 &&
227
0
            mrModel.mxPlotArea->maAxes[0]->monBaseTimeUnit.has_value() )
228
0
        {
229
0
            bIsMonthBasedTimeUnit = mrModel.mxPlotArea->maAxes[0]->monBaseTimeUnit.value() == XML_months;
230
0
        }
231
232
0
        if (!bIsMonthBasedTimeUnit) switch( mrModel.mnDispBlanksAs )
233
0
        {
234
0
            case XML_gap:   nMissingValues = LEAVE_GAP; break;
235
0
            case XML_zero:  nMissingValues = USE_ZERO;  break;
236
0
            case XML_span:  nMissingValues = CONTINUE;  break;
237
0
        }
238
239
        // use a workaround, if it's possible for the difference of OOXML and OpenDocument
240
0
        if ( nMissingValues == LEAVE_GAP && lcl_useWorkaroundForNoGapInOOXML(getChartDocument()) )
241
0
            nMissingValues = USE_ZERO;
242
243
0
        PropertySet aDiaProp( xDiagram );
244
0
        aDiaProp.setProperty( PROP_MissingValueTreatment, nMissingValues );
245
246
0
        if (mrModel.mbExplicitStyle) {
247
            // The <c:style> value, now in mrModel.mnStyle, is handled in the
248
            // ObjectFormatter code. Set it here as a property just so it can be
249
            // stored for output.
250
0
            aDiaProp.setProperty( PROP_StyleIndex, mrModel.mnStyle);
251
0
        }
252
0
    }
253
254
    /*  Following all conversions needing the old Chart1 API that involves full
255
        initialization of the chart view. */
256
0
    namespace cssc = css::chart;
257
0
    Reference< cssc::XChartDocument > xChart1Doc( getChartDocument(), UNO_QUERY );
258
0
    if( xChart1Doc.is() )
259
0
    {
260
        /*  Set the IncludeHiddenCells property via the old API as only this
261
            ensures that the data provider and all created sequences get this
262
            flag correctly. */
263
0
        PropertySet aDiaProp( xChart1Doc->getDiagram() );
264
0
        aDiaProp.setProperty( PROP_IncludeHiddenCells, !mrModel.mbPlotVisOnly );
265
266
        // plot area position and size
267
0
        aPlotAreaConv.convertPositionFromModel();
268
269
        // positions of main title and all axis titles
270
0
        convertTitlePositions();
271
0
    }
272
273
    // embedded drawing shapes
274
0
    if( !mrModel.maDrawingPath.isEmpty() ) try
275
0
    {
276
        /*  Get the internal draw page of the chart document, if no external
277
            drawing page has been passed. */
278
0
        Reference< XShapes > xShapes;
279
0
        awt::Point aShapesOffset( 0, 0 );
280
0
        if( rxExternalPage.is() )
281
0
        {
282
0
            xShapes = rxExternalPage;
283
            // offset for embedded shapes to move them inside the chart area
284
0
            aShapesOffset = rChartPos;
285
0
        }
286
0
        else
287
0
        {
288
0
            Reference< XDrawPageSupplier > xDrawPageSupp( getChartDocument(), UNO_QUERY_THROW );
289
0
            xShapes.set( xDrawPageSupp->getDrawPage(), UNO_QUERY_THROW );
290
0
        }
291
292
        /*  If an external drawing page is passed, all embedded shapes will be
293
            inserted there (used e.g. with 'chart sheets' in spreadsheet
294
            documents). In this case, all types of shapes including OLE objects
295
            are supported. If the shapes are inserted into the internal chart
296
            drawing page instead, it is not possible to embed OLE objects. */
297
0
        bool bOleSupport = rxExternalPage.is();
298
299
0
        awt::Size aChartSize = getChartSize();
300
0
        if( aChartSize.Width <= 0 || aChartSize.Height <= 0 )
301
0
            aChartSize = getDefaultPageSize();
302
303
        // now, xShapes is not null anymore
304
0
        getFilter().importFragment( new ChartDrawingFragment(
305
0
            getFilter(), mrModel.maDrawingPath, xShapes, aChartSize, aShapesOffset, bOleSupport ) );
306
0
    }
307
0
    catch( Exception& )
308
0
    {
309
0
    }
310
311
    // pivot chart
312
0
    if ( mrModel.mbPivotChart )
313
0
    {
314
0
        PropertySet aProps( getChartDocument() );
315
0
        aProps.setProperty( PROP_DisableDataTableDialog , true );
316
0
        aProps.setProperty( PROP_DisableComplexChartTypes , true );
317
0
    }
318
319
0
    if(!mrModel.maSheetPath.isEmpty() )
320
0
    {
321
0
        Reference< css::chart::XChartDocument > xChartDoc( getChartDocument(), UNO_QUERY );
322
0
        PropertySet aProps( xChartDoc->getDiagram() );
323
0
        aProps.setProperty( PROP_ExternalData , uno::Any(mrModel.maSheetPath) );
324
0
    }
325
326
0
    if (mrModel.mbDate1904) {
327
0
        util::DateTime aNullDate(0,0,0,0,1,1,1904, false);
328
0
        Any aAny;
329
0
        aAny <<= aNullDate;
330
0
        Reference< css::chart::XChartDocument > xChartDoc( getChartDocument(), UNO_QUERY );
331
0
        Reference< beans::XPropertySet > xDocPropSet(xChartDoc, UNO_QUERY );
332
0
        xDocPropSet->setPropertyValue(u"NullDate"_ustr, aAny);
333
0
    }
334
0
}
335
336
} // namespace oox::drawingml::chart
337
338
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */