1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include <sfx2/dispatch.hxx>
12 #include <svl/zforlist.hxx>
13 #include <svl/undo.hxx>
14 #include <boost/random.hpp>
15 #include <boost/scoped_ptr.hpp>
17 #include "formulacell.hxx"
18 #include "rangelst.hxx"
19 #include "scitems.hxx"
21 #include "document.hxx"
22 #include "uiitems.hxx"
23 #include "reffact.hxx"
24 #include "strload.hxx"
25 #include "docfunc.hxx"
26 #include "StatisticsDialogs.hrc"
27 #include "TableFillingAndNavigationTools.hxx"
29 #include "DescriptiveStatisticsDialog.hxx"
34 struct StatisticCalculation
{
35 sal_Int16 aCalculationNameId
;
39 static const StatisticCalculation lclCalcDefinitions
[] =
41 { STRID_CALC_MEAN
, "=AVERAGE(%RANGE%)" },
42 { STRID_CALC_STD_ERROR
, "=SQRT(VAR(%RANGE%)/COUNT(%RANGE%))"},
43 { STRID_CALC_MODE
, "=MODE(%RANGE%)"},
44 { STRID_CALC_MEDIAN
, "=MEDIAN(%RANGE%)"},
45 { STRID_CALC_FIRST_QUARTILE
, "=QUARTILE(%RANGE%; 1)" },
46 { STRID_CALC_THIRD_QUARTILE
, "=QUARTILE(%RANGE%; 3)" },
47 { STRID_CALC_VARIANCE
, "=VAR(%RANGE%)"},
48 { STRID_CALC_STD_DEVIATION
, "=STDEV(%RANGE%)"},
49 { STRID_CALC_KURTOSIS
, "=KURT(%RANGE%)"},
50 { STRID_CALC_SKEWNESS
, "=SKEW(%RANGE%)"},
51 { STRID_CALC_RANGE
, "=MAX(%RANGE%)-MIN(%RANGE%)"},
52 { STRID_CALC_MIN
, "=MIN(%RANGE%)"},
53 { STRID_CALC_MAX
, "=MAX(%RANGE%)"},
54 { STRID_CALC_SUM
, "=SUM(%RANGE%)"},
55 { STRID_CALC_COUNT
, "=COUNT(%RANGE%)" },
59 static const char strWildcardRange
[] = "%RANGE%";
60 static const char strWildcardNumber
[] = "%NUMBER%";
64 ScDescriptiveStatisticsDialog::ScDescriptiveStatisticsDialog(
65 SfxBindings
* pSfxBindings
, SfxChildWindow
* pChildWindow
,
66 vcl::Window
* pParent
, ScViewData
* pViewData
) :
67 ScStatisticsInputOutputDialog(
68 pSfxBindings
, pChildWindow
, pParent
, pViewData
,
69 "DescriptiveStatisticsDialog", "modules/scalc/ui/descriptivestatisticsdialog.ui" )
72 ScDescriptiveStatisticsDialog::~ScDescriptiveStatisticsDialog()
75 bool ScDescriptiveStatisticsDialog::Close()
77 return DoClose( ScDescriptiveStatisticsDialogWrapper::GetChildWindowId() );
80 sal_Int16
ScDescriptiveStatisticsDialog::GetUndoNameId()
82 return STR_DESCRIPTIVE_STATISTICS_UNDO_NAME
;
85 ScRange
ScDescriptiveStatisticsDialog::ApplyOutput(ScDocShell
* pDocShell
)
87 AddressWalkerWriter
aOutput(mOutputAddress
, pDocShell
, mDocument
,
88 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH
, mAddressDetails
.eConv
));
89 FormulaTemplate
aTemplate(mDocument
);
91 boost::scoped_ptr
<DataRangeIterator
> pIterator
;
92 if (mGroupedBy
== BY_COLUMN
)
93 pIterator
.reset(new DataRangeByColumnIterator(mInputRange
));
95 pIterator
.reset(new DataRangeByRowIterator(mInputRange
));
99 // Use explicit sheet name in case the input and output are on different sheets.
100 bool b3DAddress
= mInputRange
.aStart
.Tab() != mOutputAddress
.Tab();
102 // Write column/row labels
103 for( ; pIterator
->hasNext(); pIterator
->next() )
105 if (mGroupedBy
== BY_COLUMN
)
106 aTemplate
.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_COLUMN_LABEL_TEMPLATE
));
108 aTemplate
.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_ROW_LABEL_TEMPLATE
));
110 aTemplate
.applyNumber(strWildcardNumber
, pIterator
->index() + 1);
111 aOutput
.writeBoldString(aTemplate
.getTemplate());
112 aOutput
.nextColumn();
115 aOutput
.resetColumn();
118 // Write calculation labels
119 for(sal_Int32 i
= 0; lclCalcDefinitions
[i
].aFormula
!= NULL
; i
++)
121 OUString
aLabel(SC_STRLOAD(RID_STATISTICS_DLGS
, lclCalcDefinitions
[i
].aCalculationNameId
));
122 aOutput
.writeString(aLabel
);
125 aOutput
.nextColumn();
129 for( ; pIterator
->hasNext(); pIterator
->next() )
133 for(sal_Int32 i
= 0; lclCalcDefinitions
[i
].aFormula
!= NULL
; i
++)
135 aTemplate
.setTemplate(lclCalcDefinitions
[i
].aFormula
);
136 aTemplate
.applyRange(strWildcardRange
, pIterator
->get(), b3DAddress
);
137 aOutput
.writeFormula(aTemplate
.getTemplate());
140 aOutput
.nextColumn();
143 return ScRange(aOutput
.mMinimumAddress
, aOutput
.mMaximumAddress
);
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */