LibreOffice Module sc (master)  1
DescriptiveStatisticsDialog.cxx
Go to the documentation of this file.
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>
16 #include <scresid.hxx>
17 #include <strings.hrc>
18 
19 namespace
20 {
21 
22 struct StatisticCalculation {
23  const char* 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, nullptr }
45 };
46 
47 }
48 
50  SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
51  weld::Window* pParent, ScViewData* pViewData ) :
53  pSfxBindings, pChildWindow, pParent, pViewData,
54  "modules/scalc/ui/descriptivestatisticsdialog.ui",
55  "DescriptiveStatisticsDialog")
56 {}
57 
59 {}
60 
62 {
64 }
65 
67 {
68  return STR_DESCRIPTIVE_STATISTICS_UNDO_NAME;
69 }
70 
72 {
73  AddressWalkerWriter aOutput(mOutputAddress, pDocShell, &mDocument,
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  if (mGroupedBy == BY_COLUMN)
92  aTemplate.setTemplate(ScResId(STR_COLUMN_LABEL_TEMPLATE));
93  else
94  aTemplate.setTemplate(ScResId(STR_ROW_LABEL_TEMPLATE));
95 
96  aTemplate.applyNumber("%NUMBER%", pIterator->index() + 1);
97  aOutput.writeBoldString(aTemplate.getTemplate());
98  aOutput.nextColumn();
99  }
100  aOutput.nextRow();
101  aOutput.resetColumn();
102  aOutput.push();
103 
104  // Write calculation labels
105  for(sal_Int32 i = 0; lclCalcDefinitions[i].aFormula != nullptr; i++)
106  {
107  OUString aLabel(ScResId(lclCalcDefinitions[i].aCalculationNameId));
108  aOutput.writeString(aLabel);
109  aOutput.nextRow();
110  }
111  aOutput.nextColumn();
112 
113  pIterator->reset();
114 
115  for( ; pIterator->hasNext(); pIterator->next() )
116  {
117  aOutput.resetRow();
118 
119  for(sal_Int32 i = 0; lclCalcDefinitions[i].aFormula != nullptr; i++)
120  {
121  aTemplate.setTemplate(lclCalcDefinitions[i].aFormula);
122  aTemplate.applyRange("%RANGE%", pIterator->get(), b3DAddress);
123  aOutput.writeFormula(aTemplate.getTemplate());
124  aOutput.nextRow();
125  }
126  aOutput.nextColumn();
127  }
128 
129  return ScRange(aOutput.mMinimumAddress, aOutput.mMaximumAddress);
130 }
131 
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void applyNumber(const OUString &aVariable, sal_Int32 aValue)
ScAddress aStart
Definition: address.hxx:500
void writeFormula(const OUString &aFormula)
void writeString(const OUString &aString)
void push(SCCOL aRelativeCol=0, SCROW aRelativeRow=0, SCTAB aRelativeTab=0)
formula::FormulaGrammar::AddressConvention eConv
Definition: address.hxx:213
void writeBoldString(const OUString &aString)
static Grammar mergeToGrammar(const Grammar eGrammar, const AddressConvention eConv)
SCTAB Tab() const
Definition: address.hxx:271
ScDescriptiveStatisticsDialog(SfxBindings *pB, SfxChildWindow *pCW, weld::Window *pParent, ScViewData *pViewData)
int i
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
void applyRange(const OUString &aVariable, const ScRange &aRange, bool b3D=true)
virtual ScRange ApplyOutput(ScDocShell *pDocShell) override
virtual const char * GetUndoNameId() override
OUString aLabel
void setTemplate(const OUString &aTemplate)
bool DoClose(sal_uInt16 nId)
Definition: anyrefdg.cxx:694