xref: /aoo42x/main/chart2/source/tools/Scaling.cxx (revision cde9e8dc)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_chart2.hxx"
26 #include "Scaling.hxx"
27 #include <rtl/math.hxx>
28 #include "com/sun/star/uno/RuntimeException.hpp"
29 
30 namespace
31 {
32 
33 static const ::rtl::OUString lcl_aServiceName_Logarithmic(
34     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LogarithmicScaling" ));
35 static const ::rtl::OUString lcl_aServiceName_Exponential(
36     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.ExponentialScaling" ));
37 static const ::rtl::OUString lcl_aServiceName_Linear(
38     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.LinearScaling" ));
39 static const ::rtl::OUString lcl_aServiceName_Power(
40     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.chart2.PowerScaling" ));
41 
42 static const ::rtl::OUString lcl_aImplementationName_Logarithmic(
43     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.LogarithmicScaling" ));
44 static const ::rtl::OUString lcl_aImplementationName_Exponential(
45     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.ExponentialScaling" ));
46 static const ::rtl::OUString lcl_aImplementationName_Linear(
47     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.LinearScaling" ));
48 static const ::rtl::OUString lcl_aImplementationName_Power(
49     RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.chart2.PowerScaling" ));
50 }
51 
52 //.............................................................................
53 namespace chart
54 {
55 //.............................................................................
56 using namespace ::com::sun::star;
57 using namespace ::com::sun::star::chart2;
58 
LogarithmicScaling(const uno::Reference<uno::XComponentContext> & xContext)59 LogarithmicScaling::LogarithmicScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
60         m_fBase( 10.0 ),
61         m_fLogOfBase( log( 10.0 ) ),
62         m_xContext( xContext )
63 {
64 }
65 
LogarithmicScaling(double fBase)66 LogarithmicScaling::LogarithmicScaling( double fBase ) :
67         m_fBase( fBase ),
68         m_fLogOfBase( log( fBase ) )
69 {
70 }
71 
~LogarithmicScaling()72 LogarithmicScaling::~LogarithmicScaling()
73 {
74 }
75 
76     double SAL_CALL
doScaling(double value)77 LogarithmicScaling::doScaling( double value )
78     throw (uno::RuntimeException)
79 {
80     double fResult;
81     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
82         ::rtl::math::setNan( & fResult );
83     else
84         fResult = log( value ) / m_fLogOfBase;
85     return fResult;
86 }
87 
88     uno::Reference< XScaling > SAL_CALL
getInverseScaling()89 LogarithmicScaling::getInverseScaling()
90     throw (uno::RuntimeException)
91 {
92     return new ExponentialScaling( m_fBase );
93 }
94 
95     ::rtl::OUString SAL_CALL
getServiceName()96 LogarithmicScaling::getServiceName()
97     throw (uno::RuntimeException)
98 {
99     return lcl_aServiceName_Logarithmic;
100 }
101 
getSupportedServiceNames_Static()102 uno::Sequence< ::rtl::OUString > LogarithmicScaling::getSupportedServiceNames_Static()
103 {
104     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Logarithmic, 1 );
105 }
106 
107 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
APPHELPER_XSERVICEINFO_IMPL(LogarithmicScaling,lcl_aServiceName_Logarithmic)108 APPHELPER_XSERVICEINFO_IMPL( LogarithmicScaling, lcl_aServiceName_Logarithmic )
109 
110 // ----------------------------------------
111 
112 ExponentialScaling::ExponentialScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
113         m_fBase( 10.0 ),
114         m_xContext( xContext )
115 {
116 }
117 
ExponentialScaling(double fBase)118 ExponentialScaling::ExponentialScaling( double fBase ) :
119         m_fBase( fBase )
120 {
121 }
122 
~ExponentialScaling()123 ExponentialScaling::~ExponentialScaling()
124 {
125 }
126 
127     double SAL_CALL
doScaling(double value)128 ExponentialScaling::doScaling( double value )
129     throw (uno::RuntimeException)
130 {
131     double fResult;
132     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
133         ::rtl::math::setNan( & fResult );
134     else
135         fResult = pow( m_fBase, value );
136     return fResult;
137 }
138 
139     uno::Reference< XScaling > SAL_CALL
getInverseScaling()140 ExponentialScaling::getInverseScaling()
141     throw (uno::RuntimeException)
142 {
143     return new LogarithmicScaling( m_fBase );
144 }
145 
146     ::rtl::OUString SAL_CALL
getServiceName()147 ExponentialScaling::getServiceName()
148     throw (uno::RuntimeException)
149 {
150     return lcl_aServiceName_Exponential;
151 }
152 
getSupportedServiceNames_Static()153 uno::Sequence< ::rtl::OUString > ExponentialScaling::getSupportedServiceNames_Static()
154 {
155     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Exponential, 1 );
156 }
157 
158 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
APPHELPER_XSERVICEINFO_IMPL(ExponentialScaling,lcl_aServiceName_Exponential)159 APPHELPER_XSERVICEINFO_IMPL( ExponentialScaling, lcl_aServiceName_Exponential )
160 
161 // ----------------------------------------
162 
163 LinearScaling::LinearScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
164         m_fSlope( 1.0 ),
165         m_fOffset( 0.0 ),
166         m_xContext( xContext )
167 {}
168 
LinearScaling(double fSlope,double fOffset)169 LinearScaling::LinearScaling( double fSlope, double fOffset ) :
170         m_fSlope( fSlope ),
171         m_fOffset( fOffset )
172 {}
173 
~LinearScaling()174 LinearScaling::~LinearScaling()
175 {}
176 
doScaling(double value)177 double SAL_CALL LinearScaling::doScaling( double value )
178     throw (uno::RuntimeException)
179 {
180     double fResult;
181     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
182         ::rtl::math::setNan( & fResult );
183     else
184         fResult = m_fOffset + m_fSlope * value;
185     return fResult;
186 }
187 
188 uno::Reference< XScaling > SAL_CALL
getInverseScaling()189     LinearScaling::getInverseScaling()
190     throw (uno::RuntimeException)
191 {
192     // ToDo: ApproxEqual ?
193     if( m_fSlope == 0 )
194         throw uno::RuntimeException();
195 
196     return new LinearScaling( 1.0 / m_fSlope, m_fOffset / m_fSlope );
197 }
198 
199     ::rtl::OUString SAL_CALL
getServiceName()200 LinearScaling::getServiceName()
201     throw (uno::RuntimeException)
202 {
203     return lcl_aServiceName_Linear;
204 }
205 
getSupportedServiceNames_Static()206 uno::Sequence< ::rtl::OUString > LinearScaling::getSupportedServiceNames_Static()
207 {
208     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Linear, 1 );
209 }
210 
211 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
APPHELPER_XSERVICEINFO_IMPL(LinearScaling,lcl_aServiceName_Linear)212 APPHELPER_XSERVICEINFO_IMPL( LinearScaling, lcl_aServiceName_Linear )
213 
214 // ----------------------------------------
215 
216 PowerScaling::PowerScaling( const uno::Reference< uno::XComponentContext > & xContext ) :
217         m_fExponent( 10.0 ),
218         m_xContext( xContext )
219 {}
220 
PowerScaling(double fExponent)221 PowerScaling::PowerScaling( double fExponent ) :
222         m_fExponent( fExponent )
223 {}
224 
~PowerScaling()225 PowerScaling::~PowerScaling()
226 {}
227 
doScaling(double value)228 double SAL_CALL PowerScaling::doScaling( double value )
229     throw (uno::RuntimeException)
230 {
231     double fResult;
232     if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
233         ::rtl::math::setNan( & fResult );
234     else
235         fResult = pow( value, m_fExponent );
236     return fResult;
237 }
238 
239 uno::Reference< XScaling > SAL_CALL
getInverseScaling()240     PowerScaling::getInverseScaling()
241     throw (uno::RuntimeException)
242 {
243     // ToDo: ApproxEqual ?
244     if( m_fExponent == 0 )
245         throw uno::RuntimeException();
246 
247     return new PowerScaling( 1.0 / m_fExponent );
248 }
249 
250     ::rtl::OUString SAL_CALL
getServiceName()251 PowerScaling::getServiceName()
252     throw (uno::RuntimeException)
253 {
254     return lcl_aServiceName_Power;
255 }
256 
getSupportedServiceNames_Static()257 uno::Sequence< ::rtl::OUString > PowerScaling::getSupportedServiceNames_Static()
258 {
259     return uno::Sequence< ::rtl::OUString >( & lcl_aServiceName_Power, 1 );
260 }
261 
262 // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
263 APPHELPER_XSERVICEINFO_IMPL( PowerScaling, lcl_aServiceName_Power )
264 
265 //.............................................................................
266 } //namespace chart
267 //.............................................................................
268