summaryrefslogtreecommitdiff
path: root/sc/source/ui/StatisticsDialogs/AnalysisOfVarianceDialog.cxx
blob: cb00bac056159e615f0458a24a5cb90ccdce9725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include <sfx2/dispatch.hxx>
#include <svl/zforlist.hxx>
#include <svl/undo.hxx>

#include "formulacell.hxx"
#include "rangelst.hxx"
#include "scitems.hxx"
#include "docsh.hxx"
#include "document.hxx"
#include "uiitems.hxx"
#include "reffact.hxx"
#include "scresid.hxx"
#include "random.hxx"
#include "docfunc.hxx"
#include "globstr.hrc"
#include "sc.hrc"

#include "AnalysisOfVarianceDialog.hxx"

namespace
{

static const char* lclBasicStatisticsLabels[] =
{
    "Groups", "Count", "Sum", "Mean", "Variance", NULL
};

static const char* lclAnovaLabels[] =
{
    "Source of Variation", "SS", "df", "MS", "F", "P-value", "F critical", NULL
};

static const char* lclAnovaFormula[] =
{
    "=COUNT(%RANGE%)", "=SUM(%RANGE%)", "=AVERAGE(%RANGE%)", "=VAR(%RANGE%)", NULL
};

static const OUString lclWildcardRange("%RANGE%");

OUString lclCreateMultiParameterFormula(
            ScRangeList&        aRangeList, const OUString& aFormulaTemplate,
            const OUString&     aWildcard,  ScDocument*     pDocument,
            ScAddress::Details& aAddressDetails)
{
    OUString aResult;
    for (size_t i = 0; i < aRangeList.size(); i++)
    {
        OUString aRangeString;
        aRangeList[i]->Format( aRangeString, SCR_ABS, pDocument, aAddressDetails );
        OUString aFormulaString = aFormulaTemplate.replaceAll(aWildcard, aRangeString);
        aResult += aFormulaString;
        if(i != aRangeList.size() - 1) // Not Last
            aResult+= ";";
    }
    return aResult;
}

class CellAddressIterator
{
public:
    CellAddressIterator(ScAddress& aStartAddress)
    {}

    virtual ~CellAddressIterator()
    {}
};

}

ScAnalysisOfVarianceDialog::ScAnalysisOfVarianceDialog(
                    SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
                    Window* pParent, ScViewData* pViewData ) :
    ScStatisticsInputOutputDialog(
            pSfxBindings, pChildWindow, pParent, pViewData,
            "AnalysisOfVarianceDialog", "modules/scalc/ui/analysisofvariancedialog.ui" )
{
    get(mpAlpha,        "alpha-spin");
}

ScAnalysisOfVarianceDialog::~ScAnalysisOfVarianceDialog()
{}

sal_Bool ScAnalysisOfVarianceDialog::Close()
{
    return DoClose( ScAnalysisOfVarianceDialogWrapper::GetChildWindowId() );
}

