xref: /AOO41X/main/xmloff/source/chart/SchXMLSeriesHelper.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_xmloff.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "SchXMLSeriesHelper.hxx"
32*cdf0e10cSrcweir #include <com/sun/star/chart2/XChartDocument.hpp>
33*cdf0e10cSrcweir #include <com/sun/star/chart2/XChartTypeContainer.hpp>
34*cdf0e10cSrcweir #include <com/sun/star/chart2/XCoordinateSystemContainer.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/chart2/XDataSeriesContainer.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // header for define RTL_CONSTASCII_USTRINGPARAM
40*cdf0e10cSrcweir #include <rtl/ustring.h>
41*cdf0e10cSrcweir // header for define DBG_ERROR1
42*cdf0e10cSrcweir #include <tools/debug.hxx>
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir #include <typeinfo>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir using namespace ::com::sun::star;
47*cdf0e10cSrcweir using ::rtl::OUString;
48*cdf0e10cSrcweir using ::rtl::OUStringToOString;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir using ::com::sun::star::uno::Reference;
51*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence;
52*cdf0e10cSrcweir using ::rtl::OUString;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir // ----------------------------------------
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir ::std::vector< Reference< chart2::XDataSeries > >
57*cdf0e10cSrcweir     SchXMLSeriesHelper::getDataSeriesFromDiagram(
58*cdf0e10cSrcweir         const Reference< chart2::XDiagram > & xDiagram )
59*cdf0e10cSrcweir {
60*cdf0e10cSrcweir     ::std::vector< Reference< chart2::XDataSeries > > aResult;
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     try
63*cdf0e10cSrcweir     {
64*cdf0e10cSrcweir         Reference< chart2::XCoordinateSystemContainer > xCooSysCnt(
65*cdf0e10cSrcweir             xDiagram, uno::UNO_QUERY_THROW );
66*cdf0e10cSrcweir         Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq(
67*cdf0e10cSrcweir             xCooSysCnt->getCoordinateSystems());
68*cdf0e10cSrcweir         for( sal_Int32 i=0; i<aCooSysSeq.getLength(); ++i )
69*cdf0e10cSrcweir         {
70*cdf0e10cSrcweir             Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[i], uno::UNO_QUERY_THROW );
71*cdf0e10cSrcweir             Sequence< Reference< chart2::XChartType > > aChartTypeSeq( xCTCnt->getChartTypes());
72*cdf0e10cSrcweir             for( sal_Int32 j=0; j<aChartTypeSeq.getLength(); ++j )
73*cdf0e10cSrcweir             {
74*cdf0e10cSrcweir                 Reference< chart2::XDataSeriesContainer > xDSCnt( aChartTypeSeq[j], uno::UNO_QUERY_THROW );
75*cdf0e10cSrcweir                 Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xDSCnt->getDataSeries() );
76*cdf0e10cSrcweir                 ::std::copy( aSeriesSeq.getConstArray(), aSeriesSeq.getConstArray() + aSeriesSeq.getLength(),
77*cdf0e10cSrcweir                              ::std::back_inserter( aResult ));
78*cdf0e10cSrcweir             }
79*cdf0e10cSrcweir         }
80*cdf0e10cSrcweir     }
81*cdf0e10cSrcweir     catch( uno::Exception & ex )
82*cdf0e10cSrcweir     {
83*cdf0e10cSrcweir         (void)ex; // avoid warning for pro build
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         OSL_ENSURE( false, OUStringToOString( OUString(
86*cdf0e10cSrcweir                         OUString( RTL_CONSTASCII_USTRINGPARAM( "Exception caught. Type: " )) +
87*cdf0e10cSrcweir                         OUString::createFromAscii( typeid( ex ).name()) +
88*cdf0e10cSrcweir                         OUString( RTL_CONSTASCII_USTRINGPARAM( ", Message: " )) +
89*cdf0e10cSrcweir                         ex.Message), RTL_TEXTENCODING_ASCII_US ).getStr());
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     return aResult;
94*cdf0e10cSrcweir }
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir ::std::map< Reference< chart2::XDataSeries >, sal_Int32 > SchXMLSeriesHelper::getDataSeriesIndexMapFromDiagram(
97*cdf0e10cSrcweir         const Reference< chart2::XDiagram > & xDiagram )
98*cdf0e10cSrcweir {
99*cdf0e10cSrcweir     ::std::map< Reference< chart2::XDataSeries >, sal_Int32 > aRet;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir     sal_Int32 nIndex=0;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     ::std::vector< Reference< chart2::XDataSeries > > aSeriesVector( SchXMLSeriesHelper::getDataSeriesFromDiagram( xDiagram ));
104*cdf0e10cSrcweir     for( ::std::vector< Reference< chart2::XDataSeries > >::const_iterator aSeriesIt( aSeriesVector.begin() )
105*cdf0e10cSrcweir         ; aSeriesIt != aSeriesVector.end()
106*cdf0e10cSrcweir         ; aSeriesIt++, nIndex++ )
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir         Reference< chart2::XDataSeries > xSeries( *aSeriesIt );
109*cdf0e10cSrcweir         if( xSeries.is() )
110*cdf0e10cSrcweir         {
111*cdf0e10cSrcweir             if( aRet.end() == aRet.find(xSeries) )
112*cdf0e10cSrcweir                 aRet[xSeries]=nIndex;
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir     return aRet;
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir uno::Reference< chart2::XChartType > lcl_getChartTypeOfSeries(
119*cdf0e10cSrcweir 								const uno::Reference< chart2::XDiagram >&   xDiagram
120*cdf0e10cSrcweir 						      , const Reference< chart2::XDataSeries >& xSeries )
121*cdf0e10cSrcweir {
122*cdf0e10cSrcweir     if(!xDiagram.is())
123*cdf0e10cSrcweir         return 0;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 	//iterate through the model to find the given xSeries
126*cdf0e10cSrcweir 	//the found parent indicates the charttype
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir     //iterate through all coordinate systems
129*cdf0e10cSrcweir     uno::Reference< chart2::XCoordinateSystemContainer > xCooSysContainer( xDiagram, uno::UNO_QUERY );
130*cdf0e10cSrcweir     if( !xCooSysContainer.is())
131*cdf0e10cSrcweir         return 0;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     uno::Sequence< uno::Reference< chart2::XCoordinateSystem > > aCooSysList( xCooSysContainer->getCoordinateSystems() );
134*cdf0e10cSrcweir     for( sal_Int32 nCS = 0; nCS < aCooSysList.getLength(); ++nCS )
135*cdf0e10cSrcweir     {
136*cdf0e10cSrcweir         uno::Reference< chart2::XCoordinateSystem > xCooSys( aCooSysList[nCS] );
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         //iterate through all chart types in the current coordinate system
139*cdf0e10cSrcweir         uno::Reference< chart2::XChartTypeContainer > xChartTypeContainer( xCooSys, uno::UNO_QUERY );
140*cdf0e10cSrcweir         OSL_ASSERT( xChartTypeContainer.is());
141*cdf0e10cSrcweir         if( !xChartTypeContainer.is() )
142*cdf0e10cSrcweir             continue;
143*cdf0e10cSrcweir         uno::Sequence< uno::Reference< chart2::XChartType > > aChartTypeList( xChartTypeContainer->getChartTypes() );
144*cdf0e10cSrcweir         for( sal_Int32 nT = 0; nT < aChartTypeList.getLength(); ++nT )
145*cdf0e10cSrcweir         {
146*cdf0e10cSrcweir             uno::Reference< chart2::XChartType > xChartType( aChartTypeList[nT] );
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir             //iterate through all series in this chart type
149*cdf0e10cSrcweir             uno::Reference< chart2::XDataSeriesContainer > xDataSeriesContainer( xChartType, uno::UNO_QUERY );
150*cdf0e10cSrcweir             OSL_ASSERT( xDataSeriesContainer.is());
151*cdf0e10cSrcweir             if( !xDataSeriesContainer.is() )
152*cdf0e10cSrcweir                 continue;
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir             uno::Sequence< uno::Reference< chart2::XDataSeries > > aSeriesList( xDataSeriesContainer->getDataSeries() );
155*cdf0e10cSrcweir             for( sal_Int32 nS = 0; nS < aSeriesList.getLength(); ++nS )
156*cdf0e10cSrcweir             {
157*cdf0e10cSrcweir                 Reference< chart2::XDataSeries > xCurrentSeries( aSeriesList[nS] );
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir                 if( xSeries == xCurrentSeries )
160*cdf0e10cSrcweir                     return xChartType;
161*cdf0e10cSrcweir             }
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir     }
164*cdf0e10cSrcweir 	return 0;
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir bool SchXMLSeriesHelper::isCandleStickSeries(
168*cdf0e10cSrcweir                   const Reference< chart2::XDataSeries >& xSeries
169*cdf0e10cSrcweir                 , const Reference< frame::XModel >& xChartModel )
170*cdf0e10cSrcweir {
171*cdf0e10cSrcweir     bool bRet = false;
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     uno::Reference< chart2::XChartDocument > xNewDoc( xChartModel, uno::UNO_QUERY );
174*cdf0e10cSrcweir     if( xNewDoc.is() )
175*cdf0e10cSrcweir     {
176*cdf0e10cSrcweir         uno::Reference< chart2::XDiagram > xNewDiagram( xNewDoc->getFirstDiagram() );
177*cdf0e10cSrcweir         if( xNewDiagram.is() )
178*cdf0e10cSrcweir         {
179*cdf0e10cSrcweir             uno::Reference< chart2::XChartType > xChartType( lcl_getChartTypeOfSeries(
180*cdf0e10cSrcweir 								        xNewDiagram, xSeries ) );
181*cdf0e10cSrcweir             if( xChartType.is() )
182*cdf0e10cSrcweir             {
183*cdf0e10cSrcweir                 rtl::OUString aServiceName( xChartType->getChartType() );
184*cdf0e10cSrcweir                 if( aServiceName.equals( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.CandleStickChartType" ) ) ) )
185*cdf0e10cSrcweir                     bRet = true;
186*cdf0e10cSrcweir             }
187*cdf0e10cSrcweir         }
188*cdf0e10cSrcweir     }
189*cdf0e10cSrcweir     return bRet;
190*cdf0e10cSrcweir }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir // static
193*cdf0e10cSrcweir Reference< chart2::XDataSeries > SchXMLSeriesHelper::getFirstCandleStickSeries(
194*cdf0e10cSrcweir     const Reference< chart2::XDiagram > & xDiagram  )
195*cdf0e10cSrcweir {
196*cdf0e10cSrcweir     Reference< chart2::XDataSeries > xResult;
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     try
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir         Reference< chart2::XCoordinateSystemContainer > xCooSysCnt( xDiagram, uno::UNO_QUERY_THROW );
201*cdf0e10cSrcweir         Sequence< Reference< chart2::XCoordinateSystem > > aCooSysSeq( xCooSysCnt->getCoordinateSystems());
202*cdf0e10cSrcweir         for( sal_Int32 nCooSysIdx=0; !xResult.is() && nCooSysIdx<aCooSysSeq.getLength(); ++nCooSysIdx )
203*cdf0e10cSrcweir         {
204*cdf0e10cSrcweir             Reference< chart2::XChartTypeContainer > xCTCnt( aCooSysSeq[nCooSysIdx], uno::UNO_QUERY_THROW );
205*cdf0e10cSrcweir             Sequence< Reference< chart2::XChartType > > aCTSeq( xCTCnt->getChartTypes());
206*cdf0e10cSrcweir             for( sal_Int32 nCTIdx=0; !xResult.is() && nCTIdx<aCTSeq.getLength(); ++nCTIdx )
207*cdf0e10cSrcweir             {
208*cdf0e10cSrcweir                 if( aCTSeq[nCTIdx]->getChartType().equals(
209*cdf0e10cSrcweir                         ::rtl::OUString::createFromAscii("com.sun.star.chart2.CandleStickChartType")))
210*cdf0e10cSrcweir                 {
211*cdf0e10cSrcweir                     Reference< chart2::XDataSeriesContainer > xSeriesCnt( aCTSeq[nCTIdx], uno::UNO_QUERY_THROW );
212*cdf0e10cSrcweir                     Sequence< Reference< chart2::XDataSeries > > aSeriesSeq( xSeriesCnt->getDataSeries() );
213*cdf0e10cSrcweir                     if( aSeriesSeq.getLength())
214*cdf0e10cSrcweir                         xResult.set( aSeriesSeq[0] );
215*cdf0e10cSrcweir                     break;
216*cdf0e10cSrcweir                 }
217*cdf0e10cSrcweir             }
218*cdf0e10cSrcweir         }
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir     catch( const uno::Exception & )
221*cdf0e10cSrcweir     {
222*cdf0e10cSrcweir         OSL_ENSURE( false, "Exception caught" );
223*cdf0e10cSrcweir     }
224*cdf0e10cSrcweir     return xResult;
225*cdf0e10cSrcweir }
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir //static
228*cdf0e10cSrcweir uno::Reference< beans::XPropertySet > SchXMLSeriesHelper::createOldAPISeriesPropertySet(
229*cdf0e10cSrcweir             const uno::Reference< chart2::XDataSeries >& xSeries
230*cdf0e10cSrcweir             , const uno::Reference< frame::XModel >& xChartModel )
231*cdf0e10cSrcweir {
232*cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xRet;
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir     if( xSeries.is() )
235*cdf0e10cSrcweir     {
236*cdf0e10cSrcweir         try
237*cdf0e10cSrcweir         {
238*cdf0e10cSrcweir             uno::Reference< lang::XMultiServiceFactory > xFactory( xChartModel, uno::UNO_QUERY );
239*cdf0e10cSrcweir             if( xFactory.is() )
240*cdf0e10cSrcweir             {
241*cdf0e10cSrcweir                 xRet = uno::Reference< beans::XPropertySet >( xFactory->createInstance(
242*cdf0e10cSrcweir                     OUString::createFromAscii( "com.sun.star.comp.chart2.DataSeriesWrapper" ) ), uno::UNO_QUERY );
243*cdf0e10cSrcweir                 Reference< lang::XInitialization > xInit( xRet, uno::UNO_QUERY );
244*cdf0e10cSrcweir                 if(xInit.is())
245*cdf0e10cSrcweir                 {
246*cdf0e10cSrcweir                     Sequence< uno::Any > aArguments(1);
247*cdf0e10cSrcweir                     aArguments[0]=uno::makeAny(xSeries);
248*cdf0e10cSrcweir                     xInit->initialize(aArguments);
249*cdf0e10cSrcweir                 }
250*cdf0e10cSrcweir             }
251*cdf0e10cSrcweir         }
252*cdf0e10cSrcweir         catch( uno::Exception & rEx )
253*cdf0e10cSrcweir 	    {
254*cdf0e10cSrcweir             (void)rEx; // avoid warning for pro build
255*cdf0e10cSrcweir             DBG_ERROR1( "Exception caught SchXMLSeriesHelper::createOldAPISeriesPropertySet: %s",
256*cdf0e10cSrcweir                         OUStringToOString( rEx.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
257*cdf0e10cSrcweir         }
258*cdf0e10cSrcweir     }
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     return xRet;
261*cdf0e10cSrcweir }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir //static
264*cdf0e10cSrcweir uno::Reference< beans::XPropertySet > SchXMLSeriesHelper::createOldAPIDataPointPropertySet(
265*cdf0e10cSrcweir             const uno::Reference< chart2::XDataSeries >& xSeries
266*cdf0e10cSrcweir             , sal_Int32 nPointIndex
267*cdf0e10cSrcweir             , const uno::Reference< frame::XModel >& xChartModel )
268*cdf0e10cSrcweir {
269*cdf0e10cSrcweir     uno::Reference< beans::XPropertySet > xRet;
270*cdf0e10cSrcweir 
271*cdf0e10cSrcweir     if( xSeries.is() )
272*cdf0e10cSrcweir     {
273*cdf0e10cSrcweir         try
274*cdf0e10cSrcweir         {
275*cdf0e10cSrcweir             uno::Reference< lang::XMultiServiceFactory > xFactory( xChartModel, uno::UNO_QUERY );
276*cdf0e10cSrcweir             if( xFactory.is() )
277*cdf0e10cSrcweir             {
278*cdf0e10cSrcweir                 xRet = uno::Reference< beans::XPropertySet >( xFactory->createInstance(
279*cdf0e10cSrcweir                     OUString::createFromAscii( "com.sun.star.comp.chart2.DataSeriesWrapper" ) ), uno::UNO_QUERY );
280*cdf0e10cSrcweir                 Reference< lang::XInitialization > xInit( xRet, uno::UNO_QUERY );
281*cdf0e10cSrcweir                 if(xInit.is())
282*cdf0e10cSrcweir                 {
283*cdf0e10cSrcweir                     Sequence< uno::Any > aArguments(2);
284*cdf0e10cSrcweir                     aArguments[0]=uno::makeAny(xSeries);
285*cdf0e10cSrcweir                     aArguments[1]=uno::makeAny(nPointIndex);
286*cdf0e10cSrcweir                     xInit->initialize(aArguments);
287*cdf0e10cSrcweir                 }
288*cdf0e10cSrcweir             }
289*cdf0e10cSrcweir         }
290*cdf0e10cSrcweir         catch( uno::Exception & rEx )
291*cdf0e10cSrcweir 	    {
292*cdf0e10cSrcweir             (void)rEx; // avoid warning for pro build
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir             DBG_ERROR1( "Exception caught SchXMLSeriesHelper::createOldAPIDataPointPropertySet: %s",
295*cdf0e10cSrcweir                         OUStringToOString( rEx.Message, RTL_TEXTENCODING_ASCII_US ).getStr() );
296*cdf0e10cSrcweir         }
297*cdf0e10cSrcweir     }
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir     return xRet;
300*cdf0e10cSrcweir }
301*cdf0e10cSrcweir 
302