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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <cstddef>
#include "StockDataInterpreter.hxx"
#include "StockChartTypeTemplate.hxx"
#include <DataSeries.hxx>
#include <tools/diagnose_ex.h>
using namespace ::com::sun::star;
using namespace ::com::sun::star::chart2;
using namespace ::std;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::uno::Sequence;
namespace chart
{
// explicit
StockDataInterpreter::StockDataInterpreter(
StockChartTypeTemplate::StockVariant eVariant ) :
m_eStockVariant( eVariant )
{}
StockDataInterpreter::~StockDataInterpreter()
{}
// ____ XDataInterpreter ____
InterpretedData StockDataInterpreter::interpretDataSource(
const Reference< data::XDataSource >& xSource,
const Sequence< beans::PropertyValue >& rArguments,
const std::vector< rtl::Reference< ::chart::DataSeries > >& rSeriesToReUse )
{
if( ! xSource.is())
return InterpretedData();
uno::Reference< chart2::data::XLabeledDataSequence > xCategories;
std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > aData = DataInterpreter::getDataSequences(xSource);
const sal_Int32 nDataCount( aData.size());
// sub-type properties
const StockChartTypeTemplate::StockVariant eVar( GetStockVariant());
const bool bHasOpenValues (( eVar == StockChartTypeTemplate::StockVariant::Open ) ||
( eVar == StockChartTypeTemplate::StockVariant::VolumeOpen ));
const bool bHasVolume (( eVar == StockChartTypeTemplate::StockVariant::Volume ) ||
( eVar == StockChartTypeTemplate::StockVariant::VolumeOpen ));
const bool bHasCategories( HasCategories( rArguments, aData ));
// necessary roles for "full series"
// low/high/close
sal_Int32 nNumberOfNecessarySequences( 3 );
if( bHasOpenValues )
++nNumberOfNecessarySequences;
if( bHasVolume )
++nNumberOfNecessarySequences;
// calculate number of full series (nNumOfFullSeries) and the number of remaining
// sequences used for additional "incomplete series" (nRemaining)
sal_Int32 nNumOfFullSeries( 0 );
sal_Int32 nRemaining( 0 );
{
sal_Int32 nAvailableSequences( nDataCount );
if( bHasCategories )
--nAvailableSequences;
nNumOfFullSeries = nAvailableSequences / nNumberOfNecessarySequences;
nRemaining = nAvailableSequences % nNumberOfNecessarySequences;
}
sal_Int32 nCandleStickSeries = nNumOfFullSeries;
sal_Int32 nVolumeSeries = nNumOfFullSeries;
sal_Int32 nNumberOfGroups( bHasVolume ? 2 : 1 );
// sequences of data::XLabeledDataSequence per series per group
std::vector< std::vector< std::vector< uno::Reference< chart2::data::XLabeledDataSequence > > > > aSequences( nNumberOfGroups );
const sal_Int32 nBarGroupIndex( 0 );
const sal_Int32 nCandleStickGroupIndex( nNumberOfGroups - 1 );
// allocate space for labeled sequences
if( nRemaining > 0 )
++nCandleStickSeries;
aSequences[nCandleStickGroupIndex].resize( nCandleStickSeries );
auto & pCandleStickGroup = aSequences[nCandleStickGroupIndex];
if( bHasVolume )
{
// if there are remaining sequences, the first one is taken for
// additional close values, the second one is taken as volume, if volume
// is used
if( nRemaining > 1 )
++nVolumeSeries;
aSequences[nBarGroupIndex].resize( nVolumeSeries );
}
auto & pBarGroup = aSequences[nBarGroupIndex];
// create data
sal_Int32 nSourceIndex = 0; // index into aData sequence
// 1. categories
if( bHasCategories )
{
xCategories = aData[nSourceIndex];
++nSourceIndex;
}
// 2. create "full" series
for( sal_Int32 nLabeledSeqIdx=0; nLabeledSeqIdx<nNumOfFullSeries; ++nLabeledSeqIdx )
{
// bar
if( bHasVolume )
{
pBarGroup[nLabeledSeqIdx].resize( 1 );
pBarGroup[nLabeledSeqIdx][0] = aData[nSourceIndex];
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-y");
++nSourceIndex;
}
sal_Int32 nSeqIdx = 0;
if( bHasOpenValues )
{
pCandleStickGroup[nLabeledSeqIdx].resize( 4 );
pCandleStickGroup[nLabeledSeqIdx][nSeqIdx] = aData[nSourceIndex];
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-first");
++nSourceIndex;
++nSeqIdx;
}
else
pCandleStickGroup[nLabeledSeqIdx].resize( 3 );
auto & pLabeledSeq = pCandleStickGroup[nLabeledSeqIdx];
pLabeledSeq[nSeqIdx] = aData[nSourceIndex];
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-min");
++nSourceIndex;
++nSeqIdx;
pLabeledSeq[nSeqIdx] = aData[nSourceIndex];
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-max");
++nSourceIndex;
++nSeqIdx;
pLabeledSeq[nSeqIdx] = aData[nSourceIndex];
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-last");
++nSourceIndex;
++nSeqIdx;
}
// 3. create series with remaining sequences
if( bHasVolume && nRemaining > 1 )
{
OSL_ASSERT( nVolumeSeries > nNumOfFullSeries );
pBarGroup[nVolumeSeries - 1].resize( 1 );
OSL_ASSERT( nDataCount > nSourceIndex );
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-y");
pBarGroup[nVolumeSeries - 1][0] = aData[nSourceIndex];
++nSourceIndex;
--nRemaining;
OSL_ENSURE( nRemaining, "additional bar should only be used if there is at least one more sequence for a candle stick" );
}
// candle-stick
if( nRemaining > 0 )
{
OSL_ASSERT( nCandleStickSeries > nNumOfFullSeries );
const sal_Int32 nSeriesIndex = nCandleStickSeries - 1;
pCandleStickGroup[nSeriesIndex].resize( nRemaining );
auto & pLabeledSeq = pCandleStickGroup[nSeriesIndex];
OSL_ASSERT( nDataCount > nSourceIndex );
// 1. low
sal_Int32 nSeqIdx( 0 );
pLabeledSeq[nSeqIdx] = aData[nSourceIndex];
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-min");
++nSourceIndex;
++nSeqIdx;
// 2. high
if( nSeqIdx < nRemaining )
{
pLabeledSeq[nSeqIdx] = aData[nSourceIndex];
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-max");
++nSourceIndex;
++nSeqIdx;
}
// 3. close
OSL_ENSURE( bHasOpenValues || nSeqIdx >= nRemaining, "could have created full series" );
if( nSeqIdx < nRemaining )
{
pLabeledSeq[nSeqIdx] = aData[nSourceIndex];
if( aData[nSourceIndex].is())
SetRole( aData[nSourceIndex]->getValues(), "values-last");
++nSourceIndex;
++nSeqIdx;
}
// 4. open
OSL_ENSURE( nSeqIdx >= nRemaining, "could have created full series" );
}
// create DataSeries
std::vector< std::vector< rtl::Reference< DataSeries > > > aResultSeries( nNumberOfGroups );
sal_Int32 nGroupIndex;
std::size_t nReUsedSeriesIdx = 0;
for( nGroupIndex=0; nGroupIndex<nNumberOfGroups; ++nGroupIndex )
{
const sal_Int32 nNumSeriesData = aSequences[nGroupIndex].size();
aResultSeries[nGroupIndex].resize( nNumSeriesData );
auto & pResultSerie = aResultSeries[nGroupIndex];
for( sal_Int32 nSeriesIdx = 0; nSeriesIdx < nNumSeriesData; ++nSeriesIdx, ++nReUsedSeriesIdx )
{
try
{
rtl::Reference< DataSeries > xSeries;
if( nReUsedSeriesIdx < rSeriesToReUse.size())
xSeries = rSeriesToReUse[nReUsedSeriesIdx];
else
xSeries = new DataSeries;
assert( xSeries.is() );
xSeries->setData( aSequences[nGroupIndex][nSeriesIdx] );
pResultSerie[nSeriesIdx] = xSeries;
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
}
return { aResultSeries, xCategories };
}
// criterion: there must be two groups for stock-charts with volume and all
// series must have the correct number of data::XLabeledDataSequences
// todo: skip first criterion? (to allow easy switch from stock-chart without
// volume to one with volume)
bool StockDataInterpreter::isDataCompatible(
const InterpretedData& aInterpretedData )
{
// high/low/close
std::size_t nNumberOfNecessarySequences = 3;
// open
StockChartTypeTemplate::StockVariant eVar( GetStockVariant());
if( ( eVar == StockChartTypeTemplate::StockVariant::Open ) ||
( eVar == StockChartTypeTemplate::StockVariant::VolumeOpen ))
++nNumberOfNecessarySequences;
// volume
bool bHasVolume = (( eVar == StockChartTypeTemplate::StockVariant::Volume ) ||
( eVar == StockChartTypeTemplate::StockVariant::VolumeOpen ));
// 1. correct number of sub-types
if( aInterpretedData.Series.size() < (bHasVolume ? 2U : 1U ))
return false;
// 2. a. volume -- use default check
if( bHasVolume )
{
if( ! DataInterpreter::isDataCompatible(
{ std::vector< std::vector< rtl::Reference< DataSeries > > >{
aInterpretedData.Series[0] },
aInterpretedData.Categories }))
return false;
}
// 2. b. candlestick
{
OSL_ASSERT( aInterpretedData.Series.size() > (bHasVolume ? 1U : 0U));
const std::vector< rtl::Reference< DataSeries > > & aSeries = aInterpretedData.Series[(bHasVolume ? 1 : 0)];
if(aSeries.empty())
return false;
for( rtl::Reference< DataSeries > const & dataSeries : aSeries )
{
try
{
if( dataSeries->getDataSequences2().size() != nNumberOfNecessarySequences )
return false;
}
catch( const uno::Exception & )
{
DBG_UNHANDLED_EXCEPTION("chart2");
}
}
}
// 2. c. additional series
// ignore
return true;
}
InterpretedData StockDataInterpreter::reinterpretDataSeries(
const InterpretedData& aInterpretedData )
{
// prerequisite: StockDataInterpreter::isDataCompatible() returned true
return aInterpretedData;
}
uno::Any StockDataInterpreter::getChartTypeSpecificData(
const OUString& sKey )
{
if( sKey == "stock variant" )
{
StockChartTypeTemplate::StockVariant eStockVariant( GetStockVariant());
std::map< StockChartTypeTemplate::StockVariant, sal_Int32 > aTranslation {
{ StockChartTypeTemplate::StockVariant::NONE, 0 },
{ StockChartTypeTemplate::StockVariant::Open, 1 },
{ StockChartTypeTemplate::StockVariant::Volume, 2 },
{ StockChartTypeTemplate::StockVariant::VolumeOpen, 3 }
};
return uno::Any( aTranslation[eStockVariant] );
}
return uno::Any();
}
} // namespace chart
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|