void ScAnalysisOfVarianceDialog::CalculateInputAndWriteToOutput( )
{
    OUString aUndo("Analysis Of Variance");
    ScDocShell* pDocShell = mViewData->GetDocShell();
    svl::IUndoManager* pUndoManager = pDocShell->GetUndoManager();
    pUndoManager->EnterListAction( aUndo, aUndo );

    ScAddress aStart = mInputRange.aStart;
    ScAddress aEnd   = mInputRange.aEnd;

    SCTAB outTab = mOutputAddress.Tab();
    SCCOL outCol = mOutputAddress.Col();
    SCROW outRow = mOutputAddress.Row();

    OUString aReferenceString;
    ScAddress aAddress;

    for (SCROW inTab = aStart.Tab(); inTab <= aEnd.Tab(); inTab++)
    {
        outCol = mOutputAddress.Col();

        // Write labels
        for(sal_Int32 i = 0; lclBasicStatisticsLabels[i] != NULL; i++)
        {
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aCalculationName(OUString::createFromAscii(lclBasicStatisticsLabels[i]));
            pDocShell->GetDocFunc().SetStringCell(aAddress, aCalculationName, true);
            outCol++;
        }
        outRow++;

        ScRangeList aRangeList;

        // Write statistic formulas for columns
        for (SCCOL inCol = aStart.Col(); inCol <= aEnd.Col(); inCol++)
        {
            outCol = mOutputAddress.Col();
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aGroupName = OUString("Column ") + OUString::number(inCol - aStart.Col() + 1);
            pDocShell->GetDocFunc().SetStringCell(aAddress, aGroupName, true);
            outCol++;

            ScRange aColumnRange (
                ScAddress(inCol, aStart.Row(), inTab),
                ScAddress(inCol, aEnd.Row(), inTab)
            );
            aRangeList.Append(aColumnRange);

            aColumnRange.Format( aReferenceString, SCR_ABS, mDocument, mAddressDetails );
            OUString aFormulaString;
            OUString aFormulaTemplate;

            for(sal_Int32 i = 0; lclAnovaFormula[i] != NULL; i++)
            {
                aAddress = ScAddress(outCol, outRow, outTab);
                aFormulaTemplate = OUString::createFromAscii(lclAnovaFormula[i]);
                aFormulaString = aFormulaTemplate.replaceAll(lclWildcardRange, aReferenceString);
                pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aFormulaString), true);
                outCol++;
            }
            outRow++;
        }

        outRow++; // Blank row

        // Write ANOVA labels
        outCol = mOutputAddress.Col();
        for(sal_Int32 i = 0; lclAnovaLabels[i] != NULL; i++)
        {
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aCalculationName(OUString::createFromAscii(lclAnovaLabels[i]));
            pDocShell->GetDocFunc().SetStringCell(aAddress, aCalculationName, true);
            outCol++;
        }
        outRow++;

        // Between Groups
        {
            // Label
            outCol = mOutputAddress.Col();
            aAddress = ScAddress(outCol, outRow, outTab);
            pDocShell->GetDocFunc().SetStringCell(aAddress, OUString("Between Groups"), true);
            outCol++;

            // Sum of Squares
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aReferenceTotal;
            OUString aReferenceWithin;
            ScAddress aAddressTotal(outCol, outRow+2, outTab);
            aAddressTotal.Format( aReferenceTotal, SCR_ABS, mDocument, mAddressDetails );
            ScAddress aAddressWithin(outCol, outRow+1, outTab);
            aAddressWithin.Format( aReferenceWithin, SCR_ABS, mDocument, mAddressDetails );
            OUString aFormulaString = "=" + aReferenceTotal + "-" + aReferenceWithin;
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aFormulaString), true);
            outCol++;

            // Degree of freedom
            aAddress = ScAddress(outCol, outRow, outTab);
            aAddressTotal = ScAddress(outCol, outRow+2, outTab);
            aAddressTotal.Format( aReferenceTotal, SCR_ABS, mDocument, mAddressDetails );
            aAddressWithin = ScAddress(outCol, outRow+1, outTab);
            aAddressWithin.Format( aReferenceWithin, SCR_ABS, mDocument, mAddressDetails );
            aFormulaString = "=" + aReferenceTotal + "-" + aReferenceWithin;
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aFormulaString), true);
            outCol++;

            // MS
            OUString aSSRef;
            ScAddress(outCol-2, outRow, outTab).Format( aSSRef, SCR_ABS, mDocument, mAddressDetails );
            OUString aDFRef;
            ScAddress(outCol-1, outRow, outTab).Format( aDFRef, SCR_ABS, mDocument, mAddressDetails );
            aFormulaString = "=" + aSSRef + "/" + aDFRef;
            aAddress = ScAddress(outCol, outRow, outTab);
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aFormulaString), true);
            outCol++;

            // F
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aMSBetween;
            ScAddress(outCol-1, outRow, outTab).Format( aMSBetween, SCR_ABS, mDocument, mAddressDetails );
            OUString aMSWithin;
            ScAddress(outCol-1, outRow+1, outTab).Format( aMSWithin, SCR_ABS, mDocument, mAddressDetails );
            aFormulaString = "=" + aMSBetween + "/" + aMSWithin;
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aFormulaString), true);
            outCol++;

            // P-value
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aFValue;
            ScAddress(outCol-1, outRow, outTab).Format( aFValue, SCR_ABS, mDocument, mAddressDetails );
            OUString aDFBetween;
            ScAddress(outCol-3, outRow, outTab).Format( aDFBetween, SCR_ABS, mDocument, mAddressDetails );
            OUString aDFWithin;
            ScAddress(outCol-3, outRow+1, outTab).Format( aDFWithin, SCR_ABS, mDocument, mAddressDetails );
            aFormulaString = "=FDIST("+ aFValue + ";" + aDFBetween + ";" + aDFWithin + ")";
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aFormulaString), true);
            outCol++;

            // F critical
            double aAlphaValue = mpAlpha->GetValue() / 100.0;
            OUString aAlphaString = rtl::math::doubleToUString(
                                        aAlphaValue, rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
                                        ScGlobal::pLocaleData->getNumDecimalSep()[0], true);
            aAddress = ScAddress(outCol, outRow, outTab);
            aFormulaString = "=FINV(" + aAlphaString + ";" + aDFBetween + ";" + aDFWithin + ")";
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aFormulaString), true);
        }
        outRow++;

        // Within Groups
        {
            // Label
            outCol = mOutputAddress.Col();
            aAddress = ScAddress(outCol, outRow, outTab);
            pDocShell->GetDocFunc().SetStringCell(aAddress, OUString("Within Groups"), true);
            outCol++;

            // Sum of Squares
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aSum("=SUM(%RANGE%)");
            OUString aDevSQ("DEVSQ(%RANGE%)");
            OUString aSSPart = lclCreateMultiParameterFormula(aRangeList, aDevSQ, lclWildcardRange, mDocument, mAddressDetails);
            OUString aSS = aSum.replaceAll(lclWildcardRange, aSSPart);
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aSS), true);
            outCol++;

            // Degree of freedom
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aCountMinusOne("COUNT(%RANGE%)-1");
            OUString aDFPart = lclCreateMultiParameterFormula(aRangeList, aCountMinusOne, lclWildcardRange, mDocument, mAddressDetails);
            OUString aDF = aSum.replaceAll(lclWildcardRange, aDFPart);
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aDF), true);
            outCol++;

            // MS
            OUString aSSRef;
            OUString aDFRef;
            ScAddress aAddressSS(outCol-2, outRow, outTab);
            aAddressSS.Format( aSSRef, SCR_ABS, mDocument, mAddressDetails );
            ScAddress aAddressDF(outCol-1, outRow, outTab);
            aAddressDF.Format( aDFRef, SCR_ABS, mDocument, mAddressDetails );
            OUString aFormulaString = "=" + aSSRef + "/" + aDFRef;
            aAddress = ScAddress(outCol, outRow, outTab);
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aFormulaString), true);
        }
        outRow++;

        // Total
        {
            // Label
            outCol = mOutputAddress.Col();
            aAddress = ScAddress(outCol, outRow, outTab);
            pDocShell->GetDocFunc().SetStringCell(aAddress, OUString("Total"), true);
            outCol++;

            // Sum of Squares
            OUString aDevSQ("DEVSQ(%RANGE%)");
            aRangeList.Format( aReferenceString, SCR_ABS, mDocument );
            OUString aFormulaString = aDevSQ.replaceAll(lclWildcardRange, aReferenceString);

            aAddress = ScAddress(outCol, outRow, outTab);
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, "=" + aFormulaString), true);
            outCol++;

            // Degree of freedom
            aAddress = ScAddress(outCol, outRow, outTab);
            OUString aCount("COUNT(%RANGE%)");
            OUString aSumMinusOne("=SUM(%RANGE%)-1");
            OUString aDFPart = lclCreateMultiParameterFormula(aRangeList, aCount, lclWildcardRange, mDocument, mAddressDetails);
            OUString aDF = aSumMinusOne.replaceAll(lclWildcardRange, aDFPart);
            pDocShell->GetDocFunc().SetFormulaCell(aAddress, new ScFormulaCell(mDocument, aAddress, aDF), true);
        }
        outRow++;
    }

    ScRange aOutputRange(mOutputAddress, ScAddress(outTab, outRow, outTab) );
    pUndoManager->LeaveListAction();
    pDocShell->PostPaint( aOutputRange, PAINT_GRID );
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */