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 <osl/diagnose.h>
24 #include <DateHelper.hxx>
25 #include <com/sun/star/chart/TimeUnit.hpp>
29 using namespace ::com::sun::star
;
30 using namespace ::com::sun::star::chart2
;
31 using namespace ::rtl::math
;
32 using ::com::sun::star::chart::TimeUnit::DAY
;
33 using ::com::sun::star::chart::TimeUnit::MONTH
;
34 using ::com::sun::star::chart::TimeUnit::YEAR
;
36 DateTickFactory::DateTickFactory(
37 const ExplicitScaleData
& rScale
, const ExplicitIncrementData
& rIncrement
)
39 , m_aIncrement( rIncrement
)
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 an inverse Scaling" );
50 DateTickFactory::~DateTickFactory()
54 void DateTickFactory::getAllTicks( TickInfoArraysType
& rAllTickInfos
, bool bShifted
) const
56 rAllTickInfos
.resize(2);
57 TickInfoArrayType
& rMajorTicks
= rAllTickInfos
[0];
58 TickInfoArrayType
& rMinorTicks
= rAllTickInfos
[1];
62 Date
aNull(m_aScale
.NullDate
);
64 Date aDate
= aNull
+ static_cast<sal_Int32
>(::rtl::math::approxFloor(m_aScale
.Minimum
));
65 Date aMaxDate
= aNull
+ static_cast<sal_Int32
>(::rtl::math::approxFloor(m_aScale
.Maximum
));
67 uno::Reference
< chart2::XScaling
> xScaling(m_aScale
.Scaling
);
68 uno::Reference
< chart2::XScaling
> xInverseScaling(m_xInverseScaling
);
71 xScaling
= new DateScaling(aNull
,m_aScale
.TimeResolution
,true/*bShifted*/);
72 xInverseScaling
= xScaling
->getInverseScaling();
75 //create major date tickinfos
76 while( aDate
<= aMaxDate
)
78 if( bShifted
&& aDate
==aMaxDate
)
81 TickInfo
aNewTick(xInverseScaling
); aNewTick
.fScaledTickValue
= aDate
- aNull
;
83 if( xInverseScaling
.is() )
84 aNewTick
.fScaledTickValue
= xScaling
->doScaling(aNewTick
.fScaledTickValue
);
85 rMajorTicks
.push_back( aNewTick
);
87 if(m_aIncrement
.MajorTimeInterval
.Number
<=0)
90 //find next major date
91 switch( m_aIncrement
.MajorTimeInterval
.TimeUnit
)
94 aDate
.AddDays( m_aIncrement
.MajorTimeInterval
.Number
);
97 aDate
= DateHelper::GetDateSomeYearsAway( aDate
, m_aIncrement
.MajorTimeInterval
.Number
);
101 aDate
= DateHelper::GetDateSomeMonthsAway( aDate
, m_aIncrement
.MajorTimeInterval
.Number
);
106 //create minor date tickinfos
107 aDate
= aNull
+ static_cast<sal_Int32
>(::rtl::math::approxFloor(m_aScale
.Minimum
));
108 while( aDate
<= aMaxDate
)
110 if( bShifted
&& aDate
==aMaxDate
)
113 TickInfo
aNewTick(xInverseScaling
); aNewTick
.fScaledTickValue
= aDate
- aNull
;
114 if( xInverseScaling
.is() )
115 aNewTick
.fScaledTickValue
= xScaling
->doScaling(aNewTick
.fScaledTickValue
);
116 rMinorTicks
.push_back( aNewTick
);
118 if(m_aIncrement
.MinorTimeInterval
.Number
<=0)
121 //find next minor date
122 switch( m_aIncrement
.MinorTimeInterval
.TimeUnit
)
125 aDate
.AddDays( m_aIncrement
.MinorTimeInterval
.Number
);
128 aDate
= DateHelper::GetDateSomeYearsAway( aDate
, m_aIncrement
.MinorTimeInterval
.Number
);
132 aDate
= DateHelper::GetDateSomeMonthsAway( aDate
, m_aIncrement
.MinorTimeInterval
.Number
);
138 void DateTickFactory::getAllTicks( TickInfoArraysType
& rAllTickInfos
) const
140 getAllTicks( rAllTickInfos
, false );
143 void DateTickFactory::getAllTicksShifted( TickInfoArraysType
& rAllTickInfos
) const
145 getAllTicks( rAllTickInfos
, true );
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */