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 ::basegfx::B2DVector
;
31 using ::com::sun::star::chart::TimeUnit::DAY
;
32 using ::com::sun::star::chart::TimeUnit::MONTH
;
33 using ::com::sun::star::chart::TimeUnit::YEAR
;
35 DateTickFactory::DateTickFactory(
36 const ExplicitScaleData
& rScale
, const ExplicitIncrementData
& rIncrement
)
38 , m_aIncrement( rIncrement
)
39 , m_xInverseScaling(NULL
)
41 //@todo: make sure that the scale is valid for the scaling
43 if( m_aScale
.Scaling
.is() )
45 m_xInverseScaling
= m_aScale
.Scaling
->getInverseScaling();
46 OSL_ENSURE( m_xInverseScaling
.is(), "each Scaling needs to return a inverse Scaling" );
49 m_fScaledVisibleMin
= m_aScale
.Minimum
;
50 if( m_xInverseScaling
.is() )
51 m_fScaledVisibleMin
= m_aScale
.Scaling
->doScaling(m_fScaledVisibleMin
);
53 m_fScaledVisibleMax
= m_aScale
.Maximum
;
54 if( m_xInverseScaling
.is() )
55 m_fScaledVisibleMax
= m_aScale
.Scaling
->doScaling(m_fScaledVisibleMax
);
58 DateTickFactory::~DateTickFactory()
62 void DateTickFactory::getAllTicks( TickInfoArraysType
& rAllTickInfos
, bool bShifted
) const
64 rAllTickInfos
.resize(2);
65 TickInfoArrayType
& rMajorTicks
= rAllTickInfos
[0];
66 TickInfoArrayType
& rMinorTicks
= rAllTickInfos
[1];
70 Date
aNull(m_aScale
.NullDate
);
72 Date aDate
= aNull
+ static_cast<long>(::rtl::math::approxFloor(m_aScale
.Minimum
));
73 Date aMaxDate
= aNull
+ static_cast<long>(::rtl::math::approxFloor(m_aScale
.Maximum
));
75 uno::Reference
< chart2::XScaling
> xScaling(m_aScale
.Scaling
);
76 uno::Reference
< chart2::XScaling
> xInverseScaling(m_xInverseScaling
);
79 xScaling
= new DateScaling(aNull
,m_aScale
.TimeResolution
,true/*bShifted*/);
80 xInverseScaling
= xScaling
->getInverseScaling();
83 //create major date tickinfos
84 while( aDate
<= aMaxDate
)
86 if( bShifted
&& aDate
==aMaxDate
)
89 TickInfo
aNewTick(xInverseScaling
); aNewTick
.fScaledTickValue
= aDate
- aNull
;
91 if( xInverseScaling
.is() )
92 aNewTick
.fScaledTickValue
= xScaling
->doScaling(aNewTick
.fScaledTickValue
);
93 rMajorTicks
.push_back( aNewTick
);
95 if(m_aIncrement
.MajorTimeInterval
.Number
<=0)
98 //find next major date
99 switch( m_aIncrement
.MajorTimeInterval
.TimeUnit
)
102 aDate
+= m_aIncrement
.MajorTimeInterval
.Number
;
105 aDate
= DateHelper::GetDateSomeYearsAway( aDate
, m_aIncrement
.MajorTimeInterval
.Number
);
109 aDate
= DateHelper::GetDateSomeMonthsAway( aDate
, m_aIncrement
.MajorTimeInterval
.Number
);
114 //create minor date tickinfos
115 aDate
= aNull
+ static_cast<long>(::rtl::math::approxFloor(m_aScale
.Minimum
));
116 while( aDate
<= aMaxDate
)
118 if( bShifted
&& aDate
==aMaxDate
)
121 TickInfo
aNewTick(xInverseScaling
); aNewTick
.fScaledTickValue
= aDate
- aNull
;
122 if( xInverseScaling
.is() )
123 aNewTick
.fScaledTickValue
= xScaling
->doScaling(aNewTick
.fScaledTickValue
);
124 rMinorTicks
.push_back( aNewTick
);
126 if(m_aIncrement
.MinorTimeInterval
.Number
<=0)
129 //find next minor date
130 switch( m_aIncrement
.MinorTimeInterval
.TimeUnit
)
133 aDate
+= m_aIncrement
.MinorTimeInterval
.Number
;
136 aDate
= DateHelper::GetDateSomeYearsAway( aDate
, m_aIncrement
.MinorTimeInterval
.Number
);
140 aDate
= DateHelper::GetDateSomeMonthsAway( aDate
, m_aIncrement
.MinorTimeInterval
.Number
);
146 void DateTickFactory::getAllTicks( TickInfoArraysType
& rAllTickInfos
) const
148 getAllTicks( rAllTickInfos
, false );
151 void DateTickFactory::getAllTicksShifted( TickInfoArraysType
& rAllTickInfos
) const
153 getAllTicks( rAllTickInfos
, true );
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */