tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / chart2 / source / model / template / HistogramChartType.cxx
blob91304e42e14f521bb5e1e18c0c4a050d9f409585
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #include "HistogramChartType.hxx"
11 #include <servicenames_charttypes.hxx>
12 #include <CartesianCoordinateSystem.hxx>
13 #include <PropertyHelper.hxx>
14 #include <Axis.hxx>
15 #include <AxisHelper.hxx>
16 #include <AxisIndexDefines.hxx>
17 #include <DataSeries.hxx>
18 #include <LabeledDataSequence.hxx>
19 #include <HistogramDataSequence.hxx>
20 #include "HistogramCalculator.hxx"
21 #include <com/sun/star/chart2/AxisType.hpp>
22 #include <com/sun/star/beans/PropertyAttribute.hpp>
23 #include <cppuhelper/supportsservice.hxx>
25 #include <algorithm>
26 #include <cmath>
27 #include <iostream>
28 namespace com::sun::star::uno
30 class XComponentContext;
33 using namespace ::com::sun::star;
34 using ::com::sun::star::uno::Sequence;
35 using ::com::sun::star::beans::Property;
37 namespace
39 enum
41 PROP_HISTOGRAMCHARTTYPE_BINWIDTH,
42 PROP_HISTOGRAMCHARTTYPE_BINRANGE,
43 PROP_HISTOGRAMCHARTTYPE_FREQUENCYTYPE,
44 PROP_HISTOGRAMCHARTTYPE_OVERLAP_SEQUENCE,
45 PROP_HISTOGRAMCHARTTYPE_GAPWIDTH_SEQUENCE
48 void lcl_AddPropertiesToVector(std::vector<Property>& rOutProperties)
50 rOutProperties.emplace_back(
51 "BinWidth", PROP_HISTOGRAMCHARTTYPE_BINWIDTH, cppu::UnoType<double>::get(),
52 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::MAYBEDEFAULT);
54 rOutProperties.emplace_back(
55 "BinRange", PROP_HISTOGRAMCHARTTYPE_BINRANGE, cppu::UnoType<double>::get(),
56 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::MAYBEDEFAULT);
58 rOutProperties.emplace_back(
59 "FrequencyType", PROP_HISTOGRAMCHARTTYPE_FREQUENCYTYPE, cppu::UnoType<sal_Int32>::get(),
60 beans::PropertyAttribute::BOUND | beans::PropertyAttribute::MAYBEDEFAULT);
62 rOutProperties.emplace_back("OverlapSequence", PROP_HISTOGRAMCHARTTYPE_OVERLAP_SEQUENCE,
63 cppu::UnoType<Sequence<sal_Int32>>::get(),
64 beans::PropertyAttribute::BOUND
65 | beans::PropertyAttribute::MAYBEDEFAULT);
67 rOutProperties.emplace_back("GapwidthSequence", PROP_HISTOGRAMCHARTTYPE_GAPWIDTH_SEQUENCE,
68 cppu::UnoType<sal_Int32>::get(),
69 beans::PropertyAttribute::BOUND
70 | beans::PropertyAttribute::MAYBEDEFAULT);
73 ::cppu::OPropertyArrayHelper& StaticHistogramChartTypeInfoHelper()
75 static ::cppu::OPropertyArrayHelper aPropHelper = []() {
76 std::vector<css::beans::Property> aProperties;
77 lcl_AddPropertiesToVector(aProperties);
79 std::sort(aProperties.begin(), aProperties.end(), ::chart::PropertyNameLess());
81 return comphelper::containerToSequence(aProperties);
82 }();
83 return aPropHelper;
86 } // anonymous namespace
88 namespace chart
90 HistogramChartType::HistogramChartType() {}
92 HistogramChartType::HistogramChartType(const HistogramChartType& rOther)
93 : ChartType(rOther)
97 HistogramChartType::~HistogramChartType() {}
99 // ____ XCloneable ____
100 uno::Reference<util::XCloneable> SAL_CALL HistogramChartType::createClone()
102 return uno::Reference<util::XCloneable>(new HistogramChartType(*this));
105 rtl::Reference<ChartType> HistogramChartType::cloneChartType() const
107 return new HistogramChartType(*this);
110 // ____ XChartType ____
111 rtl::Reference<::chart::BaseCoordinateSystem>
112 HistogramChartType::createCoordinateSystem2(sal_Int32 DimensionCount)
114 rtl::Reference<CartesianCoordinateSystem> xResult
115 = new CartesianCoordinateSystem(DimensionCount);
117 for (sal_Int32 i = 0; i < DimensionCount; i++)
119 rtl::Reference<Axis> xAxis = xResult->getAxisByDimension2(i, MAIN_AXIS_INDEX);
120 if (xAxis.is())
122 chart2::ScaleData aScaleData = xAxis->getScaleData();
123 aScaleData.Orientation = chart2::AxisOrientation_MATHEMATICAL;
124 aScaleData.Scaling = AxisHelper::createLinearScaling();
126 if (i == 0) //X-axis
128 aScaleData.AxisType = chart2::AxisType::REALNUMBER;
129 aScaleData.AutoDateAxis = false;
131 else if (i == 1) //Y-axis
133 aScaleData.AxisType = chart2::AxisType::REALNUMBER;
136 xAxis->setScaleData(aScaleData);
140 return xResult;
143 OUString SAL_CALL HistogramChartType::getChartType()
145 return CHART2_SERVICE_NAME_CHARTTYPE_HISTOGRAM;
148 uno::Sequence<OUString> HistogramChartType::getSupportedPropertyRoles()
150 return { u"FillColor"_ustr, u"BorderColor"_ustr };
153 ::cppu::IPropertyArrayHelper& SAL_CALL HistogramChartType::getInfoHelper()
155 return StaticHistogramChartTypeInfoHelper();
158 // ____ OPropertySet ____
159 void HistogramChartType::GetDefaultValue(sal_Int32 nHandle, uno::Any& rAny) const
161 static const ::chart::tPropertyValueMap aStaticDefaults = []() {
162 ::chart::tPropertyValueMap aTmp;
163 Sequence<sal_Int32> aSeq{ 0 }; // No gap for histogram
164 ::chart::PropertyHelper::setPropertyValueDefault(
165 aTmp, PROP_HISTOGRAMCHARTTYPE_GAPWIDTH_SEQUENCE, aSeq);
166 ::chart::PropertyHelper::setPropertyValueDefault(
167 aTmp, PROP_HISTOGRAMCHARTTYPE_OVERLAP_SEQUENCE, aSeq);
168 ::chart::PropertyHelper::setPropertyValueDefault(aTmp, PROP_HISTOGRAMCHARTTYPE_BINWIDTH,
169 2.0);
170 ::chart::PropertyHelper::setPropertyValueDefault(aTmp, PROP_HISTOGRAMCHARTTYPE_BINRANGE,
171 1.0);
172 ::chart::PropertyHelper::setPropertyValueDefault(
173 aTmp, PROP_HISTOGRAMCHARTTYPE_FREQUENCYTYPE, sal_Int32(0));
174 return aTmp;
175 }();
177 tPropertyValueMap::const_iterator aFound(aStaticDefaults.find(nHandle));
178 if (aFound == aStaticDefaults.end())
179 rAny.clear();
180 else
181 rAny = (*aFound).second;
183 // ____ XPropertySet ____
184 uno::Reference<beans::XPropertySetInfo> SAL_CALL HistogramChartType::getPropertySetInfo()
186 static uno::Reference<beans::XPropertySetInfo> xPropertySetInfo(
187 ::cppu::OPropertySetHelper::createPropertySetInfo(StaticHistogramChartTypeInfoHelper()));
188 return xPropertySetInfo;
191 OUString SAL_CALL HistogramChartType::getImplementationName()
193 return "com.sun.star.comp.chart.HistogramChartType";
196 sal_Bool SAL_CALL HistogramChartType::supportsService(const OUString& rServiceName)
198 return cppu::supportsService(this, rServiceName);
201 css::uno::Sequence<OUString> SAL_CALL HistogramChartType::getSupportedServiceNames()
203 return { CHART2_SERVICE_NAME_CHARTTYPE_HISTOGRAM, "com.sun.star.chart2.ChartType" };
206 namespace
208 void setRoleToTheSequence(uno::Reference<chart2::data::XDataSequence> const& xSequence,
209 OUString const& rRole)
211 if (!xSequence.is())
212 return;
215 uno::Reference<beans::XPropertySet> xProperty(xSequence, uno::UNO_QUERY_THROW);
216 xProperty->setPropertyValue(u"Role"_ustr, uno::Any(rRole));
218 catch (const uno::Exception&)
224 void HistogramChartType::createCalculatedDataSeries()
226 if (m_aDataSeries.empty())
227 return;
229 std::vector<uno::Reference<chart2::data::XLabeledDataSequence>> const& aDataSequences
230 = m_aDataSeries[0]->getDataSequences2();
232 if (aDataSequences.empty() || !aDataSequences[0].is())
233 return;
235 // Extract raw data from the spreadsheet
236 std::vector<double> rawData;
237 uno::Reference<chart2::data::XDataSequence> xValues = aDataSequences[0]->getValues();
239 uno::Sequence<uno::Any> aRawAnyValues = xValues->getData();
240 for (const auto& aAny : aRawAnyValues)
242 double fValue = 0.0;
243 if (aAny >>= fValue) // Extract double from Any
245 rawData.push_back(fValue);
249 // Perform histogram calculations
250 HistogramCalculator aHistogramCalculator;
251 aHistogramCalculator.computeBinFrequencyHistogram(rawData);
253 // Get bin ranges and frequencies
254 const auto& binRanges = aHistogramCalculator.getBinRanges();
255 const auto& binFrequencies = aHistogramCalculator.getBinFrequencies();
257 // Create labels and values for HistogramDataSequence
258 std::vector<OUString> aLabels;
259 std::vector<double> aValues;
260 for (size_t i = 0; i < binRanges.size(); ++i)
262 OUString aLabel;
263 if (i == 0)
265 aLabel = u"["_ustr + OUString::number(binRanges[i].first) + u"-"_ustr
266 + OUString::number(binRanges[i].second) + u"]"_ustr;
268 else
270 aLabel = u"("_ustr + OUString::number(binRanges[i].first) + u"-"_ustr
271 + OUString::number(binRanges[i].second) + u"]"_ustr;
273 aLabels.push_back(aLabel);
274 aValues.push_back(static_cast<double>(binFrequencies[i]));
277 rtl::Reference<HistogramDataSequence> aValuesDataSequence = new HistogramDataSequence();
278 aValuesDataSequence->setValues(comphelper::containerToSequence(aValues));
279 aValuesDataSequence->setLabels(comphelper::containerToSequence(aLabels));
281 uno::Reference<chart2::data::XDataSequence> aDataSequence = aValuesDataSequence;
282 setRoleToTheSequence(aDataSequence, u"values-y"_ustr);
284 m_aDataSeries[0]->addDataSequence(new LabeledDataSequence(aDataSequence));
287 } // namespace chart
289 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
290 com_sun_star_comp_chart_HistogramChartType_get_implementation(
291 css::uno::XComponentContext* /*context*/, css::uno::Sequence<css::uno::Any> const&)
293 return cppu::acquire(new ::chart::HistogramChartType());
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */