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>
15 #include "formulacell.hxx"
16 #include "rangelst.hxx"
17 #include "scitems.hxx"
19 #include "document.hxx"
20 #include "uiitems.hxx"
21 #include "reffact.hxx"
22 #include "strload.hxx"
24 #include "docfunc.hxx"
25 #include "StatisticsDialogs.hrc"
26 #include "TableFillingAndNavigationTools.hxx"
28 #include "AnalysisOfVarianceDialog.hxx"
33 static sal_Int16 lclBasicStatisticsLabels
[] =
35 STR_ANOVA_LABEL_GROUPS
,
43 static const char* lclBasicStatisticsFormula
[] =
45 "=COUNT(%RANGE%)", "=SUM(%RANGE%)", "=AVERAGE(%RANGE%)", "=VAR(%RANGE%)", NULL
48 static sal_Int16 lclAnovaLabels
[] =
50 STR_ANOVA_LABEL_SOURCE_OF_VARIATION
,
55 STR_ANOVA_LABEL_P_VALUE
,
56 STR_ANOVA_LABEL_F_CRITICAL
,
60 static const OUString
strWildcardRange("%RANGE%");
61 static const OUString
strWildcardNumber("%NUMBER%");
63 OUString
lclCreateMultiParameterFormula(
64 ScRangeList
& aRangeList
, const OUString
& aFormulaTemplate
,
65 const OUString
& aWildcard
, ScDocument
* pDocument
,
66 ScAddress::Details
& aAddressDetails
)
69 for (size_t i
= 0; i
< aRangeList
.size(); i
++)
71 OUString
aRangeString(aRangeList
[i
]->Format(SCR_ABS
, pDocument
, aAddressDetails
));
72 OUString aFormulaString
= aFormulaTemplate
.replaceAll(aWildcard
, aRangeString
);
73 aResult
+= aFormulaString
;
74 if(i
!= aRangeList
.size() - 1) // Not Last
82 ScAnalysisOfVarianceDialog::ScAnalysisOfVarianceDialog(
83 SfxBindings
* pSfxBindings
, SfxChildWindow
* pChildWindow
,
84 Window
* pParent
, ScViewData
* pViewData
) :
85 ScStatisticsInputOutputDialog(
86 pSfxBindings
, pChildWindow
, pParent
, pViewData
,
87 "AnalysisOfVarianceDialog", "modules/scalc/ui/analysisofvariancedialog.ui" )
89 get(mpAlpha
, "alpha-spin");
92 ScAnalysisOfVarianceDialog::~ScAnalysisOfVarianceDialog()
95 sal_Bool
ScAnalysisOfVarianceDialog::Close()
97 return DoClose( ScAnalysisOfVarianceDialogWrapper::GetChildWindowId() );
100 sal_Int16
ScAnalysisOfVarianceDialog::GetUndoNameId()
102 return STR_ANALYSIS_OF_VARIANCE_UNDO_NAME
;
105 ScRange
ScAnalysisOfVarianceDialog::ApplyOutput(ScDocShell
* pDocShell
)
107 AddressWalkerWriter
output(mOutputAddress
, pDocShell
, mDocument
,
108 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH
, mAddressDetails
.eConv
));
109 FormulaTemplate
aTemplate(mDocument
, mAddressDetails
);
111 output
.writeBoldString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_ANOVA_SINGLE_FACTOR_LABEL
));
116 for(sal_Int32 i
= 0; lclBasicStatisticsLabels
[i
] != 0; i
++)
118 output
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, lclBasicStatisticsLabels
[i
]));
123 ScRangeList aRangeList
;
125 boost::scoped_ptr
<DataRangeIterator
> pIterator
;
126 if (mGroupedBy
== BY_COLUMN
)
127 pIterator
.reset(new DataRangeByColumnIterator(mInputRange
));
129 pIterator
.reset(new DataRangeByRowIterator(mInputRange
));
131 // Write statistic formulas for rows/columns
132 for( ; pIterator
->hasNext(); pIterator
->next() )
134 output
.resetColumn();
136 if (mGroupedBy
== BY_COLUMN
)
137 aTemplate
.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_COLUMN_LABEL_TEMPLATE
));
139 aTemplate
.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_ROW_LABEL_TEMPLATE
));
141 aTemplate
.applyNumber(strWildcardNumber
, pIterator
->index() + 1);
142 output
.writeString(aTemplate
.getTemplate());
146 ScRange aColumnRange
= pIterator
->get();
148 aRangeList
.Append(aColumnRange
);
150 for(sal_Int32 i
= 0; lclBasicStatisticsFormula
[i
] != NULL
; i
++)
152 aTemplate
.setTemplate(lclBasicStatisticsFormula
[i
]);
153 aTemplate
.applyRange(strWildcardRange
, aColumnRange
);
154 output
.writeFormula(aTemplate
.getTemplate());
160 output
.nextRow(); // Blank row
162 // Write ANOVA labels
163 output
.resetColumn();
164 for(sal_Int32 i
= 0; lclAnovaLabels
[i
] != 0; i
++)
166 output
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, lclAnovaLabels
[i
]));
174 output
.resetColumn();
175 output
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_ANOVA_LABEL_BETWEEN_GROUPS
));
179 aTemplate
.setTemplate("=%TOTAL% - %WITHIN%");
180 aTemplate
.applyAddress("%TOTAL%", output
.current(0, 2));
181 aTemplate
.applyAddress("%WITHIN%", output
.current(0, 1));
182 output
.writeFormula(aTemplate
.getTemplate());
186 aTemplate
.setTemplate("=%TOTAL% - %WITHIN%");
187 aTemplate
.applyAddress("%TOTAL%", output
.current(0, 2));
188 aTemplate
.applyAddress("%WITHIN%", output
.current(0, 1));
189 output
.writeFormula(aTemplate
.getTemplate());
193 aTemplate
.setTemplate("=%SS_REF% / %DF_REF%");
194 aTemplate
.applyAddress("%SS_REF%", output
.current(-2, 0));
195 aTemplate
.applyAddress("%DF_REF%", output
.current(-1, 0));
196 output
.writeFormula(aTemplate
.getTemplate());
200 aTemplate
.setTemplate("=%MS_BETWEEN% / %MS_WITHIN%");
201 aTemplate
.applyAddress("%MS_BETWEEN%", output
.current(-1, 0));
202 aTemplate
.applyAddress("%MS_WITHIN%", output
.current(-1, 1));
203 output
.writeFormula(aTemplate
.getTemplate());
207 aTemplate
.setTemplate("=FDIST(%F_VAL%; %DF_BETWEEN%; %DF_WITHIN%");
208 aTemplate
.applyAddress("%F_VAL%", output
.current(-1, 0));
209 aTemplate
.applyAddress("%DF_BETWEEN%", output
.current(-3, 0));
210 aTemplate
.applyAddress("%DF_WITHIN%", output
.current(-3, 1));
211 output
.writeFormula(aTemplate
.getTemplate());
215 double aAlphaValue
= mpAlpha
->GetValue() / 100.0;
216 OUString aAlphaString
= rtl::math::doubleToUString(
217 aAlphaValue
, rtl_math_StringFormat_Automatic
, rtl_math_DecimalPlaces_Max
,
218 ScGlobal::pLocaleData
->getNumDecimalSep()[0], true);
220 aTemplate
.setTemplate("=FINV(%ALPHA%; %DF_BETWEEN%; %DF_WITHIN%");
221 aTemplate
.applyString("%ALPHA%", aAlphaString
);
222 aTemplate
.applyAddress("%DF_BETWEEN%", output
.current(-4, 0));
223 aTemplate
.applyAddress("%DF_WITHIN%", output
.current(-4, 1));
224 output
.writeFormula(aTemplate
.getTemplate());
231 output
.resetColumn();
232 output
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_ANOVA_LABEL_WITHIN_GROUPS
));
236 OUString aSSPart
= lclCreateMultiParameterFormula(aRangeList
, OUString("DEVSQ(%RANGE%)"), strWildcardRange
, mDocument
, mAddressDetails
);
237 aTemplate
.setTemplate("=SUM(%RANGE%)");
238 aTemplate
.applyString(strWildcardRange
, aSSPart
);
239 output
.writeFormula(aTemplate
.getTemplate());
243 OUString aDFPart
= lclCreateMultiParameterFormula(aRangeList
, OUString("COUNT(%RANGE%)-1"), strWildcardRange
, mDocument
, mAddressDetails
);
244 aTemplate
.setTemplate("=SUM(%RANGE%)");
245 aTemplate
.applyString(strWildcardRange
, aDFPart
);
246 output
.writeFormula(aTemplate
.getTemplate());
250 aTemplate
.setTemplate("=%SS% / %DF%");
251 aTemplate
.applyAddress("%SS%", output
.current(-2, 0));
252 aTemplate
.applyAddress("%DF%", output
.current(-1, 0));
253 output
.writeFormula(aTemplate
.getTemplate());
260 output
.resetColumn();
261 output
.writeString(SC_STRLOAD(RID_STATISTICS_DLGS
, STR_ANOVA_LABEL_TOTAL
));
265 aTemplate
.setTemplate("=DEVSQ(%RANGE%)");
266 aTemplate
.applyRangeList(strWildcardRange
, aRangeList
);
267 output
.writeFormula(aTemplate
.getTemplate());
271 OUString aDFPart
= lclCreateMultiParameterFormula(aRangeList
, "COUNT(%RANGE%)", strWildcardRange
, mDocument
, mAddressDetails
);
272 aTemplate
.setTemplate("=SUM(%RANGE%) - 1");
273 aTemplate
.applyString(strWildcardRange
, aDFPart
);
274 output
.writeFormula(aTemplate
.getTemplate());
278 return ScRange(output
.mMinimumAddress
, output
.mMaximumAddress
);
281 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */