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 "Tickmarks_Dates.hxx"
21 #include "DateScaling.hxx"
22 #include <rtl/math.hxx>
23 #include <DateHelper.hxx>
27 using namespace ::com::sun::star
;
28 using namespace ::com::sun::star::chart2
;
29 using namespace ::rtl::math
;
30 using ::com::sun::star::chart::TimeUnit::DAY
;
31 using ::com::sun::star::chart::TimeUnit::MONTH
;
32 using ::com::sun::star::chart::TimeUnit::YEAR
;
34 DateTickFactory::DateTickFactory(
35 const ExplicitScaleData
& rScale
, const ExplicitIncrementData
& rIncrement
)
37 , m_aIncrement( rIncrement
)
38 , m_xInverseScaling(nullptr)
40 //@todo: make sure that the scale is valid for the scaling
42 if( m_aScale
.Scaling
.is() )
44 m_xInverseScaling
= m_aScale
.Scaling
->getInverseScaling();
45 OSL_ENSURE( m_xInverseScaling
.is(), "each Scaling needs to return a inverse Scaling" );
49 DateTickFactory::~DateTickFactory()
53 void DateTickFactory::getAllTicks( TickInfoArraysType
& rAllTickInfos
, bool bShifted
) const
55 rAllTickInfos
.resize(2);
56 TickInfoArrayType
& rMajorTicks
= rAllTickInfos
[0];
57 TickInfoArrayType
& rMinorTicks
= rAllTickInfos
[1];
61 Date
aNull(m_aScale
.NullDate
);
63 Date aDate
= aNull
+ static_cast<sal_Int32
>(::rtl::math::approxFloor(m_aScale
.Minimum
));
64 Date aMaxDate
= aNull
+ static_cast<sal_Int32
>(::rtl::math::approxFloor(m_aScale
.Maximum
));
66 uno::Reference
< chart2::XScaling
> xScaling(m_aScale
.Scaling
);
67 uno::Reference
< chart2::XScaling
> xInverseScaling(m_xInverseScaling
);
70 xScaling
= new DateScaling(aNull
,m_aScale
.TimeResolution
,true/*bShifted*/);
71 xInverseScaling
= xScaling
->getInverseScaling();
74 //create major date tickinfos
75 while( aDate
<= aMaxDate
)
77 if( bShifted
&& aDate
==aMaxDate
)
80 TickInfo
aNewTick(xInverseScaling
); aNewTick
.fScaledTickValue
= aDate
- aNull
;
82 if( xInverseScaling
.is() )
83 aNewTick
.fScaledTickValue
= xScaling
->doScaling(aNewTick
.fScaledTickValue
);
84 rMajorTicks
.push_back( aNewTick
);
86 if(m_aIncrement
.MajorTimeInterval
.Number
<=0)
89 //find next major date
90 switch( m_aIncrement
.MajorTimeInterval
.TimeUnit
)
93 aDate
.AddDays( m_aIncrement
.MajorTimeInterval
.Number
);
96 aDate
= DateHelper::GetDateSomeYearsAway( aDate
, m_aIncrement
.MajorTimeInterval
.Number
);
100 aDate
= DateHelper::GetDateSomeMonthsAway( aDate
, m_aIncrement
.MajorTimeInterval
.Number
);
105 //create minor date tickinfos
106 aDate
= aNull
+ static_cast<sal_Int32
>(::rtl::math::approxFloor(m_aScale
.Minimum
));
107 while( aDate
<= aMaxDate
)
109 if( bShifted
&& aDate
==aMaxDate
)
112 TickInfo
aNewTick(xInverseScaling
); aNewTick
.fScaledTickValue
= aDate
- aNull
;
113 if( xInverseScaling
.is() )
114 aNewTick
.fScaledTickValue
= xScaling
->doScaling(aNewTick
.fScaledTickValue
);
115 rMinorTicks
.push_back( aNewTick
);
117 if(m_aIncrement
.MinorTimeInterval
.Number
<=0)
120 //find next minor date
121 switch( m_aIncrement
.MinorTimeInterval
.TimeUnit
)
124 aDate
.AddDays( m_aIncrement
.MinorTimeInterval
.Number
);
127 aDate
= DateHelper::GetDateSomeYearsAway( aDate
, m_aIncrement
.MinorTimeInterval
.Number
);
131 aDate
= DateHelper::GetDateSomeMonthsAway( aDate
, m_aIncrement
.MinorTimeInterval
.Number
);
137 void DateTickFactory::getAllTicks( TickInfoArraysType
& rAllTickInfos
) const
139 getAllTicks( rAllTickInfos
, false );
142 void DateTickFactory::getAllTicksShifted( TickInfoArraysType
& rAllTickInfos
) const
144 getAllTicks( rAllTickInfos
, true );
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */