diff --git a/gui/include/Visualization/AxisRenderingUtils.h b/gui/include/Visualization/AxisRenderingUtils.h index f64e43c..381b279 100644 --- a/gui/include/Visualization/AxisRenderingUtils.h +++ b/gui/include/Visualization/AxisRenderingUtils.h @@ -10,6 +10,9 @@ class QCPAxis; class QCPColorScale; class QCustomPlot; +/// Formats a data value according to the axis on which it is present +QString formatValue(double value, const QCPAxis &axis); + /** * Helper used to handle axes rendering */ diff --git a/gui/include/Visualization/VisualizationGraphRenderingDelegate.h b/gui/include/Visualization/VisualizationGraphRenderingDelegate.h index 8be36b6..bb9a95d 100644 --- a/gui/include/Visualization/VisualizationGraphRenderingDelegate.h +++ b/gui/include/Visualization/VisualizationGraphRenderingDelegate.h @@ -3,6 +3,9 @@ #include +#include + +class IDataSeries; class QCustomPlot; class QMouseEvent; class Unit; @@ -17,8 +20,9 @@ public: void onMouseMove(QMouseEvent *event) noexcept; - /// Sets properties of the plot's axes - void setAxesProperties(const Unit &xAxisUnit, const Unit &valuesUnit) noexcept; + /// Sets properties of the plot's axes from the data series passed as parameter + void setAxesProperties(std::shared_ptr dataSeries) noexcept; + /// Shows or hides graph overlay (name, close button, etc.) void showGraphOverlay(bool show) noexcept; diff --git a/gui/src/Visualization/AxisRenderingUtils.cpp b/gui/src/Visualization/AxisRenderingUtils.cpp index bbdb842..2e7ea44 100644 --- a/gui/src/Visualization/AxisRenderingUtils.cpp +++ b/gui/src/Visualization/AxisRenderingUtils.cpp @@ -7,6 +7,47 @@ namespace { +const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz"); + +/// 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 axisTicker(bool isTimeAxis) +{ + if (isTimeAxis) { + auto dateTicker = QSharedPointer::create(); + dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); + dateTicker->setDateTimeSpec(Qt::UTC); + + return dateTicker; + } + else { + // default ticker + return QSharedPointer::create(); + } +} + +/** + * Sets properties of the axis passed as parameter + * @param axis the axis to set + * @param unit the unit to set for the axis + * @param scaleType the scale type to set for the axis + */ +void setAxisProperties(QCPAxis &axis, const Unit &unit, + QCPAxis::ScaleType scaleType = QCPAxis::stLinear) +{ + // label (unit name) + axis.setLabel(unit.m_Name); + + // scale type + axis.setScaleType(scaleType); + + // ticker (depending on the type of unit) + axis.setTicker(axisTicker(unit.m_TimeUnit)); +} + /** * Delegate used to set axes properties */ @@ -28,7 +69,13 @@ struct AxisSetter: or std::is_base_of::value> > { static void setProperties(T &dataSeries, QCustomPlot &plot, QCPColorScale &) { - /// @todo ALX + dataSeries.lockRead(); + auto xAxisUnit = dataSeries.xAxisUnit(); + auto valuesUnit = dataSeries.valuesUnit(); + dataSeries.unlock(); + + setAxisProperties(*plot.xAxis, xAxisUnit); + setAxisProperties(*plot.yAxis, valuesUnit); } }; @@ -50,6 +97,17 @@ struct AxisHelper : public IAxisHelper { } // namespace +QString formatValue(double value, const QCPAxis &axis) +{ + // If the axis is a time axis, formats the value as a date + if (auto axisTicker = qSharedPointerDynamicCast(axis.ticker())) { + return DateUtils::dateTime(value, axisTicker->dateTimeSpec()).toString(DATETIME_FORMAT); + } + else { + return QString::number(value); + } +} + std::unique_ptr IAxisHelperFactory::create(std::shared_ptr dataSeries) noexcept { diff --git a/gui/src/Visualization/VisualizationGraphRenderingDelegate.cpp b/gui/src/Visualization/VisualizationGraphRenderingDelegate.cpp index 730c5d1..de7921c 100644 --- a/gui/src/Visualization/VisualizationGraphRenderingDelegate.cpp +++ b/gui/src/Visualization/VisualizationGraphRenderingDelegate.cpp @@ -1,4 +1,5 @@ #include "Visualization/VisualizationGraphRenderingDelegate.h" +#include "Visualization/AxisRenderingUtils.h" #include "Visualization/VisualizationGraphWidget.h" #include "Visualization/qcustomplot.h" @@ -13,11 +14,6 @@ namespace { /// Name of the axes layer in QCustomPlot const auto AXES_LAYER = QStringLiteral("axes"); -const auto DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd hh:mm:ss:zzz"); - -/// Format for datetimes on a axis -const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); - /// Icon used to show x-axis properties const auto HIDE_AXIS_ICON_PATH = QStringLiteral(":/icones/down.png"); @@ -38,35 +34,6 @@ const auto TOOLTIP_RECT = QRect{10, 10, 10, 10}; /// Timeout after which the tooltip is displayed const auto TOOLTIP_TIMEOUT = 500; -/// Generates the appropriate ticker for an axis, depending on whether the axis displays time or -/// non-time data -QSharedPointer axisTicker(bool isTimeAxis) -{ - if (isTimeAxis) { - auto dateTicker = QSharedPointer::create(); - dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); - dateTicker->setDateTimeSpec(Qt::UTC); - - return dateTicker; - } - else { - // default ticker - return QSharedPointer::create(); - } -} - -/// Formats a data value according to the axis on which it is present -QString formatValue(double value, const QCPAxis &axis) -{ - // If the axis is a time axis, formats the value as a date - if (auto axisTicker = qSharedPointerDynamicCast(axis.ticker())) { - return DateUtils::dateTime(value, axisTicker->dateTimeSpec()).toString(DATETIME_FORMAT); - } - else { - return QString::number(value); - } -} - void initPointTracerStyle(QCPItemTracer &tracer) noexcept { tracer.setInterpolating(false); @@ -245,21 +212,14 @@ void VisualizationGraphRenderingDelegate::onMouseMove(QMouseEvent *event) noexce } } -void VisualizationGraphRenderingDelegate::setAxesProperties(const Unit &xAxisUnit, - const Unit &valuesUnit) noexcept +void VisualizationGraphRenderingDelegate::setAxesProperties( + std::shared_ptr dataSeries) noexcept { // Stores x-axis label to be able to retrieve it when x-axis pixmap is unselected - impl->m_XAxisLabel = xAxisUnit.m_Name; - - auto setAxisProperties = [](auto axis, const auto &unit) { - // label (unit name) - axis->setLabel(unit.m_Name); + impl->m_XAxisLabel = dataSeries->xAxisUnit().m_Name; - // ticker (depending on the type of unit) - axis->setTicker(axisTicker(unit.m_TimeUnit)); - }; - setAxisProperties(impl->m_Plot.xAxis, xAxisUnit); - setAxisProperties(impl->m_Plot.yAxis, valuesUnit); + auto axisHelper = IAxisHelperFactory::create(dataSeries); + axisHelper->setProperties(impl->m_Plot, *impl->m_ColorScale); // Updates x-axis state impl->updateXAxisState(); diff --git a/gui/src/Visualization/VisualizationGraphWidget.cpp b/gui/src/Visualization/VisualizationGraphWidget.cpp index 94ea8eb..0317796 100644 --- a/gui/src/Visualization/VisualizationGraphWidget.cpp +++ b/gui/src/Visualization/VisualizationGraphWidget.cpp @@ -114,21 +114,13 @@ void VisualizationGraphWidget::addVariable(std::shared_ptr variable, S { // Uses delegate to create the qcpplot components according to the variable auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); - impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)}); - - // Set axes properties according to the units of the data series - /// @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 xAxisUnit = Unit{}; - auto valuesUnit = Unit{}; if (auto dataSeries = variable->dataSeries()) { - dataSeries->lockRead(); - xAxisUnit = dataSeries->xAxisUnit(); - valuesUnit = dataSeries->valuesUnit(); - dataSeries->unlock(); + // Set axes properties according to the units of the data series + impl->m_RenderingDelegate->setAxesProperties(dataSeries); } - impl->m_RenderingDelegate->setAxesProperties(xAxisUnit, valuesUnit); + + impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)}); connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));