Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / chart2 / source / view / axes / Tickmarks_Dates.cxx
blobb9442374a7377673e5c94fef36ea1b2c7740e89f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <DateHelper.hxx>
25 namespace chart
27 using namespace ::com::sun::star;
28 using namespace ::com::sun::star::chart2;
29 using namespace ::rtl::math;
30 using ::com::sun::star::chart::TimeUnit::DAY;
31 using ::com::sun::star::chart::TimeUnit::MONTH;
32 using ::com::sun::star::chart::TimeUnit::YEAR;
34 DateTickFactory::DateTickFactory(
35 const ExplicitScaleData& rScale, const ExplicitIncrementData& rIncrement )
36 : m_aScale( rScale )
37 , m_aIncrement( rIncrement )
38 , m_xInverseScaling(nullptr)
40 //@todo: make sure that the scale is valid for the scaling
42 if( m_aScale.Scaling.is() )
44 m_xInverseScaling = m_aScale.Scaling->getInverseScaling();
45 OSL_ENSURE( m_xInverseScaling.is(), "each Scaling needs to return a inverse Scaling" );
49 DateTickFactory::~DateTickFactory()
53 void DateTickFactory::getAllTicks( TickInfoArraysType& rAllTickInfos, bool bShifted ) const
55 rAllTickInfos.resize(2);
56 TickInfoArrayType& rMajorTicks = rAllTickInfos[0];
57 TickInfoArrayType& rMinorTicks = rAllTickInfos[1];
58 rMajorTicks.clear();
59 rMinorTicks.clear();
61 Date aNull(m_aScale.NullDate);
63 Date aDate = aNull + static_cast<sal_Int32>(::rtl::math::approxFloor(m_aScale.Minimum));
64 Date aMaxDate = aNull + static_cast<sal_Int32>(::rtl::math::approxFloor(m_aScale.Maximum));
66 uno::Reference< chart2::XScaling > xScaling(m_aScale.Scaling);
67 uno::Reference< chart2::XScaling > xInverseScaling(m_xInverseScaling);
68 if( bShifted )
70 xScaling = new DateScaling(aNull,m_aScale.TimeResolution,true/*bShifted*/);
71 xInverseScaling = xScaling->getInverseScaling();
74 //create major date tickinfos
75 while( aDate<= aMaxDate )
77 if( bShifted && aDate==aMaxDate )
78 break;
80 TickInfo aNewTick(xInverseScaling); aNewTick.fScaledTickValue = aDate - aNull;
82 if( xInverseScaling.is() )
83 aNewTick.fScaledTickValue = xScaling->doScaling(aNewTick.fScaledTickValue);
84 rMajorTicks.push_back( aNewTick );
86 if(m_aIncrement.MajorTimeInterval.Number<=0)
87 break;
89 //find next major date
90 switch( m_aIncrement.MajorTimeInterval.TimeUnit )
92 case DAY:
93 aDate.AddDays( m_aIncrement.MajorTimeInterval.Number );
94 break;
95 case YEAR:
96 aDate = DateHelper::GetDateSomeYearsAway( aDate, m_aIncrement.MajorTimeInterval.Number );
97 break;
98 case MONTH:
99 default:
100 aDate = DateHelper::GetDateSomeMonthsAway( aDate, m_aIncrement.MajorTimeInterval.Number );
101 break;
105 //create minor date tickinfos
106 aDate = aNull + static_cast<sal_Int32>(::rtl::math::approxFloor(m_aScale.Minimum));
107 while( aDate<= aMaxDate )
109 if( bShifted && aDate==aMaxDate )
110 break;
112 TickInfo aNewTick(xInverseScaling); aNewTick.fScaledTickValue = aDate - aNull;
113 if( xInverseScaling.is() )
114 aNewTick.fScaledTickValue = xScaling->doScaling(aNewTick.fScaledTickValue);
115 rMinorTicks.push_back( aNewTick );
117 if(m_aIncrement.MinorTimeInterval.Number<=0)
118 break;
120 //find next minor date
121 switch( m_aIncrement.MinorTimeInterval.TimeUnit )
123 case DAY:
124 aDate.AddDays( m_aIncrement.MinorTimeInterval.Number );
125 break;
126 case YEAR:
127 aDate = DateHelper::GetDateSomeYearsAway( aDate, m_aIncrement.MinorTimeInterval.Number );
128 break;
129 case MONTH:
130 default:
131 aDate = DateHelper::GetDateSomeMonthsAway( aDate, m_aIncrement.MinorTimeInterval.Number );
132 break;
137 void DateTickFactory::getAllTicks( TickInfoArraysType& rAllTickInfos ) const
139 getAllTicks( rAllTickInfos, false );
142 void DateTickFactory::getAllTicksShifted( TickInfoArraysType& rAllTickInfos ) const
144 getAllTicks( rAllTickInfos, true );
147 } //namespace chart
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */