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