LibreOffice Module chart2 (master) 1
DateScaling.cxx
Go to the documentation of this file.
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>
24
25#include <limits>
26
27namespace
28{
29
30constexpr OUStringLiteral lcl_aServiceName_DateScaling = u"com.sun.star.chart2.DateScaling";
31constexpr OUStringLiteral lcl_aServiceName_InverseDateScaling
32 = u"com.sun.star.chart2.InverseDateScaling";
33
34const double lcl_fNumberOfMonths = 12.0;//todo: this needs to be offered by basic tools Date class if it should be more generic
35}
36
37namespace chart
38{
39using namespace ::com::sun::star;
40using namespace ::com::sun::star::chart2;
41using ::com::sun::star::chart::TimeUnit::DAY;
42using ::com::sun::star::chart::TimeUnit::MONTH;
43using ::com::sun::star::chart::TimeUnit::YEAR;
44
45DateScaling::DateScaling( const Date& rNullDate, sal_Int32 nTimeUnit, bool bShifted )
46 : m_aNullDate( rNullDate )
47 , m_nTimeUnit( nTimeUnit )
48 , m_bShifted( bShifted )
49{
50}
51
53{
54}
55
56double SAL_CALL DateScaling::doScaling( double value )
57{
58 double fResult(value);
59 if( std::isnan( value ) || std::isinf( value ) )
60 return std::numeric_limits<double>::quiet_NaN();
61 switch( m_nTimeUnit )
62 {
63 case DAY:
64 fResult = value;
65 if(m_bShifted)
66 fResult+=0.5;
67 break;
68 case YEAR:
69 case MONTH:
70 default:
71 {
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();
77
78 double fDayOfMonth = aDate.GetDay();
79 fDayOfMonth -= 1.0;
80 double fDaysInMonth = aDate.GetDaysInMonth();
81 fResult += fDayOfMonth/fDaysInMonth;
82 if(m_bShifted)
83 {
84 if( m_nTimeUnit==YEAR )
85 fResult += 0.5*lcl_fNumberOfMonths;
86 else
87 fResult += 0.5;
88 }
89 break;
90 }
91 }
92 return fResult;
93}
94
96{
98}
99
101{
102 return lcl_aServiceName_DateScaling;
103}
104
106{
107 return lcl_aServiceName_DateScaling;
108}
109
110sal_Bool SAL_CALL DateScaling::supportsService( const OUString& rServiceName )
111{
112 return cppu::supportsService(this, rServiceName);
113}
114
115css::uno::Sequence< OUString > SAL_CALL DateScaling::getSupportedServiceNames()
116{
117 return { lcl_aServiceName_DateScaling };
118}
119
120InverseDateScaling::InverseDateScaling( const Date& rNullDate, sal_Int32 nTimeUnit, bool bShifted )
121 : m_aNullDate( rNullDate )
122 , m_nTimeUnit( nTimeUnit )
123 , m_bShifted( bShifted )
124{
125}
126
128{
129}
130
131double SAL_CALL InverseDateScaling::doScaling( double value )
132{
133 double fResult(value);
134 if( std::isnan( value ) || std::isinf( value ) )
135 return std::numeric_limits<double>::quiet_NaN();
136 else
137 {
138 switch( m_nTimeUnit )
139 {
140 case DAY:
141 if(m_bShifted)
142 value -= 0.5;
143 fResult = value;
144 break;
145 case YEAR:
146 case MONTH:
147 default:
148 //Date aDate(m_aNullDate);
149 if(m_bShifted)
150 {
151 if( m_nTimeUnit==YEAR )
152 value -= 0.5*lcl_fNumberOfMonths;
153 else
154 value -= 0.5;
155 }
156 Date aDate( Date::EMPTY );
157 double fYear = ::rtl::math::approxFloor(value/lcl_fNumberOfMonths);
158 double fMonth = ::rtl::math::approxFloor(value-(fYear*lcl_fNumberOfMonths));
159 if( fMonth==0.0 )
160 {
161 fYear--;
162 fMonth=12.0;
163 }
164 aDate.SetYear( static_cast<sal_uInt16>(fYear) );
165 aDate.SetMonth( static_cast<sal_uInt16>(fMonth) );
166 aDate.SetDay( 1 );
167 double fMonthCount = (fYear*lcl_fNumberOfMonths)+fMonth;
168 double fDay = (value-fMonthCount)*aDate.GetDaysInMonth();
169 fDay += 1.0;
170 aDate.SetDay( static_cast<sal_uInt16>(::rtl::math::round(fDay)) );
171 fResult = aDate - m_aNullDate;
172 break;
173 }
174 }
175 return fResult;
176}
177
179{
181}
182
184{
185 return lcl_aServiceName_InverseDateScaling;
186}
187
189{
190 return lcl_aServiceName_InverseDateScaling;
191}
192
193sal_Bool SAL_CALL InverseDateScaling::supportsService( const OUString& rServiceName )
194{
195 return cppu::supportsService(this, rServiceName);
196}
197
198css::uno::Sequence< OUString > SAL_CALL InverseDateScaling::getSupportedServiceNames()
199{
200 return { lcl_aServiceName_InverseDateScaling };
201}
202
203} //namespace chart
204
205/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void AddDays(sal_Int32 nAddDays)
sal_Int16 GetYear() const
sal_uInt16 GetDay() const
sal_uInt16 GetDaysInMonth() const
void SetMonth(sal_uInt16 nNewMonth)
void SetYear(sal_Int16 nNewYear)
void SetDay(sal_uInt16 nNewDay)
sal_uInt16 GetMonth() const
virtual css::uno::Reference< css::chart2::XScaling > SAL_CALL getInverseScaling() override
Definition: DateScaling.cxx:95
virtual double SAL_CALL doScaling(double value) override
Definition: DateScaling.cxx:56
virtual ~DateScaling() override
Definition: DateScaling.cxx:52
virtual OUString SAL_CALL getServiceName() override
DateScaling(const Date &rNullDate, sal_Int32 nTimeUnit, bool bShifted)
Definition: DateScaling.cxx:45
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
virtual OUString SAL_CALL getImplementationName() override
declare XServiceInfo methods
const sal_Int32 m_nTimeUnit
Definition: DateScaling.hxx:58
const Date m_aNullDate
Definition: DateScaling.hxx:57
const bool m_bShifted
Definition: DateScaling.hxx:59
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
const sal_Int32 m_nTimeUnit
Definition: DateScaling.hxx:89
virtual ~InverseDateScaling() override
virtual OUString SAL_CALL getImplementationName() override
declare XServiceInfo methods
InverseDateScaling(const Date &rNullDate, sal_Int32 nTimeUnit, bool bShifted)
virtual double SAL_CALL doScaling(double value) override
virtual css::uno::Reference< css::chart2::XScaling > SAL_CALL getInverseScaling() override
virtual OUString SAL_CALL getServiceName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Any value
float u
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
unsigned char sal_Bool