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
DateTickFactory(const ExplicitScaleData & rScale,const ExplicitIncrementData & rIncrement)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
~DateTickFactory()67 DateTickFactory::~DateTickFactory()
68 {
69 }
70
getAllTicks(::std::vector<::std::vector<TickInfo>> & rAllTickInfos,bool bShifted) const71 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 const Date aTmpDate (aDate);
109 switch( m_aIncrement.MajorTimeInterval.TimeUnit )
110 {
111 case DAY:
112 aDate += m_aIncrement.MajorTimeInterval.Number;
113 break;
114 case YEAR:
115 aDate = DateHelper::GetDateSomeYearsAway( aDate, m_aIncrement.MajorTimeInterval.Number );
116 break;
117 case MONTH:
118 default:
119 aDate = DateHelper::GetDateSomeMonthsAway( aDate, m_aIncrement.MajorTimeInterval.Number );
120 break;
121 }
122 if ( ! aDate.IsValid() || aDate == aTmpDate)
123 break;
124 }
125
126 //create minor date tickinfos
127 aDate = aNull + static_cast<long>(::rtl::math::approxFloor(m_aScale.Minimum));
128 while( aDate<= aMaxDate )
129 {
130 if( bShifted && aDate==aMaxDate )
131 break;
132
133 TickInfo aNewTick(xInverseScaling); aNewTick.fScaledTickValue = aDate - aNull;
134 if( xInverseScaling.is() )
135 aNewTick.fScaledTickValue = xScaling->doScaling(aNewTick.fScaledTickValue);
136 rMinorTicks.push_back( aNewTick );
137
138 if(m_aIncrement.MinorTimeInterval.Number<=0)
139 break;
140
141 //find next minor date
142 const Date aTmpDate (aDate);
143 switch( m_aIncrement.MinorTimeInterval.TimeUnit )
144 {
145 case DAY:
146 aDate += m_aIncrement.MinorTimeInterval.Number;
147 break;
148 case YEAR:
149 aDate = DateHelper::GetDateSomeYearsAway( aDate, m_aIncrement.MinorTimeInterval.Number );
150 break;
151 case MONTH:
152 default:
153 aDate = DateHelper::GetDateSomeMonthsAway( aDate, m_aIncrement.MinorTimeInterval.Number );
154 break;
155 }
156 if ( ! aDate.IsValid() || aDate == aTmpDate)
157 break;
158 }
159 }
160
getAllTicks(::std::vector<::std::vector<TickInfo>> & rAllTickInfos) const161 void DateTickFactory::getAllTicks( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
162 {
163 getAllTicks( rAllTickInfos, false );
164 }
165
getAllTicksShifted(::std::vector<::std::vector<TickInfo>> & rAllTickInfos) const166 void DateTickFactory::getAllTicksShifted( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
167 {
168 getAllTicks( rAllTickInfos, true );
169 }
170
171 //.............................................................................
172 } //namespace chart
173 //.............................................................................
174