VisualizationGraphHelper.cpp
155 lines
| 5.0 KiB
| text/x-c
|
CppLexer
r227 | #include "Visualization/VisualizationGraphHelper.h" | |||
Alexandre Leroux
|
r168 | #include "Visualization/qcustomplot.h" | ||
Alexandre Leroux
|
r169 | #include <Data/ScalarSeries.h> | ||
Alexandre Leroux
|
r168 | #include <Variable/Variable.h> | ||
r219 | #include <QElapsedTimer> | |||
r227 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") | |||
Alexandre Leroux
|
r168 | |||
Alexandre Leroux
|
r169 | namespace { | ||
Alexandre Leroux
|
r170 | /// Format for datetimes on a axis | ||
const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); | ||||
/// Generates the appropriate ticker for an axis, depending on whether the axis displays time or | ||||
/// non-time data | ||||
QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) | ||||
{ | ||||
if (isTimeAxis) { | ||||
auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); | ||||
dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); | ||||
return dateTicker; | ||||
} | ||||
else { | ||||
// default ticker | ||||
return QSharedPointer<QCPAxisTicker>::create(); | ||||
} | ||||
} | ||||
Alexandre Leroux
|
r169 | |||
r219 | void updateScalarData(QCPAbstractPlottable *component, ScalarSeries &scalarSeries, | |||
const SqpDateTime &dateTime) | ||||
{ | ||||
QElapsedTimer timer; | ||||
timer.start(); | ||||
if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { | ||||
// Clean the graph | ||||
// NAIVE approch | ||||
const auto &xData = scalarSeries.xAxisData()->data(); | ||||
const auto &valuesData = scalarSeries.valuesData()->data(); | ||||
r276 | const auto count = xData.count(); | |||
qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points in cache" << xData.count(); | ||||
auto xValue = QVector<double>(count); | ||||
auto vValue = QVector<double>(count); | ||||
r219 | ||||
r276 | int n = 0; | |||
r219 | for (auto i = 0; i < count; ++i) { | |||
r276 | const auto x = xData[i]; | |||
r219 | if (x >= dateTime.m_TStart && x <= dateTime.m_TEnd) { | |||
r276 | xValue[n] = x; | |||
vValue[n] = valuesData[i]; | ||||
++n; | ||||
r219 | } | |||
} | ||||
r276 | xValue.resize(n); | |||
vValue.resize(n); | ||||
r219 | ||||
r291 | qCInfo(LOG_VisualizationGraphHelper()) << "TORM: Current points displayed" | |||
<< xValue.count(); | ||||
r276 | ||||
qcpGraph->setData(xValue, vValue); | ||||
r290 | ||||
// Display all data | ||||
component->rescaleAxes(); | ||||
component->parentPlot()->replot(); | ||||
r219 | } | |||
else { | ||||
/// @todo DEBUG | ||||
} | ||||
} | ||||
QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot, | ||||
const SqpDateTime &dateTime) | ||||
Alexandre Leroux
|
r169 | { | ||
auto component = plot.addGraph(); | ||||
if (component) { | ||||
r219 | // // Graph data | |||
Alexandre Leroux
|
r169 | component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), | ||
true); | ||||
r219 | updateScalarData(component, scalarSeries, dateTime); | |||
Alexandre Leroux
|
r170 | // Axes properties | ||
/// @todo : for the moment, no control is performed on the axes: the units and the tickers | ||||
/// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | ||||
auto setAxisProperties = [](auto axis, const auto &unit) { | ||||
// label (unit name) | ||||
axis->setLabel(unit.m_Name); | ||||
// ticker (depending on the type of unit) | ||||
axis->setTicker(axisTicker(unit.m_TimeUnit)); | ||||
}; | ||||
setAxisProperties(plot.xAxis, scalarSeries.xAxisUnit()); | ||||
setAxisProperties(plot.yAxis, scalarSeries.valuesUnit()); | ||||
Alexandre Leroux
|
r169 | // Display all data | ||
component->rescaleAxes(); | ||||
plot.replot(); | ||||
} | ||||
else { | ||||
r227 | qCDebug(LOG_VisualizationGraphHelper()) | |||
Alexandre Leroux
|
r169 | << QObject::tr("Can't create graph for the scalar series"); | ||
} | ||||
return component; | ||||
} | ||||
} // namespace | ||||
r227 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | |||
QCustomPlot &plot) noexcept | ||||
Alexandre Leroux
|
r168 | { | ||
auto result = QVector<QCPAbstractPlottable *>{}; | ||||
if (variable) { | ||||
Alexandre Leroux
|
r169 | // Gets the data series of the variable to call the creation of the right components | ||
// according to its type | ||||
if (auto scalarSeries = dynamic_cast<ScalarSeries *>(variable->dataSeries())) { | ||||
r219 | result.append(createScalarSeriesComponent(*scalarSeries, plot, variable->dateTime())); | |||
Alexandre Leroux
|
r169 | } | ||
else { | ||||
r227 | qCDebug(LOG_VisualizationGraphHelper()) | |||
Alexandre Leroux
|
r169 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); | ||
} | ||||
Alexandre Leroux
|
r168 | } | ||
else { | ||||
r227 | qCDebug(LOG_VisualizationGraphHelper()) | |||
Alexandre Leroux
|
r168 | << QObject::tr("Can't create graph plottables : the variable is null"); | ||
} | ||||
return result; | ||||
} | ||||
r219 | ||||
r227 | void VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *> plotableVect, | |||
IDataSeries *dataSeries, const SqpDateTime &dateTime) | ||||
r219 | { | |||
if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { | ||||
if (plotableVect.size() == 1) { | ||||
updateScalarData(plotableVect.at(0), *scalarSeries, dateTime); | ||||
} | ||||
else { | ||||
r227 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( | |||
r219 | "Can't update Data of a scalarSeries because there is not only one component " | |||
"associated"); | ||||
} | ||||
} | ||||
else { | ||||
/// @todo DEBUG | ||||
} | ||||
} | ||||