GraphPlottablesFactory.cpp
91 lines
| 2.9 KiB
| text/x-c
|
CppLexer
Alexandre Leroux
|
r168 | #include "Visualization/GraphPlottablesFactory.h" | ||
#include "Visualization/qcustomplot.h" | ||||
Alexandre Leroux
|
r169 | #include <Data/ScalarSeries.h> | ||
Alexandre Leroux
|
r168 | #include <Variable/Variable.h> | ||
Q_LOGGING_CATEGORY(LOG_GraphPlottablesFactory, "GraphPlottablesFactory") | ||||
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 | |||
QCPAbstractPlottable *createScalarSeriesComponent(ScalarSeries &scalarSeries, QCustomPlot &plot) | ||||
{ | ||||
auto component = plot.addGraph(); | ||||
if (component) { | ||||
// Graph data | ||||
component->setData(scalarSeries.xAxisData()->data(), scalarSeries.valuesData()->data(), | ||||
true); | ||||
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 { | ||||
qCDebug(LOG_GraphPlottablesFactory()) | ||||
<< QObject::tr("Can't create graph for the scalar series"); | ||||
} | ||||
return component; | ||||
} | ||||
} // namespace | ||||
Alexandre Leroux
|
r174 | QVector<QCPAbstractPlottable *> GraphPlottablesFactory::create(std::shared_ptr<Variable> variable, | ||
Alexandre Leroux
|
r168 | QCustomPlot &plot) noexcept | ||
{ | ||||
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())) { | ||||
result.append(createScalarSeriesComponent(*scalarSeries, plot)); | ||||
} | ||||
else { | ||||
qCDebug(LOG_GraphPlottablesFactory()) | ||||
<< QObject::tr("Can't create graph plottables : unmanaged data series type"); | ||||
} | ||||
Alexandre Leroux
|
r168 | } | ||
else { | ||||
qCDebug(LOG_GraphPlottablesFactory()) | ||||
<< QObject::tr("Can't create graph plottables : the variable is null"); | ||||
} | ||||
return result; | ||||
} | ||||