Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "Tickmarks_Dates.hxx"
21 : #include "DateScaling.hxx"
22 : #include <rtl/math.hxx>
23 : #include "DateHelper.hxx"
24 :
25 : namespace chart
26 : {
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;
34 :
35 0 : DateTickFactory::DateTickFactory(
36 : const ExplicitScaleData& rScale, const ExplicitIncrementData& rIncrement )
37 : : m_aScale( rScale )
38 : , m_aIncrement( rIncrement )
39 0 : , m_xInverseScaling(NULL)
40 : {
41 : //@todo: make sure that the scale is valid for the scaling
42 :
43 0 : if( m_aScale.Scaling.is() )
44 : {
45 0 : m_xInverseScaling = m_aScale.Scaling->getInverseScaling();
46 : OSL_ENSURE( m_xInverseScaling.is(), "each Scaling needs to return a inverse Scaling" );
47 : }
48 :
49 0 : m_fScaledVisibleMin = m_aScale.Minimum;
50 0 : if( m_xInverseScaling.is() )
51 0 : m_fScaledVisibleMin = m_aScale.Scaling->doScaling(m_fScaledVisibleMin);
52 :
53 0 : m_fScaledVisibleMax = m_aScale.Maximum;
54 0 : if( m_xInverseScaling.is() )
55 0 : m_fScaledVisibleMax = m_aScale.Scaling->doScaling(m_fScaledVisibleMax);
56 0 : }
57 :
58 0 : DateTickFactory::~DateTickFactory()
59 : {
60 0 : }
61 :
62 0 : void DateTickFactory::getAllTicks( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos, bool bShifted ) const
63 : {
64 0 : rAllTickInfos.resize(2);
65 0 : ::std::vector< TickInfo >& rMajorTicks = rAllTickInfos[0];
66 0 : ::std::vector< TickInfo >& rMinorTicks = rAllTickInfos[1];
67 0 : rMajorTicks.clear();
68 0 : rMinorTicks.clear();
69 :
70 0 : Date aNull(m_aScale.NullDate);
71 :
72 0 : Date aDate = aNull + static_cast<long>(::rtl::math::approxFloor(m_aScale.Minimum));
73 0 : Date aMaxDate = aNull + static_cast<long>(::rtl::math::approxFloor(m_aScale.Maximum));
74 :
75 0 : uno::Reference< chart2::XScaling > xScaling(m_aScale.Scaling);
76 0 : uno::Reference< chart2::XScaling > xInverseScaling(m_xInverseScaling);
77 0 : if( bShifted )
78 : {
79 0 : xScaling = new DateScaling(aNull,m_aScale.TimeResolution,true/*bShifted*/);
80 0 : xInverseScaling = xScaling->getInverseScaling();
81 : }
82 :
83 : //create major date tickinfos
84 0 : while( aDate<= aMaxDate )
85 : {
86 0 : if( bShifted && aDate==aMaxDate )
87 0 : break;
88 :
89 0 : TickInfo aNewTick(xInverseScaling); aNewTick.fScaledTickValue = aDate - aNull;
90 :
91 0 : if( xInverseScaling.is() )
92 0 : aNewTick.fScaledTickValue = xScaling->doScaling(aNewTick.fScaledTickValue);
93 0 : rMajorTicks.push_back( aNewTick );
94 :
95 0 : if(m_aIncrement.MajorTimeInterval.Number<=0)
96 0 : break;
97 :
98 : //find next major date
99 0 : switch( m_aIncrement.MajorTimeInterval.TimeUnit )
100 : {
101 : case DAY:
102 0 : aDate += m_aIncrement.MajorTimeInterval.Number;
103 0 : break;
104 : case YEAR:
105 0 : aDate = DateHelper::GetDateSomeYearsAway( aDate, m_aIncrement.MajorTimeInterval.Number );
106 0 : break;
107 : case MONTH:
108 : default:
109 0 : aDate = DateHelper::GetDateSomeMonthsAway( aDate, m_aIncrement.MajorTimeInterval.Number );
110 0 : break;
111 : }
112 0 : }
113 :
114 : //create minor date tickinfos
115 0 : aDate = aNull + static_cast<long>(::rtl::math::approxFloor(m_aScale.Minimum));
116 0 : while( aDate<= aMaxDate )
117 : {
118 0 : if( bShifted && aDate==aMaxDate )
119 0 : break;
120 :
121 0 : TickInfo aNewTick(xInverseScaling); aNewTick.fScaledTickValue = aDate - aNull;
122 0 : if( xInverseScaling.is() )
123 0 : aNewTick.fScaledTickValue = xScaling->doScaling(aNewTick.fScaledTickValue);
124 0 : rMinorTicks.push_back( aNewTick );
125 :
126 0 : if(m_aIncrement.MinorTimeInterval.Number<=0)
127 0 : break;
128 :
129 : //find next minor date
130 0 : switch( m_aIncrement.MinorTimeInterval.TimeUnit )
131 : {
132 : case DAY:
133 0 : aDate += m_aIncrement.MinorTimeInterval.Number;
134 0 : break;
135 : case YEAR:
136 0 : aDate = DateHelper::GetDateSomeYearsAway( aDate, m_aIncrement.MinorTimeInterval.Number );
137 0 : break;
138 : case MONTH:
139 : default:
140 0 : aDate = DateHelper::GetDateSomeMonthsAway( aDate, m_aIncrement.MinorTimeInterval.Number );
141 0 : break;
142 : }
143 0 : }
144 0 : }
145 :
146 0 : void DateTickFactory::getAllTicks( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
147 : {
148 0 : getAllTicks( rAllTickInfos, false );
149 0 : }
150 :
151 0 : void DateTickFactory::getAllTicksShifted( ::std::vector< ::std::vector< TickInfo > >& rAllTickInfos ) const
152 : {
153 0 : getAllTicks( rAllTickInfos, true );
154 0 : }
155 :
156 : } //namespace chart
157 :
158 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|