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