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
19namespace
20{
21
22struct StatisticCalculation {
23 TranslateId aCalculationNameId;
24 const char* aFormula;
25};
26
27const 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
50 SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
51 weld::Window* pParent, ScViewData& rViewData ) :
53 pSfxBindings, pChildWindow, pParent, rViewData,
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{
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: */
void writeFormula(const OUString &aFormula)
void writeBoldString(const OUString &aString)
void writeString(const OUString &aString)
void push(SCCOL aRelativeCol=0, SCROW aRelativeRow=0, SCTAB aRelativeTab=0)
void setTemplate(const OUString &aTemplate)
void applyRange(std::u16string_view aVariable, const ScRange &aRange, bool b3D=true)
void applyNumber(std::u16string_view aVariable, sal_Int32 aValue)
SCTAB Tab() const
Definition: address.hxx:283
virtual TranslateId GetUndoNameId() override
ScDescriptiveStatisticsDialog(SfxBindings *pB, SfxChildWindow *pCW, weld::Window *pParent, ScViewData &rViewData)
virtual ScRange ApplyOutput(ScDocShell *pDocShell) override
SC_DLLPUBLIC OUString GetString(SCCOL nCol, SCROW nRow, SCTAB nTab, const ScInterpreterContext *pContext=nullptr) const
Definition: document.cxx:3505
ScAddress aStart
Definition: address.hxx:497
bool DoClose(sal_uInt16 nId)
Definition: anyrefdg.cxx:714
static Grammar mergeToGrammar(const Grammar eGrammar, const AddressConvention eConv)
float u
int i
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
formula::FormulaGrammar::AddressConvention eConv
Definition: address.hxx:225
OUString aLabel