1cde9e8dcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3cde9e8dcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4cde9e8dcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5cde9e8dcSAndrew Rist  * distributed with this work for additional information
6cde9e8dcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7cde9e8dcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8cde9e8dcSAndrew Rist  * "License"); you may not use this file except in compliance
9cde9e8dcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cde9e8dcSAndrew Rist  *
11cde9e8dcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cde9e8dcSAndrew Rist  *
13cde9e8dcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14cde9e8dcSAndrew Rist  * software distributed under the License is distributed on an
15cde9e8dcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16cde9e8dcSAndrew Rist  * KIND, either express or implied.  See the License for the
17cde9e8dcSAndrew Rist  * specific language governing permissions and limitations
18cde9e8dcSAndrew Rist  * under the License.
19cde9e8dcSAndrew Rist  *
20cde9e8dcSAndrew Rist  *************************************************************/
21cde9e8dcSAndrew Rist 
22cde9e8dcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_chart2.hxx"
26cdf0e10cSrcweir #include "Tickmarks_Dates.hxx"
27cdf0e10cSrcweir #include "DateScaling.hxx"
28cdf0e10cSrcweir #include <rtl/math.hxx>
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir #include "DateHelper.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir //.............................................................................
33cdf0e10cSrcweir namespace chart
34cdf0e10cSrcweir {
35cdf0e10cSrcweir //.............................................................................
36cdf0e10cSrcweir using namespace ::com::sun::star;
37cdf0e10cSrcweir using namespace ::com::sun::star::chart2;
38cdf0e10cSrcweir using namespace ::rtl::math;
39cdf0e10cSrcweir using ::basegfx::B2DVector;
40cdf0e10cSrcweir using ::com::sun::star::chart::TimeUnit::DAY;
41cdf0e10cSrcweir using ::com::sun::star::chart::TimeUnit::MONTH;
42cdf0e10cSrcweir using ::com::sun::star::chart::TimeUnit::YEAR;
43cdf0e10cSrcweir 
DateTickFactory(const ExplicitScaleData & rScale,const ExplicitIncrementData & rIncrement)44cdf0e10cSrcweir DateTickFactory::DateTickFactory(
45cdf0e10cSrcweir           const ExplicitScaleData& rScale, const ExplicitIncrementData& rIncrement )
46cdf0e10cSrcweir             : m_aScale( rScale )
47cdf0e10cSrcweir             , m_aIncrement( rIncrement )
48cdf0e10cSrcweir             , m_xInverseScaling(NULL)
49cdf0e10cSrcweir {
50cdf0e10cSrcweir     //@todo: make sure that the scale is valid for the scaling
51cdf0e10cSrcweir 
52cdf0e10cSrcweir     if( m_aScale.Scaling.is() )
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         m_xInverseScaling = m_aScale.Scaling->getInverseScaling();
55cdf0e10cSrcweir         DBG_ASSERT( m_xInverseScaling.is(), "each Scaling needs to return a inverse Scaling" );
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     m_fScaledVisibleMin = m_aScale.Minimum;
59cdf0e10cSrcweir     if( m_xInverseScaling.is() )
60cdf0e10cSrcweir         m_fScaledVisibleMin = m_aScale.Scaling->doScaling(m_fScaledVisibleMin);
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     m_fScaledVisibleMax = m_aScale.Maximum;
63cdf0e10cSrcweir     if( m_xInverseScaling.is() )
64cdf0e10cSrcweir         m_fScaledVisibleMax = m_aScale.Scaling->doScaling(m_fScaledVisibleMax);
65cdf0e10cSrcweir }
66cdf0e10cSrcweir 
~DateTickFactory()67cdf0e10cSrcweir DateTickFactory::~DateTickFactory()
68cdf0e10cSrcweir {
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
getAllTicks(::std::vector<::std::vector<TickInfo>> & rAllTickInfos,bool bShifted) const71cdf0e10cSrcweir void DateTickFactory::getAllTicks( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos, bool bShifted ) const
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     rAllTickInfos.resize(2);
74cdf0e10cSrcweir     ::std::vector< TickInfo >& rMajorTicks = rAllTickInfos[0];
75cdf0e10cSrcweir     ::std::vector< TickInfo >& rMinorTicks = rAllTickInfos[1];
76cdf0e10cSrcweir     rMajorTicks.clear();
77cdf0e10cSrcweir     rMinorTicks.clear();
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     Date aNull(m_aScale.NullDate);
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     Date aDate = aNull + static_cast<long>(::rtl::math::approxFloor(m_aScale.Minimum));
82cdf0e10cSrcweir     Date aMaxDate = aNull + static_cast<long>(::rtl::math::approxFloor(m_aScale.Maximum));
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     uno::Reference< chart2::XScaling > xScaling(m_aScale.Scaling);
85cdf0e10cSrcweir     uno::Reference< chart2::XScaling > xInverseScaling(m_xInverseScaling);
86cdf0e10cSrcweir     if( bShifted )
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         xScaling = new DateScaling(aNull,m_aScale.TimeResolution,true/*bShifted*/);
89cdf0e10cSrcweir         xInverseScaling = xScaling->getInverseScaling();
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir 
92cdf0e10cSrcweir     //create major date tickinfos
93cdf0e10cSrcweir     while( aDate<= aMaxDate )
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         if( bShifted && aDate==aMaxDate )
96cdf0e10cSrcweir             break;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         TickInfo aNewTick(xInverseScaling); aNewTick.fScaledTickValue = aDate - aNull;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         if( xInverseScaling.is() )
101cdf0e10cSrcweir             aNewTick.fScaledTickValue = xScaling->doScaling(aNewTick.fScaledTickValue);
102cdf0e10cSrcweir         rMajorTicks.push_back( aNewTick );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         if(m_aIncrement.MajorTimeInterval.Number<=0)
105cdf0e10cSrcweir             break;
106cdf0e10cSrcweir 
107cdf0e10cSrcweir         //find next major date
108*9a1ec794SAndre Fischer         const Date aTmpDate (aDate);
109cdf0e10cSrcweir         switch( m_aIncrement.MajorTimeInterval.TimeUnit )
110cdf0e10cSrcweir         {
111cdf0e10cSrcweir         case DAY:
112cdf0e10cSrcweir             aDate += m_aIncrement.MajorTimeInterval.Number;
113cdf0e10cSrcweir             break;
114cdf0e10cSrcweir         case YEAR:
115cdf0e10cSrcweir             aDate = DateHelper::GetDateSomeYearsAway( aDate, m_aIncrement.MajorTimeInterval.Number );
116cdf0e10cSrcweir             break;
117cdf0e10cSrcweir         case MONTH:
118cdf0e10cSrcweir         default:
119cdf0e10cSrcweir             aDate = DateHelper::GetDateSomeMonthsAway( aDate, m_aIncrement.MajorTimeInterval.Number );
120cdf0e10cSrcweir             break;
121cdf0e10cSrcweir         }
122*9a1ec794SAndre Fischer         if ( ! aDate.IsValid() || aDate == aTmpDate)
123*9a1ec794SAndre Fischer             break;
124cdf0e10cSrcweir     }
125cdf0e10cSrcweir 
126cdf0e10cSrcweir     //create minor date tickinfos
127cdf0e10cSrcweir     aDate = aNull + static_cast<long>(::rtl::math::approxFloor(m_aScale.Minimum));
128cdf0e10cSrcweir     while( aDate<= aMaxDate )
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         if( bShifted && aDate==aMaxDate )
131cdf0e10cSrcweir             break;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         TickInfo aNewTick(xInverseScaling); aNewTick.fScaledTickValue = aDate - aNull;
134cdf0e10cSrcweir         if( xInverseScaling.is() )
135cdf0e10cSrcweir             aNewTick.fScaledTickValue = xScaling->doScaling(aNewTick.fScaledTickValue);
136cdf0e10cSrcweir         rMinorTicks.push_back( aNewTick );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         if(m_aIncrement.MinorTimeInterval.Number<=0)
139cdf0e10cSrcweir             break;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         //find next minor date
142*9a1ec794SAndre Fischer         const Date aTmpDate (aDate);
143cdf0e10cSrcweir         switch( m_aIncrement.MinorTimeInterval.TimeUnit )
144cdf0e10cSrcweir         {
145cdf0e10cSrcweir         case DAY:
146cdf0e10cSrcweir             aDate += m_aIncrement.MinorTimeInterval.Number;
147cdf0e10cSrcweir             break;
148cdf0e10cSrcweir         case YEAR:
149cdf0e10cSrcweir             aDate = DateHelper::GetDateSomeYearsAway( aDate, m_aIncrement.MinorTimeInterval.Number );
150cdf0e10cSrcweir             break;
151cdf0e10cSrcweir         case MONTH:
152cdf0e10cSrcweir         default:
153cdf0e10cSrcweir             aDate = DateHelper::GetDateSomeMonthsAway( aDate, m_aIncrement.MinorTimeInterval.Number );
154cdf0e10cSrcweir             break;
155cdf0e10cSrcweir         }
156*9a1ec794SAndre Fischer         if ( ! aDate.IsValid() || aDate == aTmpDate)
157*9a1ec794SAndre Fischer             break;
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir }
160cdf0e10cSrcweir 
getAllTicks(::std::vector<::std::vector<TickInfo>> & rAllTickInfos) const161cdf0e10cSrcweir void DateTickFactory::getAllTicks( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
162cdf0e10cSrcweir {
163cdf0e10cSrcweir     getAllTicks( rAllTickInfos, false );
164cdf0e10cSrcweir }
165cdf0e10cSrcweir 
getAllTicksShifted(::std::vector<::std::vector<TickInfo>> & rAllTickInfos) const166cdf0e10cSrcweir void DateTickFactory::getAllTicksShifted( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
167cdf0e10cSrcweir {
168cdf0e10cSrcweir     getAllTicks( rAllTickInfos, true );
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir //.............................................................................
172cdf0e10cSrcweir } //namespace chart
173cdf0e10cSrcweir //.............................................................................
174