LibreOffice Module chart2 (master) 1
ColumnLineChartTypeTemplate.cxx
Go to the documentation of this file.
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
21#include "ColumnChartType.hxx"
22#include "LineChartType.hxx"
23#include <CommonConverters.hxx>
25#include <Diagram.hxx>
26#include <DiagramHelper.hxx>
27#include <DataSeries.hxx>
28#include <DataSeriesHelper.hxx>
31#include <PropertyHelper.hxx>
32#include <com/sun/star/beans/PropertyAttribute.hpp>
33#include <com/sun/star/drawing/LineStyle.hpp>
34#include <com/sun/star/uno/XComponentContext.hpp>
36
37#include <algorithm>
38
39using namespace ::com::sun::star::chart2;
40using namespace ::com::sun::star;
41
42using ::com::sun::star::uno::Reference;
43using ::com::sun::star::uno::Sequence;
44using ::com::sun::star::beans::Property;
45
46namespace
47{
48
49enum
50{
51 PROP_COL_LINE_NUMBER_OF_LINES
52};
53
54::chart::tPropertyValueMap& StaticColumnLineChartTypeTemplateDefaults()
55{
56 static ::chart::tPropertyValueMap aStaticDefaults =
57 []()
58 {
60 ::chart::PropertyHelper::setPropertyValueDefault< sal_Int32 >( aOutMap, PROP_COL_LINE_NUMBER_OF_LINES, 1 );
61 return aOutMap;
62 }();
63 return aStaticDefaults;
64}
65
66::cppu::OPropertyArrayHelper& StaticColumnLineChartTypeTemplateInfoHelper()
67{
68 static ::cppu::OPropertyArrayHelper aPropHelper(
69 []()
70 {
71 std::vector< css::beans::Property > aProperties {
72 { "NumberOfLines",
73 PROP_COL_LINE_NUMBER_OF_LINES,
75 beans::PropertyAttribute::BOUND
76 | beans::PropertyAttribute::MAYBEDEFAULT } };
77
78 std::sort( aProperties.begin(), aProperties.end(),
80
81 return comphelper::containerToSequence( aProperties );
82 }());
83 return aPropHelper;
84}
85
86uno::Reference< beans::XPropertySetInfo >& StaticColumnLineChartTypeTemplateInfo()
87{
88 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
89 ::cppu::OPropertySetHelper::createPropertySetInfo(StaticColumnLineChartTypeTemplateInfoHelper() ) );
90 return xPropertySetInfo;
91}
92
93} // anonymous namespace
94
95namespace chart
96{
97
100 uno::XComponentContext > const & xContext,
101 const OUString & rServiceName,
102 StackMode eStackMode,
103 sal_Int32 nNumberOfLines ) :
104 ChartTypeTemplate( xContext, rServiceName ),
105 m_eStackMode( eStackMode )
106{
107 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::Any( nNumberOfLines ));
108}
109
111{}
112
113// ____ OPropertySet ____
114void ColumnLineChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle, uno::Any& rAny ) const
115{
116 const tPropertyValueMap& rStaticDefaults = StaticColumnLineChartTypeTemplateDefaults();
117 tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
118 if( aFound == rStaticDefaults.end() )
119 rAny.clear();
120 else
121 rAny = (*aFound).second;
122}
123
125{
126 return StaticColumnLineChartTypeTemplateInfoHelper();
127}
128
129// ____ XPropertySet ____
131{
132 return StaticColumnLineChartTypeTemplateInfo();
133}
134
136 const std::vector< std::vector< rtl::Reference< DataSeries > > > & aSeriesSeq,
137 const std::vector< rtl::Reference< BaseCoordinateSystem > > & rCoordSys,
138 const std::vector< rtl::Reference< ChartType > >& aOldChartTypesSeq )
139{
140 if( rCoordSys.empty() )
141 return;
142
143 try
144 {
145 const std::vector< rtl::Reference< DataSeries > > aFlatSeriesSeq( FlattenSequence( aSeriesSeq ));
146 sal_Int32 nNumberOfSeries = aFlatSeriesSeq.size();
147 sal_Int32 nNumberOfLines = 0;
148 sal_Int32 nNumberOfColumns = 0;
149
150 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
151 OSL_ENSURE( nNumberOfLines>=0, "number of lines should be not negative" );
152 if( nNumberOfLines < 0 )
153 nNumberOfLines = 0;
154
155 if( nNumberOfLines >= nNumberOfSeries )
156 {
157 if( nNumberOfSeries > 0 )
158 {
159 nNumberOfLines = nNumberOfSeries - 1;
160 nNumberOfColumns = 1;
161 }
162 else
163 nNumberOfLines = 0;
164 }
165 else
166 nNumberOfColumns = nNumberOfSeries - nNumberOfLines;
167
168 // Columns
169
171
173
174 rCoordSys[ 0 ]->setChartTypes( std::vector{xCT} );
175
176 if( nNumberOfColumns > 0 )
177 {
178 std::vector< rtl::Reference< DataSeries > > aColumnSeq( nNumberOfColumns );
179 std::copy( aFlatSeriesSeq.begin(),
180 aFlatSeriesSeq.begin() + nNumberOfColumns,
181 aColumnSeq.begin());
182 xCT->setDataSeries( aColumnSeq );
183 }
184
185 // Lines
186
187 xCT = new LineChartType();
188 rCoordSys[ 0 ]->addChartType( xCT );
189
190 if( nNumberOfLines > 0 )
191 {
192 std::vector< rtl::Reference< DataSeries > > aLineSeq( nNumberOfLines );
193 std::copy( aFlatSeriesSeq.begin() + nNumberOfColumns,
194 aFlatSeriesSeq.end(),
195 aLineSeq.begin());
196 xCT->setDataSeries( aLineSeq );
197 }
198 }
199 catch( const uno::Exception & )
200 {
201 DBG_UNHANDLED_EXCEPTION("chart2");
202 }
203}
204
206 const rtl::Reference< DataSeries >& xSeries,
207 ::sal_Int32 nChartTypeIndex,
208 ::sal_Int32 nSeriesIndex,
209 ::sal_Int32 nSeriesCount )
210{
211 ChartTypeTemplate::applyStyle2( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
212
213 if( nChartTypeIndex==0 ) // columns
214 {
215 DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, "BorderStyle", uno::Any( drawing::LineStyle_NONE ) );
216 }
217 else if( nChartTypeIndex==1 ) // lines
218 {
220 DataSeriesHelper::switchSymbolsOnOrOff( xSeries, false, nSeriesIndex );
222 }
223}
224
225StackMode ColumnLineChartTypeTemplate::getStackMode( sal_Int32 nChartTypeIndex ) const
226{
227 if( nChartTypeIndex == 0 )
228 return m_eStackMode;
229 return StackMode::NONE;
230}
231
232// ____ XChartTypeTemplate ____
235 bool bAdaptProperties )
236{
237 bool bResult = false;
238
239 if( ! xDiagram.is())
240 return bResult;
241
242 try
243 {
244 rtl::Reference< ChartType > xColumnChartType;
246 rtl::Reference< ChartType > xLineChartType;
247 sal_Int32 nNumberOfChartTypes = 0;
248
249 for( rtl::Reference< BaseCoordinateSystem > const & coords : xDiagram->getBaseCoordinateSystems() )
250 {
251 const std::vector< rtl::Reference< ChartType > > aChartTypeSeq( coords->getChartTypes2());
252 for( rtl::Reference< ChartType > const & chartType : aChartTypeSeq )
253 {
254 ++nNumberOfChartTypes;
255 if( nNumberOfChartTypes > 2 )
256 break;
257 OUString aCTService = chartType->getChartType();
258 if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_COLUMN )
259 {
260 xColumnChartType = chartType;
261 xColumnChartCooSys = coords;
262 }
263 else if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_LINE )
264 xLineChartType = chartType;
265 }
266 if( nNumberOfChartTypes > 2 )
267 break;
268 }
269
270 if( nNumberOfChartTypes == 2 &&
271 xColumnChartType.is() &&
272 xLineChartType.is())
273 {
274 OSL_ASSERT( xColumnChartCooSys.is());
275
276 // check stackmode of bars
277 bResult = (xColumnChartCooSys->getDimension() == getDimension());
278 if( bResult )
279 {
280 bool bFound=false;
281 bool bAmbiguous=false;
283 xColumnChartType, bFound, bAmbiguous,
284 xColumnChartCooSys )
285 == getStackMode( 0 ) );
286
287 if( bResult && bAdaptProperties )
288 {
289 if( xLineChartType.is() )
290 {
291 sal_Int32 nNumberOfLines = xLineChartType->getDataSeries().getLength();
292 setFastPropertyValue_NoBroadcast( PROP_COL_LINE_NUMBER_OF_LINES, uno::Any( nNumberOfLines ));
293 }
294 }
295 }
296 }
297 }
298 catch( const uno::Exception & )
299 {
300 DBG_UNHANDLED_EXCEPTION("chart2");
301 }
302
303 return bResult;
304}
305
307{
308 if( nChartTypeIndex == 0 )
309 return new ColumnChartType();
310 else
311 return new LineChartType();
312}
313
315 const std::vector< rtl::Reference< ChartType > >& aFormerlyUsedChartTypes )
316{
318
319 try
320 {
321 xResult = new LineChartType();
322 ChartTypeTemplate::copyPropertiesFromOldToNewCoordinateSystem( aFormerlyUsedChartTypes, xResult );
323 }
324 catch( const uno::Exception & )
325 {
326 DBG_UNHANDLED_EXCEPTION("chart2");
327 }
328
329 return xResult;
330}
331
333{
334 if( ! m_xDataInterpreter.is())
335 {
336 sal_Int32 nNumberOfLines = 1;
337 getFastPropertyValue( PROP_COL_LINE_NUMBER_OF_LINES ) >>= nNumberOfLines;
338 m_xDataInterpreter = new ColumnLineDataInterpreter( nNumberOfLines );
339 }
340 else
341 {
342 //todo...
343 OSL_FAIL( "number of lines may not be valid anymore in the datainterpreter" );
344
345 }
346
347 return m_xDataInterpreter;
348}
349
352
353} // namespace chart
354
355/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const StackMode m_eStackMode
PropertiesInfo aProperties
For creating diagrams and modifying existing diagrams.
virtual sal_Int32 getDimension() const
returns 2 by default. Supported are 2 and 3
static void copyPropertiesFromOldToNewCoordinateSystem(const std::vector< rtl::Reference< ChartType > > &rOldChartTypesSeq, const rtl::Reference< ChartType > &xNewChartType)
rtl::Reference< ::chart::DataInterpreter > m_xDataInterpreter
virtual void applyStyle2(const rtl::Reference< ::chart::DataSeries > &xSeries, ::sal_Int32 nChartTypeIndex, ::sal_Int32 nSeriesIndex, ::sal_Int32 nSeriesCount)
virtual bool matchesTemplate2(const rtl::Reference< ::chart::Diagram > &xDiagram, bool bAdaptProperties) override
virtual void createChartTypes(const std::vector< std::vector< rtl::Reference< ::chart::DataSeries > > > &aSeriesSeq, const std::vector< rtl::Reference< ::chart::BaseCoordinateSystem > > &rCoordSys, const std::vector< rtl::Reference< ChartType > > &aOldChartTypesSeq) override
create a data series tree, that fits the requirements of the chart type.
virtual void applyStyle2(const rtl::Reference< ::chart::DataSeries > &xSeries, ::sal_Int32 nChartTypeGroupIndex, ::sal_Int32 nSeriesIndex, ::sal_Int32 nSeriesCount) override
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
ColumnLineChartTypeTemplate(css::uno::Reference< css::uno::XComponentContext > const &xContext, const OUString &rServiceName, StackMode eStackMode, sal_Int32 nNumberOfLines)
virtual rtl::Reference< ::chart::DataInterpreter > getDataInterpreter2() override
virtual StackMode getStackMode(sal_Int32 nChartTypeIndex) const override
returns StackMode::NONE by default.
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
The InfoHelper table contains all property names and types of this object.
virtual rtl::Reference< ::chart::ChartType > getChartTypeForIndex(sal_Int32 nChartTypeIndex) override
virtual void GetDefaultValue(sal_Int32 nHandle, css::uno::Any &rAny) const override
merge XInterface implementations
virtual rtl::Reference< ::chart::ChartType > getChartTypeForNewSeries2(const std::vector< rtl::Reference< ::chart::ChartType > > &aFormerlyUsedChartTypes) override
static StackMode getStackModeFromChartType(const rtl::Reference< ::chart::ChartType > &xChartType, bool &rbFound, bool &rbAmbiguous, const rtl::Reference< ::chart::BaseCoordinateSystem > &xCorrespondingCoordinateSystem)
Retrieves the stackmode of the first DataSeries or none.
static css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL createPropertySetInfo(IPropertyArrayHelper &rProperties)
css::uno::Type const & get()
virtual void SAL_CALL getFastPropertyValue(css::uno::Any &rValue, sal_Int32 nHandle) const override
The same as getFastPropertyValue, but return the value through rValue and nHandle is always valid.
virtual void SAL_CALL setFastPropertyValue_NoBroadcast(sal_Int32 nHandle, const css::uno::Any &rValue) override
The same as setFastPropertyValue; nHandle is always valid.
#define DBG_UNHANDLED_EXCEPTION(...)
OOO_DLLPUBLIC_CHARTTOOLS void switchLinesOnOrOff(const rtl::Reference< ::chart::DataSeries > &xSeries, bool bLinesOn)
OOO_DLLPUBLIC_CHARTTOOLS void setPropertyAlsoToAllAttributedDataPoints(const rtl::Reference< ::chart::DataSeries > &xSeries, const OUString &rPropertyName, const css::uno::Any &rPropertyValue)
OOO_DLLPUBLIC_CHARTTOOLS void makeLinesThickOrThin(const rtl::Reference< ::chart::DataSeries > &xSeries, bool bThick)
OOO_DLLPUBLIC_CHARTTOOLS void switchSymbolsOnOrOff(const rtl::Reference< ::chart::DataSeries > &xSeries, bool bSymbolsOn, sal_Int32 nSeriesIndex)
std::unordered_map< tPropertyValueMapKey, css::uno::Any > tPropertyValueMap
std::vector< T > FlattenSequence(const std::vector< std::vector< T > > &aSeqSeq)
std::vector< std::vector< T > > -> std::vector< T >
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
IMPLEMENT_FORWARD_XTYPEPROVIDER2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
IMPLEMENT_FORWARD_XINTERFACE2(ChildWindowPane, ChildWindowPaneInterfaceBase, Pane)
sal_Int32 nHandle
constexpr OUStringLiteral CHART2_SERVICE_NAME_CHARTTYPE_COLUMN
constexpr OUStringLiteral CHART2_SERVICE_NAME_CHARTTYPE_LINE