LibreOffice Module chart2 (master) 1
StockChartTypeTemplate.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"
23#include "LineChartType.hxx"
24#include <DataSeries.hxx>
25#include <DataSeriesHelper.hxx>
27#include <DiagramHelper.hxx>
28#include <Diagram.hxx>
31#include <com/sun/star/uno/XComponentContext.hpp>
32#include <PropertyHelper.hxx>
33#include <com/sun/star/beans/PropertyAttribute.hpp>
34#include <com/sun/star/drawing/LineStyle.hpp>
36
37#include <algorithm>
38#include <cstddef>
39
40using namespace ::com::sun::star;
41using namespace ::com::sun::star::chart2;
42
43using ::com::sun::star::uno::Reference;
44using ::com::sun::star::uno::Sequence;
45using ::com::sun::star::beans::Property;
46
47namespace
48{
49
50enum
51{
52 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
53 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
54 PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH,
55 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE
56};
57
58void lcl_AddPropertiesToVector(
59 std::vector< Property > & rOutProperties )
60{
61 rOutProperties.emplace_back( "Volume",
62 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
64 beans::PropertyAttribute::BOUND
65 | beans::PropertyAttribute::MAYBEDEFAULT );
66 rOutProperties.emplace_back( "Open",
67 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
69 beans::PropertyAttribute::BOUND
70 | beans::PropertyAttribute::MAYBEDEFAULT );
71 rOutProperties.emplace_back( "LowHigh",
72 PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH,
74 beans::PropertyAttribute::BOUND
75 | beans::PropertyAttribute::MAYBEDEFAULT );
76 rOutProperties.emplace_back( "Japanese",
77 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE,
79 beans::PropertyAttribute::BOUND
80 | beans::PropertyAttribute::MAYBEDEFAULT );
81}
82
83::chart::tPropertyValueMap& GetStaticStockChartTypeTemplateDefaults()
84{
85 static ::chart::tPropertyValueMap aStaticDefaults =
86 [](){
88 ::chart::PropertyHelper::setPropertyValueDefault( aTmp, PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME, false );
89 ::chart::PropertyHelper::setPropertyValueDefault( aTmp, PROP_STOCKCHARTTYPE_TEMPLATE_OPEN, false );
90 ::chart::PropertyHelper::setPropertyValueDefault( aTmp, PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH, true );
91 ::chart::PropertyHelper::setPropertyValueDefault( aTmp, PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE, false );
92 return aTmp;
93 }();
94 return aStaticDefaults;
95};
96
97::cppu::OPropertyArrayHelper& GetStaticStockChartTypeTemplateInfoHelper()
98{
99 static ::cppu::OPropertyArrayHelper aPropHelper =
100 [](){
101 std::vector< css::beans::Property > aProperties;
102 lcl_AddPropertiesToVector( aProperties );
103
104 std::sort( aProperties.begin(), aProperties.end(),
106
107 return comphelper::containerToSequence( aProperties );
108 }();
109 return aPropHelper;
110};
111
112
113uno::Reference< beans::XPropertySetInfo >& GetStaticStockChartTypeTemplateInfo()
114{
115 static uno::Reference< beans::XPropertySetInfo > xPropertySetInfo(
116 ::cppu::OPropertySetHelper::createPropertySetInfo(GetStaticStockChartTypeTemplateInfoHelper() ) );
117 return xPropertySetInfo;
118};
119
120} // anonymous namespace
121
122namespace chart
123{
124
127 uno::XComponentContext > const & xContext,
128 const OUString & rServiceName,
129 StockVariant eVariant,
130 bool bJapaneseStyle ) :
131 ChartTypeTemplate( xContext, rServiceName ),
132 m_eStockVariant( eVariant )
133{
135 PROP_STOCKCHARTTYPE_TEMPLATE_OPEN,
136 uno::Any( ( eVariant == StockVariant::Open ||
137 eVariant == StockVariant::VolumeOpen )));
139 PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME,
140 uno::Any( ( eVariant == StockVariant::Volume ||
141 eVariant == StockVariant::VolumeOpen )));
143 PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE,
144 uno::Any( bJapaneseStyle ));
145}
146
148{}
149
150// ____ OPropertySet ____
151void StockChartTypeTemplate::GetDefaultValue( sal_Int32 nHandle, uno::Any& rAny ) const
152{
153 const tPropertyValueMap& rStaticDefaults = GetStaticStockChartTypeTemplateDefaults();
154 tPropertyValueMap::const_iterator aFound( rStaticDefaults.find( nHandle ) );
155 if( aFound == rStaticDefaults.end() )
156 rAny.clear();
157 else
158 rAny = (*aFound).second;
159}
160
162{
163 return GetStaticStockChartTypeTemplateInfoHelper();
164}
165
166// ____ XPropertySet ____
168{
169 return GetStaticStockChartTypeTemplateInfo();
170}
171
172sal_Int32 StockChartTypeTemplate::getAxisCountByDimension( sal_Int32 nDimension )
173{
174 // one x-axis
175 if( nDimension <= 0 )
176 return 1;
177 // no further axes
178 if( nDimension >= 2 )
179 return 0;
180
181 // one or two y-axes depending on volume
182 OSL_ASSERT( nDimension == 1 );
183 bool bHasVolume = false;
184 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
185 return bHasVolume ? 2 : 1;
186}
187
189 const rtl::Reference< DataSeries >& xSeries,
190 ::sal_Int32 nChartTypeIndex,
191 ::sal_Int32 nSeriesIndex,
192 ::sal_Int32 nSeriesCount )
193{
194 ChartTypeTemplate::applyStyle2( xSeries, nChartTypeIndex, nSeriesIndex, nSeriesCount );
195 try
196 {
197 sal_Int32 nNewAxisIndex = 0;
198
199 bool bHasVolume = false;
200 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
201 if( bHasVolume && nChartTypeIndex != 0 )
202 nNewAxisIndex = 1;
203
204 xSeries->setPropertyValue( "AttachedAxisIndex", uno::Any( nNewAxisIndex ) );
205
206 if( bHasVolume && nChartTypeIndex==0 )
207 {
208 //switch lines off for volume bars
209 DataSeriesHelper::setPropertyAlsoToAllAttributedDataPoints( xSeries, "BorderStyle", uno::Any( drawing::LineStyle_NONE ) );
210 }
211 else
212 {
213 //ensure that lines are on
214 drawing::LineStyle eStyle = drawing::LineStyle_NONE;
215 xSeries->getPropertyValue( "LineStyle" ) >>= eStyle;
216 if( eStyle == drawing::LineStyle_NONE )
217 xSeries->setPropertyValue( "LineStyle", uno::Any( drawing::LineStyle_SOLID ));
218 }
219
220 }
221 catch( const uno::Exception & )
222 {
223 DBG_UNHANDLED_EXCEPTION("chart2");
224 }
225}
226
228 const rtl::Reference< ::chart::Diagram >& xDiagram )
229{
231 if( getDimension() == 3 )
232 {
233 std::vector< rtl::Reference< DataSeries > > aSeriesVec =
234 xDiagram->getDataSeries();
235 for (auto const& series : aSeriesVec)
236 {
237 series->setPropertyValue( "AttachedAxisIndex", uno::Any( sal_Int32(0) ) );
238 }
239 }
240
241 xDiagram->setVertical( false );
242}
243
245{
247 bool bHasVolume = false;
248 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
249 if( bHasVolume )
250 {
251 if( nChartTypeIndex == 0 )
252 xCT = new ColumnChartType();
253 else if( nChartTypeIndex == 1 )
254 xCT = new CandleStickChartType();
255 else
256 xCT = new LineChartType();
257 }
258 else
259 {
260 if( nChartTypeIndex == 0 )
261 xCT = new CandleStickChartType();
262 else
263 xCT = new LineChartType();
264 }
265 return xCT;
266}
267
269 const std::vector< std::vector< rtl::Reference< DataSeries > > > & aSeriesSeq,
270 const std::vector< rtl::Reference< BaseCoordinateSystem > > & rCoordSys,
271 const std::vector< rtl::Reference< ChartType > >& /* aOldChartTypesSeq */ )
272{
273 if( rCoordSys.empty() )
274 return;
275
276 try
277 {
278 bool bHasVolume = false;
279 bool bShowFirst = false;
280 bool bJapaneseStyle = false;
281 bool bShowHighLow = true;
282
283 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
284 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_OPEN ) >>= bShowFirst;
285 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE ) >>= bJapaneseStyle;
286 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_LOW_HIGH ) >>= bShowHighLow;
287
288 std::size_t nSeriesIndex = 0;
289
290 std::vector< rtl::Reference< ChartType > > aChartTypeVec;
291 // Bars (Volume)
292 if( bHasVolume )
293 {
294 // chart type
296 aChartTypeVec.push_back( xCT );
297
298 if( aSeriesSeq.size() > nSeriesIndex &&
299 !aSeriesSeq[nSeriesIndex].empty() )
300 {
301 xCT->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
302 }
303 ++nSeriesIndex;
304 }
305
307 aChartTypeVec.push_back( xCT );
308
309 xCT->setPropertyValue( "Japanese", uno::Any( bJapaneseStyle ));
310 xCT->setPropertyValue( "ShowFirst", uno::Any( bShowFirst ));
311 xCT->setPropertyValue( "ShowHighLow", uno::Any( bShowHighLow ));
312
313 if( aSeriesSeq.size() > nSeriesIndex &&
314 !aSeriesSeq[ nSeriesIndex ].empty() )
315 {
316 xCT->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
317 }
318 ++nSeriesIndex;
319
320 // Lines (remaining series)
321 if( aSeriesSeq.size() > nSeriesIndex &&
322 !aSeriesSeq[ nSeriesIndex ].empty() )
323 {
324 xCT = new LineChartType();
325 aChartTypeVec.push_back( xCT );
326
327 xCT->setDataSeries( aSeriesSeq[ nSeriesIndex ] );
328 }
329
330 rCoordSys[ 0 ]->setChartTypes( aChartTypeVec );
331 }
332 catch( const uno::Exception & )
333 {
334 DBG_UNHANDLED_EXCEPTION("chart2");
335 }
336}
337
338// ____ ChartTypeTemplate ____
341 bool /* bAdaptProperties */ )
342{
343 bool bResult = false;
344
345 if( ! xDiagram.is())
346 return bResult;
347
348 try
349 {
350 bool bHasVolume = false, bHasOpenValue = false, bHasJapaneseStyle = false;
351
352 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_VOLUME ) >>= bHasVolume;
353 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_OPEN ) >>= bHasOpenValue;
354 getFastPropertyValue( PROP_STOCKCHARTTYPE_TEMPLATE_JAPANESE ) >>= bHasJapaneseStyle;
355
356 rtl::Reference< ChartType > xVolumeChartType;
357 rtl::Reference< ChartType > xCandleStickChartType;
358 rtl::Reference< ChartType > xLineChartType;
359 sal_Int32 nNumberOfChartTypes = 0;
360
361 for( rtl::Reference< BaseCoordinateSystem > const & coords : xDiagram->getBaseCoordinateSystems() )
362 {
363 for( rtl::Reference< ChartType > const & chartType : coords->getChartTypes2() )
364 {
365 ++nNumberOfChartTypes;
366 if( nNumberOfChartTypes > 3 )
367 break;
368 OUString aCTService = chartType->getChartType();
369 if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_COLUMN )
370 xVolumeChartType = chartType;
371 else if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_CANDLESTICK )
372 xCandleStickChartType = chartType;
373 else if( aCTService == CHART2_SERVICE_NAME_CHARTTYPE_LINE )
374 xLineChartType = chartType;
375 }
376 if( nNumberOfChartTypes > 3 )
377 break;
378 }
379
380 if (xCandleStickChartType.is() && bHasVolume == xVolumeChartType.is())
381 {
382 bResult = true;
383
384 // check for japanese style
385 bool bJapaneseProp = false;
386 xCandleStickChartType->getPropertyValue( "Japanese") >>= bJapaneseProp;
387 bResult = bResult && ( bHasJapaneseStyle == bJapaneseProp );
388
389 // in old chart japanese == showFirst
390 bool bShowFirstProp = false;
391 xCandleStickChartType->getPropertyValue( "ShowFirst") >>= bShowFirstProp;
392 bResult = bResult && ( bHasOpenValue == bShowFirstProp );
393 }
394 }
395 catch( const uno::Exception & )
396 {
397 DBG_UNHANDLED_EXCEPTION("chart2");
398 }
399
400 return bResult;
401}
402
404 const std::vector< rtl::Reference< ChartType > >& aFormerlyUsedChartTypes )
405{
407
408 try
409 {
410 xResult = new LineChartType();
411 ChartTypeTemplate::copyPropertiesFromOldToNewCoordinateSystem( aFormerlyUsedChartTypes, xResult );
412 }
413 catch( const uno::Exception & )
414 {
415 DBG_UNHANDLED_EXCEPTION("chart2");
416 }
417
418 return xResult;
419}
420
422{
423 if( ! m_xDataInterpreter.is())
425
426 return m_xDataInterpreter;
427}
428
431
432} // namespace chart
433
434/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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 void resetStyles2(const rtl::Reference< ::chart::Diagram > &xDiagram)
virtual bool matchesTemplate2(const rtl::Reference< ::chart::Diagram > &xDiagram, bool bAdaptProperties) override
virtual rtl::Reference< ::chart::DataInterpreter > getDataInterpreter2() 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 GetDefaultValue(sal_Int32 nHandle, css::uno::Any &rAny) const override
merge XInterface implementations
virtual rtl::Reference< ::chart::ChartType > getChartTypeForIndex(sal_Int32 nChartTypeIndex) override
StockChartTypeTemplate(css::uno::Reference< css::uno::XComponentContext > const &xContext, const OUString &rServiceName, StockVariant eVariant, bool bJapaneseStyle)
CTOR.
virtual rtl::Reference< ::chart::ChartType > getChartTypeForNewSeries2(const std::vector< rtl::Reference< ::chart::ChartType > > &aFormerlyUsedChartTypes) override
virtual void applyStyle2(const rtl::Reference< ::chart::DataSeries > &xSeries, ::sal_Int32 nChartTypeIndex, ::sal_Int32 nSeriesIndex, ::sal_Int32 nSeriesCount) override
virtual ::cppu::IPropertyArrayHelper &SAL_CALL getInfoHelper() override
The InfoHelper table contains all property names and types of this object.
virtual css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL getPropertySetInfo() override
virtual void resetStyles2(const rtl::Reference< ::chart::Diagram > &xDiagram) override
virtual sal_Int32 getAxisCountByDimension(sal_Int32 nDimension) override
Give the number of requested axis per dimension here.
static css::uno::Reference< css::beans::XPropertySetInfo > SAL_CALL createPropertySetInfo(IPropertyArrayHelper &rProperties)
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 setPropertyAlsoToAllAttributedDataPoints(const rtl::Reference< ::chart::DataSeries > &xSeries, const OUString &rPropertyName, const css::uno::Any &rPropertyValue)
void setPropertyValueDefault(tPropertyValueMap &rOutMap, tPropertyValueMapKey key, const Value &value)
Calls setPropertyValue() but asserts that the given property hasn't been set before.
std::unordered_map< tPropertyValueMapKey, css::uno::Any > tPropertyValueMap
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_CANDLESTICK
constexpr OUStringLiteral CHART2_SERVICE_NAME_CHARTTYPE_LINE