1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "DateScaling.hxx"
21 #include <com/sun/star/chart/TimeUnit.hpp>
22 #include <rtl/math.hxx>
23 #include <cppuhelper/supportsservice.hxx>
30 constexpr OUString lcl_aServiceName_DateScaling
= u
"com.sun.star.chart2.DateScaling"_ustr
;
31 constexpr OUString lcl_aServiceName_InverseDateScaling
32 = u
"com.sun.star.chart2.InverseDateScaling"_ustr
;
34 const double lcl_fNumberOfMonths
= 12.0;//todo: this needs to be offered by basic tools Date class if it should be more generic
39 using namespace ::com::sun::star
;
40 using namespace ::com::sun::star::chart2
;
41 using ::com::sun::star::chart::TimeUnit::DAY
;
42 using ::com::sun::star::chart::TimeUnit::MONTH
;
43 using ::com::sun::star::chart::TimeUnit::YEAR
;
45 DateScaling::DateScaling( const Date
& rNullDate
, sal_Int32 nTimeUnit
, bool bShifted
)
46 : m_aNullDate( rNullDate
)
47 , m_nTimeUnit( nTimeUnit
)
48 , m_bShifted( bShifted
)
52 DateScaling::~DateScaling()
56 double SAL_CALL
DateScaling::doScaling( double value
)
58 double fResult(value
);
59 if( std::isnan( value
) || std::isinf( value
) )
60 return std::numeric_limits
<double>::quiet_NaN();
72 Date
aDate(m_aNullDate
);
73 aDate
.AddDays(::rtl::math::approxFloor(value
));
74 fResult
= aDate
.GetYear();
75 fResult
*= lcl_fNumberOfMonths
;//assuming equal count of months in each year
76 fResult
+= aDate
.GetMonth();
78 double fDayOfMonth
= aDate
.GetDay();
80 double fDaysInMonth
= aDate
.GetDaysInMonth();
81 fResult
+= fDayOfMonth
/fDaysInMonth
;
84 if( m_nTimeUnit
==YEAR
)
85 fResult
+= 0.5*lcl_fNumberOfMonths
;
95 uno::Reference
< XScaling
> SAL_CALL
DateScaling::getInverseScaling()
97 return new InverseDateScaling( m_aNullDate
, m_nTimeUnit
, m_bShifted
);
100 OUString SAL_CALL
DateScaling::getServiceName()
102 return lcl_aServiceName_DateScaling
;
105 OUString SAL_CALL
DateScaling::getImplementationName()
107 return lcl_aServiceName_DateScaling
;
110 sal_Bool SAL_CALL
DateScaling::supportsService( const OUString
& rServiceName
)
112 return cppu::supportsService(this, rServiceName
);
115 css::uno::Sequence
< OUString
> SAL_CALL
DateScaling::getSupportedServiceNames()
117 return { lcl_aServiceName_DateScaling
};
120 InverseDateScaling::InverseDateScaling( const Date
& rNullDate
, sal_Int32 nTimeUnit
, bool bShifted
)
121 : m_aNullDate( rNullDate
)
122 , m_nTimeUnit( nTimeUnit
)
123 , m_bShifted( bShifted
)
127 InverseDateScaling::~InverseDateScaling()
131 double SAL_CALL
InverseDateScaling::doScaling( double value
)
133 double fResult(value
);
134 if( std::isnan( value
) || std::isinf( value
) )
135 return std::numeric_limits
<double>::quiet_NaN();
138 switch( m_nTimeUnit
)
148 //Date aDate(m_aNullDate);
151 if( m_nTimeUnit
==YEAR
)
152 value
-= 0.5*lcl_fNumberOfMonths
;
156 Date
aDate( Date::EMPTY
);
157 double fYear
= ::rtl::math::approxFloor(value
/lcl_fNumberOfMonths
);
158 double fMonth
= ::rtl::math::approxFloor(value
-(fYear
*lcl_fNumberOfMonths
));
164 aDate
.SetYear( static_cast<sal_uInt16
>(fYear
) );
165 aDate
.SetMonth( static_cast<sal_uInt16
>(fMonth
) );
167 double fMonthCount
= (fYear
*lcl_fNumberOfMonths
)+fMonth
;
168 double fDay
= (value
-fMonthCount
)*aDate
.GetDaysInMonth();
170 aDate
.SetDay( static_cast<sal_uInt16
>(::rtl::math::round(fDay
)) );
171 fResult
= aDate
- m_aNullDate
;
178 uno::Reference
< XScaling
> SAL_CALL
InverseDateScaling::getInverseScaling()
180 return new DateScaling( m_aNullDate
, m_nTimeUnit
, m_bShifted
);
183 OUString SAL_CALL
InverseDateScaling::getServiceName()
185 return lcl_aServiceName_InverseDateScaling
;
188 OUString SAL_CALL
InverseDateScaling::getImplementationName()
190 return lcl_aServiceName_InverseDateScaling
;
193 sal_Bool SAL_CALL
InverseDateScaling::supportsService( const OUString
& rServiceName
)
195 return cppu::supportsService(this, rServiceName
);
198 css::uno::Sequence
< OUString
> SAL_CALL
InverseDateScaling::getSupportedServiceNames()
200 return { lcl_aServiceName_InverseDateScaling
};
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */