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 */
10
11 #include <memory>
12
13 #include <reffact.hxx>
14 #include <TableFillingAndNavigationTools.hxx>
15 #include <DescriptiveStatisticsDialog.hxx>
16 #include <scresid.hxx>
17 #include <strings.hrc>
18
19 namespace
20 {
21
22 struct StatisticCalculation {
23 TranslateId aCalculationNameId;
24 const char* aFormula;
25 };
26
27 const StatisticCalculation lclCalcDefinitions[] =
28 {
29 { STRID_CALC_MEAN, "=AVERAGE(%RANGE%)" },
30 { STRID_CALC_STD_ERROR, "=SQRT(VAR(%RANGE%)/COUNT(%RANGE%))"},
31 { STRID_CALC_MODE, "=MODE(%RANGE%)"},
32 { STRID_CALC_MEDIAN, "=MEDIAN(%RANGE%)"},
33 { STRID_CALC_FIRST_QUARTILE, "=QUARTILE(%RANGE%; 1)" },
34 { STRID_CALC_THIRD_QUARTILE, "=QUARTILE(%RANGE%; 3)" },
35 { STRID_CALC_VARIANCE, "=VAR(%RANGE%)"},
36 { STRID_CALC_STD_DEVIATION, "=STDEV(%RANGE%)"},
37 { STRID_CALC_KURTOSIS, "=KURT(%RANGE%)"},
38 { STRID_CALC_SKEWNESS, "=SKEW(%RANGE%)"},
39 { STRID_CALC_RANGE, "=MAX(%RANGE%)-MIN(%RANGE%)"},
40 { STRID_CALC_MIN, "=MIN(%RANGE%)"},
41 { STRID_CALC_MAX, "=MAX(%RANGE%)"},
42 { STRID_CALC_SUM, "=SUM(%RANGE%)"},
43 { STRID_CALC_COUNT, "=COUNT(%RANGE%)" },
44 { {}, nullptr }
45 };
46
47 }
48
ScDescriptiveStatisticsDialog(SfxBindings * pSfxBindings,SfxChildWindow * pChildWindow,weld::Window * pParent,ScViewData & rViewData)49 ScDescriptiveStatisticsDialog::ScDescriptiveStatisticsDialog(
50 SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
51 weld::Window* pParent, ScViewData& rViewData ) :
52 ScStatisticsInputOutputDialog(
53 pSfxBindings, pChildWindow, pParent, rViewData,
54 u"modules/scalc/ui/descriptivestatisticsdialog.ui"_ustr,
55 u"DescriptiveStatisticsDialog"_ustr)
56 {}
57
~ScDescriptiveStatisticsDialog()58 ScDescriptiveStatisticsDialog::~ScDescriptiveStatisticsDialog()
59 {}
60
Close()61 void ScDescriptiveStatisticsDialog::Close()
62 {
63 DoClose( ScDescriptiveStatisticsDialogWrapper::GetChildWindowId() );
64 }
65
GetUndoNameId()66 TranslateId ScDescriptiveStatisticsDialog::GetUndoNameId()
67 {
68 return STR_DESCRIPTIVE_STATISTICS_UNDO_NAME;
69 }
70
ApplyOutput(ScDocShell * pDocShell)71 ScRange ScDescriptiveStatisticsDialog::ApplyOutput(ScDocShell* pDocShell)
72 {
73 AddressWalkerWriter aOutput(mOutputAddress, pDocShell, mDocument,
74 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
75 FormulaTemplate aTemplate(&mDocument);
76
77 std::unique_ptr<DataRangeIterator> pIterator;
78 if (mGroupedBy == BY_COLUMN)
79 pIterator.reset(new DataRangeByColumnIterator(mInputRange));
80 else
81 pIterator.reset(new DataRangeByRowIterator(mInputRange));
82
83 aOutput.nextColumn();
84
85 // Use explicit sheet name in case the input and output are on different sheets.
86 bool b3DAddress = mInputRange.aStart.Tab() != mOutputAddress.Tab();
87
88 // Write column/row labels
89 for( ; pIterator->hasNext(); pIterator->next() )
90 {
91 // tdf#128018 - add column/row labels to the output
92 OUString aColRowLabel = mDocument.GetString(pIterator->get().aStart);
93 if (aColRowLabel.isEmpty())
94 {
95 if (mGroupedBy == BY_COLUMN)
96 aTemplate.setTemplate(ScResId(STR_COLUMN_LABEL_TEMPLATE));
97 else
98 aTemplate.setTemplate(ScResId(STR_ROW_LABEL_TEMPLATE));
99
100 aTemplate.applyNumber(u"%NUMBER%", pIterator->index() + 1);
101 aOutput.writeBoldString(aTemplate.getTemplate());
102 }
103 else
104 {
105 aOutput.writeBoldString(aColRowLabel);
106 }
107 aOutput.nextColumn();
108 }
109 aOutput.nextRow();
110 aOutput.resetColumn();
111 aOutput.push();
112
113 // Write calculation labels
114 for(sal_Int32 i = 0; lclCalcDefinitions[i].aFormula != nullptr; i++)
115 {
116 OUString aLabel(ScResId(lclCalcDefinitions[i].aCalculationNameId));
117 aOutput.writeString(aLabel);
118 aOutput.nextRow();
119 }
120 aOutput.nextColumn();
121
122 pIterator->reset();
123
124 for( ; pIterator->hasNext(); pIterator->next() )
125 {
126 aOutput.resetRow();
127
128 for(sal_Int32 i = 0; lclCalcDefinitions[i].aFormula != nullptr; i++)
129 {
130 aTemplate.setTemplate(lclCalcDefinitions[i].aFormula);
131 aTemplate.applyRange(u"%RANGE%", pIterator->get(), b3DAddress);
132 aOutput.writeFormula(aTemplate.getTemplate());
133 aOutput.nextRow();
134 }
135 aOutput.nextColumn();
136 }
137
138 return ScRange(aOutput.mMinimumAddress, aOutput.mMaximumAddress);
139 }
140
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
142