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