Line data Source code
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 :
20 : #include "DateScaling.hxx"
21 : #include <com/sun/star/chart/TimeUnit.hpp>
22 : #include <rtl/math.hxx>
23 : #include "com/sun/star/uno/RuntimeException.hpp"
24 :
25 : namespace
26 : {
27 :
28 11 : static const OUString lcl_aServiceName_DateScaling( "com.sun.star.chart2.DateScaling" );
29 11 : static const OUString lcl_aServiceName_InverseDateScaling( "com.sun.star.chart2.InverseDateScaling" );
30 :
31 : static const double lcl_fNumberOfMonths = 12.0;//todo: this needs to be offered by basic tools Date class if it should be more generic
32 : }
33 :
34 : //.............................................................................
35 : namespace chart
36 : {
37 : //.............................................................................
38 : using namespace ::com::sun::star;
39 : using namespace ::com::sun::star::chart2;
40 : using ::com::sun::star::chart::TimeUnit::DAY;
41 : using ::com::sun::star::chart::TimeUnit::MONTH;
42 : using ::com::sun::star::chart::TimeUnit::YEAR;
43 :
44 0 : DateScaling::DateScaling( const Date& rNullDate, sal_Int32 nTimeUnit, bool bShifted )
45 : : m_aNullDate( rNullDate )
46 : , m_nTimeUnit( nTimeUnit )
47 0 : , m_bShifted( bShifted )
48 : {
49 0 : }
50 :
51 0 : DateScaling::~DateScaling()
52 : {
53 0 : }
54 :
55 0 : double SAL_CALL DateScaling::doScaling( double value )
56 : throw (uno::RuntimeException)
57 : {
58 0 : double fResult(value);
59 0 : if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
60 0 : ::rtl::math::setNan( & fResult );
61 : else
62 : {
63 0 : Date aDate(m_aNullDate);
64 0 : aDate += static_cast<long>(::rtl::math::approxFloor(value));
65 0 : switch( m_nTimeUnit )
66 : {
67 : case DAY:
68 0 : fResult = value;
69 0 : if(m_bShifted)
70 0 : fResult+=0.5;
71 0 : break;
72 : case YEAR:
73 : case MONTH:
74 : default:
75 0 : fResult = aDate.GetYear();
76 0 : fResult *= lcl_fNumberOfMonths;//asssuming equal count of months in each year
77 0 : fResult += aDate.GetMonth();
78 :
79 0 : double fDayOfMonth = aDate.GetDay();
80 0 : fDayOfMonth -= 1.0;
81 0 : double fDaysInMonth = aDate.GetDaysInMonth();
82 0 : fResult += fDayOfMonth/fDaysInMonth;
83 0 : if(m_bShifted)
84 : {
85 0 : if( YEAR==m_nTimeUnit )
86 0 : fResult += 0.5*lcl_fNumberOfMonths;
87 : else
88 0 : fResult += 0.5;
89 : }
90 0 : break;
91 : }
92 : }
93 0 : return fResult;
94 : }
95 :
96 0 : uno::Reference< XScaling > SAL_CALL DateScaling::getInverseScaling()
97 : throw (uno::RuntimeException)
98 : {
99 0 : return new InverseDateScaling( m_aNullDate, m_nTimeUnit, m_bShifted );
100 : }
101 :
102 0 : OUString SAL_CALL DateScaling::getServiceName()
103 : throw (uno::RuntimeException)
104 : {
105 0 : return lcl_aServiceName_DateScaling;
106 : }
107 :
108 0 : uno::Sequence< OUString > DateScaling::getSupportedServiceNames_Static()
109 : {
110 0 : return uno::Sequence< OUString >( & lcl_aServiceName_DateScaling, 1 );
111 : }
112 :
113 : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
114 0 : APPHELPER_XSERVICEINFO_IMPL( DateScaling, lcl_aServiceName_DateScaling )
115 :
116 : // ----------------------------------------
117 :
118 0 : InverseDateScaling::InverseDateScaling( const Date& rNullDate, sal_Int32 nTimeUnit, bool bShifted )
119 : : m_aNullDate( rNullDate )
120 : , m_nTimeUnit( nTimeUnit )
121 0 : , m_bShifted( bShifted )
122 : {
123 0 : }
124 :
125 0 : InverseDateScaling::~InverseDateScaling()
126 : {
127 0 : }
128 :
129 0 : double SAL_CALL InverseDateScaling::doScaling( double value )
130 : throw (uno::RuntimeException)
131 : {
132 0 : double fResult(value);
133 0 : if( ::rtl::math::isNan( value ) || ::rtl::math::isInf( value ) )
134 0 : ::rtl::math::setNan( & fResult );
135 : else
136 : {
137 0 : switch( m_nTimeUnit )
138 : {
139 : case DAY:
140 0 : if(m_bShifted)
141 0 : value -= 0.5;
142 0 : fResult = value;
143 0 : break;
144 : case YEAR:
145 : case MONTH:
146 : default:
147 : //Date aDate(m_aNullDate);
148 0 : if(m_bShifted)
149 : {
150 0 : if( YEAR==m_nTimeUnit )
151 0 : value -= 0.5*lcl_fNumberOfMonths;
152 : else
153 0 : value -= 0.5;
154 : }
155 0 : Date aDate( Date::EMPTY );
156 0 : double fYear = ::rtl::math::approxFloor(value/lcl_fNumberOfMonths);
157 0 : double fMonth = ::rtl::math::approxFloor(value-(fYear*lcl_fNumberOfMonths));
158 0 : if( fMonth==0.0 )
159 : {
160 0 : fYear--;
161 0 : fMonth=12.0;
162 : }
163 0 : aDate.SetYear( static_cast<sal_uInt16>(fYear) );
164 0 : aDate.SetMonth( static_cast<sal_uInt16>(fMonth) );
165 0 : aDate.SetDay( 1 );
166 0 : double fMonthCount = (fYear*lcl_fNumberOfMonths)+fMonth;
167 0 : double fDay = (value-fMonthCount)*aDate.GetDaysInMonth();
168 0 : fDay += 1.0;
169 0 : aDate.SetDay( static_cast<sal_uInt16>(::rtl::math::round(fDay)) );
170 0 : fResult = aDate - m_aNullDate;
171 0 : break;
172 : }
173 : }
174 0 : return fResult;
175 : }
176 :
177 0 : uno::Reference< XScaling > SAL_CALL InverseDateScaling::getInverseScaling()
178 : throw (uno::RuntimeException)
179 : {
180 0 : return new DateScaling( m_aNullDate, m_nTimeUnit, m_bShifted );
181 : }
182 :
183 0 : OUString SAL_CALL InverseDateScaling::getServiceName()
184 : throw (uno::RuntimeException)
185 : {
186 0 : return lcl_aServiceName_InverseDateScaling;
187 : }
188 :
189 0 : uno::Sequence< OUString > InverseDateScaling::getSupportedServiceNames_Static()
190 : {
191 0 : return uno::Sequence< OUString >( & lcl_aServiceName_InverseDateScaling, 1 );
192 : }
193 :
194 : // implement XServiceInfo methods basing upon getSupportedServiceNames_Static
195 0 : APPHELPER_XSERVICEINFO_IMPL( InverseDateScaling, lcl_aServiceName_InverseDateScaling )
196 :
197 : //.............................................................................
198 33 : } //namespace chart
199 : //.............................................................................
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|